Skip to content

Commit

Permalink
Add flag to emit solver steps to the result file
Browse files Browse the repository at this point in the history
simflag: -steps
  • Loading branch information
ptaeuber authored and OpenModelica-Hudson committed Jun 2, 2016
1 parent 81b6765 commit 38e81d6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
22 changes: 19 additions & 3 deletions SimulationRuntime/c/simulation/results/simulation_result_mat.cpp
Expand Up @@ -78,6 +78,7 @@ static const VAR_INFO** calcDataNames(simulation_result *self,DATA *data,int dat

static const struct VAR_INFO timeValName = {0,-1,"time","Simulation time [s]",{"",-1,-1,-1,-1}};
static const struct VAR_INFO cpuTimeValName = {0,-1,"$cpuTime","cpu time [s]",{"",-1,-1,-1,-1}};
static const struct VAR_INFO solverStepsValName = {0,-1,"$solverSteps","number of steps taken by the integrator",{"",-1,-1,-1,-1}};

static int calcDataSize(simulation_result *self,DATA *data)
{
Expand All @@ -89,6 +90,9 @@ static int calcDataSize(simulation_result *self,DATA *data)
if(self->cpuTime)
sz++;

if(omc_flag[FLAG_SOLVER_STEPS])
sz++;

for(int i = 0; i < modelData->nVariablesReal; i++)
if(!modelData->realVarsData[i].filterOutput)
{
Expand Down Expand Up @@ -138,10 +142,16 @@ static const VAR_INFO** calcDataNames(simulation_result *self,DATA *data,int dat
int curVar = 0;
int sz = 1;
names[curVar++] = &timeValName;

if(self->cpuTime)
names[curVar++] = &cpuTimeValName;

if(omc_flag[FLAG_SOLVER_STEPS])
names[curVar++] = &solverStepsValName;

for(int i = 0; i < modelData->nVariablesReal; i++) if(!modelData->realVarsData[i].filterOutput)
names[curVar++] = &(modelData->realVarsData[i].info);

/* put sensitivity analysis also to the result file */
if (omc_flag[FLAG_IDAS])
{
Expand Down Expand Up @@ -271,7 +281,7 @@ void mat4_init(simulation_result *self,DATA *data, threadData_t *threadData)
/* remember data2HdrPos */
matData->data2HdrPos = matData->fp.tellp();
/* write `data_2' header */
mat_writeMatVer4MatrixHeader(self,data,threadData,"data_2", matData->r_indx_map.size() + matData->i_indx_map.size() + matData->b_indx_map.size() + matData->negatedboolaliases + 1 /* add one more for timeValue*/ + self->cpuTime + nSensitivities, 0, sizeof(double));
mat_writeMatVer4MatrixHeader(self,data,threadData,"data_2", matData->r_indx_map.size() + matData->i_indx_map.size() + matData->b_indx_map.size() + matData->negatedboolaliases + 1 /* add one more for timeValue*/ + self->cpuTime + /* add one more for solverSteps*/ + omc_flag[FLAG_SOLVER_STEPS] + nSensitivities, 0, sizeof(double));

free(doubleMatrix);
free(intMatrix);
Expand Down Expand Up @@ -308,7 +318,7 @@ void mat4_free(simulation_result *self,DATA *data, threadData_t *threadData)
try
{
matData->fp.seekp(matData->data2HdrPos);
mat_writeMatVer4MatrixHeader(self,data,threadData,"data_2", matData->r_indx_map.size() + matData->i_indx_map.size() + matData->b_indx_map.size() + matData->negatedboolaliases + 1 /* add one more for timeValue*/ + self->cpuTime + nSensitivities, matData->ntimepoints, sizeof(double));
mat_writeMatVer4MatrixHeader(self,data,threadData,"data_2", matData->r_indx_map.size() + matData->i_indx_map.size() + matData->b_indx_map.size() + matData->negatedboolaliases + 1 /* add one more for timeValue*/ + self->cpuTime + /* add one more for solverSteps*/ + omc_flag[FLAG_SOLVER_STEPS] + nSensitivities, matData->ntimepoints, sizeof(double));
matData->fp.close();
}
catch (...)
Expand All @@ -335,10 +345,16 @@ void mat4_emit(simulation_result *self,DATA *data, threadData_t *threadData)
although ofstream does have some buffering, but it is not enough and
not for this purpose */
matData->fp.write((char*)&(data->localData[0]->timeValue), sizeof(double));

if(self->cpuTime)
matData->fp.write((char*)&cpuTimeValue, sizeof(double));

if(omc_flag[FLAG_SOLVER_STEPS])
matData->fp.write((char*)&(data->simulationInfo->solverSteps), sizeof(double));

for(int i = 0; i < data->modelData->nVariablesReal; i++) if(!data->modelData->realVarsData[i].filterOutput)
matData->fp.write((char*)&(data->localData[0]->realVars[i]),sizeof(double));

/* put parameter sensitivity analysis also to the result file */
if (omc_flag[FLAG_IDAS])
{
Expand Down Expand Up @@ -490,7 +506,7 @@ void generateDataInfo(simulation_result *self, DATA *data, threadData_t *threadD
dataInfo = (int*) calloc(rows*cols,sizeof(int));
assertStreamPrint(threadData, 0!=dataInfo,"Cannot alloc memory");
/* continuous and discrete variables, including time */
for(size_t i = 0; i < (size_t)(matData->r_indx_map.size() + matData->i_indx_map.size() + matData->b_indx_map.size() + 1 /* add one more for timeValue*/ + self->cpuTime + nSensitivities); ++i) {
for(size_t i = 0; i < (size_t)(matData->r_indx_map.size() + matData->i_indx_map.size() + matData->b_indx_map.size() + 1 /* add one more for timeValue*/ + self->cpuTime /* add one more for solverSteps*/ + omc_flag[FLAG_SOLVER_STEPS] + nSensitivities); ++i) {
/* row 1 - which table */
dataInfo[ccol++] = 2;
/* row 2 - index of var in table (variable 'Time' have index 1) */
Expand Down
18 changes: 18 additions & 0 deletions SimulationRuntime/c/simulation/solver/solver_main.c
Expand Up @@ -101,17 +101,23 @@ int solver_main_step(DATA* data, threadData_t *threadData, SOLVER_INFO* solverIn
{
case S_EULER:
retVal = euler_ex_step(data, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
TRACE_POP
return retVal;
case S_HEUN:
case S_RUNGEKUTTA:
retVal = rungekutta_step(data, threadData, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
TRACE_POP
return retVal;

#if !defined(OMC_MINIMAL_RUNTIME)
case S_DASSL:
retVal = dassl_step(data, threadData, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
TRACE_POP
return retVal;
#endif
Expand All @@ -124,6 +130,8 @@ int solver_main_step(DATA* data, threadData_t *threadData, SOLVER_INFO* solverIn
solverInfo->solverMethod = S_EULER;
retVal = euler_ex_step(data, solverInfo);
}
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
TRACE_POP
return retVal;
#endif
Expand All @@ -135,18 +143,26 @@ int solver_main_step(DATA* data, threadData_t *threadData, SOLVER_INFO* solverIn
case S_LOBATTO4:
case S_LOBATTO6:
retVal = radau_lobatto_step(data, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
TRACE_POP
return retVal;
case S_IDA:
retVal = ida_solver_step(data, threadData, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
TRACE_POP
return retVal;
#endif
case S_SYM_EULER:
retVal = sym_euler_im_step(data, threadData, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
return retVal;
case S_SYM_IMP_EULER:
retVal = sym_euler_im_with_step_size_control_step(data, threadData, solverInfo);
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = solverInfo->solverStats[0] + solverInfo->solverStatsTmp[0];
return retVal;
}

Expand Down Expand Up @@ -717,6 +733,8 @@ int solver_main(DATA* data, threadData_t *threadData, const char* init_initMetho
omc_alloc_interface.collect_a_little();
} else {
/* starts the simulation main loop - standard solver interface */
if(omc_flag[FLAG_SOLVER_STEPS])
data->simulationInfo->solverSteps = 0;
if(solverInfo.solverMethod != S_OPTIMIZATION) {
sim_result.emit(&sim_result,data,threadData);
}
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/c/simulation_data.h
Expand Up @@ -562,6 +562,8 @@ typedef struct SIMULATION_INFO
modelica_boolean solveContinuous; /* =1 during the continuous integration to avoid zero-crossings jumps, 0 otherwise. */
modelica_boolean noThrowDivZero; /* =1 if solving nonlinear system to avoid THROW for division by zero, 0 otherwise. */

double solverSteps; /* Number of integration steps so far for writing to the result file*/

void** extObjs; /* External objects */

double nextSampleEvent; /* point in time of next sample-call */
Expand Down
9 changes: 7 additions & 2 deletions SimulationRuntime/c/util/simulation_options.c
Expand Up @@ -96,6 +96,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
/* FLAG_R */ "r",
/* FLAG_RT */ "rt",
/* FLAG_S */ "s",
/* FLAG_SOLVER_STEPS */ "steps",
/* FLAG_UP_HESSIAN */ "keepHessian",
/* FLAG_W */ "w",

Expand All @@ -108,7 +109,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
/* FLAG_ABORT_SLOW */ "aborts if the simulation chatters",
/* FLAG_ALARM */ "aborts after the given number of seconds (0 disables)",
/* FLAG_CLOCK */ "selects the type of clock to use -clock=RT, -clock=CYC or -clock=CPU",
/* FLAG_CPU */ "dumps the cpu-time into the results-file",
/* FLAG_CPU */ "dumps the cpu-time into the result file",
/* FLAG_CSV_OSTEP */ "value specifies csv-files for debuge values for optimizer step",
/* FLAG_DASSL_NO_RESTART */ "flag deactivates the restart of dassl after an event is performed.",
/* FLAG_DASSL_NO_ROOTFINDING */ "flag deactivates the internal root finding procedure of dassl.",
Expand Down Expand Up @@ -168,6 +169,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
/* FLAG_R */ "value specifies a new result file than the default Model_res.mat",
/* FLAG_RT */ "value specifies the scaling factor for real-time synchronization (0 disables)",
/* FLAG_S */ "value specifies the solver",
/* FLAG_SOLVER_STEPS */ "dumps the number of integration steps into the result file",
/* FLAG_UP_HESSIAN */ "value specifies the number of steps, which keep hessian matrix constant",
/* FLAG_W */ "shows all warnings even if a related log-stream is inactive",

Expand All @@ -186,7 +188,7 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
" * CYC (cpu cycles measured with RDTSC)\n"
" * CPU (process-based CPU-time)",
/* FLAG_CPU */
" Dumps the cpu-time into the result-file using the variable named $cpuTime",
" Dumps the cpu-time into the result file using the variable named $cpuTime",
/* FLAG_CSV_OSTEP */
"value specifies csv-files for debuge values for optimizer step",
/* FLAG_DASSL_NO_RESTART */
Expand Down Expand Up @@ -351,6 +353,8 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
" A value > 1 means the simulation takes a longer time to simulate.\n",
/* FLAG_S */
" Value specifies the solver (integration method).",
/* FLAG_SOLVER_STEPS */
" dumps the number of integration steps into the result file",
/* FLAG_UP_HESSIAN */
" Value specifies the number of steps, which keep hessian matrix constant.",
/* FLAG_W */
Expand Down Expand Up @@ -425,6 +429,7 @@ const int FLAG_TYPE[FLAG_MAX] = {
/* FLAG_R */ FLAG_TYPE_OPTION,
/* FLAG_RT */ FLAG_TYPE_OPTION,
/* FLAG_S */ FLAG_TYPE_OPTION,
/* FLAG_SOLVER_STEPS */ FLAG_TYPE_FLAG,
/* FLAG_UP_HESSIAN */ FLAG_TYPE_OPTION,
/* FLAG_W */ FLAG_TYPE_FLAG
};
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/util/simulation_options.h
Expand Up @@ -104,6 +104,7 @@ enum _FLAG
FLAG_R,
FLAG_RT,
FLAG_S,
FLAG_SOLVER_STEPS,
FLAG_UP_HESSIAN,
FLAG_W,

Expand Down

0 comments on commit 38e81d6

Please sign in to comment.