Skip to content

Commit f066c75

Browse files
committed
- update LOG_NLS and LOG_NLS_JAC
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15097 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 3414397 commit f066c75

File tree

3 files changed

+159
-131
lines changed

3 files changed

+159
-131
lines changed

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ void dumpInitialSolution(DATA *simData)
272272
{
273273
INFO6(LOG_SOTI, "[%ld] Real %s(start=%g, nominal=%g) = %g (pre: %g)", i+1,
274274
mData->realVarsData[i].info.name,
275-
simData->modelData.realVarsData[i].attribute.start,
276-
simData->modelData.realVarsData[i].attribute.nominal,
275+
mData->realVarsData[i].attribute.start,
276+
mData->realVarsData[i].attribute.nominal,
277277
simData->localData[0]->realVars[i],
278278
sInfo->realVarsPre[i]);
279279
}
@@ -283,9 +283,9 @@ void dumpInitialSolution(DATA *simData)
283283
INDENT(LOG_SOTI);
284284
for(i=mData->nStates; i<2*mData->nStates; ++i)
285285
{
286-
INFO4(LOG_SOTI, "[%ld] Real %s = %g (pre: %g)", i+1,
286+
INFO4(LOG_SOTI, "[%ld] Real %s = %g (pre: %g)", i+1,
287287
mData->realVarsData[i].info.name,
288-
simData->localData[0]->realVars[i],
288+
simData->localData[0]->realVars[i],
289289
sInfo->realVarsPre[i]);
290290
}
291291
RELEASE(LOG_SOTI);
@@ -294,11 +294,11 @@ void dumpInitialSolution(DATA *simData)
294294
INDENT(LOG_SOTI);
295295
for(i=2*mData->nStates; i<mData->nVariablesReal; ++i)
296296
{
297-
INFO6(LOG_SOTI, "[%ld] Real %s(start=%g, nominal=%g) = %g (pre: %g)", i+1,
298-
mData->realVarsData[i].info.name,
299-
simData->modelData.realVarsData[i].attribute.start,
300-
simData->modelData.realVarsData[i].attribute.nominal,
301-
simData->localData[0]->realVars[i],
297+
INFO6(LOG_SOTI, "[%ld] Real %s(start=%g, nominal=%g) = %g (pre: %g)", i+1,
298+
mData->realVarsData[i].info.name,
299+
mData->realVarsData[i].attribute.start,
300+
mData->realVarsData[i].attribute.nominal,
301+
simData->localData[0]->realVars[i],
302302
sInfo->realVarsPre[i]);
303303
}
304304
RELEASE(LOG_SOTI);
@@ -307,22 +307,22 @@ void dumpInitialSolution(DATA *simData)
307307
INDENT(LOG_SOTI);
308308
for(i=0; i<mData->nVariablesInteger; ++i)
309309
{
310-
INFO5(LOG_SOTI, "[%ld] Integer %s(start=%ld) = %ld (pre: %ld)", i+1,
311-
mData->integerVarsData[i].info.name,
312-
mData->integerVarsData[i].attribute.start,
313-
simData->localData[0]->integerVars[i],
314-
sInfo->integerVarsPre[i]);
310+
INFO5(LOG_SOTI, "[%ld] Integer %s(start=%ld) = %ld (pre: %ld)", i+1,
311+
mData->integerVarsData[i].info.name,
312+
mData->integerVarsData[i].attribute.start,
313+
simData->localData[0]->integerVars[i],
314+
sInfo->integerVarsPre[i]);
315315
}
316316
RELEASE(LOG_SOTI);
317317

318318
INFO(LOG_SOTI, "boolean variables");
319319
INDENT(LOG_SOTI);
320320
for(i=0; i<mData->nVariablesBoolean; ++i)
321321
{
322-
INFO5(LOG_SOTI, "[%ld] Boolean %s(start=%s) = %s (pre: %s)", i+1,
323-
mData->booleanVarsData[i].info.name,
324-
mData->booleanVarsData[i].attribute.start ? "true" : "false",
325-
simData->localData[0]->booleanVars[i] ? "true" : "false",
322+
INFO5(LOG_SOTI, "[%ld] Boolean %s(start=%s) = %s (pre: %s)", i+1,
323+
mData->booleanVarsData[i].info.name,
324+
mData->booleanVarsData[i].attribute.start ? "true" : "false",
325+
simData->localData[0]->booleanVars[i] ? "true" : "false",
326326
sInfo->booleanVarsPre[i] ? "true" : "false");
327327
}
328328
RELEASE(LOG_SOTI);
@@ -331,10 +331,10 @@ void dumpInitialSolution(DATA *simData)
331331
INDENT(LOG_SOTI);
332332
for(i=0; i<mData->nVariablesString; ++i)
333333
{
334-
INFO5(LOG_SOTI, "[%ld] String %s(start=%s) = %s (pre: %s)", i+1,
335-
mData->stringVarsData[i].info.name,
336-
mData->stringVarsData[i].attribute.start,
337-
simData->localData[0]->stringVars[i],
334+
INFO5(LOG_SOTI, "[%ld] String %s(start=%s) = %s (pre: %s)", i+1,
335+
mData->stringVarsData[i].info.name,
336+
mData->stringVarsData[i].attribute.start,
337+
simData->localData[0]->stringVars[i],
338338
sInfo->stringVarsPre[i]);
339339
}
340340
RELEASE(LOG_SOTI);
@@ -418,7 +418,7 @@ static int initialize2(INIT_DATA *initData, int optiMethod, int useScaling)
418418
}
419419
else if(retVal >= 0 && funcValue == bestFuncValue)
420420
{
421-
/*WARNING("local minimum");*/
421+
/* WARNING("local minimum"); */
422422
INFO(LOG_INIT, "not updating bestZ");
423423
break;
424424
}

SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c

Lines changed: 121 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,28 @@ void printVector(const double *vector, const int *size, const int logLevel, cons
187187
*
188188
* \author wbraun
189189
*/
190-
void printStatus(DATA_HYBRD *solverData, const int *nfunc_evals, const double *xerror, const double *xerror_scaled, const int logLevel){
191-
190+
void printStatus(DATA_HYBRD *solverData, const int *nfunc_evals, const double *xerror, const double *xerror_scaled, const int logLevel)
191+
{
192192
int i;
193193

194-
INFO3(logLevel, "nfunc = %d +++ error = %.20e +++ error_scaled = %.20e", *nfunc_evals, *xerror, *xerror_scaled);
194+
INFO(logLevel, "nls status");
195195
INDENT(logLevel);
196196

197-
INFO(logLevel, "x");
197+
INFO(logLevel, "variables");
198198
INDENT(logLevel);
199-
for(i = 0; i < solverData->n; i++)
200-
INFO3(logLevel, "x[%d] = %.20e\n\tscaling factor = %f", i, solverData->x[i], solverData->diag[i]);
199+
for(i=0; i<solverData->n; i++)
200+
INFO3(logLevel, "x[%d] = %.20e [scaling factor = %f]", i, solverData->x[i], solverData->diag[i]);
201201
RELEASE(logLevel);
202202

203-
INFO(logLevel, "fvec");
203+
INFO(logLevel, "functions");
204204
INDENT(logLevel);
205-
for(i = 0; i < solverData->n; i++)
206-
INFO3(logLevel, "res[%d] = %.20e\n\tscaling factor = %f", i, solverData->fvec[i], solverData->resScaling[i]);
205+
for(i=0; i<solverData->n; i++)
206+
INFO3(logLevel, "res[%d] = %.20e [scaling factor = %f]", i, solverData->fvec[i], solverData->resScaling[i]);
207+
RELEASE(logLevel);
208+
209+
INFO(logLevel, "statistics");
210+
INDENT(logLevel);
211+
INFO3(logLevel, "nfunc = %d\nerror = %.20e\nerror_scaled = %.20e", *nfunc_evals, *xerror, *xerror_scaled);
207212
RELEASE(logLevel);
208213

209214
RELEASE(logLevel);
@@ -414,20 +419,17 @@ int solveHybrd(DATA *data, int sysNumber)
414419
/* debug output */
415420
if(ACTIVE_STREAM(LOG_NLS))
416421
{
417-
INFO2(LOG_NLS, "Start solving Non-Linear System %s at time %e",
422+
INFO2(LOG_NLS, "start solving non-linear system >>%s<< at time %g",
418423
modelInfoXmlGetEquation(&data->modelData.modelDataXml,eqSystemNumber).name,
419424
data->localData[0]->timeValue);
420425
INDENT(LOG_NLS);
421-
422-
for(i = 0; i < solverData->n; i++)
426+
for(i=0; i<solverData->n; i++)
423427
{
428+
INFO2(LOG_NLS, "x[%d] = %g", i, systemData->nlsx[i]);
424429
INDENT(LOG_NLS);
425-
INFO2(LOG_NLS, "x[%d] = %.20e", i, systemData->nlsx[i]);
426-
INDENT(LOG_NLS);
427-
INFO3(LOG_NLS, "scaling = %f +++ old = %.20e +++ extrapolated = %.20e",
430+
INFO3(LOG_NLS, "scaling = %g\nold = %g\nextrapolated = %g",
428431
systemData->nominal[i], systemData->nlsxOld[i], systemData->nlsxExtrapolation[i]);
429432
RELEASE(LOG_NLS);
430-
RELEASE(LOG_NLS);
431433
}
432434
RELEASE(LOG_NLS);
433435
}
@@ -526,7 +528,7 @@ int solveHybrd(DATA *data, int sysNumber)
526528
storeRelations(data);
527529
}
528530

529-
/* Scaling residual vector */
531+
/* scaling residual vector */
530532
{
531533
int l=0;
532534
for(i=0;i<solverData->n;i++){
@@ -540,12 +542,13 @@ int solveHybrd(DATA *data, int sysNumber)
540542
}
541543
}
542544

543-
544-
/* Debug output */
545-
if(ACTIVE_STREAM(LOG_NLS_V)) {
545+
/* debug output */
546+
if(ACTIVE_STREAM(LOG_NLS_V))
547+
{
546548
INFO(LOG_NLS_V, "scaling factors for residual vector");
547549
INDENT(LOG_NLS_V);
548-
for(i=0;i<solverData->n;i++){
550+
for(i=0;i<solverData->n;i++)
551+
{
549552
INFO2(LOG_NLS_V, "scaled residual [%d] : %.20e", i, solverData->fvecScaled[i]);
550553
INDENT(LOG_NLS_V);
551554
INFO2(LOG_NLS_V, "scaling factor [%d] : %.20e", i, solverData->resScaling[i]);
@@ -554,17 +557,21 @@ int solveHybrd(DATA *data, int sysNumber)
554557
RELEASE(LOG_NLS_V);
555558
}
556559

557-
/* Debug output */
560+
/* debug output */
558561
if(ACTIVE_STREAM(LOG_NLS_JAC))
559562
{
560-
INFO(LOG_NLS_JAC,"Print jacobian matrix:");
561-
for(i=0; i < solverData->n;i++)
563+
char buffer[4096];
564+
565+
INFO2(LOG_NLS_JAC, "jacobian matrix [%dx%d]", solverData->n, solverData->n);
566+
INDENT(LOG_NLS_JAC);
567+
for(i=0; i<solverData->n;i++)
562568
{
563-
printf("%d : ", i);
564-
for(j=0; j < solverData->n;j++)
565-
printf("%f ",solverData->fjacobian[i*solverData->n+j]);
566-
printf("\n");
569+
buffer[0] = 0;
570+
for(j=0; j<solverData->n; j++)
571+
sprintf(buffer, "%s%10g ", buffer, solverData->fjacobian[i*solverData->n+j]);
572+
INFO(LOG_NLS_JAC, buffer);
567573
}
574+
RELEASE(LOG_NLS_JAC);
568575
}
569576

570577
/* check for error */
@@ -574,87 +581,104 @@ int solveHybrd(DATA *data, int sysNumber)
574581
if(solverData->info == 1 && (xerror > local_tol && xerror_scaled > local_tol))
575582
solverData->info = 4;
576583

577-
578584
/* solution found */
579-
if(solverData->info == 1 || xerror <= local_tol || xerror_scaled <= local_tol) {
585+
if(solverData->info == 1 || xerror <= local_tol || xerror_scaled <= local_tol)
586+
{
580587
success = 1;
581588
nfunc_evals += solverData->nfev;
582-
if(ACTIVE_STREAM(LOG_NLS)) {
589+
if(ACTIVE_STREAM(LOG_NLS))
590+
{
591+
INFO(LOG_NLS, "system solved");
592+
INDENT(LOG_NLS);
593+
INFO2(LOG_NLS, "%d retries\n%d restarts", retries, retries2+retries3);
583594
RELEASE(LOG_NLS);
584-
INFO2(LOG_NLS, "*** System solved ***\n%d retries +++ %d restarts", retries,
585-
retries2+retries3);
586595

587596
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS);
588597

589598
}
590-
/* first try to decrease factor*/
591-
} else if((solverData->info == 4 || solverData->info == 5) && retries < 3) {
592-
/* set x vector */
593-
if(data->simulationInfo.discreteCall)
594-
memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
595-
else
596-
memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));
597-
598-
solverData->factor = solverData->factor / 10.0;
599+
}
600+
else if((solverData->info == 4 || solverData->info == 5) && retries < 3)
601+
{
602+
/* first try to decrease factor */
603+
604+
/* set x vector */
605+
if(data->simulationInfo.discreteCall)
606+
memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
607+
else
608+
memcpy(solverData->x, systemData->nlsxExtrapolation, solverData->n*(sizeof(double)));
599609

600-
retries++;
601-
giveUp = 0;
602-
nfunc_evals += solverData->nfev;
603-
if(ACTIVE_STREAM(LOG_NLS)) {
604-
INFO1(LOG_NLS, " - iteration making no progress:\t decreasing initial step bound to %f.",
605-
solverData->factor);
606-
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
607-
}
608-
/* try to vary the initial values */
609-
} else if((solverData->info == 4 || solverData->info == 5) && retries < 4) {
610-
for(i = 0; i < solverData->n; i++) {
611-
solverData->x[i] += systemData->nominal[i] * 0.1;
612-
};
613-
solverData->factor = initial_factor;
614-
retries++;
615-
giveUp = 0;
616-
nfunc_evals += solverData->nfev;
617-
if(ACTIVE_STREAM(LOG_NLS)) {
618-
INFO(LOG_NLS,
619-
" - iteration making no progress:\t vary solution point by 1%%.");
620-
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
621-
}
622-
/* try old values as x-Scaling factors */
623-
} else if((solverData->info == 4 || solverData->info == 5) && retries < 5) {
610+
solverData->factor = solverData->factor / 10.0;
624611

625-
for(i=0;i<solverData->n;i++){
626-
solverData->xScalefactors[i] = fmax(systemData->nlsxOld[i], systemData->nominal[i]);
627-
}
628-
retries++;
629-
giveUp = 0;
630-
nfunc_evals += solverData->nfev;
631-
if(ACTIVE_STREAM(LOG_NLS)) {
632-
INFO(LOG_NLS,
633-
" - iteration making no progress:\t try without scaling at all.");
634-
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
635-
}
636-
/* try to disable x-Scaling */
637-
} else if((solverData->info == 4 || solverData->info == 5) && retries < 6) {
638-
int scaling = solverData->useXScaling;
639-
if(scaling)
640-
solverData->useXScaling = 0;
641-
memcpy(solverData->xScalefactors, systemData->nominal, solverData->n*(sizeof(double)));
642-
retries++;
643-
giveUp = 0;
644-
nfunc_evals += solverData->nfev;
645-
if(ACTIVE_STREAM(LOG_NLS)) {
646-
INFO(LOG_NLS,
647-
" - iteration making no progress:\t try without scaling at all.");
648-
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
649-
}
650-
/* try to solve non-continuous
651-
* work-a-round: since other wise some model does
652-
* stuck in event iteration. e.g.: Modelica.Mechanics.Rotational.Examples.HeatLosses
653-
*/
654-
} else if((solverData->info == 4 || solverData->info == 5) && retries < 7 && data->simulationInfo.discreteCall) {
612+
retries++;
613+
giveUp = 0;
614+
nfunc_evals += solverData->nfev;
615+
if(ACTIVE_STREAM(LOG_NLS))
616+
{
617+
INFO1(LOG_NLS, " - iteration making no progress:\t decreasing initial step bound to %f.", solverData->factor);
618+
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
619+
}
620+
}
621+
else if((solverData->info == 4 || solverData->info == 5) && retries < 4)
622+
{
623+
/* try to vary the initial values */
624+
625+
for(i = 0; i < solverData->n; i++)
626+
solverData->x[i] += systemData->nominal[i] * 0.1;
627+
628+
solverData->factor = initial_factor;
629+
retries++;
630+
giveUp = 0;
631+
nfunc_evals += solverData->nfev;
632+
633+
if(ACTIVE_STREAM(LOG_NLS))
634+
{
635+
INFO(LOG_NLS, "iteration making no progress:\t vary solution point by 1%%.");
636+
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
637+
}
638+
}
639+
else if((solverData->info == 4 || solverData->info == 5) && retries < 5)
640+
{
641+
/* try old values as x-Scaling factors */
642+
643+
for(i=0;i<solverData->n;i++)
644+
solverData->xScalefactors[i] = fmax(systemData->nlsxOld[i], systemData->nominal[i]);
655645

646+
retries++;
647+
giveUp = 0;
648+
nfunc_evals += solverData->nfev;
649+
if(ACTIVE_STREAM(LOG_NLS))
650+
{
651+
INFO(LOG_NLS, "iteration making no progress:\t try without scaling at all.");
652+
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
653+
}
654+
}
655+
else if((solverData->info == 4 || solverData->info == 5) && retries < 6)
656+
{
657+
/* try to disable x-Scaling */
658+
659+
int scaling = solverData->useXScaling;
660+
if(scaling)
661+
solverData->useXScaling = 0;
662+
663+
memcpy(solverData->xScalefactors, systemData->nominal, solverData->n*(sizeof(double)));
664+
retries++;
665+
giveUp = 0;
666+
nfunc_evals += solverData->nfev;
667+
668+
if(ACTIVE_STREAM(LOG_NLS))
669+
{
670+
INFO(LOG_NLS, "iteration making no progress:\t try without scaling at all.");
671+
printStatus(solverData, &nfunc_evals, &xerror, &xerror_scaled, LOG_NLS_V);
672+
}
673+
}
674+
else if((solverData->info == 4 || solverData->info == 5) && retries < 7 && data->simulationInfo.discreteCall)
675+
{
676+
/* try to solve non-continuous
677+
* work-a-round: since other wise some model does
678+
* stuck in event iteration. e.g.: Modelica.Mechanics.Rotational.Examples.HeatLosses
679+
*/
680+
656681
memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
657-
658682
retries++;
659683

660684
/* try to solve a discontinuous system */

0 commit comments

Comments
 (0)