From 9ec3abdfe0e12aceae5ff29cfe2b3be426e13db1 Mon Sep 17 00:00:00 2001 From: ptaeuber Date: Tue, 21 Nov 2017 10:44:53 +0100 Subject: [PATCH] Add flag to start running the homotopy path ... ... in negative direction first. Belonging to [master]: - OpenModelica/OMCompiler#2040 --- .../c/simulation/solver/nonlinearSolverHomotopy.c | 5 +++-- SimulationRuntime/c/util/simulation_options.c | 5 +++++ SimulationRuntime/c/util/simulation_options.h | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c b/SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c index 54e2bcfa1b..3a98263918 100644 --- a/SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c +++ b/SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c @@ -42,6 +42,7 @@ #include #include /* memcpy */ +#include "simulation/options.h" #include "simulation/simulation_info_json.h" #include "util/omc_error.h" #include "util/varinfo.h" @@ -2250,7 +2251,7 @@ int solveHomotopy(DATA *data, threadData_t *threadData, int sysNumber) if (runHomotopy == 1) { solverData->h_function = wrapper_fvec; solverData->hJac_dh = wrapper_fvec_der; - solverData->startDirection = 1.0; + solverData->startDirection = omc_flag[FLAG_HOMOTOPY_NEG_START_DIR] ? -1.0 : 1.0; debugInt(LOG_INIT, "Homotopy run: ", runHomotopy); debugDouble(LOG_INIT,"startDirection = ", solverData->startDirection); } @@ -2258,7 +2259,7 @@ int solveHomotopy(DATA *data, threadData_t *threadData, int sysNumber) if (runHomotopy == 2) { solverData->h_function = wrapper_fvec; solverData->hJac_dh = wrapper_fvec_der; - solverData->startDirection = -1.0; + solverData->startDirection = omc_flag[FLAG_HOMOTOPY_NEG_START_DIR] ? 1.0 : -1.0; debugInt(LOG_INIT, "Homotopy run: ", runHomotopy); debugDouble(LOG_INIT,"Try again with startDirection = ", solverData->startDirection); } diff --git a/SimulationRuntime/c/util/simulation_options.c b/SimulationRuntime/c/util/simulation_options.c index 8c00312ef8..03ead1e3b9 100644 --- a/SimulationRuntime/c/util/simulation_options.c +++ b/SimulationRuntime/c/util/simulation_options.c @@ -52,6 +52,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = { /* FLAG_HOMOTOPY_MAX_LAMBDA_STEPS */ "homMaxLambdaSteps", /* FLAG_HOMOTOPY_MAX_NEWTON_STEPS */ "homMaxNewtonSteps", /* FLAG_HOMOTOPY_MAX_TRIES */ "homMaxTries", + /* FLAG_HOMOTOPY_NEG_START_DIR */ "homNegStartDir", /* FLAG_HOMOTOPY_ON_FIRST_TRY */ "homotopyOnFirstTry", /* FLAG_HOMOTOPY_TAU_DEC_FACTOR */ "homTauDecFac", /* FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED */ "homTauDecFacPredictor", @@ -160,6 +161,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = { /* FLAG_HOMOTOPY_MAX_LAMBDA_STEPS */ "[int (default size dependent)] maximum lambda steps allowed to run the homotopy path", /* FLAG_HOMOTOPY_MAX_NEWTON_STEPS */ "[int (default 20)] maximum newton steps in the homotopy corrector step", /* 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_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", @@ -294,6 +296,8 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = { " maximum newton steps in the homotopy corrector step", /* FLAG_HOMOTOPY_MAX_TRIES */ " 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 */ " 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.", @@ -540,6 +544,7 @@ const int FLAG_TYPE[FLAG_MAX] = { /* FLAG_HOMOTOPY_MAX_LAMBDA_STEPS */ FLAG_TYPE_OPTION, /* FLAG_HOMOTOPY_MAX_NEWTON_STEPS */ FLAG_TYPE_OPTION, /* FLAG_HOMOTOPY_MAX_TRIES */ FLAG_TYPE_OPTION, + /* FLAG_HOMOTOPY_NEG_START_DIR */ FLAG_TYPE_FLAG, /* FLAG_HOMOTOPY_ON_FIRST_TRY */ FLAG_TYPE_FLAG, /* FLAG_HOMOTOPY_TAU_DEC_FACTOR */ FLAG_TYPE_OPTION, /* FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED */ FLAG_TYPE_OPTION, diff --git a/SimulationRuntime/c/util/simulation_options.h b/SimulationRuntime/c/util/simulation_options.h index 055dfdf4e2..6df64cdda6 100644 --- a/SimulationRuntime/c/util/simulation_options.h +++ b/SimulationRuntime/c/util/simulation_options.h @@ -60,6 +60,7 @@ enum _FLAG FLAG_HOMOTOPY_MAX_LAMBDA_STEPS, FLAG_HOMOTOPY_MAX_NEWTON_STEPS, FLAG_HOMOTOPY_MAX_TRIES, + FLAG_HOMOTOPY_NEG_START_DIR, FLAG_HOMOTOPY_ON_FIRST_TRY, FLAG_HOMOTOPY_TAU_DEC_FACTOR, FLAG_HOMOTOPY_TAU_DEC_FACTOR_PRED,