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

Commit 5ee6abb

Browse files
ptaeuberOpenModelica-Hudson
authored andcommitted
Enable global homotopy as fallback option by default
Introduce runtime flag homotopyOnFirstTry (default: false). If that flag is not activated first try to solve the initialization problem without homotopy.
1 parent d0deadf commit 5ee6abb

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,22 @@ algorithm
261261
end if;
262262

263263
// initialization stuff
264-
(initialEquations, removedInitialEquations, uniqueEqIndex, tempvars) := createInitialEquations(inInitDAE, inRemovedInitialEquationLst, uniqueEqIndex, {});
264+
// ********************
265+
266+
// generate equations for initDAE
267+
(initialEquations, uniqueEqIndex, tempvars) := createInitialEquations(inInitDAE, uniqueEqIndex, {});
268+
269+
// generate equations for initDAE_lambda0
265270
if isSome(inInitDAE_lambda0) then
266271
SOME(initDAE_lambda0) := inInitDAE_lambda0;
267272
(initialEquations_lambda0, uniqueEqIndex, tempvars) := createInitialEquations_lambda0(initDAE_lambda0, uniqueEqIndex, tempvars);
268273
else
269274
initialEquations_lambda0 := {};
270275
end if;
276+
277+
// generate equations for removed initial equations
278+
(removedInitialEquations, uniqueEqIndex, tempvars) := createNonlinearResidualEquations(inRemovedInitialEquationLst, uniqueEqIndex, tempvars);
279+
271280
execStat("simCode: created initialization part");
272281

273282
shared as BackendDAE.SHARED(globalKnownVars=globalKnownVars,
@@ -6358,18 +6367,16 @@ end createSingleAlgorithmCode;
63586367

63596368
protected function createInitialEquations "author: lochel"
63606369
input BackendDAE.BackendDAE inInitDAE;
6361-
input List<BackendDAE.Equation> inRemovedEqnLst;
63626370
input Integer iuniqueEqIndex;
63636371
input list<SimCodeVar.SimVar> itempvars;
63646372
output list<SimCode.SimEqSystem> outInitialEqns = {};
6365-
output list<SimCode.SimEqSystem> outRemovedInitialEqns = {};
63666373
output Integer ouniqueEqIndex = iuniqueEqIndex;
63676374
output list<SimCodeVar.SimVar> otempvars = itempvars;
63686375
protected
63696376
BackendDAE.EquationArray removedEqs;
63706377
list<SimCodeVar.SimVar> tempvars;
63716378
Integer uniqueEqIndex;
6372-
list<SimCode.SimEqSystem> allEquations, knownVarEquations, solvedEquations, removedEquations, aliasEquations, removedInitialEquations;
6379+
list<SimCode.SimEqSystem> allEquations, knownVarEquations, solvedEquations, removedEquations, aliasEquations;
63736380
BackendDAE.EqSystems systs;
63746381
BackendDAE.Shared shared;
63756382
BackendDAE.Variables globalKnownVars, aliasVars;
@@ -6391,12 +6398,8 @@ algorithm
63916398
allEquations := List.append_reverse(solvedEquations, allEquations);
63926399
allEquations := listAppend(knownVarEquations, allEquations);
63936400

6394-
// generate equations from removed initial equations
6395-
(removedInitialEquations, uniqueEqIndex, tempvars) := createNonlinearResidualEquations(inRemovedEqnLst, uniqueEqIndex, tempvars);
6396-
63976401
// output
63986402
outInitialEqns := allEquations;
6399-
outRemovedInitialEqns := removedInitialEquations;
64006403
ouniqueEqIndex := uniqueEqIndex;
64016404
otempvars := tempvars;
64026405
end createInitialEquations;

Compiler/Util/Flags.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,10 +1373,10 @@ constant ConfigFlag ZEROMQ_FILE_SUFFIX = CONFIG_FLAG(115, "zeroMQFileSuffix",
13731373
SOME("z"), EXTERNAL(), STRING_FLAG(""), NONE(),
13741374
Util.gettext("Sets the file suffix for zeroMQ port file if --interactive=zmq is used."));
13751375
constant ConfigFlag HOMOTOPY_APPROACH = CONFIG_FLAG(116, "homotopyApproach",
1376-
NONE(), EXTERNAL(), STRING_FLAG("local"),
1376+
NONE(), EXTERNAL(), STRING_FLAG("global"),
13771377
SOME(STRING_DESC_OPTION({
1378-
("local", Util.gettext("Default, local homotopy approach. The homotopy parameter only effects the local strongly connected component.")),
1379-
("global", Util.gettext("Global homotopy approach. The homotopy parameter effects the entire initialization system."))
1378+
("local", Util.gettext("Local homotopy approach. The homotopy parameter only effects the local strongly connected component.")),
1379+
("global", Util.gettext("Default, global homotopy approach. The homotopy parameter effects the entire initialization system."))
13801380
})),
13811381
Util.gettext("Sets the homotopy approach."));
13821382

SimulationRuntime/c/simulation/solver/initialization/initialization.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "util/omc_error.h"
3838
#include "openmodelica.h"
3939
#include "openmodelica_func.h"
40+
#include "simulation/options.h"
4041
#include "simulation/solver/model_help.h"
4142
#if !defined(OMC_MINIMAL_RUNTIME)
4243
#include "util/read_matlab4.h"
@@ -73,7 +74,7 @@
7374
#include <math.h>
7475
#include <string.h>
7576

76-
int init_lambda_steps = 1;
77+
int init_lambda_steps = 4;
7778

7879
/*! \fn void dumpInitializationStatus(DATA *data)
7980
*
@@ -193,6 +194,39 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
193194
storePreValues(data);
194195
overwriteOldSimulationData(data);
195196

197+
/* If there is no homotopy in the model or local homotopy is activated
198+
or homotopy is disabled by runtime flag '-ils=<lambda_steps>',
199+
solve WITHOUT HOMOTOPY. */
200+
if (data->callback->useHomotopy == 0 || init_lambda_steps < 2){
201+
data->simulationInfo->lambda = 1.0;
202+
data->callback->functionInitialEquations(data, threadData);
203+
204+
/* If there is homotopy in the model and global homotopy is activated
205+
and homotopy on first try is deactivated,
206+
TRY TO SOLVE WITHOUT HOMOTOPY FIRST. */
207+
} else if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY]) {
208+
/* try */
209+
#ifndef OMC_EMCC
210+
MMC_TRY_INTERNAL(simulationJumpBuffer)
211+
#endif
212+
213+
data->simulationInfo->lambda = 1.0;
214+
infoStreamPrint(LOG_INIT, 0, "Try to solve the initialization problem without homotopy first.");
215+
data->callback->functionInitialEquations(data, threadData);
216+
init_lambda_steps = 0;
217+
infoStreamPrint(LOG_INIT, 0, "Initialization finished without homotopy.");
218+
219+
/* catch */
220+
#ifndef OMC_EMCC
221+
MMC_CATCH_INTERNAL(simulationJumpBuffer)
222+
#endif
223+
if(init_lambda_steps > 0)
224+
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve the initial system without homotopy. If homotopy is available the homotopy method is now used.");
225+
}
226+
227+
/* If there is homotopy in the model and global homotopy is activated
228+
and solving without homotopy failed or is not wanted,
229+
use GLOBAL HOMOTOPY METHOD. */
196230
if (data->callback->useHomotopy == 1 && init_lambda_steps > 1)
197231
{
198232
long step;
@@ -203,15 +237,15 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
203237
{
204238
sprintf(buffer, "%s_global_homotopy.csv", mData->modelFilePrefix);
205239
pFile = fopen(buffer, "wt");
206-
fprintf(pFile, "%s", "lambda");
240+
fprintf(pFile, "\"sep=,\"\n%s", "lambda");
207241
for(i=0; i<mData->nVariablesReal; ++i)
208242
fprintf(pFile, ",%s", mData->realVarsData[i].info.name);
209243
fprintf(pFile, "\n");
210244
}
211245
#endif
212246

213247
infoStreamPrint(LOG_INIT, 1, "homotopy process\n---------------------------");
214-
for(step=0; step<init_lambda_steps-1; ++step)
248+
for(step=0; step<init_lambda_steps; ++step)
215249
{
216250
data->simulationInfo->lambda = ((double)step)/(init_lambda_steps-1);
217251
infoStreamPrint(LOG_INIT, 0, "homotopy parameter lambda = %g", data->simulationInfo->lambda);
@@ -243,23 +277,13 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
243277
break;
244278
}
245279
messageClose(LOG_INIT);
246-
infoStreamPrint(LOG_INIT, 0, "homotopy parameter lambda = 1");
247280
}
248281

249-
data->simulationInfo->lambda = 1.0;
250-
data->callback->functionInitialEquations(data, threadData);
251282
storeRelations(data);
252283

253284
#if !defined(OMC_NO_FILESYSTEM)
254285
if(data->callback->useHomotopy == 1 && init_lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT))
255-
{
256-
infoStreamPrint(LOG_INIT, 0, "homotopy parameter lambda = %g done\n---------------------------", data->simulationInfo->lambda);
257-
fprintf(pFile, "%.16g", data->simulationInfo->lambda);
258-
for(i=0; i<mData->nVariablesReal; ++i)
259-
fprintf(pFile, ",%.16g", data->localData[0]->realVars[i]);
260-
fprintf(pFile, "\n");
261286
fclose(pFile);
262-
}
263287
#endif
264288

265289
/* check for over-determined systems */

SimulationRuntime/c/util/simulation_options.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
4646
/* FLAG_EMIT_PROTECTED */ "emit_protected",
4747
/* FLAG_F */ "f",
4848
/* FLAG_HELP */ "help",
49+
/* FLAG_HOMOTOPY_ON_FIRST_TRY */ "homotopyOnFirstTry",
4950
/* FLAG_IDA_MAXERRORTESTFAIL */ "idaMaxErrorTestFails",
5051
/* FLAG_IDA_MAXNONLINITERS */ "idaMaxNonLinIters",
5152
/* FLAG_IDA_MAXCONVFAILS */ "idaMaxConvFails",
@@ -134,6 +135,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
134135
/* FLAG_EMIT_PROTECTED */ "emits protected variables to the result-file",
135136
/* FLAG_F */ "value specifies a new setup XML file to the generated simulation code",
136137
/* FLAG_HELP */ "get detailed information that specifies the command-line flag",
138+
/* FLAG_HOMOTOPY_ON_FIRST_TRY */ "Directly use the homotopy method to solve the initialization problem.",
137139
/* FLAG_IDA_MAXERRORTESTFAIL */ "value specifies the maximum number of error test failures in attempting one step. The default value is 7.",
138140
/* FLAG_IDA_MAXNONLINITERS */ "value specifies the maximum number of nonlinear solver iterations at one step. The default value is 3.",
139141
/* FLAG_IDA_MAXCONVFAILS */ "value specifies the maximum number of nonlinear solver convergence failures at one step. The default value is 10.",
@@ -144,7 +146,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
144146
/* FLAG_IIF */ "value specifies an external file for the initialization of the model",
145147
/* FLAG_IIM */ "value specifies the initialization method",
146148
/* FLAG_IIT */ "[double] value specifies a time for the initialization of the model",
147-
/* FLAG_ILS */ "[int] default: 1",
149+
/* FLAG_ILS */ "[int] default: 4",
148150
/* FLAG_IMPRK_ORDER */ "[int (default 5)] value specifies the integration order of the implicit Runge-Kutta method. Valid values: 1-6",
149151
/* FLAG_IMPRK_LS */ "selects the linear solver of the integration methods: impeuler, trapezoid and imprungekuta",
150152
/* FLAG_INITIAL_STEP_SIZE */ "value specifies an initial step size for supported solver",
@@ -242,6 +244,9 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
242244
/* FLAG_HELP */
243245
" Get detailed information that specifies the command-line flag\n"
244246
" For example, -help=f prints detailed information for command-line flag f.",
247+
/* FLAG_HOMOTOPY_ON_FIRST_TRY */
248+
" If the model contains the homotopy operator, directly use the homotopy method to solve the initialization problem.\n"
249+
" If disabled, first try to solve without homotopy and only use homotopy as fallback option.",
245250
/* FLAG_IDA_MAXERRORTESTFAIL */
246251
" value specifies the maximum number of error test failures in attempting one step. The default value is 7.",
247252
/* FLAG_IDA_MAXNONLINITERS */
@@ -269,7 +274,7 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
269274
" Value [Real] specifies a time for the initialization of the model.",
270275
/* FLAG_ILS */
271276
" Value specifies the number of steps for homotopy method (required: -iim=symbolic).\n"
272-
" The value is an Integer with default value 1.",
277+
" The value is an Integer with default value 4.",
273278
/* FLAG_IMPRK_ORDER */
274279
" Value specifies the integration order of the implicit Runge-Kutta method. Valid values: 1 to 6. Default order is 5.",
275280
/* FLAG_IMPRK_LS */
@@ -452,6 +457,7 @@ const int FLAG_TYPE[FLAG_MAX] = {
452457
/* FLAG_EMIT_PROTECTED */ FLAG_TYPE_FLAG,
453458
/* FLAG_F */ FLAG_TYPE_OPTION,
454459
/* FLAG_HELP */ FLAG_TYPE_OPTION,
460+
/* FLAG_HOMOTOPY_ON_FIRST_TRY */ FLAG_TYPE_FLAG,
455461
/* FLAG_IDA_MAXERRORTESTFAIL */ FLAG_TYPE_OPTION,
456462
/* FLAG_IDA_MAXNONLINITERS */ FLAG_TYPE_OPTION,
457463
/* FLAG_IDA_MAXCONVFAILS */ FLAG_TYPE_OPTION,

SimulationRuntime/c/util/simulation_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ enum _FLAG
5454
FLAG_EMIT_PROTECTED,
5555
FLAG_F,
5656
FLAG_HELP,
57+
FLAG_HOMOTOPY_ON_FIRST_TRY,
5758
FLAG_IDA_MAXERRORTESTFAIL,
5859
FLAG_IDA_MAXNONLINITERS,
5960
FLAG_IDA_MAXCONVFAILS,

0 commit comments

Comments
 (0)