Skip to content

Commit

Permalink
- revert for now r23902, since it break some build
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23905 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Dec 23, 2014
1 parent e5f52ba commit 4da0c1d
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 522 deletions.
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenC.tpl
Expand Up @@ -1637,7 +1637,7 @@ template functionSetupLinearSystemsTemp(list<SimEqSystem> allEquations, String m
let vectorb = (eq.beqs |> exp hasindex i0 =>
let &preExp = buffer "" /*BUFD*/
let expPart = daeExp(exp, contextSimulationDiscrete, &preExp, &varDecls2, &auxFunction)
'<%preExp%>linearSystemData->setBElement(<%i0%>, <%expPart%>, linearSystemData);'
'<%preExp%>linearSystemData->b[<%i0%>] = <%expPart%>;'
;separator="\n")
let body_initializeStaticLSData = (eq.vars |> var hasindex i0 =>
<<
Expand Down
4 changes: 2 additions & 2 deletions Compiler/runtime/config.h
Expand Up @@ -55,9 +55,9 @@
#endif

/* adrpo: add -loleaut32 as is used by ExternalMedia */
#define BASIC_LDFLAGS_RT " -lgc -lexpat -lregex -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -llis -lumfpack -lamd -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -lcminpack -ltmglib-mingw -lblas-mingw -lf2c"
#define BASIC_LDFLAGS_RT " -lgc -lexpat -lregex -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -lcminpack -ltmglib-mingw -lblas-mingw -lf2c"
#define LDFLAGS_RT " -lOpenModelicaRuntimeC" BASIC_LDFLAGS_RT
#define LDFLAGS_RT_SIM " -lSimulationRuntimeC" BASIC_LDFLAGS_RT " -linteractive -lwsock32 -lstdc++"
#define LDFLAGS_RT_SIM " -lSimulationRuntimeC" BASIC_LDFLAGS_RT " -linteractive -lwsock32 -llis -lstdc++"
#define CONFIG_EXE_EXT ".exe"
#define CONFIG_DLL_EXT ".dll"
#define CONFIG_OS "Windows_NT"
Expand Down
4 changes: 2 additions & 2 deletions Makefile.common
Expand Up @@ -24,7 +24,7 @@ INSTALL_MANDIR = ${DESTDIR}${datadir}/man/
INSTALL_JAVADIR = ${DESTDIR}${datadir}/omc/java
INSTALL_LOCALEDIR = ${DESTDIR}${datadir}/locale

.PHONY : interactive omc release qtclients mosh all mkbuilddirs fmi test install-dirs susan susan_all susgen sustst install-python install-openturns runtimeCPPinstall CMinpack Cdaskr umfpack boehm-gc
.PHONY : interactive omc release qtclients mosh all mkbuilddirs fmi test install-dirs susan susan_all susgen sustst install-python install-openturns runtimeCPPinstall CMinpack Cdaskr boehm-gc

mkbuilddirs:
if [ "$(APP)" = ".app" ]; then mkdir -p $(builddir_app); fi
Expand Down Expand Up @@ -63,7 +63,7 @@ install-python:
install-openturns: mkbuilddirs
(time cp SimulationRuntime/OpenTurns/* $(builddir_share)/omc/scripts/OpenTurns/)

interactive: .testvariables mkbuilddirs boehm-gc fmil msl-external-libs lis Cdaskr CMinpack $(IPOPT_TARGET) umfpack
interactive: .testvariables mkbuilddirs boehm-gc fmil msl-external-libs lis Cdaskr CMinpack $(IPOPT_TARGET) $(UMFPACK_TARGET)
$(MAKE) -C SimulationRuntime/interactive -f $(defaultMakefileTarget)
# Depends on libinteractive.a
$(MAKE) -C SimulationRuntime/c -f $(defaultMakefileTarget)
Expand Down
40 changes: 20 additions & 20 deletions SimulationRuntime/c/simulation/solver/linearSolverLapack.c
Expand Up @@ -49,6 +49,19 @@
extern int dgesv_(int *n, int *nrhs, double *a, int *lda,
int *ipiv, double *b, int *ldb, int *info);

typedef struct DATA_LAPACK
{
int *ipiv; /* vector pivot values */
int nrhs; /* number of righthand sides*/
int info; /* output */
_omc_vector* work;

_omc_vector* x;
_omc_vector* b;
_omc_matrix* A;

} DATA_LAPACK;

/*! \fn allocate memory for linear system solver lapack
*
*/
Expand Down Expand Up @@ -167,7 +180,7 @@ static int wrapper_fvec_lapack(_omc_vector* x, _omc_vector* f, int* iflag, void*
/*! \fn solve linear system with lapack method
*
* \param [in] [data]
* [sysNumber] index of the corresponding linear system
* [sysNumber] index of the corresponding non-linear system
*
* \author wbraun
*/
Expand All @@ -182,31 +195,23 @@ int solveLapack(DATA *data, int sysNumber)
/* We are given the number of the linear system.
* We want to look it up among all equations. */
int eqSystemNumber = systemData->equationIndex;
int indexes[2] = {1,eqSystemNumber};
_omc_scalar residualNorm = 0;

infoStreamPrintWithEquationIndexes(LOG_LS, 0, indexes, "Start solving Linear System %d (size %d) at time %g with Lapack Solver",
eqSystemNumber, (int) systemData->size,
data->localData[0]->timeValue);


/* set data */
_omc_setVectorData(solverData->x, systemData->x);
_omc_setVectorData(solverData->b, systemData->b);
_omc_setMatrixData(solverData->A, systemData->A);


rt_ext_tp_tick(&(solverData->timeClock));
if (0 == systemData->method) {

/* reset matrix A */
memset(systemData->A, 0, (systemData->size)*(systemData->size)*sizeof(double));

/* update matrix A */
systemData->setA(data, systemData);

/* update vector b (rhs) */
systemData->setb(data, systemData);

} else {

/* calculate jacobian -> matrix A*/
Expand All @@ -220,7 +225,6 @@ int solveLapack(DATA *data, int sysNumber)
_omc_copyVector(solverData->work, solverData->x);
wrapper_fvec_lapack(solverData->work, solverData->b, &iflag, data, sysNumber);
}
infoStreamPrint(LOG_LS, 0, "### %f time to set Matrix A and vector b.", rt_ext_tp_tock(&(solverData->timeClock)));

/* Log A*x=b */
if(ACTIVE_STREAM(LOG_LS_V)){
Expand All @@ -229,8 +233,6 @@ int solveLapack(DATA *data, int sysNumber)
_omc_printVector(solverData->b, "Vector b", LOG_LS_V);
}

rt_ext_tp_tick(&(solverData->timeClock));

/* Solve system */
dgesv_((int*) &systemData->size,
(int*) &solverData->nrhs,
Expand All @@ -241,8 +243,6 @@ int solveLapack(DATA *data, int sysNumber)
(int*) &systemData->size,
&solverData->info);

infoStreamPrint(LOG_LS, 0, "Solve System: %f", rt_ext_tp_tock(&(solverData->timeClock)));

if(solverData->info < 0)
{
warningStreamPrint(LOG_STDOUT, 0, "Error solving linear system of equations (no. %d) at time %f. Argument %d illegal.", (int)systemData->equationIndex, data->localData[0]->timeValue, (int)solverData->info);
Expand Down Expand Up @@ -278,15 +278,15 @@ int solveLapack(DATA *data, int sysNumber)
}


if (ACTIVE_STREAM(LOG_LS_V)){
infoStreamPrint(LOG_LS_V, 1, "Residual Norm %f of solution x:", residualNorm);
infoStreamPrint(LOG_LS_V, 0, "System %d numVars %d.", eqSystemNumber, modelInfoGetEquation(&data->modelData.modelDataXml,eqSystemNumber).numVar);
if (ACTIVE_STREAM(LOG_LS)){
infoStreamPrint(LOG_LS, 1, "Residual Norm %f of solution x:", residualNorm);
infoStreamPrint(LOG_LS, 0, "System %d numVars %d.", eqSystemNumber, modelInfoGetEquation(&data->modelData.modelDataXml,eqSystemNumber).numVar);

for(i = 0; i < systemData->size; ++i) {
infoStreamPrint(LOG_LS_V, 0, "[%d] %s = %g", i+1, modelInfoGetEquation(&data->modelData.modelDataXml,eqSystemNumber).vars[i], systemData->x[i]);
infoStreamPrint(LOG_LS, 0, "[%d] %s = %g", i+1, modelInfoGetEquation(&data->modelData.modelDataXml,eqSystemNumber).vars[i], systemData->x[i]);
}

messageClose(LOG_LS_V);
messageClose(LOG_LS);
}
}

Expand Down
22 changes: 9 additions & 13 deletions SimulationRuntime/c/simulation/solver/linearSolverLapack.h
Expand Up @@ -35,22 +35,18 @@
#define _LINEARSOLVERLAPACK_H_

#include "simulation_data.h"
#include "omc_math.h"

typedef struct DATA_LAPACK
{
int *ipiv; /* vector pivot values */
int nrhs; /* number of righthand sides*/
int info; /* output */
_omc_vector* work;

_omc_vector* x;
_omc_vector* b;
_omc_matrix* A;
#ifdef __cplusplus
extern "C" {
#endif

rtclock_t timeClock; /* time clock */
#ifdef VOID
#undef VOID
#endif

} DATA_LAPACK;
#ifdef __cplusplus
}
#endif

int allocateLapackData(int size, void **data);
int freeLapackData(void **data);
Expand Down
160 changes: 41 additions & 119 deletions SimulationRuntime/c/simulation/solver/linearSolverLis.c
Expand Up @@ -36,7 +36,6 @@
#include <string.h>

#include "simulation_data.h"
#include "simulation_info_xml.h"
#include "omc_error.h"
#include "varinfo.h"
#include "model_help.h"
Expand All @@ -52,41 +51,24 @@ allocateLisData(int n_row, int n_col, int nz, void** voiddata)
{
DATA_LIS* data = (DATA_LIS*) malloc(sizeof(DATA_LIS));
assertStreamPrint(NULL, 0 != data, "Could not allocate data for linear solver Lis.");
char buffer[128];

data->n_col = n_col;
data->n_row = n_row;
data->nnz = nz;

lis_vector_create(LIS_COMM_WORLD, &(data->b));
lis_vector_create(0, &(data->b));
lis_vector_set_size(data->b, data->n_row, 0);

lis_vector_create(LIS_COMM_WORLD, &(data->x));
lis_vector_set_size(data->x, data->n_row, 0);

lis_matrix_create(LIS_COMM_WORLD, &(data->A));
lis_matrix_set_size(data->A, data->n_row, 0);
lis_matrix_set_type(data->A, LIS_MATRIX_CSR);
lis_vector_create(0, &(data->x));
lis_vector_set_size(data->b, data->n_row, 0);

lis_solver_create(&(data->solver));

lis_solver_set_option("-print none",data->solver);
sprintf(buffer,"-maxiter %d", n_row*100);
lis_solver_set_option(buffer,data->solver);
lis_solver_set_option("-scale none",data->solver);
lis_solver_set_option("-p none",data->solver);
lis_solver_set_option("-initx_zeros 0",data->solver);
lis_solver_set_option("-tol 1.0e-12",data->solver);


rt_ext_tp_tick(&(data->timeClock));

lis_solver_set_option("-i fgmres ",data->solver);

*voiddata = (void*)data;
return 0;
}


/*! \fn free memory for linear system solver Lis
*
*/
Expand All @@ -95,125 +77,65 @@ freeLisData(void **voiddata)
{
DATA_LIS* data = (DATA_LIS*) *voiddata;

lis_matrix_destroy(data->A);
lis_solver_destroy(data->solver);
lis_vector_destroy(data->b);
lis_vector_destroy(data->x);
lis_solver_destroy(data->solver);

return 0;
}


void printLisMatrixCSR(LIS_MATRIX A, int n)
{
char buffer[16384];
int i, j;
/* A matrix */
infoStreamPrint(LOG_LS_V, 1, "A matrix [%dx%d] nnz = %d ", n, n, A->nnz);
for(i=0; i<n; i++)
{
buffer[0] = 0;
for(j=A->ptr[i]; j<A->ptr[i+1]; j++){
sprintf(buffer, "%s(%d,%d,%g) ", buffer, i, A->index[j], A->value[j]);
}
infoStreamPrint(LOG_LS_V, 0, "%s", buffer);
}

messageClose(LOG_LS_V);

}



/*! \fn solve linear system with Lis method
*
* \param [in] [data]
* [sysNumber] index of the corresponding linear system
* [sysNumber] index of the corresponing non-linear system
*
* \author swagner
*/

int
solveLis(DATA *data, int sysNumber)
{
int i, ret, success = 1;
LINEAR_SYSTEM_DATA* systemData = &(data->simulationInfo.linearSystemData[sysNumber]);
DATA_LIS* solverData = (DATA_LIS*)systemData->solverData;
int i, ret, success = 1, ni, iflag = 1, n = systemData->size, eqSystemNumber = systemData->equationIndex;
char *lis_returncode[] = {"LIS_SUCCESS", "LIS_ILL_OPTION", "LIS_BREAKDOWN", "LIS_OUT_OF_MEMORY", "LIS_MAXITER", "LIS_NOT_IMPLEMENTED", "LIS_ERR_FILE_IO"};
LIS_INT err;

int indexes[2] = {1,eqSystemNumber};
infoStreamPrintWithEquationIndexes(LOG_LS, 0, indexes, "Start solving Linear System %d (size %d) at time %g with Lis Solver",
eqSystemNumber, (int) systemData->size,
data->localData[0]->timeValue);

/* set old values as start value for the iteration */
for(i=0; i<n; i++){
err = lis_vector_set_value(LIS_INS_VALUE, i, systemData->x[i], solverData->x);
}

rt_ext_tp_tick(&(solverData->timeClock));
if (0 == systemData->method)
DATA_LIS* sData = (DATA_LIS*)systemData->solverData;

/* Destroy the old matrix, create a new one and fill it with values */
lis_matrix_destroy(sData->A);
lis_matrix_create(0, &(sData->A));
lis_matrix_set_size(sData->A, sData->n_row, 0);
systemData->setA(data, systemData);
lis_matrix_set_type(sData->A, LIS_MATRIX_CSR);
lis_matrix_assemble(sData->A);

/* fill b with values */
systemData->setb(data, systemData);
for(i=0; i<sData->n_row; i++)
{

lis_matrix_set_size(solverData->A, solverData->n_row, 0);
/* set A matrix */
systemData->setA(data, systemData);
lis_matrix_assemble(solverData->A);

/* set b vector */
systemData->setb(data, systemData);

} else {
assertStreamPrint(data->threadData, 0, "Tearing system not implemented yet!");

lis_vector_set_value(LIS_INS_VALUE, i, systemData->b[i], sData->b);
}
infoStreamPrint(LOG_LS, 0, "### %f time to set Matrix A and vector b.", rt_ext_tp_tock(&(solverData->timeClock)));


rt_ext_tp_tick(&(solverData->timeClock));
err = lis_solve(solverData->A,solverData->b,solverData->x,solverData->solver);
infoStreamPrint(LOG_LS, 0, "Solve System: %f", rt_ext_tp_tock(&(solverData->timeClock)));

if (err){
warningStreamPrint(LOG_LS_V, 0, "lis_solve : %s(code=%lld)\n\n ", lis_returncode[err], err);
printLisMatrixCSR(solverData->A, solverData->n_row);
success = 0;
/* solve */
ret = lis_solve(sData->A,sData->b,sData->x,sData->solver);

/* handle return status */
switch(ret){
case LIS_SUCCESS:
success = 1;
break;
case LIS_ILL_OPTION:
case LIS_BREAKDOWN:
case LIS_OUT_OF_MEMORY:
case LIS_MAXITER:
case LIS_ERR_NOT_IMPLEMENTED:
case LIS_ERR_FILE_IO:
default:
success = 0;
break;
}


/* Log A*x=b */
if(ACTIVE_STREAM(LOG_LS_V))
{
char buffer[16384];

printLisMatrixCSR(solverData->A, n);

/* b vector */
infoStreamPrint(LOG_LS_V, 1, "b vector [%d]", n);
for(i=0; i<n; i++)
{
buffer[0] = 0;
sprintf(buffer, "%s%20.12g ", buffer, solverData->b->value[i]);
infoStreamPrint(LOG_LS_V, 0, "%s", buffer);
}
messageClose(LOG_LS_V);

messageClose(LOG_LS_V);
}


/* write solution */
lis_vector_get_values(solverData->x, 0, solverData->n_col, systemData->x);

if (ACTIVE_STREAM(LOG_LS_V)){
infoStreamPrint(LOG_LS_V, 1, "System %d numVars %d.", eqSystemNumber, modelInfoGetEquation(&data->modelData.modelDataXml,eqSystemNumber).numVar);
lis_vector_get_values(sData->x, 0, sData->n_col, systemData->x);

for(i = 0; i < systemData->size; ++i) {
infoStreamPrint(LOG_LS_V, 0, "[%d] %s = %g", i+1, modelInfoGetEquation(&data->modelData.modelDataXml,eqSystemNumber).vars[i], systemData->x[i]);
}

messageClose(LOG_LS);
}
lis_matrix_destroy(sData->A);

return success;
}

0 comments on commit 4da0c1d

Please sign in to comment.