Skip to content

Commit

Permalink
Add simulation flag lv_system (#11102)
Browse files Browse the repository at this point in the history
Fixes #10455

- Using `-lv_system=123,456` only prints logs for (non)linear systems
  with equationIndex 123 and 456.

- I also added `LOG_MIXED` for logging mixed systems so they don't show
  up when activating `LOG_NLS`.
  
- TODO This might not work properly when `lv_time` is on simultaneously.
  • Loading branch information
phannebohm committed Aug 30, 2023
1 parent c56ae67 commit ba314ac
Show file tree
Hide file tree
Showing 34 changed files with 244 additions and 170 deletions.
88 changes: 87 additions & 1 deletion OMCompiler/SimulationRuntime/c/simulation/simulation_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void setGlobalLoggingTime(SIMULATION_INFO *simulationInfo)
/* Check if lv_time flag is given */
if (flagStr==NULL || *flagStr=='\0')
{
/* default activated --> Log everything*/
/* default activated --> Log everything */
simulationInfo->useLoggingTime = 0;
return;
}
Expand Down Expand Up @@ -829,6 +829,89 @@ static int callSolver(DATA* simData, threadData_t *threadData, string init_initM
return retVal;
}

/**
* @brief Set log activation from equationIndex and list from lv_system.
*
* @param data Data object
*/
static void setLVSystems(DATA *data, threadData_t *threadData)
{
int i;
int N = 0; /* largest equationIndex */
modelica_boolean* a = NULL;
const char* p;
char* endptr;

MIXED_SYSTEM_DATA *mixedsys = data->simulationInfo->mixedSystemData;
LINEAR_SYSTEM_DATA *linsys = data->simulationInfo->linearSystemData;
NONLINEAR_SYSTEM_DATA *nonlinsys = data->simulationInfo->nonlinearSystemData;

if (omc_flag[FLAG_LV_SYSTEM]) {
/* get largest equationIndex */
for (i = 0; i < data->modelData->nMixedSystems; ++i)
if (mixedsys[i].equationIndex > N)
N = mixedsys[i].equationIndex;
for (i = 0; i < data->modelData->nLinearSystems; ++i)
if (linsys[i].equationIndex > N)
N = linsys[i].equationIndex;
for (i = 0; i < data->modelData->nNonLinearSystems; ++i)
if (nonlinsys[i].equationIndex > N)
N = nonlinsys[i].equationIndex;

/* initialize a with FALSE */
a = (modelica_boolean*) calloc(N+1, sizeof(modelica_boolean));
assertStreamPrint(threadData, NULL != a, "setLVSystems: Out of memory.");

/* set a[i] to true for all i in lv_system */
p = omc_flagValue[FLAG_LV_SYSTEM];
do {
errno = 0;
i = strtol(p, &endptr, 10);
if (errno == ERANGE) {
throwStreamPrint(threadData,
"setLVSystems: %s takes equation indices (got '%s')",
endptr, omc_flagValue[FLAG_LV_SYSTEM]);
}
if (i > N) {
throwStreamPrint(threadData,
"setLVSystems: %d is not a valid equation index", i);
}
a[i] = TRUE;
p = endptr;
} while(*(p++) == ',');

/* activate corresponding system */
for (i = 0; i < data->modelData->nMixedSystems; ++i) {
mixedsys[i].logActive = a[mixedsys[i].equationIndex];
a[mixedsys[i].equationIndex] = FALSE;
}
for (i = 0; i < data->modelData->nLinearSystems; ++i) {
linsys[i].logActive = a[linsys[i].equationIndex];
a[linsys[i].equationIndex] = FALSE;
}
for (i = 0; i < data->modelData->nNonLinearSystems; ++i) {
nonlinsys[i].logActive = a[nonlinsys[i].equationIndex];
a[nonlinsys[i].equationIndex] = FALSE;
}

for (i = 0; i <= N; ++i){
if (a[i]) {
throwStreamPrint(threadData,
"setLVSystems: %d is not a valid equation index.", i);
}
}
/* done */
free(a);
} else {
/* if no list is given then all systems are active */
for (i = 0; i < data->modelData->nMixedSystems; ++i)
mixedsys[i].logActive = TRUE;
for (i = 0; i < data->modelData->nLinearSystems; ++i)
linsys[i].logActive = TRUE;
for (i = 0; i < data->modelData->nNonLinearSystems; ++i)
nonlinsys[i].logActive = TRUE;
}
}

/**
* Initialization is the same for interactive or non-interactive simulation
Expand Down Expand Up @@ -1134,6 +1217,9 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr
initializeLinearSystems(data, threadData);
initializeNonlinearSystems(data, threadData);

/* set log activation from equationIndex and lv_system */
setLVSystems(data, threadData);

sim_noemit = omc_flag[FLAG_NOEMIT];

#ifndef NO_INTERACTIVE_DEPENDENCY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ void resetKinsolMemory(NLS_KINSOL_DATA *kinsolData) {
}

/* Set error handler and print level */
if (ACTIVE_STREAM(LOG_NLS_V)) {
if (!nlsData->logActive) {
printLevel = 0;
} else if (ACTIVE_STREAM(LOG_NLS_V)) {
printLevel = 3;
} else if (ACTIVE_STREAM(LOG_NLS)) {
printLevel = 1;
Expand Down Expand Up @@ -862,7 +864,7 @@ static void nlsKinsolFScaling(DATA *data, NLS_KINSOL_DATA *kinsolData,
nlsKinsolResiduals(x, kinsolData->fTmp, kinsolData->userData);
nlsSparseJac(x, kinsolData->fTmp, spJac, kinsolData->userData, tmp1, tmp2);
}
/* Copy the sparse Jacobian into the kinsol data structure for later use*/
/* Copy the sparse Jacobian into the kinsol data structure for later use */
SUNMatCopy_Sparse(spJac, kinsolData->J);
}
/* Scale the current Jacobian */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ int solve_linear_system(DATA *data, threadData_t *threadData, int sysNumber, dou
int logLevel;
LINEAR_SYSTEM_DATA* linsys = &(data->simulationInfo->linearSystemData[sysNumber]);

if (!linsys->logActive) {
deactivateLogging();
}

rt_ext_tp_tick(&(linsys->totalTimeClock));

/* enable to avoid division by zero */
Expand Down Expand Up @@ -688,6 +692,10 @@ int solve_linear_system(DATA *data, threadData_t *threadData, int sysNumber, dou

retVal = check_linear_solution(data, 1, sysNumber);

if (!linsys->logActive) {
reactivateLogging();
}

TRACE_POP
return retVal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ int solveMixedSearch(DATA *data, int sysNumber)
int mixedIterations = 0;
int success = 0;

debugStreamPrint(LOG_NLS, 1, "\n#### Start solver mixed equation system at time %f.", data->localData[0]->timeValue);
debugStreamPrint(LOG_MIXED, 1, "\n#### Start solver mixed equation system at time %f.", data->localData[0]->timeValue);

memset(solverData->stateofSearch, 0, systemData->size);

Expand All @@ -211,14 +211,14 @@ int solveMixedSearch(DATA *data, int sysNumber)


found_solution = systemData->continuous_solution;
debugStreamPrint(LOG_NLS, 0, "#### continuous system solution status = %d", found_solution);
debugStreamPrint(LOG_MIXED, 0, "#### continuous system solution status = %d", found_solution);

/* restart if any relation has changed */
if(checkRelations(data))
{
updateRelationsPre(data);
systemData->updateIterationExps(data);
debugStreamPrint(LOG_NLS, 0, "#### System relation changed restart iteration");
debugStreamPrint(LOG_MIXED, 0, "#### System relation changed restart iteration");
if(mixedIterations++ > 200)
found_solution = -4; /* mixedIterations++ > 200 */
}
Expand All @@ -227,41 +227,41 @@ int solveMixedSearch(DATA *data, int sysNumber)
{
/* system of equations failed */
found_solution = -2;
debugStreamPrint(LOG_NLS, 0, "#### NO SOLUTION ");
debugStreamPrint(LOG_MIXED, 0, "#### NO SOLUTION ");
}
else
{
found_solution = 1;
for(i = 0; i < systemData->size; i++)
{
debugStreamPrint(LOG_NLS, 0, " check iterationVar[%d] = %d <-> %d", i, solverData->iterationVars[i], solverData->iterationVars2[i]);
debugStreamPrint(LOG_MIXED, 0, " check iterationVar[%d] = %d <-> %d", i, solverData->iterationVars[i], solverData->iterationVars2[i]);
if(solverData->iterationVars[i] != solverData->iterationVars2[i])
{
found_solution = 0;
break;
}
}
debugStreamPrint(LOG_NLS, 0, "#### SOLUTION = %c", found_solution ? 'T' : 'F');
debugStreamPrint(LOG_MIXED, 0, "#### SOLUTION = %c", found_solution ? 'T' : 'F');
}

if(!found_solution )
{
/* try next set of values*/
if(nextVar(solverData->stateofSearch, systemData->size))
{
debugStreamPrint(LOG_NLS, 0, "#### set next STATE ");
debugStreamPrint(LOG_MIXED, 0, "#### set next STATE ");
for(i = 0; i < systemData->size; i++)
*(systemData->iterationVarsPtr[i]) = *(systemData->iterationPreVarsPtr[i]) != solverData->stateofSearch[i];

/* debug output */
if(ACTIVE_STREAM(LOG_NLS))
if(ACTIVE_STREAM(LOG_MIXED))
{
const char * __name;
for(i = 0; i < systemData->size; i++)
{
ix = (systemData->iterationVarsPtr[i]-data->localData[0]->booleanVars);
__name = data->modelData->booleanVarsData[ix].info.name;
debugStreamPrint(LOG_NLS, 0, "%s changed : %d -> %d", __name, solverData->iterationVars[i], *(systemData->iterationVarsPtr[i]));
debugStreamPrint(LOG_MIXED, 0, "%s changed : %d -> %d", __name, solverData->iterationVars[i], *(systemData->iterationVarsPtr[i]));
}
}
}
Expand All @@ -283,15 +283,15 @@ int solveMixedSearch(DATA *data, int sysNumber)
if(found_solution == 1)
{
success = 1;
if(ACTIVE_STREAM(LOG_NLS))
if(ACTIVE_STREAM(LOG_MIXED))
{
const char * __name;
debugStreamPrint(LOG_NLS, 0, "#### SOLUTION FOUND! (system %d)", eqSystemNumber);
debugStreamPrint(LOG_MIXED, 0, "#### SOLUTION FOUND! (system %d)", eqSystemNumber);
for(i = 0; i < systemData->size; i++)
{
ix = (systemData->iterationVarsPtr[i]-data->localData[0]->booleanVars);
__name = data->modelData->booleanVarsData[ix].info.name;
debugStreamPrint(LOG_NLS, 0, "%s = %d pre(%s)= %d", __name, *systemData->iterationVarsPtr[i], __name,
debugStreamPrint(LOG_MIXED, 0, "%s = %d pre(%s)= %d", __name, *systemData->iterationVarsPtr[i], __name,
*systemData->iterationPreVarsPtr[i]);
}
}
Expand All @@ -302,7 +302,7 @@ int solveMixedSearch(DATA *data, int sysNumber)

}while(!found_solution);

messageClose(LOG_NLS);
debugStreamPrint(LOG_NLS, 0, "#### Finished mixed equation system in steps %d.\n", stepCount);
messageClose(LOG_MIXED);
debugStreamPrint(LOG_MIXED, 0, "#### Finished mixed equation system in steps %d.\n", stepCount);
return success;
}
24 changes: 16 additions & 8 deletions OMCompiler/SimulationRuntime/c/simulation/solver/mixedSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ int initializeMixedSystems(DATA *data, threadData_t *threadData)
int size;
MIXED_SYSTEM_DATA *system = data->simulationInfo->mixedSystemData;

infoStreamPrint(LOG_NLS, 1, "initialize mixed system solvers");
infoStreamPrint(LOG_NLS, 0, "%ld mixed systems", data->modelData->nMixedSystems);
infoStreamPrint(LOG_MIXED, 1, "initialize mixed system solvers");
infoStreamPrint(LOG_MIXED, 0, "%ld mixed systems", data->modelData->nMixedSystems);

for(i=0; i<data->modelData->nMixedSystems; ++i)
{
Expand All @@ -71,7 +71,7 @@ int initializeMixedSystems(DATA *data, threadData_t *threadData)
}
}

messageClose(LOG_NLS);
messageClose(LOG_MIXED);
return 0;
}

Expand All @@ -86,7 +86,7 @@ int freeMixedSystems(DATA *data, threadData_t *threadData)
int i;
MIXED_SYSTEM_DATA* system = data->simulationInfo->mixedSystemData;

infoStreamPrint(LOG_NLS, 1, "free mixed system solvers");
infoStreamPrint(LOG_MIXED, 1, "free mixed system solvers");

for(i=0;i<data->modelData->nMixedSystems;++i)
{
Expand All @@ -107,7 +107,7 @@ int freeMixedSystems(DATA *data, threadData_t *threadData)
free(system[i].solverData);
}

messageClose(LOG_NLS);
messageClose(LOG_MIXED);
return 0;
}

Expand All @@ -123,6 +123,10 @@ int solve_mixed_system(DATA *data, threadData_t *threadData, int sysNumber)
int success;
MIXED_SYSTEM_DATA* system = data->simulationInfo->mixedSystemData;

if (!system->logActive) {
deactivateLogging();
}

/* for now just use lapack solver as before */
switch(data->simulationInfo->mixedMethod)
{
Expand All @@ -134,6 +138,10 @@ int solve_mixed_system(DATA *data, threadData_t *threadData, int sysNumber)
}
system[sysNumber].solved = success;

if (!system->logActive) {
reactivateLogging();
}

return 0;
}

Expand All @@ -155,10 +163,10 @@ int check_mixed_solutions(DATA *data, int printFailingSystems)
if(system[i].solved == 0)
{
retVal = 1;
if(printFailingSystems && ACTIVE_WARNING_STREAM(LOG_NLS))
if(printFailingSystems && ACTIVE_WARNING_STREAM(LOG_MIXED))
{
warningStreamPrint(LOG_NLS, 1, "mixed system fails: %d at t=%g", modelInfoGetEquation(&data->modelData->modelDataXml, system->equationIndex).id, data->localData[0]->timeValue);
messageClose(LOG_NLS);
warningStreamPrint(LOG_MIXED, 1, "mixed system fails: %d at t=%g", modelInfoGetEquation(&data->modelData->modelDataXml, system->equationIndex).id, data->localData[0]->timeValue);
messageClose(LOG_MIXED);
}
}

Expand Down

0 comments on commit ba314ac

Please sign in to comment.