Skip to content

Commit

Permalink
- improve maintainability of NLS code
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16289 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Jun 10, 2013
1 parent 16eb2cc commit 01e3a84
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
24 changes: 11 additions & 13 deletions SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -238,27 +238,25 @@ void setGlobalVerboseLevel(int argc, char**argv)

int getNonlinearSolverMethod(int argc, char**argv)
{
int i;
const char *cflags = omc_flagValue[FLAG_NLS];
const string *method = cflags ? new string(cflags) : NULL;

if(!method)
return NS_HYBRID; /* default method */
return NLS_HYBRID; /* default method */

if(*method == string("hybrid"))
return NS_HYBRID;
else if(*method == string("kinsol"))
return NS_KINSOL;
else if(*method == string("newton"))
return NS_NEWTON;
for(i=1; i<NLS_MAX; ++i)
if(*method == NLS_NAME[i])
return i;

WARNING1(LOG_STDOUT, "unrecognized option -nls %s", method->c_str());
WARNING1(LOG_STDOUT, "unrecognized option -nls=%s", method->c_str());
WARNING(LOG_STDOUT, "current options are:");
INDENT(LOG_STDOUT);
WARNING2(LOG_STDOUT, "%-18s [%s]", "hybrid", "default method");
WARNING2(LOG_STDOUT, "%-18s [%s]", "kinsol", "sundials/kinsol");
WARNING2(LOG_STDOUT, "%-18s [%s]", "newton", "newton Raphson");
for(i=1; i<NLS_MAX; ++i)
WARNING2(LOG_STDOUT, "%-18s [%s]", NLS_NAME[i], NLS_DESC[i]);
THROW("see last warning");
return NS_NONE;

return NLS_NONE;
}

int getlinearSolverMethod(int argc, char**argv)
Expand All @@ -277,7 +275,7 @@ int getlinearSolverMethod(int argc, char**argv)
INDENT(LOG_STDOUT);
WARNING2(LOG_STDOUT, "%-18s [%s]", "lapack", "default method");
THROW("see last warning");
return NS_NONE;
return LS_NONE;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/solver/model_help.c
Expand Up @@ -700,7 +700,7 @@ void initializeDataStruc(DATA *data)
data->simulationInfo.samples = (modelica_boolean*) calloc(data->modelData.nSamples, sizeof(modelica_boolean));

/* set default solvers for algebraic loops */
data->simulationInfo.nlsMethod = NS_HYBRID;
data->simulationInfo.nlsMethod = NLS_HYBRID;
data->simulationInfo.lsMethod = LS_LAPACK;
data->simulationInfo.mixedMethod = MIXED_SEARCH;

Expand Down
Expand Up @@ -198,8 +198,7 @@ static int wrapper_fvec_newton(integer* n, double* x, double* f, integer* iflag,
/* NONLINEAR_SYSTEM_DATA* systemData = &(((DATA*)data)->simulationInfo.nonlinearSystemData[currentSys]); */
/* DATA_NEWTON* solverData = (DATA_NEWTON*)(systemData->solverData); */

(*((DATA*)data)->simulationInfo.nonlinearSystemData[currentSys].residualFunc)(data,
x, f, iflag);
(*((DATA*)data)->simulationInfo.nonlinearSystemData[currentSys].residualFunc)(data, x, f, iflag);
return 0;
}

Expand Down
40 changes: 30 additions & 10 deletions SimulationRuntime/c/simulation/solver/nonlinearSystem.c
Expand Up @@ -45,6 +45,27 @@

extern doublereal enorm_(integer *n, doublereal *x);


const char *NLS_NAME[NLS_MAX+1] = {
"NLS_UNKNOWN",

/* NLS_HYBRID */ "hybrid",
/* NLS_KINSOL */ "kinsol",
/* NLS_NEWTON */ "newton",

"NLS_MAX"
};

const char *NLS_DESC[NLS_MAX+1] = {
"unknown",

/* NLS_HYBRID */ "default method",
/* NLS_KINSOL */ "sundials/kinsol",
/* NLS_NEWTON */ "Newton Raphson",

"NLS_MAX"
};

/*! \fn int allocateNonlinearSystem(DATA *data)
*
* This function allocates memory for all nonlinear systems.
Expand Down Expand Up @@ -72,7 +93,6 @@ int allocateNonlinearSystem(DATA *data)
}
}


/* allocate system data */
nonlinsys[i].nlsx = (double*) malloc(size*sizeof(double));
nonlinsys[i].nlsxExtrapolation = (double*) malloc(size*sizeof(double));
Expand All @@ -93,13 +113,13 @@ int allocateNonlinearSystem(DATA *data)
{
switch(data->simulationInfo.nlsMethod)
{
case NS_HYBRID:
case NLS_HYBRID:
allocateHybrdData(size, &nonlinsys[i].solverData);
break;
case NS_KINSOL:
case NLS_KINSOL:
nls_kinsol_allocate(data, &nonlinsys[i]);
break;
case NS_NEWTON:
case NLS_NEWTON:
allocateNewtonData(size, &nonlinsys[i].solverData);
break;
default:
Expand Down Expand Up @@ -141,13 +161,13 @@ int freeNonlinearSystem(DATA *data)
{
switch(data->simulationInfo.nlsMethod)
{
case NS_HYBRID:
case NLS_HYBRID:
freeHybrdData(&nonlinsys[i].solverData);
break;
case NS_KINSOL:
case NLS_KINSOL:
nls_kinsol_free(&nonlinsys[i]);
break;
case NS_NEWTON:
case NLS_NEWTON:
freeNewtonData(&nonlinsys[i].solverData);
break;
default:
Expand Down Expand Up @@ -193,13 +213,13 @@ int solve_nonlinear_system(DATA *data, int sysNumber)
{
switch(data->simulationInfo.nlsMethod)
{
case NS_HYBRID:
case NLS_HYBRID:
success = solveHybrd(data, sysNumber);
break;
case NS_KINSOL:
case NLS_KINSOL:
success = nonlinearSolve_kinsol(data, sysNumber);
break;
case NS_NEWTON:
case NLS_NEWTON:
success = solveNewton(data, sysNumber);
break;
default:
Expand Down
16 changes: 11 additions & 5 deletions SimulationRuntime/c/simulation/solver/nonlinearSystem.h
Expand Up @@ -52,13 +52,19 @@ extern "C" {

enum NONLINEAR_SOLVER
{
NS_NONE = 0,
NS_HYBRID,
NS_KINSOL,
NS_NEWTON,
NS_MAX
NLS_NONE = 0,

NLS_HYBRID,
NLS_KINSOL,
NLS_NEWTON,

NLS_MAX
};


extern const char *NLS_NAME[NLS_MAX+1];
extern const char *NLS_DESC[NLS_MAX+1];

typedef void* NLS_SOLVER_DATA;

int allocateNonlinearSystem(DATA *data);
Expand Down

0 comments on commit 01e3a84

Please sign in to comment.