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

Commit d981c30

Browse files
lochelOpenModelica-Hudson
authored andcommitted
New feature: detect steady state
- New runtime flag -steadyState aborts simulation as soon as steady state is detected. - New runtime flag -steadyStateTol=<value> overrides default tolerance to detect steady state.
1 parent 4c362d1 commit d981c30

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,11 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr
863863
infoStreamPrint(LOG_STDOUT, 0, "Tolerance for accepting accuracy in Newton solver changed to %g", newtonFTol);
864864
}
865865

866+
if(omc_flag[FLAG_STEADY_STATE_TOL]) {
867+
steadyStateTol = atof(omc_flagValue[FLAG_STEADY_STATE_TOL]);
868+
infoStreamPrint(LOG_STDOUT, 0, "Tolerance for steady state detection changed to %g", steadyStateTol);
869+
}
870+
866871
rt_tick(SIM_TIMER_INIT_XML);
867872
read_input_xml(data->modelData, data->simulationInfo);
868873
rt_accumulate(SIM_TIMER_INIT_XML);

SimulationRuntime/c/simulation/solver/model_help.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ double nonlinearSparseSolverMaxDensity = 0.2;
5757
int nonlinearSparseSolverMinSize = 10001;
5858
double newtonXTol = 1e-12;
5959
double newtonFTol = 1e-12;
60+
double steadyStateTol = 1e-3;
6061
const size_t SIZERINGBUFFER = 3;
6162
int compiledInDAEMode = 0;
6263
int compiledWithSymSolver = 0;

SimulationRuntime/c/simulation/solver/model_help.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ extern double nonlinearSparseSolverMaxDensity;
8181
extern int nonlinearSparseSolverMinSize;
8282
extern double newtonXTol;
8383
extern double newtonFTol;
84+
extern double steadyStateTol;
8485
extern const size_t SIZERINGBUFFER;
8586
extern int compiledInDAEMode;
8687
extern int compiledWithSymSolver;

SimulationRuntime/c/simulation/solver/perform_simulation.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,27 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_
361361
}
362362
#endif
363363

364+
/* check for steady state */
365+
if (omc_flag[FLAG_STEADY_STATE])
366+
{
367+
if (0 < data->modelData->nStates)
368+
{
369+
int i;
370+
double maxDer = 0.0;
371+
double currDer;
372+
for(i=data->modelData->nStates; i<2*data->modelData->nStates; ++i)
373+
{
374+
currDer = fabs(data->localData[0]->realVars[i] / data->modelData->realVarsData[i].attribute.nominal);
375+
if(maxDer < currDer)
376+
maxDer = currDer;
377+
}
378+
if(maxDer < steadyStateTol)
379+
throwStreamPrint(threadData, "steady state reached at time = %g\n * max(|d(x_i)/dt|/nominal(x_i)) = %g\n * relative tolerance = %g", solverInfo->currentTime, maxDer, steadyStateTol);
380+
}
381+
else
382+
throwStreamPrint(threadData, "No states in model. Flag -steadyState can only be used if states are present.");
383+
}
384+
364385
omc_alloc_interface.collect_a_little();
365386

366387
/* try */
@@ -472,6 +493,9 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_
472493

473494
fmtClose(&fmt);
474495

496+
if (omc_flag[FLAG_STEADY_STATE])
497+
warningStreamPrint(LOG_STDOUT, 0, "Steady state has not been reached.\nThis may be due to too restrictive relative tolerance (%g) or short stopTime (%g).", steadyStateTol, simInfo->stopTime);
498+
475499
TRACE_POP
476500
return retValue;
477501
}

SimulationRuntime/c/util/simulation_options.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
109109
/* FLAG_RT */ "rt",
110110
/* FLAG_S */ "s",
111111
/* FLAG_SOLVER_STEPS */ "steps",
112+
/* FLAG_STEADY_STATE */ "steadyState",
113+
/* FLAG_STEADY_STATE_TOL */ "steadyStateTol",
112114
/* FLAG_UP_HESSIAN */ "keepHessian",
113115
/* FLAG_W */ "w",
114116

@@ -194,6 +196,8 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
194196
/* FLAG_RT */ "value specifies the scaling factor for real-time synchronization (0 disables)",
195197
/* FLAG_S */ "value specifies the integration method",
196198
/* FLAG_SOLVER_STEPS */ "dumps the number of integration steps into the result file",
199+
/* FLAG_STEADY_STATE */ "aborts if steady state is reached",
200+
/* FLAG_STEADY_STATE_TOL */ "[double (default 1e-3)] This relative tolerance is used to detect steady state.",
197201
/* FLAG_UP_HESSIAN */ "value specifies the number of steps, which keep hessian matrix constant",
198202
/* FLAG_W */ "shows all warnings even if a related log-stream is inactive",
199203

@@ -416,6 +420,10 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
416420
" Value specifies the integration method.",
417421
/* FLAG_SOLVER_STEPS */
418422
" dumps the number of integration steps into the result file",
423+
/* FLAG_STEADY_STATE */
424+
" Aborts the simulation if steady state is reached.",
425+
/* FLAG_STEADY_STATE_TOL */
426+
"This relative tolerance is used to detect steady state: max(|d(x_i)/dt|/nominal(x_i)) < steadyStateTol",
419427
/* FLAG_UP_HESSIAN */
420428
" Value specifies the number of steps, which keep Hessian matrix constant.",
421429
/* FLAG_W */
@@ -503,6 +511,8 @@ const int FLAG_TYPE[FLAG_MAX] = {
503511
/* FLAG_RT */ FLAG_TYPE_OPTION,
504512
/* FLAG_S */ FLAG_TYPE_OPTION,
505513
/* FLAG_SOLVER_STEPS */ FLAG_TYPE_FLAG,
514+
/* FLAG_STEADY_STATE */ FLAG_TYPE_FLAG,
515+
/* FLAG_STEADY_STATE_TOL */ FLAG_TYPE_OPTION,
506516
/* FLAG_UP_HESSIAN */ FLAG_TYPE_OPTION,
507517
/* FLAG_W */ FLAG_TYPE_FLAG
508518
};

SimulationRuntime/c/util/simulation_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ enum _FLAG
117117
FLAG_RT,
118118
FLAG_S,
119119
FLAG_SOLVER_STEPS,
120+
FLAG_STEADY_STATE,
121+
FLAG_STEADY_STATE_TOL,
120122
FLAG_UP_HESSIAN,
121123
FLAG_W,
122124

0 commit comments

Comments
 (0)