Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 0ed97da

Browse files
WilliOpenModelica-Hudson
authored andcommitted
added -nlssMaxDensity and -nlssMinSize simulation flags
1 parent e66beae commit 0ed97da

File tree

7 files changed

+50
-9
lines changed

7 files changed

+50
-9
lines changed

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,14 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr
845845
linearSparseSolverMinSize = atoi(omc_flagValue[FLAG_LSS_MIN_SIZE]);
846846
infoStreamPrint(LOG_STDOUT, 0, "Maximum system size for using linear sparse solver changed to %d", linearSparseSolverMinSize);
847847
}
848+
if(omc_flag[FLAG_NLS_MAX_DENSITY]) {
849+
nonlinearSparseSolverMaxDensity = atof(omc_flagValue[FLAG_NLS_MAX_DENSITY]);
850+
infoStreamPrint(LOG_STDOUT, 0, "Maximum density for using non-linear sparse solver changed to %f", nonlinearSparseSolverMaxDensity);
851+
}
852+
if(omc_flag[FLAG_NLS_MIN_SIZE]) {
853+
nonlinearSparseSolverMinSize = atoi(omc_flagValue[FLAG_NLS_MIN_SIZE]);
854+
infoStreamPrint(LOG_STDOUT, 0, "Maximum system size for using non-linear sparse solver changed to %d", nonlinearSparseSolverMinSize);
855+
}
848856
if(omc_flag[FLAG_NEWTON_XTOL]) {
849857
newtonXTol = atof(omc_flagValue[FLAG_NEWTON_XTOL]);
850858
infoStreamPrint(LOG_STDOUT, 0, "Tolerance for updating solution vector in Newton solver changed to %g", newtonXTol);

SimulationRuntime/c/simulation/solver/model_help.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
int maxEventIterations = 20;
5454
double linearSparseSolverMaxDensity = 0.2;
5555
int linearSparseSolverMinSize = 4001;
56+
double nonlinearSparseSolverMaxDensity = 0.2;
57+
int nonlinearSparseSolverMinSize = 10001;
5658
double newtonXTol = 1e-12;
5759
double newtonFTol = 1e-12;
5860
const size_t SIZERINGBUFFER = 3;

SimulationRuntime/c/simulation/solver/model_help.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ extern "C" {
7777
extern int maxEventIterations;
7878
extern double linearSparseSolverMaxDensity;
7979
extern int linearSparseSolverMinSize;
80+
extern double nonlinearSparseSolverMaxDensity;
81+
extern int nonlinearSparseSolverMinSize;
8082
extern double newtonXTol;
8183
extern double newtonFTol;
8284
extern const size_t SIZERINGBUFFER;

SimulationRuntime/c/simulation/solver/nonlinearSystem.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "nonlinearSolverHomotopy.h"
4747
#include "simulation/simulation_info_json.h"
4848
#include "simulation/simulation_runtime.h"
49+
#include "simulation/solver/model_help.h"
4950

5051
/* for try and catch simulationJumpBuffer */
5152
#include "meta/meta_modelica.h"
@@ -338,7 +339,7 @@ int initializeNonlinearSystems(DATA *data, threadData_t *threadData)
338339
{
339340
TRACE_PUSH
340341
int i,j;
341-
int size;
342+
int size, nnz;
342343
NONLINEAR_SYSTEM_DATA *nonlinsys = data->simulationInfo->nonlinearSystemData;
343344
struct dataNewtonAndHybrid *mixedSolverData;
344345

@@ -363,6 +364,19 @@ int initializeNonlinearSystems(DATA *data, threadData_t *threadData)
363364
}
364365
}
365366

367+
#if !defined(OMC_MINIMAL_RUNTIME)
368+
if (nonlinsys[i].isPatternAvailable)
369+
{
370+
nnz = nonlinsys[i].sparsePattern.numberOfNoneZeros;
371+
372+
if(nnz/(double)(size*size)<=nonlinearSparseSolverMaxDensity && size >= nonlinearSparseSolverMinSize)
373+
{
374+
data->simulationInfo->nlsMethod = NLS_KINSOL;
375+
infoStreamPrint(LOG_STDOUT, 0, "Using sparse solver kinsol for nonlinear system %d,\nbecause density of %.2f remains under threshold of %.2f and size of %d exceeds threshold of %d.\nThe maximum density and the minimal system size for using sparse solvers can be specified\nusing the runtime flags '<-nlsMaxDensity=value>' and '<-nlsMinSize=value>'.", i, nnz/(double)(size*size), nonlinearSparseSolverMaxDensity, size, nonlinearSparseSolverMinSize);
376+
}
377+
}
378+
#endif
379+
366380
/* allocate system data */
367381
nonlinsys[i].nlsx = (double*) malloc(size*sizeof(double));
368382
nonlinsys[i].nlsxExtrapolation = (double*) malloc(size*sizeof(double));
@@ -394,6 +408,7 @@ int initializeNonlinearSystems(DATA *data, threadData_t *threadData)
394408
}
395409
}
396410
#endif
411+
397412
/* allocate solver data */
398413
switch(data->simulationInfo->nlsMethod)
399414
{

SimulationRuntime/c/simulation_data.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ typedef struct NONLINEAR_SYSTEM_DATA
269269
int (*initialAnalyticalJacobian)(void*, threadData_t*);
270270
modelica_integer jacobianIndex;
271271

272-
SPARSE_PATTERN sparsePattern; /* sparse pattern if no jacobian is available */
272+
SPARSE_PATTERN sparsePattern; /* sparse pattern if no jacobian is available */
273273
modelica_boolean isPatternAvailable;
274274

275275
void (*residualFunc)(void**, const double*, double*, const int*);
@@ -289,15 +289,15 @@ typedef struct NONLINEAR_SYSTEM_DATA
289289

290290
modelica_real residualError; /* not used */
291291
modelica_boolean solved; /* 1: solved in current step - else not */
292-
modelica_real lastTimeSolved; /* save last successful solved point in time */
292+
modelica_real lastTimeSolved; /* save last successful solved point in time */
293293

294294
/* statistics */
295-
unsigned long numberOfCall; /* number of solving calls of this system */
296-
unsigned long numberOfFEval; /* number of function evaluations of this system */
297-
unsigned long numberOfIterations; /* number of iteration of non-linear solvers of this system */
298-
double totalTime; /* save the totalTime */
299-
rtclock_t totalTimeClock; /* time clock for the totalTime */
300-
void* csvData; /* information to save csv data */
295+
unsigned long numberOfCall; /* number of solving calls of this system */
296+
unsigned long numberOfFEval; /* number of function evaluations of this system */
297+
unsigned long numberOfIterations; /* number of iteration of non-linear solvers of this system */
298+
double totalTime; /* save the totalTime */
299+
rtclock_t totalTimeClock; /* time clock for the totalTime */
300+
void* csvData; /* information to save csv data */
301301
} NONLINEAR_SYSTEM_DATA;
302302
#else
303303
typedef void* NONLINEAR_SYSTEM_DATA;

SimulationRuntime/c/util/simulation_options.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
8585
/* FLAG_NEWTON_STRATEGY */ "newton",
8686
/* FLAG_NLS */ "nls",
8787
/* FLAG_NLS_INFO */ "nlsInfo",
88+
/* FLAG_NLS_MAX_DENSITY */ "nlssMaxDensity",
89+
/* FLAG_NLS_MIN_SIZE */ "nlssMinSize",
8890
/* FLAG_NLS_LS */ "nlsLS",
8991
/* FLAG_NOEMIT */ "noemit",
9092
/* FLAG_NOEQUIDISTANT_GRID */ "noEquidistantTimeGrid",
@@ -166,6 +168,8 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
166168
/* FLAG_NEWTON_STRATEGY */ "value specifies the damping strategy for the newton solver",
167169
/* FLAG_NLS */ "value specifies the nonlinear solver",
168170
/* FLAG_NLS_INFO */ "outputs detailed information about solving process of non-linear systems into csv files.",
171+
/* FLAG_NLS_MAX_DENSITY */ "[double (default 0.2)] value specifies the maximum density for using a non-linear sparse solver",
172+
/* FLAG_NLS_MIN_SIZE */ "[int (default 10001)] value specifies the minimum system size for using a non-linear sparse solver",
169173
/* FLAG_NLS_LS */ "value specifies the linear solver used by the non-linear solver",
170174
/* FLAG_NOEMIT */ "do not emit any results to the result file",
171175
/* FLAG_NOEQUIDISTANT_GRID */ "stores results not in equidistant time grid as given by stepSize or numberOfIntervals, instead the variable step size of dassl is used.",
@@ -337,6 +341,12 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
337341
" * kinsol\n"
338342
" * newton\n"
339343
" * mixed",
344+
/* FLAG_NLS_MAX_DENSITY */
345+
" Value specifies the maximum density for using a non-linear sparse solver.\n"
346+
" The value is a Double with default value 0.2.",
347+
/* FLAG_NLS_MIN_SIZE */
348+
" Value specifies the minimum system size for using a non-linear sparse solver.\n"
349+
" The value is an Integer with default value 10001.",
340350
/* FLAG_NLS_INFO */
341351
" Outputs detailed information about solving process of non-linear systems into csv files.",
342352
/* FLAG_NLS_LS */
@@ -457,6 +467,8 @@ const int FLAG_TYPE[FLAG_MAX] = {
457467
/* FLAG_NEWTON_XTOL */ FLAG_TYPE_OPTION,
458468
/* FLAG_NEWTON_STRATEGY */ FLAG_TYPE_OPTION,
459469
/* FLAG_NLS */ FLAG_TYPE_OPTION,
470+
/* FLAG_NLS_MAX_DENSITY */ FLAG_TYPE_OPTION,
471+
/* FLAG_NLS_MIN_SIZE */ FLAG_TYPE_OPTION,
460472
/* FLAG_NLS_INFO */ FLAG_TYPE_FLAG,
461473
/* FLAG_NLS_LS */ FLAG_TYPE_OPTION,
462474
/* FLAG_NOEMIT */ FLAG_TYPE_FLAG,

SimulationRuntime/c/util/simulation_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ enum _FLAG
9292
FLAG_NEWTON_XTOL,
9393
FLAG_NEWTON_STRATEGY,
9494
FLAG_NLS,
95+
FLAG_NLS_MAX_DENSITY,
96+
FLAG_NLS_MIN_SIZE,
9597
FLAG_NLS_INFO,
9698
FLAG_NLS_LS,
9799
FLAG_NOEMIT,

0 commit comments

Comments
 (0)