Skip to content

Commit

Permalink
[C-Runtime] Initialize with homotopy solver for models with homotopy …
Browse files Browse the repository at this point in the history
…operator

Use the homotopy method for initialization as default, if the model contains
the homotopy-operator. In that case use 2 homotopy steps in stead of 4 when
doing homotopy after initialization failed without homotopy.
Added new flag "-noHomotopyOnFirstTry" to disable this behaviour. Useful for
models which contains homotopy operator which is "wrong", e.g. some of the
MSL Fluid examples.

  - Fix for ticket #5139.
  - Updated tests which are using homotopy operators and where not initializing
    with homoptopyOnFirstTry.
  - Model from ticket-5206.mos converges to different solution with homotopy,
    since parameter y0 is not fixed.
  • Loading branch information
AnHeuermann authored and adrpo committed Dec 16, 2019
1 parent 4d4d0e8 commit a96be8d
Show file tree
Hide file tree
Showing 38 changed files with 170 additions and 107 deletions.
Expand Up @@ -203,7 +203,15 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
}
}
#endif
adaptiveGlobal = data->callback->useHomotopy == 2;
/* useHomotopy=1: global homotopy (equidistant lambda) */
if ( (data->callback->useHomotopy == 1 && omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY] != 1) && omc_flag[FLAG_NO_HOMOTOPY_ON_FIRST_TRY] !=1 ) {
omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY] = 1;
init_lambda_steps = 2;
infoStreamPrint(LOG_INIT, 0, "Model contains homotopy operator: Use adaptive homotopy method to solve initialization problem. "
"To disable initialization with homotpy operator use \"-noHomotopyOnFirstTry\".");
}

adaptiveGlobal = data->callback->useHomotopy == 2; /* new global homotopy approach (adaptive lambda) */
solveWithGlobalHomotopy = homotopySupport
&& ((data->callback->useHomotopy == 1 && init_lambda_steps > 1) || adaptiveGlobal);

Expand Down Expand Up @@ -236,7 +244,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
infoStreamPrint(LOG_INIT, 0, "Automatically set -homotopyOnFirstTry, because trying without homotopy first is not supported for the adaptive global approach in combination with KINSOL.");
} else {
if (adaptiveGlobal)
data->callback->useHomotopy = 1;
data->callback->useHomotopy = 1; /* global homotopy (equidistant lambda) */
data->simulationInfo->lambda = 1.0;
infoStreamPrint(LOG_INIT, 0, "Try to solve the initialization problem without homotopy first.");
data->callback->functionInitialEquations(data, threadData);
Expand All @@ -248,7 +256,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
MMC_CATCH_INTERNAL(simulationJumpBuffer)
#endif
if (adaptiveGlobal)
data->callback->useHomotopy = 2;
data->callback->useHomotopy = 2; /* new global homotopy approach (adaptive lambda) */
if(solveWithGlobalHomotopy) {
if (!kinsol)
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve the initialization problem without homotopy method. If homotopy is available the homotopy method is used now.");
Expand Down
6 changes: 6 additions & 0 deletions OMCompiler/SimulationRuntime/c/util/simulation_options.c
Expand Up @@ -56,6 +56,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
/* FLAG_HOMOTOPY_MAX_TRIES */ "homMaxTries",
/* FLAG_HOMOTOPY_NEG_START_DIR */ "homNegStartDir",
/* FLAG_HOMOTOPY_ON_FIRST_TRY */ "homotopyOnFirstTry",
/* FLAG_NO_HOMOTOPY_ON_FIRST_TRY */ "noHomotopyOnFirstTry",
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR */ "homTauDecFac",
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED */ "homTauDecFacPredictor",
/* FLAG_HOMOTOPY_TAU_INC_FACTOR */ "homTauIncFac",
Expand Down Expand Up @@ -171,6 +172,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
/* FLAG_HOMOTOPY_MAX_TRIES */ "[int (default 10)] maximum number of tries for one homotopy lambda step",
/* FLAG_HOMOTOPY_NEG_START_DIR */ "start to run along the homotopy path in the negative direction",
/* FLAG_HOMOTOPY_ON_FIRST_TRY */ "directly use the homotopy method to solve the initialization problem",
/* FLAG_NO_HOMOTOPY_ON_FIRST_TRY */ "disable the use of the homotopy method to solve the initialization problem",
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR */ "[double (default 10.0)] decrease homotopy step size tau by this factor if tau is too big in the homotopy corrector step",
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED */ "[double (default 2.0)] decrease homotopy step size tau by this factor if tau is too big in the homotopy predictor step",
/* FLAG_HOMOTOPY_TAU_INC_FACTOR */ "[double (default 2.0)] increase homotopy step size tau by this factor if tau is too small in the homotopy corrector step",
Expand Down Expand Up @@ -322,6 +324,9 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
/* FLAG_HOMOTOPY_ON_FIRST_TRY */
" If the model contains the homotopy operator, directly use the homotopy method to solve the initialization problem.\n"
" Without this flag, the solver first tries to solve the initialization problem without homotopy and only uses homotopy as fallback option.",
/* FLAG_NO_HOMOTOPY_ON_FIRST_TRY */
" Disable the use of the homotopy method to solve the initialization problem.\n"
" Without this flag, the solver tries to solve the initialization problem with homotopy if the model contains the homotopy-operator.",
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR */
" Decrease homotopy step size tau by this factor if tau is too big in the homotopy corrector step (default: 10.0).",
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED */
Expand Down Expand Up @@ -565,6 +570,7 @@ const int FLAG_TYPE[FLAG_MAX] = {
/* FLAG_HOMOTOPY_MAX_TRIES */ FLAG_TYPE_OPTION,
/* FLAG_HOMOTOPY_NEG_START_DIR */ FLAG_TYPE_FLAG,
/* FLAG_HOMOTOPY_ON_FIRST_TRY */ FLAG_TYPE_FLAG,
/* FLAG_NO_HOMOTOPY_ON_FIRST_TRY */ FLAG_TYPE_FLAG,
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR */ FLAG_TYPE_OPTION,
/* FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED */ FLAG_TYPE_OPTION,
/* FLAG_HOMOTOPY_TAU_INC_FACTOR */ FLAG_TYPE_OPTION,
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/util/simulation_options.h
Expand Up @@ -64,6 +64,7 @@ enum _FLAG
FLAG_HOMOTOPY_MAX_TRIES,
FLAG_HOMOTOPY_NEG_START_DIR,
FLAG_HOMOTOPY_ON_FIRST_TRY,
FLAG_NO_HOMOTOPY_ON_FIRST_TRY,
FLAG_HOMOTOPY_TAU_DEC_FACTOR,
FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED,
FLAG_HOMOTOPY_TAU_INC_FACTOR,
Expand Down
2 changes: 1 addition & 1 deletion testsuite/flattening/modelica/modification/Bug3817.mos
Expand Up @@ -761,7 +761,7 @@ simulate(PowerSystems.Examples.Spot.DrivesAC3ph.SM_ctrlAv); getErrorString();
// record SimulationResult
// resultFile = "PowerSystems.Examples.Spot.DrivesAC3ph.SM_ctrlAv_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'PowerSystems.Examples.Spot.DrivesAC3ph.SM_ctrlAv', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down
Expand Up @@ -19,7 +19,11 @@ simulate(ThermoPower.Examples.CISE.CISESim120501); getErrorString();
// record SimulationResult
// resultFile = "ThermoPower.Examples.CISE.CISESim120501_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1200.0, numberOfIntervals = 1000, tolerance = 1e-07, method = 'dassl', fileNamePrefix = 'ThermoPower.Examples.CISE.CISESim120501', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// messages = "stdout | warning | While solving non-linear system an assertion failed during initialization.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down
Expand Up @@ -11,7 +11,7 @@ simulate(ThermoPower.Examples.HRB.Simulators.ClosedLoopDigitalSimulator);getErro
// record SimulationResult
// resultFile = "ThermoPower.Examples.HRB.Simulators.ClosedLoopDigitalSimulator_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 400.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'ThermoPower.Examples.HRB.Simulators.ClosedLoopDigitalSimulator', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down
Expand Up @@ -12,7 +12,7 @@ simulate(ThermoPower.Test.DistributedParameterComponents.TestFlow1D2phChen); get
// record SimulationResult
// resultFile = "ThermoPower.Test.DistributedParameterComponents.TestFlow1D2phChen_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1000.0, numberOfIntervals = 500, tolerance = 1e-08, method = 'dassl', fileNamePrefix = 'ThermoPower.Test.DistributedParameterComponents.TestFlow1D2phChen', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down
Expand Up @@ -12,7 +12,7 @@ simulate(ThermoPower.Test.DistributedParameterComponents.TestFlow1D2phDB_hf); ge
// record SimulationResult
// resultFile = "ThermoPower.Test.DistributedParameterComponents.TestFlow1D2phDB_hf_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 300.0, numberOfIntervals = 500, tolerance = 1e-07, method = 'dassl', fileNamePrefix = 'ThermoPower.Test.DistributedParameterComponents.TestFlow1D2phDB_hf', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down
Expand Up @@ -15,18 +15,7 @@ simulate(ThermoPower.Test.DistributedParameterComponents.TestWaterFlow1DFV2ph);
// record SimulationResult
// resultFile = "ThermoPower.Test.DistributedParameterComponents.TestWaterFlow1DFV2ph_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1000.0, numberOfIntervals = 500, tolerance = 1e-08, method = 'dassl', fileNamePrefix = 'ThermoPower.Test.DistributedParameterComponents.TestWaterFlow1DFV2ph', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "stdout | warning | While solving non-linear system an assertion failed during initialization.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// stdout | warning | While solving non-linear system an assertion failed during initialization.
// | | | | | The non-linear solver tries to solve the problem that could take some time.
// | | | | | It could help to provide better start-values for the iteration variables.
// | | | | | For more information simulate with -lv LOG_NLS_V
// assert | debug | Solving non-linear system 453 failed at time=0.
// | | | | For more information please use -lv LOG_NLS.
// assert | warning | Failed to solve the initialization problem without homotopy method. If homotopy is available the homotopy method is used now.
// LOG_SUCCESS | info | The initialization finished successfully with 4 homotopy steps.
// messages = "LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down
Expand Up @@ -27,7 +27,7 @@ runScript(modelTesting);getErrorString();
// {"tank.level","tank.medium.T"}
// Simulation options: startTime = 0.0, stopTime = 100.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.AST_BatchPlant.Test.OneTank', options = '', outputFormat = 'mat', variableFilter = 'time|tank.level|tank.medium.T', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Fluid.Examples.AST_BatchPlant.Test.OneTank_res.mat
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
Expand Down
Expand Up @@ -27,7 +27,7 @@ runScript(modelTesting);getErrorString();
// {"tank1.level","tank1.medium.T"}
// Simulation options: startTime = 0.0, stopTime = 35.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.AST_BatchPlant.Test.TankWithEmptyingPipe1', options = '', outputFormat = 'mat', variableFilter = 'time|tank1.level|tank1.medium.T', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Fluid.Examples.AST_BatchPlant.Test.TankWithEmptyingPipe1_res.mat
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
Expand Down
Expand Up @@ -27,7 +27,7 @@ runScript(modelTesting);getErrorString();
// {"tank1.level","tank1.medium.T"}
// Simulation options: startTime = 0.0, stopTime = 120.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.AST_BatchPlant.Test.TankWithEmptyingPipe2', options = '', outputFormat = 'mat', variableFilter = 'time|tank1.level|tank1.medium.T', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Fluid.Examples.AST_BatchPlant.Test.TankWithEmptyingPipe2_res.mat
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
Expand Down
Expand Up @@ -29,7 +29,7 @@ runScript(modelTesting);getErrorString();
// {"tank1.level","tank1.medium.T","tank2.level","tank2.medium.T"}
// Simulation options: startTime = 0.0, stopTime = 120.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.AST_BatchPlant.Test.TanksWithEmptyingPipe1', options = '', outputFormat = 'mat', variableFilter = 'time|tank1.level|tank1.medium.T|tank2.level|tank2.medium.T', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Fluid.Examples.AST_BatchPlant.Test.TanksWithEmptyingPipe1_res.mat
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
Expand Down
Expand Up @@ -31,7 +31,7 @@ runScript(modelTesting);getErrorString();
// {"tank3.level","tank3.medium.T","tank1.level","tank1.medium.T","tank2.level","tank2.medium.T"}
// Simulation options: startTime = 0.0, stopTime = 120.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.AST_BatchPlant.Test.TanksWithEmptyingPipe2', options = '', outputFormat = 'mat', variableFilter = 'time|tank3.level|tank3.medium.T|tank1.level|tank1.medium.T|tank2.level|tank2.medium.T', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Fluid.Examples.AST_BatchPlant.Test.TanksWithEmptyingPipe2_res.mat
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
Expand Down
Expand Up @@ -29,7 +29,7 @@ runScript(modelTesting);getErrorString();
// {"tank1.level","tank1.medium.T","tank2.level","tank2.medium.T"}
// Simulation options: startTime = 0.0, stopTime = 100.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Fluid.Examples.AST_BatchPlant.Test.TwoTanks', options = '', outputFormat = 'mat', variableFilter = 'time|tank1.level|tank1.medium.T|tank2.level|tank2.medium.T', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Fluid.Examples.AST_BatchPlant.Test.TwoTanks_res.mat
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully with 2 homotopy steps.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
Expand Down

0 comments on commit a96be8d

Please sign in to comment.