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

Commit b67fdd1

Browse files
ptaeuberOpenModelica-Hudson
authored andcommitted
Try without homotopy also for local homotopy method
1 parent 9db6254 commit b67fdd1

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
185185
FILE *pFile = NULL;
186186
long i;
187187
MODEL_DATA *mData = data->modelData;
188-
int solvedWithHomotopy = 0;
188+
189+
data->simulationInfo->homotopyUsed = 0;
189190

190191
#if !defined(OMC_NDELAY_EXPRESSIONS) || OMC_NDELAY_EXPRESSIONS>0
191192
/* initial sample and delay before initial the system */
@@ -221,7 +222,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
221222
MMC_CATCH_INTERNAL(simulationJumpBuffer)
222223
#endif
223224
if(init_lambda_steps > 0)
224-
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve the initial system without homotopy method. If homotopy is available the homotopy method is used now.");
225+
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve the initialization problem without homotopy method. If homotopy is available the homotopy method is used now.");
225226
}
226227

227228
/* If there is homotopy in the model and global homotopy is activated
@@ -236,6 +237,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
236237
if(ACTIVE_STREAM(LOG_INIT))
237238
{
238239
sprintf(buffer, "%s_global_homotopy.csv", mData->modelFilePrefix);
240+
infoStreamPrint(LOG_INIT, 0, "The homotopy path will be exported to %s.", buffer);
239241
pFile = fopen(buffer, "wt");
240242
fprintf(pFile, "\"sep=,\"\n%s", "lambda");
241243
for(i=0; i<mData->nVariablesReal; ++i)
@@ -276,7 +278,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
276278
check_mixed_solutions(data, 0))
277279
break;
278280
}
279-
solvedWithHomotopy = 1;
281+
data->simulationInfo->homotopyUsed = 1;
280282
messageClose(LOG_INIT);
281283
}
282284

@@ -291,7 +293,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
291293
retVal = data->callback->functionRemovedInitialEquations(data, threadData);
292294

293295
if (!retVal)
294-
infoStreamPrint(LOG_STDOUT, 0, "The initialization finished successfully %s homotopy method.", solvedWithHomotopy ? "with" : "without");
296+
infoStreamPrint(LOG_STDOUT, 0, "The initialization finished successfully %s homotopy method.", data->simulationInfo->homotopyUsed ? "with" : "without");
295297

296298
TRACE_POP
297299
return retVal;

SimulationRuntime/c/simulation/solver/nonlinearSystem.c

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "newtonIteration.h"
4545
#endif
4646
#include "nonlinearSolverHomotopy.h"
47+
#include "simulation/options.h"
4748
#include "simulation/simulation_info_json.h"
4849
#include "simulation/simulation_runtime.h"
4950
#include "simulation/solver/model_help.h"
@@ -817,54 +818,67 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
817818
(data->callback->useHomotopy == 0) && init_lambda_steps > 1)
818819
lambda_steps = init_lambda_steps;
819820

820-
#if !defined(OMC_NO_FILESYSTEM)
821-
if(lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT))
822-
{
823-
sprintf(buffer, "%s_homotopy_nls_%d.csv", data->modelData->modelFilePrefix, sysNumber);
824-
infoStreamPrint(LOG_INIT, 0, "The homotopy path of system %d will be exported to %s.", sysNumber, buffer);
825-
pFile = fopen(buffer, "wt");
826-
fprintf(pFile, "\"sep=,\"\n%s", "lambda");
827-
for(j=0; j<nonlinsys->size; ++j)
828-
fprintf(pFile, ",%s", modelInfoGetEquation(&data->modelData->modelDataXml, nonlinsys->equationIndex).vars[j]);
829-
fprintf(pFile, "\n");
821+
nonlinsys->solved = 0;
822+
if (lambda_steps == 1 || !omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY]) {
823+
824+
if(data->callback->useHomotopy == 0)
825+
data->simulationInfo->lambda = 1.0;
826+
827+
if (lambda_steps > 1)
828+
infoStreamPrint(LOG_INIT, 0, "Try to solve initial system %d without homotopy first.", sysNumber);
829+
830+
/* SOLVE! */
831+
nonlinsys->solved = solveNLS(data, threadData, sysNumber);
830832
}
831-
#endif
832833

833-
for(step=0; step<lambda_steps-1; ++step)
834-
{
835-
data->simulationInfo->lambda = ((double)step)/(lambda_steps-1);
836-
infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g", sysNumber, data->simulationInfo->lambda);
837-
solveNLS(data, threadData, sysNumber);
834+
if (lambda_steps > 1 && !nonlinsys->solved) {
835+
if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY])
836+
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve initial system %d without homotopy method. The homotopy method is used now.", sysNumber);
838837

839838
#if !defined(OMC_NO_FILESYSTEM)
840839
if(ACTIVE_STREAM(LOG_INIT))
841840
{
842-
infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g done\n---------------------------", sysNumber, data->simulationInfo->lambda);
843-
fprintf(pFile, "%.16g", data->simulationInfo->lambda);
841+
sprintf(buffer, "%s_homotopy_nls_%d.csv", data->modelData->modelFilePrefix, sysNumber);
842+
infoStreamPrint(LOG_INIT, 0, "The homotopy path of system %d will be exported to %s.", sysNumber, buffer);
843+
pFile = fopen(buffer, "wt");
844+
fprintf(pFile, "\"sep=,\"\n%s", "lambda");
844845
for(j=0; j<nonlinsys->size; ++j)
845-
fprintf(pFile, ",%.16g", nonlinsys->nlsx[j]);
846+
fprintf(pFile, ",%s", modelInfoGetEquation(&data->modelData->modelDataXml, nonlinsys->equationIndex).vars[j]);
846847
fprintf(pFile, "\n");
847848
}
848849
#endif
849-
}
850850

851-
if(data->callback->useHomotopy == 0)
852-
data->simulationInfo->lambda = 1.0;
851+
for(step=0; step<lambda_steps; ++step)
852+
{
853+
data->simulationInfo->lambda = ((double)step)/(lambda_steps-1);
854+
855+
if(data->simulationInfo->lambda > 1.0) {
856+
data->simulationInfo->lambda = 1.0;
857+
}
858+
859+
infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g", sysNumber, data->simulationInfo->lambda);
860+
nonlinsys->solved = solveNLS(data, threadData, sysNumber);
853861

854-
/* SOLVE! */
855-
nonlinsys->solved = solveNLS(data, threadData, sysNumber);
862+
#if !defined(OMC_NO_FILESYSTEM)
863+
if(ACTIVE_STREAM(LOG_INIT))
864+
{
865+
infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g done\n---------------------------", sysNumber, data->simulationInfo->lambda);
866+
fprintf(pFile, "%.16g", data->simulationInfo->lambda);
867+
for(j=0; j<nonlinsys->size; ++j)
868+
fprintf(pFile, ",%.16g", nonlinsys->nlsx[j]);
869+
fprintf(pFile, "\n");
870+
}
871+
#endif
872+
}
856873

857874
#if !defined(OMC_NO_FILESYSTEM)
858-
if(lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT))
859-
{
860-
infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g done\n---------------------------", sysNumber, data->simulationInfo->lambda);
861-
fprintf(pFile, "%.16g", data->simulationInfo->lambda);
862-
for(j=0; j<nonlinsys->size; ++j)
863-
fprintf(pFile, ",%.16g", nonlinsys->nlsx[j]);
864-
fprintf(pFile, "\n");
865-
fclose(pFile);
866-
}
875+
if(lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT))
876+
{
877+
fclose(pFile);
878+
}
867879
#endif
880+
data->simulationInfo->homotopyUsed = 1;
881+
}
868882

869883
/* handle asserts */
870884
threadData->currentErrorStage = saveJumpState;

SimulationRuntime/c/simulation_data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ typedef struct SIMULATION_INFO
590590
int jacobianEvals; /* number of different columns to evaluate functionODE */
591591
int currentJacobianEval; /* current column to evaluate functionODE for Jacobian*/
592592

593+
int homotopyUsed; /* =1 the initialization problem was solved with a homotopy method, =0 otherwise */
593594
double lambda; /* homotopy parameter E [0, 1.0] */
594595

595596
/* indicators for simulations state */

0 commit comments

Comments
 (0)