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

Commit d4ea7ac

Browse files
ptaeuberOpenModelica-Hudson
authored andcommitted
Solve lambda0-system separately ...
... before applying adaptive local homotopy method. Belonging to [master]: - #2038
1 parent 34369f3 commit d4ea7ac

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
218218
/* If there is homotopy in the model and global homotopy is activated
219219
and homotopy on first try is deactivated,
220220
TRY TO SOLVE WITHOUT HOMOTOPY FIRST.
221-
To-Do: Activate trying without homotopy first also for the new global homotopy approach */
221+
To-Do: Activate trying without homotopy first also for the adaptive global homotopy approach */
222222
} else if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY] && data->callback->useHomotopy != 2) {
223223
/* try */
224224
#ifndef OMC_EMCC
@@ -240,7 +240,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
240240

241241
/* If there is homotopy in the model and global homotopy is activated
242242
and solving without homotopy failed or is not wanted,
243-
use GLOBAL HOMOTOPY METHOD. */
243+
use EQUIDISTANT GLOBAL HOMOTOPY METHOD. */
244244
if (data->callback->useHomotopy == 1 && solveWithGlobalHomotopy)
245245
{
246246
long step;
@@ -250,7 +250,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
250250
const char sep[] = ",";
251251
if(ACTIVE_STREAM(LOG_INIT))
252252
{
253-
sprintf(buffer, "%s_global_homotopy.csv", mData->modelFilePrefix);
253+
sprintf(buffer, "%s_equidistant_global_homotopy.csv", mData->modelFilePrefix);
254254
infoStreamPrint(LOG_INIT, 0, "The homotopy path will be exported to %s.", buffer);
255255
pFile = fopen(buffer, "wt");
256256
fprintf(pFile, "\"sep=%s\"\n%s", sep, "\"lambda\"");
@@ -297,9 +297,9 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
297297
#endif
298298
}
299299

300-
/* If there is homotopy in the model and the new global homotopy approach is activated
300+
/* If there is homotopy in the model and the adaptive global homotopy approach is activated
301301
and solving without homotopy failed or is not wanted,
302-
use NEW GLOBAL HOMOTOPY APPROACH. */
302+
use ADAPTIVE GLOBAL HOMOTOPY APPROACH. */
303303
if (data->callback->useHomotopy == 2 && solveWithGlobalHomotopy)
304304
{
305305
if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY])
@@ -318,7 +318,6 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
318318
infoStreamPrint(LOG_INIT, 0, "run along the homotopy path and solve the actual system");
319319
data->callback->functionInitialEquations(data, threadData);
320320

321-
data->simulationInfo->homotopyUsed = 1;
322321
messageClose(LOG_INIT);
323322
}
324323

SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ static int homotopyAlgorithm(DATA_HOMOTOPY* solverData, double *x)
16131613
const char sep[] = ",";
16141614
if(solverData->initHomotopy && ACTIVE_STREAM(LOG_INIT))
16151615
{
1616-
sprintf(buffer, "%s_syst%d_new_global_homotopy_%s.csv", solverData->data->modelData->modelFilePrefix, solverData->sysNumber, solverData->startDirection > 0 ? "pos" : "neg");
1616+
sprintf(buffer, "%s_nonlinsys%d_adaptive_%s_homotopy_%s.csv", solverData->data->modelData->modelFilePrefix, solverData->sysNumber, solverData->data->callback->useHomotopy == 2 ? "global" : "local", solverData->startDirection > 0 ? "pos" : "neg");
16171617
infoStreamPrint(LOG_INIT, 0, "The homotopy path will be exported to %s.", buffer);
16181618
pFile = fopen(buffer, "wt");
16191619
fprintf(pFile, "\"sep=%s\"\n%s", sep, "\"lambda\"");
@@ -1792,6 +1792,7 @@ static int homotopyAlgorithm(DATA_HOMOTOPY* solverData, double *x)
17921792
debugString(LOG_NLS_HOMOTOPY, "Newton iteration for corrector step begins!");
17931793
for(j=0;j<maxiter;j++)
17941794
{
1795+
debugInt(LOG_NLS_HOMOTOPY, "Iteration: ", j+1);
17951796
if (vec2Norm(solverData->n, solverData->hvec)<hEps || vec2Norm(solverData->n, solverData->hvecScaled)<hEps)
17961797
{
17971798
debugString(LOG_NLS_HOMOTOPY, "step accepted!");
@@ -2244,24 +2245,31 @@ int solveHomotopy(DATA *data, threadData_t *threadData, int sysNumber)
22442245
runHomotopy++;
22452246
/* debug output */
22462247
debugString(LOG_NLS_HOMOTOPY, "======================================================");
2247-
debugInt(LOG_NLS_HOMOTOPY, "RUN HOMOTOPY",runHomotopy);
22482248

22492249
if (solverData->initHomotopy) {
22502250
if (runHomotopy == 1) {
22512251
solverData->h_function = wrapper_fvec;
22522252
solverData->hJac_dh = wrapper_fvec_der;
22532253
solverData->startDirection = 1.0;
2254-
debugDouble(LOG_INIT,"STARTING HOMOTOPY METHOD; startDirection = ", solverData->startDirection);
2254+
debugInt(LOG_INIT, "Homotopy run: ", runHomotopy);
2255+
debugDouble(LOG_INIT,"startDirection = ", solverData->startDirection);
22552256
}
22562257

22572258
if (runHomotopy == 2) {
22582259
solverData->h_function = wrapper_fvec;
22592260
solverData->hJac_dh = wrapper_fvec_der;
22602261
solverData->startDirection = -1.0;
2261-
debugDouble(LOG_INIT,"STARTING HOMOTOPY METHOD; startDirection = ", solverData->startDirection);
2262+
debugInt(LOG_INIT, "Homotopy run: ", runHomotopy);
2263+
debugDouble(LOG_INIT,"Try again with startDirection = ", solverData->startDirection);
2264+
}
2265+
2266+
if (runHomotopy == 3) {
2267+
success = 0;
2268+
break;
22622269
}
22632270
}
22642271
else {
2272+
debugInt(LOG_NLS_HOMOTOPY, "Homotopy run: ", runHomotopy);
22652273
if (runHomotopy == 1)
22662274
{
22672275
/* store x0 and calculate f(x0) -> newton homotopy, fJac(x0) -> taylor, affin homotopy */

SimulationRuntime/c/simulation/solver/nonlinearSystem.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ int initializeNonlinearSystems(DATA *data, threadData_t *threadData)
426426
#endif
427427

428428
/* allocate solver data */
429-
if((data->callback->useHomotopy == 2 || data->callback->useHomotopy == 3) && nonlinsys[i].homotopySupport)
429+
if(data->callback->useHomotopy == 2 && nonlinsys[i].homotopySupport)
430430
allocateHomotopyData(size-1, &nonlinsys[i].solverData);
431431
else{
432432
switch(data->simulationInfo->nlsMethod)
@@ -447,6 +447,8 @@ int initializeNonlinearSystems(DATA *data, threadData_t *threadData)
447447
break;
448448
#if !defined(OMC_MINIMAL_RUNTIME)
449449
case NLS_MIXED:
450+
if (data->callback->useHomotopy == 3 && nonlinsys[i].homotopySupport)
451+
size--;
450452
mixedSolverData = (struct dataNewtonAndHybrid*) malloc(sizeof(struct dataNewtonAndHybrid));
451453
allocateHomotopyData(size, &(mixedSolverData->newtonData));
452454

@@ -530,7 +532,7 @@ int freeNonlinearSystems(DATA *data, threadData_t *threadData)
530532
}
531533
#endif
532534
/* free solver data */
533-
if((data->callback->useHomotopy == 2 || data->callback->useHomotopy == 3) && nonlinsys[i].homotopySupport)
535+
if(data->callback->useHomotopy == 2 && nonlinsys[i].homotopySupport)
534536
freeHomotopyData(&nonlinsys[i].solverData);
535537
else{
536538
switch(data->simulationInfo->nlsMethod)
@@ -796,6 +798,7 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
796798
int equidistantHomotopy = 0;
797799
int solveWithHomotopySolver = 0;
798800
int j;
801+
struct dataNewtonAndHybrid *mixedSolverData;
799802
char buffer[4096];
800803
FILE *pFile = NULL;
801804

@@ -809,7 +812,7 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
809812
rt_ext_tp_tick(&nonlinsys->totalTimeClock);
810813

811814
/* grab the initial guess */
812-
infoStreamPrint(LOG_NLS_EXTRAPOLATE, 1, "############ Start new iteration for system %ld at time %g ############", nonlinsys->equationIndex, data->localData[0]->timeValue);
815+
infoStreamPrint(LOG_NLS_EXTRAPOLATE, 1, "############ Start new iteration for nonlinear system %ld at time %g ############", nonlinsys->equationIndex, data->localData[0]->timeValue);
813816
/* if last solving is too long ago use just old values */
814817
if (fabs(data->localData[0]->timeValue - nonlinsys->lastTimeSolved) < 5*data->simulationInfo->stepSize || casualTearingSet)
815818
{
@@ -847,11 +850,31 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
847850
/* If the adaptive local/global homotopy approach is activated and the component contains the homotopy operator
848851
use the HOMOTOPY SOLVER, otherwise use the selected solver */
849852
if (solveWithHomotopySolver) {
850-
if (data->callback->useHomotopy == 3)
851-
infoStreamPrint(LOG_INIT, 0, "Local homotopy with adaptive step size started for system %d.", sysNumber);
852853
data->simulationInfo->lambda = 0.0;
854+
if (data->callback->useHomotopy == 3) {
855+
// First solve the lambda0-system separately
856+
infoStreamPrint(LOG_INIT, 0, "Local homotopy with adaptive step size started for nonlinear system %d.", sysNumber);
857+
infoStreamPrint(LOG_INIT, 1, "homotopy process\n---------------------------");
858+
infoStreamPrint(LOG_INIT, 0, "solve lambda0-system");
859+
nonlinsys->homotopySupport = 0;
860+
nonlinsys->solved = solveNLS(data, threadData, sysNumber);
861+
nonlinsys->homotopySupport = 1;
862+
infoStreamPrint(LOG_INIT, 0, "solving lambda0-system done\n---------------------------");
863+
infoStreamPrint(LOG_INIT, 0, "run along the homotopy path and solve the actual system");
864+
messageClose(LOG_INIT);
865+
}
853866
/* SOLVE! */
854-
nonlinsys->solved = solveHomotopy(data, threadData, sysNumber);
867+
if (data->callback->useHomotopy == 2 || nonlinsys->solved) {
868+
if (data->callback->useHomotopy == 3) {
869+
mixedSolverData = nonlinsys->solverData;
870+
nonlinsys->solverData = mixedSolverData->newtonData;
871+
}
872+
nonlinsys->solved = solveHomotopy(data, threadData, sysNumber);
873+
if (data->callback->useHomotopy == 3) {
874+
nonlinsys->solverData = mixedSolverData;
875+
}
876+
}
877+
data->simulationInfo->homotopyUsed = 1;
855878
}
856879
else {
857880
if (!(equidistantHomotopy && omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY])) {
@@ -870,12 +893,12 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber)
870893
if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY])
871894
warningStreamPrint(LOG_ASSERT, 0, "Failed to solve initial system %d without homotopy method. The local homotopy method with equidistant step size is used now.", sysNumber);
872895
else
873-
infoStreamPrint(LOG_INIT, 0, "Local homotopy with equidistant step size started for system %d.", sysNumber);
896+
infoStreamPrint(LOG_INIT, 0, "Local homotopy with equidistant step size started for nonlinear system %d.", sysNumber);
874897
#if !defined(OMC_NO_FILESYSTEM)
875898
const char sep[] = ",";
876899
if(ACTIVE_STREAM(LOG_INIT))
877900
{
878-
sprintf(buffer, "%s_homotopy_nls_%d.csv", data->modelData->modelFilePrefix, sysNumber);
901+
sprintf(buffer, "%s_nonlinsys%d_equidistant_local_homotopy.csv", data->modelData->modelFilePrefix, sysNumber);
879902
infoStreamPrint(LOG_INIT, 0, "The homotopy path of system %d will be exported to %s.", sysNumber, buffer);
880903
pFile = fopen(buffer, "wt");
881904
fprintf(pFile, "\"sep=%s\"\n%s", sep, "\"lambda\"");

0 commit comments

Comments
 (0)