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

Commit a766c29

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Unify reading enumeration flags
This commit simplifies the handling of the command-line arguments. It fixed some typos from copy-paste, etc. Hopefully generated FMUs will be able to set flags in the future as well (then the routines handling the flags need to be moved to a separate module since they are all part of simulation_runtime.cpp right now). Belonging to [master]: - #1908
1 parent 0e39b73 commit a766c29

File tree

8 files changed

+58
-139
lines changed

8 files changed

+58
-139
lines changed

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 16 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -218,124 +218,26 @@ void setGlobalVerboseLevel(int argc, char**argv)
218218
delete flags;
219219
}
220220

221-
static int getNonlinearSolverMethod()
221+
static void readFlag(int *flag, int max, const char *value, const char *flagName, const char **names, const char **desc)
222222
{
223223
int i;
224-
const char *cflags = omc_flagValue[FLAG_NLS];
225-
const string *method = cflags ? new string(cflags) : NULL;
226-
227-
if(!method)
228-
return NLS_MIXED; /* default method */
229-
230-
for(i=1; i<NLS_MAX; ++i)
231-
if(*method == NLS_NAME[i])
232-
return i;
233-
234-
warningStreamPrint(LOG_STDOUT, 1, "unrecognized option -nls=%s, current options are:", method->c_str());
235-
for(i=1; i<NLS_MAX; ++i)
236-
warningStreamPrint(LOG_STDOUT, 0, "%-18s [%s]", NLS_NAME[i], NLS_DESC[i]);
237-
messageClose(LOG_STDOUT);
238-
throwStreamPrint(NULL,"see last warning");
239-
240-
return NLS_NONE;
241-
}
242-
243-
static int getlinearSolverMethod()
244-
{
245-
int i;
246-
const char *cflags = omc_flagValue[FLAG_LS];
247-
const string *method = cflags ? new string(cflags) : NULL;
248-
249-
if(!method)
250-
return LS_DEFAULT; /* default method */
251-
252-
for(i=1; i<LS_MAX; ++i)
253-
if(*method == LS_NAME[i])
254-
return i;
255-
256-
warningStreamPrint(LOG_STDOUT, 1, "unrecognized option -ls=%s, current options are:", method->c_str());
257-
for(i=1; i<LS_MAX; ++i)
258-
warningStreamPrint(LOG_STDOUT, 0, "%-18s [%s]", LS_NAME[i], LS_DESC[i]);
259-
messageClose(LOG_STDOUT);
260-
throwStreamPrint(NULL,"see last warning");
261-
262-
return LS_NONE;
263-
}
264-
265-
static int getlinearSparseSolverMethod()
266-
{
267-
int i;
268-
const char *cflags = omc_flagValue[FLAG_LSS];
269-
const string *method = cflags ? new string(cflags) : NULL;
270-
271-
if (!method) {
272-
return LSS_DEFAULT; /* default method */
224+
if (!value) {
225+
return; /* keep the default value */
273226
}
274227

275-
for(i=1; i<LSS_MAX; ++i)
276-
if(*method == LSS_NAME[i])
277-
return i;
278-
279-
warningStreamPrint(LOG_STDOUT, 1, "unrecognized option -lss=%s, current options are:", method->c_str());
280-
for(i=1; i<LSS_MAX; ++i)
281-
warningStreamPrint(LOG_STDOUT, 0, "%-18s [%s]", LSS_NAME[i], LSS_DESC[i]);
282-
messageClose(LOG_STDOUT);
283-
throwStreamPrint(NULL,"see last warning");
284-
285-
return LSS_NONE;
286-
}
287-
288-
static int getNewtonStrategy()
289-
{
290-
int i;
291-
const char *cflags = omc_flagValue[FLAG_NEWTON_STRATEGY];
292-
const string *method = cflags ? new string(cflags) : NULL;
293-
294-
if(!method)
295-
return NEWTON_DAMPED2; /* default method */
296-
297-
for(i=1; i<NEWTON_MAX; ++i)
298-
if(*method == NEWTONSTRATEGY_NAME[i])
299-
return i;
300-
301-
warningStreamPrint(LOG_STDOUT, 1, "unrecognized option -nls=%s, current options are:", method->c_str());
302-
for(i=1; i<NEWTON_MAX; ++i)
303-
warningStreamPrint(LOG_STDOUT, 0, "%-18s [%s]", NEWTONSTRATEGY_NAME[i], NEWTONSTRATEGY_DESC[i]);
304-
messageClose(LOG_STDOUT);
305-
throwStreamPrint(NULL,"see last warning");
306-
307-
return NEWTON_NONE;
308-
}
309-
310-
static int getNlsLSSolver(int nlsSolver)
311-
{
312-
int i;
313-
const char *cflags = omc_flagValue[FLAG_NLS_LS];
314-
const string *method = cflags ? new string(cflags) : NULL;
315-
316-
if(!method)
317-
{
318-
if (nlsSolver == NLS_KINSOL)
319-
{
320-
return NLS_LS_KLU; /* default kinsol linear solver method */
321-
}
322-
else
323-
{
324-
return NLS_LS_LAPACK; /* default method */
228+
for (i=1; i<max; ++i) {
229+
if (0 == strcmp(value, names[i])) {
230+
*flag = i;
231+
return;
325232
}
326233
}
327234

328-
for(i=1; i<NLS_LS_MAX; ++i)
329-
if(*method == NLS_LS_METHOD[i])
330-
return i;
331-
332-
warningStreamPrint(LOG_STDOUT, 1, "unrecognized option -nls=%s, current options are:", method->c_str());
333-
for(i=1; i<NLS_LS_MAX; ++i)
334-
warningStreamPrint(LOG_STDOUT, 0, "%-18s [%s]", NLS_LS_METHOD[i], NLS_LS_METHOD_DESC[i]);
235+
warningStreamPrint(LOG_STDOUT, 1, "unrecognized option %s=%s, current options are:", flagName, value);
236+
for (i=1; i<LS_MAX; ++i) {
237+
warningStreamPrint(LOG_STDOUT, 0, "%-18s [%s]", names[i], desc[i]);
238+
}
335239
messageClose(LOG_STDOUT);
336240
throwStreamPrint(NULL,"see last warning");
337-
338-
return NLS_LS_UNKNOWN;
339241
}
340242

341243
static double getFlagReal(enum _FLAG flag, double res)
@@ -839,12 +741,12 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr
839741
EXIT(1);
840742
}
841743

842-
data->simulationInfo->nlsMethod = getNonlinearSolverMethod();
843-
data->simulationInfo->lsMethod = getlinearSolverMethod();
844-
data->simulationInfo->lssMethod = getlinearSparseSolverMethod();
845-
data->simulationInfo->newtonStrategy = getNewtonStrategy();
744+
readFlag(&data->simulationInfo->nlsMethod, NLS_MAX, omc_flagValue[FLAG_NLS], "-nls", NLS_NAME, NLS_DESC);
745+
readFlag(&data->simulationInfo-> lsMethod, LS_MAX, omc_flagValue[FLAG_LS ], "-ls", LS_NAME, LS_DESC);
746+
readFlag(&data->simulationInfo->lssMethod, LSS_MAX, omc_flagValue[FLAG_LSS], "-lss", LSS_NAME, LSS_DESC);
747+
readFlag(&data->simulationInfo->newtonStrategy, NEWTON_MAX, omc_flagValue[FLAG_NEWTON_STRATEGY], "-newton", NEWTONSTRATEGY_NAME, NEWTONSTRATEGY_DESC);
846748
data->simulationInfo->nlsCsvInfomation = omc_flag[FLAG_NLS_INFO];
847-
data->simulationInfo->nlsLinearSolver = getNlsLSSolver(data->simulationInfo->nlsMethod);
749+
readFlag(&data->simulationInfo->nlsLinearSolver, NLS_LS_MAX, omc_flagValue[FLAG_NLS_LS], "-nlsLS", NLS_LS_METHOD, NLS_LS_METHOD_DESC);
848750

849751
if(omc_flag[FLAG_HOMOTOPY_ADAPT_BEND]) {
850752
homAdaptBend = atof(omc_flagValue[FLAG_HOMOTOPY_ADAPT_BEND]);

SimulationRuntime/c/simulation/solver/fmi_events.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
/*! \file events.h
3232
*/
3333

34-
#ifndef _EVENTS_H_
35-
#define _EVENTS_H_
34+
#ifndef _OMC_FMI_EVENTS_H_
35+
#define _OMC_FMI_EVENTS_H_
3636

3737
#include "simulation_data.h"
3838

SimulationRuntime/c/simulation/solver/kinsolSolver.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int nlsKinsolAllocate(int size, NONLINEAR_SYSTEM_DATA *nlsData, int linearSolver
211211
}
212212

213213
/* Specify linear solver and/or corresponding jacobian function*/
214-
if (kinsolData->linearSolverMethod == 3)
214+
if (kinsolData->linearSolverMethod == NLS_LS_KLU)
215215
{
216216
if(nlsData->isPatternAvailable)
217217
{
@@ -229,23 +229,21 @@ int nlsKinsolAllocate(int size, NONLINEAR_SYSTEM_DATA *nlsData, int linearSolver
229229
if (checkReturnFlag(flag)){
230230
errorStreamPrint(LOG_STDOUT, 0, "##KINSOL## Something goes wrong while initialize KINSOL Sparse Solver!");
231231
}
232-
}
233-
else
234-
{
232+
} else {
235233
flag = KINDense(kinsolData->kinsolMemory, size);
236234
if (checkReturnFlag(flag)){
237235
errorStreamPrint(LOG_STDOUT, 0, "##KINSOL## Something goes wrong while initialize KINSOL solver!");
238236
}
239237
}
240238
}
241-
else if (kinsolData->linearSolverMethod == 1)
239+
else if (kinsolData->linearSolverMethod == NLS_LS_TOTALPIVOT)
242240
{
243241
flag = KINDense(kinsolData->kinsolMemory, size);
244242
if (checkReturnFlag(flag)){
245243
errorStreamPrint(LOG_STDOUT, 0, "##KINSOL## Something goes wrong while initialize KINSOL solver!");
246244
}
247245
}
248-
else if (kinsolData->linearSolverMethod == 2)
246+
else if (kinsolData->linearSolverMethod == NLS_LS_LAPACK)
249247
{
250248
flag = KINDense(kinsolData->kinsolMemory, size);
251249
if (checkReturnFlag(flag)){
@@ -261,12 +259,13 @@ int nlsKinsolAllocate(int size, NONLINEAR_SYSTEM_DATA *nlsData, int linearSolver
261259
nlsKinsolConfigSetup(kinsolData);
262260

263261
/* debug print level of kinsol */
264-
if (ACTIVE_STREAM(LOG_NLS_V))
262+
if (ACTIVE_STREAM(LOG_NLS_V)) {
265263
printLevel = 3;
266-
else if (ACTIVE_STREAM(LOG_NLS))
264+
} else if (ACTIVE_STREAM(LOG_NLS)) {
267265
printLevel = 1;
268-
else
266+
} else {
269267
printLevel = 0;
268+
}
270269
KINSetPrintLevel(kinsolData->kinsolMemory, printLevel);
271270

272271
return 0;
@@ -751,7 +750,7 @@ void nlsKinsolFScaling(DATA* data, NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM
751750
nlsKinsolResiduals(x, kinsolData->fTmp, &kinsolData->userData);
752751

753752
/* calculate the right jacobian */
754-
if(nlsData->isPatternAvailable && kinsolData->linearSolverMethod == 3)
753+
if(nlsData->isPatternAvailable && kinsolData->linearSolverMethod == NLS_LS_KLU)
755754
{
756755
spJac = NewSparseMat(kinsolData->size,kinsolData->size,kinsolData->nnz);
757756
if (nlsData->analyticalJacobianColumn != NULL){
@@ -871,7 +870,7 @@ int nlsKinsolErrorHandler(int errorCode, DATA *data, NONLINEAR_SYSTEM_DATA *nlsD
871870
break;
872871
case KIN_LSETUP_FAIL:
873872
/* in case of something goes wrong with the symbolic jacobian try the numerical */
874-
if ( kinsolData->linearSolverMethod == 3 && nlsData->isPatternAvailable && nlsData->analyticalJacobianColumn != NULL){
873+
if ( kinsolData->linearSolverMethod == NLS_LS_KLU && nlsData->isPatternAvailable && nlsData->analyticalJacobianColumn != NULL){
875874
flag = KINSlsSetSparseJacFn(kinsolData->kinsolMemory, nlsSparseJac);
876875
}
877876
if (checkReturnFlag(flag)){

SimulationRuntime/c/simulation/solver/model_help.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "mixedSystem.h"
4848
#include "delay.h"
4949
#include "epsilon.h"
50+
#include "simulation/solver/fmi_events.h"
5051
#include "simulation/solver/stateset.h"
5152
#include "meta/meta_modelica.h"
5253

@@ -939,11 +940,11 @@ void initializeDataStruc(DATA *data, threadData_t *threadData)
939940
#else
940941
data->simulationInfo->nlsMethod = NLS_HOMOTOPY;
941942
#endif
942-
data->simulationInfo->nlsLinearSolver = NLS_LS_LAPACK;
943+
data->simulationInfo->nlsLinearSolver = NLS_LS_DEFAULT;
943944
data->simulationInfo->lsMethod = LS_DEFAULT;
944945
data->simulationInfo->lssMethod = LSS_DEFAULT;
945946
data->simulationInfo->mixedMethod = MIXED_SEARCH;
946-
data->simulationInfo->newtonStrategy = NEWTON_PURE;
947+
data->simulationInfo->newtonStrategy = NEWTON_DAMPED2;
947948
data->simulationInfo->nlsCsvInfomation = 0;
948949
data->simulationInfo->currentContext = CONTEXT_ALGEBRAIC;
949950
data->simulationInfo->jacobianEvals = data->modelData->nStates;

SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ int linearSolverWrapper(int n, double* x, double* A, int* indRow, int* indCol, i
11461146
debugVectorDouble(LOG_NLS_JAC,"vector b:", x, n);
11471147

11481148
switch(method){
1149-
case (1): /* NLS_LS_TOTALPIVOT */
1149+
case NLS_LS_TOTALPIVOT:
11501150

11511151
solverinfo = solveSystemWithTotalPivotSearch(n, x, A, indRow, indCol, pos, rank, casualTearingSet);
11521152
/* in case of failing */
@@ -1165,7 +1165,7 @@ int linearSolverWrapper(int n, double* x, double* A, int* indRow, int* indCol, i
11651165
returnValue = 0;
11661166
}
11671167
break;
1168-
case 2: /* NLS_LS_LAPACK */
1168+
case NLS_LS_LAPACK:
11691169
/* Solve system with lapack */
11701170
dgesv_((int*) &n,
11711171
(int*) &nrhs,
@@ -1200,7 +1200,7 @@ int linearSolverWrapper(int n, double* x, double* A, int* indRow, int* indCol, i
12001200
}
12011201
break;
12021202
default:
1203-
warningStreamPrint(LOG_STDOUT, 0, "Non-Linear solver try to run with a unknown linear solver.");
1203+
throwStreamPrint(0, "Non-Linear solver try to run with a unknown linear solver (%d).", method);
12041204
}
12051205

12061206
/* Debugging error of linear system */

SimulationRuntime/c/simulation/solver/nonlinearSystem.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <math.h>
3535
#include <string.h>
3636

37+
#include "util/simulation_options.h"
3738
#include "util/omc_error.h"
3839
#include "nonlinearSystem.h"
3940
#include "nonlinearValuesList.h"
@@ -348,6 +349,17 @@ int initializeNonlinearSystems(DATA *data, threadData_t *threadData)
348349

349350
infoStreamPrint(LOG_NLS, 1, "initialize non-linear system solvers");
350351
infoStreamPrint(LOG_NLS, 0, "%ld non-linear systems", data->modelData->nNonLinearSystems);
352+
if (data->simulationInfo->nlsLinearSolver == NLS_LS_DEFAULT) {
353+
#if !defined(OMC_MINIMAL_RUNTIME)
354+
if (data->simulationInfo->nlsMethod == NLS_KINSOL) {
355+
data->simulationInfo->nlsLinearSolver = NLS_LS_KLU;
356+
} else {
357+
data->simulationInfo->nlsLinearSolver = NLS_LS_LAPACK;
358+
}
359+
#else
360+
data->simulationInfo->nlsLinearSolver = NLS_LS_LAPACK;
361+
#endif
362+
}
351363

352364
for(i=0; i<data->modelData->nNonLinearSystems; ++i)
353365
{
@@ -837,16 +849,17 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
837849
/* SOLVE!
838850
If the new global homotopy approach is activated and the component contains the homotopy operator
839851
use the HOMOTOPY SOLVER, otherwise use the selected solver */
840-
if(data->callback->useHomotopy == 2 && nonlinsys->homotopySupport)
852+
if(data->callback->useHomotopy == 2 && nonlinsys->homotopySupport) {
841853
nonlinsys->solved = solveHomotopy(data, threadData, sysNumber);
842-
else
854+
} else {
843855
nonlinsys->solved = solveNLS(data, threadData, sysNumber);
856+
}
844857
}
845858

846859
if (lambda_steps > 1 && !nonlinsys->solved) {
847-
if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY])
860+
if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY]) {
848861
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve initial system %d without homotopy method. The homotopy method is used now.", sysNumber);
849-
862+
}
850863
#if !defined(OMC_NO_FILESYSTEM)
851864
if(ACTIVE_STREAM(LOG_INIT))
852865
{
@@ -864,9 +877,9 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
864877
{
865878
data->simulationInfo->lambda = ((double)step)/(lambda_steps-1);
866879

867-
if(data->simulationInfo->lambda > 1.0) {
868-
data->simulationInfo->lambda = 1.0;
869-
}
880+
if (data->simulationInfo->lambda > 1.0) {
881+
data->simulationInfo->lambda = 1.0;
882+
}
870883

871884
infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g", sysNumber, data->simulationInfo->lambda);
872885
nonlinsys->solved = solveNLS(data, threadData, sysNumber);

SimulationRuntime/c/util/simulation_options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ const char *IDA_LS_METHOD_DESC[IDA_LS_MAX] = {
803803
const char *NLS_LS_METHOD[NLS_LS_MAX] = {
804804
"unknown",
805805

806+
"default",
806807
"totalpivot",
807808
"lapack",
808809
"klu"
@@ -811,6 +812,7 @@ const char *NLS_LS_METHOD[NLS_LS_MAX] = {
811812
const char *NLS_LS_METHOD_DESC[NLS_LS_MAX] = {
812813
"unknown",
813814

815+
"chooses the nls linear solver based on which nls is being used.",
814816
"internal total pivot implementation. Solve in some case even under-determined systems.",
815817
"use external lapack implementation.",
816818
"use klu direct sparse solver."

SimulationRuntime/c/util/simulation_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ enum NLS_LS
305305
{
306306
NLS_LS_UNKNOWN = 0,
307307

308+
NLS_LS_DEFAULT,
309+
308310
NLS_LS_TOTALPIVOT,
309311
NLS_LS_LAPACK,
310312
NLS_LS_KLU,

0 commit comments

Comments
 (0)