Welcome to mirror list, hosted at ThFree Co, Russian Federation.

opennl.cpp « intern « opennl « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 446a1a38f0d372b2752c614b3f8be6db970c86bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/** \file opennl/intern/opennl.c
 *  \ingroup opennlintern
 */
/*
 *
 *  OpenNL: Numerical Library
 *  Copyright (C) 2004 Bruno Levy
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  If you modify this software, you should include a notice giving the
 *  name of the person performing the modification, the date of modification,
 *  and the reason for such modification.
 *
 *  Contact: Bruno Levy
 *
 *	 levy@loria.fr
 *
 *	 ISA Project
 *	 LORIA, INRIA Lorraine,
 *	 Campus Scientifique, BP 239
 *	 54506 VANDOEUVRE LES NANCY CEDEX
 *	 FRANCE
 *
 *  Note that the GNU General Public License does not permit incorporating
 *  the Software into proprietary programs.
 */

#include "ONL_opennl.h"

#include <Eigen/Sparse>

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>

/* Eigen data structures */

typedef Eigen::SparseMatrix<double, Eigen::ColMajor> EigenSparseMatrix;
typedef Eigen::SparseLU<EigenSparseMatrix> EigenSparseSolver;
typedef Eigen::VectorXd EigenVectorX;
typedef Eigen::Triplet<double> EigenTriplet;

/* NLContext data structure */

typedef struct {
	NLuint index;
	NLdouble value;
} NLCoeff;

typedef struct {
	NLdouble value[4];
	NLboolean locked;
	NLuint index;
	std::vector<NLCoeff> a;
} NLVariable;

#define NL_STATE_INITIAL            0
#define NL_STATE_SYSTEM             1
#define NL_STATE_MATRIX             2
#define NL_STATE_MATRIX_CONSTRUCTED 3
#define NL_STATE_SYSTEM_CONSTRUCTED 4
#define NL_STATE_SYSTEM_SOLVED      5

struct NLContext {
   NLContext()
   {
      state = NL_STATE_INITIAL;
      n = 0;
      m = 0;
      sparse_solver = NULL;
      nb_variables = 0;
      nb_rhs = 1;
      nb_rows = 0;
      least_squares = false;
      solve_again = false;
   }

   ~NLContext()
   {
      delete sparse_solver;
   }

	NLenum state;

	NLuint n;
	NLuint m;

	std::vector<EigenTriplet> Mtriplets;
	EigenSparseMatrix M;
	EigenSparseMatrix MtM;
	std::vector<EigenVectorX> b;
	std::vector<EigenVectorX> Mtb;
	std::vector<EigenVectorX> x;

	EigenSparseSolver *sparse_solver;

	NLuint nb_variables;
	std::vector<NLVariable> variable;

	NLuint nb_rows;
	NLuint nb_rhs;

	NLboolean least_squares;
	NLboolean solve_again;
};

NLContext *nlNewContext(void)
{
	return new NLContext();
}

void nlDeleteContext(NLContext *context)
{
	delete context;
}

static void __nlCheckState(NLContext *context, NLenum state)
{
	assert(context->state == state);
}

static void __nlTransition(NLContext *context, NLenum from_state, NLenum to_state)
{
	__nlCheckState(context, from_state);
	context->state = to_state;
}

/* Get/Set parameters */

void nlSolverParameteri(NLContext *context, NLenum pname, NLint param)
{
	__nlCheckState(context, NL_STATE_INITIAL);
	switch(pname) {
	case NL_NB_VARIABLES: {
		assert(param > 0);
		context->nb_variables = (NLuint)param;
	} break;
	case NL_NB_ROWS: {
		assert(param > 0);
		context->nb_rows = (NLuint)param;
	} break;
	case NL_LEAST_SQUARES: {
		context->least_squares = (NLboolean)param;
	} break;
	case NL_NB_RIGHT_HAND_SIDES: {
		context->nb_rhs = (NLuint)param;
	} break;
	default: {
		assert(0);
	} break;
	}
}

/* Get/Set Lock/Unlock variables */

void nlSetVariable(NLContext *context, NLuint rhsindex, NLuint index, NLdouble value)
{
	__nlCheckState(context, NL_STATE_SYSTEM);
	context->variable[index].value[rhsindex] = value;
}

NLdouble nlGetVariable(NLContext *context, NLuint rhsindex, NLuint index)
{
	assert(context->state != NL_STATE_INITIAL);
	return context->variable[index].value[rhsindex];
}

void nlLockVariable(NLContext *context, NLuint index)
{
	__nlCheckState(context, NL_STATE_SYSTEM);
	context->variable[index].locked = true;
}

void nlUnlockVariable(NLContext *context, NLuint index)
{
	__nlCheckState(context, NL_STATE_SYSTEM);
	context->variable[index].locked = false;
}

/* System construction */

static void __nlVariablesToVector(NLContext *context)
{
	NLuint i, j, nb_rhs;

	nb_rhs= context->nb_rhs;

	for(i=0; i<context->nb_variables; i++) {
		NLVariable* v = &(context->variable[i]);
		if(!v->locked) {
			for(j=0; j<nb_rhs; j++)
				context->x[j][v->index] = v->value[j];
		}
	}
}

static void __nlVectorToVariables(NLContext *context)
{
	NLuint i, j, nb_rhs;

	nb_rhs= context->nb_rhs;

	for(i=0; i<context->nb_variables; i++) {
		NLVariable* v = &(context->variable[i]);
		if(!v->locked) {
			for(j=0; j<nb_rhs; j++)
				v->value[j] = context->x[j][v->index];
		}
	}
}

static void __nlBeginSystem(NLContext *context)
{
	assert(context->nb_variables > 0);

	if (context->solve_again)
		__nlTransition(context, NL_STATE_SYSTEM_SOLVED, NL_STATE_SYSTEM);
	else {
		__nlTransition(context, NL_STATE_INITIAL, NL_STATE_SYSTEM);

		context->variable.resize(context->nb_variables);
	}
}

static void __nlEndSystem(NLContext *context)
{
	__nlTransition(context, NL_STATE_MATRIX_CONSTRUCTED, NL_STATE_SYSTEM_CONSTRUCTED);
}

static void __nlBeginMatrix(NLContext *context)
{
	NLuint i;
	NLuint m = 0, n = 0;

	__nlTransition(context, NL_STATE_SYSTEM, NL_STATE_MATRIX);

	if (!context->solve_again) {
		for(i=0; i<context->nb_variables; i++) {
			if(context->variable[i].locked)
				context->variable[i].index = ~0;
			else
				context->variable[i].index = n++;
		}

		m = (context->nb_rows == 0)? n: context->nb_rows;

		context->m = m;
		context->n = n;

		/* reserve reasonable estimate */
		context->Mtriplets.clear();
		context->Mtriplets.reserve(std::max(m, n)*3);

		context->b.resize(context->nb_rhs);
		context->x.resize(context->nb_rhs);

		for (i=0; i<context->nb_rhs; i++) {
			context->b[i].setZero(m);
			context->x[i].setZero(n);
		}
	}
	else {
		/* need to recompute b only, A is not constructed anymore */
		for (i=0; i<context->nb_rhs; i++)
			context->b[i].setZero(context->m);
	}

	__nlVariablesToVector(context);
}

static void __nlEndMatrixRHS(NLContext *context, NLuint rhs)
{
	NLVariable *variable;
	NLuint i, j;

	EigenVectorX& b = context->b[rhs];

	for(i=0; i<context->nb_variables; i++) {
		variable = &(context->variable[i]);

		if(variable->locked) {
			std::vector<NLCoeff>& a = variable->a;

			for(j=0; j<a.size(); j++) {
				b[a[j].index] -= a[j].value*variable->value[rhs];
			}
		}
	}

	if(context->least_squares)
		context->Mtb[rhs] = context->M.transpose() * b;
}

static void __nlEndMatrix(NLContext *context)
{
	__nlTransition(context, NL_STATE_MATRIX, NL_STATE_MATRIX_CONSTRUCTED);

	if(!context->solve_again) {
		context->M.resize(context->m, context->n);
		context->M.setFromTriplets(context->Mtriplets.begin(), context->Mtriplets.end());
		context->Mtriplets.clear();

		if(context->least_squares) {
			context->MtM = context->M.transpose() * context->M;

			context->Mtb.resize(context->nb_rhs);
			for (NLuint rhs=0; rhs<context->nb_rhs; rhs++)
				context->Mtb[rhs].setZero(context->n);
		}
	}

	for (NLuint rhs=0; rhs<context->nb_rhs; rhs++)
		__nlEndMatrixRHS(context, rhs);
}

void nlMatrixAdd(NLContext *context, NLuint row, NLuint col, NLdouble value)
{
	__nlCheckState(context, NL_STATE_MATRIX);

	if(context->solve_again)
		return;

	if (!context->least_squares && context->variable[row].locked);
	else if (context->variable[col].locked) {
		if(!context->least_squares)
			row = context->variable[row].index;

		NLCoeff coeff = {row, value};
		context->variable[col].a.push_back(coeff);
	}
	else {
		if(!context->least_squares)
			row = context->variable[row].index;
		col = context->variable[col].index;

		// direct insert into matrix is too slow, so use triplets
		EigenTriplet triplet(row, col, value);
		context->Mtriplets.push_back(triplet);
	}
}

void nlRightHandSideAdd(NLContext *context, NLuint rhsindex, NLuint index, NLdouble value)
{
	__nlCheckState(context, NL_STATE_MATRIX);

	if(context->least_squares) {
		context->b[rhsindex][index] += value;
	}
	else {
		if(!context->variable[index].locked) {
			index = context->variable[index].index;
			context->b[rhsindex][index] += value;
		}
	}
}

void nlRightHandSideSet(NLContext *context, NLuint rhsindex, NLuint index, NLdouble value)
{
	__nlCheckState(context, NL_STATE_MATRIX);

	if(context->least_squares) {
		context->b[rhsindex][index] = value;
	}
	else {
		if(!context->variable[index].locked) {
			index = context->variable[index].index;
			context->b[rhsindex][index] = value;
		}
	}
}

void nlBegin(NLContext *context, NLenum prim)
{
	switch(prim) {
	case NL_SYSTEM: {
		__nlBeginSystem(context);
	} break;
	case NL_MATRIX: {
		__nlBeginMatrix(context);
	} break;
	default: {
		assert(0);
	}
	}
}

void nlEnd(NLContext *context, NLenum prim)
{
	switch(prim) {
	case NL_SYSTEM: {
		__nlEndSystem(context);
	} break;
	case NL_MATRIX: {
		__nlEndMatrix(context);
	} break;
	default: {
		assert(0);
	}
	}
}

void nlPrintMatrix(NLContext *context)
{
	std::cout << "A:" << context->M << std::endl;

	for(NLuint rhs=0; rhs<context->nb_rhs; rhs++)
		std::cout << "b " << rhs << ":" << context->b[rhs] << std::endl;

	if (context->MtM.rows() && context->MtM.cols())
		std::cout << "AtA:" << context->MtM << std::endl;
}

/* Solving */

NLboolean nlSolve(NLContext *context, NLboolean solveAgain)
{
	NLboolean result = true;

	__nlCheckState(context, NL_STATE_SYSTEM_CONSTRUCTED);

	if (!context->solve_again) {
		EigenSparseMatrix& M = (context->least_squares)? context->MtM: context->M;

		assert(M.rows() == M.cols());

		/* Convert M to compressed column format */
		M.makeCompressed();

		/* Perform sparse LU factorization */
		EigenSparseSolver *sparse_solver = new EigenSparseSolver();
		context->sparse_solver = sparse_solver;

		sparse_solver->analyzePattern(M);
		sparse_solver->factorize(M);

		result = (sparse_solver->info() == Eigen::Success);

		/* Free M, don't need it anymore at this point */
		M.resize(0, 0);
	}

	if (result) {
		/* Solve each right hand side */
		for(NLuint rhs=0; rhs<context->nb_rhs; rhs++) {
			EigenVectorX& b = (context->least_squares)? context->Mtb[rhs]: context->b[rhs];
			context->x[rhs] = context->sparse_solver->solve(b);

			if (context->sparse_solver->info() != Eigen::Success)
				result = false;
		}

		if (result) {
			__nlVectorToVariables(context);

			if (solveAgain)
				context->solve_again = true;

			__nlTransition(context, NL_STATE_SYSTEM_CONSTRUCTED, NL_STATE_SYSTEM_SOLVED);
		}
	}

	return result;
}