Exemple #1
0
static void __nlBeginSystem() {
	__nl_assert(__nlCurrentContext->nb_variables > 0);

	if (__nlCurrentContext->solve_again)
		__nlTransition(__NL_STATE_SYSTEM_SOLVED, __NL_STATE_SYSTEM);
	else {
		__nlTransition(__NL_STATE_INITIAL, __NL_STATE_SYSTEM);

		__nlCurrentContext->variable = __NL_NEW_ARRAY(
			__NLVariable, __nlCurrentContext->nb_variables);
		
		__nlCurrentContext->alloc_variable = NL_TRUE;
	}
}
Exemple #2
0
static void __nlBeginMatrix() {
	NLuint i;
	NLuint n = 0;
	NLenum storage = __NL_ROWS;

	__nlTransition(__NL_STATE_SYSTEM, __NL_STATE_MATRIX);

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

		__nlCurrentContext->n = n;

		/* a least squares problem results in a symmetric matrix */
		if(__nlCurrentContext->least_squares)
			__nlCurrentContext->symmetric = NL_TRUE;

		if(__nlCurrentContext->symmetric)
			storage = (storage | __NL_SYMMETRIC);

		/* SuperLU storage does not support symmetric storage */
		storage = (storage & ~__NL_SYMMETRIC);

		__nlSparseMatrixConstruct(&__nlCurrentContext->M, n, n, storage);
		__nlCurrentContext->alloc_M = NL_TRUE;

		__nlCurrentContext->x = __NL_NEW_ARRAY(NLfloat, n);
		__nlCurrentContext->alloc_x = NL_TRUE;
		
		__nlCurrentContext->b = __NL_NEW_ARRAY(NLfloat, n);
		__nlCurrentContext->alloc_b = NL_TRUE;
	}
	else {
		/* need to recompute b only, A is not constructed anymore */
		__NL_CLEAR_ARRAY(NLfloat, __nlCurrentContext->b, __nlCurrentContext->n);
	}

	__nlVariablesToVector();

	__nlRowColumnConstruct(&__nlCurrentContext->af);
	__nlCurrentContext->alloc_af = NL_TRUE;
	__nlRowColumnConstruct(&__nlCurrentContext->al);
	__nlCurrentContext->alloc_al = NL_TRUE;

	__nlCurrentContext->current_row = 0;
}
Exemple #3
0
NLboolean nlSolve() {

    NLboolean result = NL_TRUE;

    __nlCheckState(__NL_STATE_SYSTEM_CONSTRUCTED);
    result = __nlSolve_SUPERLU(NL_TRUE);

    __nlVectorToVariables();
    __nlTransition(__NL_STATE_SYSTEM_CONSTRUCTED, __NL_STATE_SOLVED);

    return result;

	return nlSolveAdvanced(NULL, NL_FALSE);
}
Exemple #4
0
static void __nlEndRow() {
	__NLRowColumn*	af = &__nlCurrentContext->af;
	__NLRowColumn*	al = &__nlCurrentContext->al;
	__NLSparseMatrix* M  = &__nlCurrentContext->M;
	NLfloat* b		= __nlCurrentContext->b;
	NLuint nf		  = af->size;
	NLuint nl		  = al->size;
	NLuint current_row = __nlCurrentContext->current_row;
	NLuint i;
	NLuint j;
	NLfloat S;
	__nlTransition(__NL_STATE_ROW, __NL_STATE_MATRIX);

	if(__nlCurrentContext->least_squares) {
		if (!__nlCurrentContext->solve_again) {
			for(i=0; i<nf; i++) {
				for(j=0; j<nf; j++) {
					__nlSparseMatrixAdd(
						M, af->coeff[i].index, af->coeff[j].index,
						af->coeff[i].value * af->coeff[j].value
					);
				}
			}
		}

		S = -__nlCurrentContext->right_hand_side;
		for(j=0; j<nl; j++)
			S += al->coeff[j].value;

		for(i=0; i<nf; i++)
			b[ af->coeff[i].index ] -= af->coeff[i].value * S;
	} else {
		if (!__nlCurrentContext->solve_again) {
			for(i=0; i<nf; i++) {
				__nlSparseMatrixAdd(
					M, current_row, af->coeff[i].index, af->coeff[i].value
				);
			}
		}
		b[current_row] = -__nlCurrentContext->right_hand_side;
		for(i=0; i<nl; i++) {
			b[current_row] -= al->coeff[i].value;
		}
	}
	__nlCurrentContext->current_row++;
	__nlCurrentContext->right_hand_side = 0.0;	
}
Exemple #5
0
static void __nlEndMatrix() {
	__nlTransition(__NL_STATE_MATRIX, __NL_STATE_MATRIX_CONSTRUCTED);	
	
	__nlRowColumnDestroy(&__nlCurrentContext->af);
	__nlCurrentContext->alloc_af = NL_FALSE;
	__nlRowColumnDestroy(&__nlCurrentContext->al);
	__nlCurrentContext->alloc_al = NL_FALSE;
	
#if 0
	if(!__nlCurrentContext->least_squares) {
		__nl_assert(
			__nlCurrentContext->current_row == 
			__nlCurrentContext->n
		);
	}
#endif
}
Exemple #6
0
static void __nlEndMatrix() {
	__NLContext *context = __nlCurrentContext;
	NLuint i;

	__nlTransition(__NL_STATE_MATRIX, __NL_STATE_MATRIX_CONSTRUCTED);	
	
	if(context->least_squares) {
		if(!__nlCurrentContext->solve_again) {
			__nlSparseMatrix_square(&context->MtM, &context->M);
			context->alloc_MtM = NL_TRUE;

			context->Mtb =
				__NL_NEW_ARRAY(NLfloat, context->n*context->nb_rhs);
			context->alloc_Mtb = NL_TRUE;
		}
	}

	for(i=0; i<context->nb_rhs; i++)
		__nlEndMatrixRHS(i);
}
Exemple #7
0
static void __nlBeginMatrix() {
	NLuint i;
	NLuint m = 0, n = 0;
	NLenum storage = __NL_ROWS;
	__NLContext *context = __nlCurrentContext;

	__nlTransition(__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;
				context->variable[i].a = __NL_NEW(__NLRowColumn);
				__nlRowColumnConstruct(context->variable[i].a);
			}
			else
				context->variable[i].index = n++;
		}

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

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

		__nlSparseMatrixConstruct(&context->M, m, n, storage);
		context->alloc_M = NL_TRUE;

		context->b = __NL_NEW_ARRAY(NLfloat, m*context->nb_rhs);
		context->alloc_b = NL_TRUE;

		context->x = __NL_NEW_ARRAY(NLfloat, n*context->nb_rhs);
		context->alloc_x = NL_TRUE;
	}
	else {
		/* need to recompute b only, A is not constructed anymore */
		__NL_CLEAR_ARRAY(NLfloat, context->b, context->m*context->nb_rhs);
	}

	__nlVariablesToVector();
}
Exemple #8
0
NLboolean nlSolveAdvanced(NLint *permutation, NLboolean solveAgain) {
	NLboolean result = NL_TRUE;

	__nlCheckState(__NL_STATE_SYSTEM_CONSTRUCTED);

	if (!__nlCurrentContext->solve_again)
		result = __nlFactorize_SUPERLU(__nlCurrentContext, permutation);

	if (result) {
		result = __nlInvert_SUPERLU(__nlCurrentContext);

		if (result) {
			__nlVectorToVariables();

			if (solveAgain)
				__nlCurrentContext->solve_again = NL_TRUE;

			__nlTransition(__NL_STATE_SYSTEM_CONSTRUCTED, __NL_STATE_SYSTEM_SOLVED);
		}
	}

	return result;
}
Exemple #9
0
static void __nlEndSystem() {
	__nlTransition(__NL_STATE_MATRIX_CONSTRUCTED, __NL_STATE_SYSTEM_CONSTRUCTED);	
}
Exemple #10
0
static void __nlBeginRow() {
	__nlTransition(__NL_STATE_MATRIX, __NL_STATE_ROW);
	__nlRowColumnZero(&__nlCurrentContext->af);
	__nlRowColumnZero(&__nlCurrentContext->al);
}