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

Commit 0bae97f

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
further fixes to ticket:4395
- adjust default linear solver to klu - fix tolerances - fix scaling factors
1 parent d62633c commit 0bae97f

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,23 @@ static int getNewtonStrategy()
306306
return NEWTON_NONE;
307307
}
308308

309-
static int getNlsLSSolver()
309+
static int getNlsLSSolver(int nlsSolver)
310310
{
311311
int i;
312312
const char *cflags = omc_flagValue[FLAG_NLS_LS];
313313
const string *method = cflags ? new string(cflags) : NULL;
314314

315315
if(!method)
316-
return NLS_LS_LAPACK; /* default method */
316+
{
317+
if (nlsSolver == NLS_KINSOL)
318+
{
319+
return NLS_LS_KLU; /* default kinsol linear solver method */
320+
}
321+
else
322+
{
323+
return NLS_LS_LAPACK; /* default method */
324+
}
325+
}
317326

318327
for(i=1; i<NLS_LS_MAX; ++i)
319328
if(*method == NLS_LS_METHOD[i])
@@ -834,7 +843,7 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr
834843
data->simulationInfo->lssMethod = getlinearSparseSolverMethod();
835844
data->simulationInfo->newtonStrategy = getNewtonStrategy();
836845
data->simulationInfo->nlsCsvInfomation = omc_flag[FLAG_NLS_INFO];
837-
data->simulationInfo->nlsLinearSolver = getNlsLSSolver();
846+
data->simulationInfo->nlsLinearSolver = getNlsLSSolver(data->simulationInfo->nlsMethod);
838847

839848
if(omc_flag[FLAG_LSS_MAX_DENSITY]) {
840849
linearSparseSolverMaxDensity = atof(omc_flagValue[FLAG_LSS_MAX_DENSITY]);

SimulationRuntime/c/simulation/solver/kinsolSolver.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ int nlsKinsolAllocate(int size, NONLINEAR_SYSTEM_DATA *nlsData, int linearSolver
185185
kinsolData->linearSolverMethod = linearSolverMethod;
186186
kinsolData->solved = 0;
187187

188-
kinsolData->fnormtol = sqrt(newtonFTol); /* function tolerance */
189-
kinsolData->scsteptol = sqrt(newtonXTol); /* step tolerance */
188+
kinsolData->fnormtol = newtonFTol; /* function tolerance */
189+
kinsolData->scsteptol = newtonXTol; /* step tolerance */
190190

191191
kinsolData->maxstepfactor = maxStepFactor; /* step tolerance */
192192

@@ -317,9 +317,6 @@ static int nlsKinsolResiduals(N_Vector x, N_Vector f, void *userData)
317317
data->simulationInfo->nonlinearSystemData[sysNumber].residualFunc(dataAndThreadData, xdata, fdata, (const int*) &iflag);
318318
iflag = 0;
319319

320-
_omc_printVectorWithEquationInfo(_omc_createVector(kinsolData->size, fdata),
321-
"fResiduals", LOG_NLS_V, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber));
322-
323320
#ifndef OMC_EMCC
324321
MMC_CATCH_INTERNAL(simulationJumpBuffer)
325322
#endif
@@ -695,7 +692,7 @@ void nlsKinsolXScaling(DATA* data, NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM
695692
{
696693
case SCALING_NOMINALSTART:
697694
for (i=0;i<nlsData->size;i++){
698-
xScaling[i] = fmax(nlsData->nominal[i],fabs(xStart[i]));
695+
xScaling[i] = 1/fmax(nlsData->nominal[i],fabs(xStart[i]));
699696
}
700697
break;
701698
case SCALING_ONES:
@@ -720,7 +717,6 @@ void nlsKinsolFScaling(DATA* data, NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM
720717
N_Vector tmp2 = N_VNew_Serial(kinsolData->size);
721718

722719
/* calculate the right jacobian */
723-
nlsKinsolResiduals(x, kinsolData->fTmp, &kinsolData->userData);
724720
if(nlsData->isPatternAvailable && kinsolData->linearSolverMethod == 3)
725721
{
726722
spJac = NewSparseMat(kinsolData->size,kinsolData->size,kinsolData->nnz);
@@ -750,6 +746,7 @@ void nlsKinsolFScaling(DATA* data, NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM
750746
fScaling[spJac->rowvals[i]] = fabs(spJac->data[i]);
751747
}
752748
}
749+
N_VInv(kinsolData->fScale, kinsolData->fScale);
753750

754751
N_VDestroy_Serial(tmp1);
755752
N_VDestroy_Serial(tmp2);
@@ -788,6 +785,7 @@ int nlsKinsolConfigPrint(NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM_DATA *nls
788785
infoStreamPrint(LOG_NLS_V, 0, "KINSOL strategy %d", kinsolData->kinsolStrategy);
789786
infoStreamPrint(LOG_NLS_V, 0, "KINSOL current retry %d", kinsolData->retries);
790787
infoStreamPrint(LOG_NLS_V, 0, "KINSOL max step %g", (*(KINMem)kinsolData->kinsolMemory).kin_mxnstepin);
788+
infoStreamPrint(LOG_NLS_V, 0, "KINSOL linear solver %d", kinsolData->linearSolverMethod);
791789

792790
messageClose(LOG_NLS_V);
793791

@@ -797,7 +795,7 @@ int nlsKinsolConfigPrint(NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM_DATA *nls
797795
static
798796
int nlsKinsolErrorHandler(int errorCode, DATA *data, NONLINEAR_SYSTEM_DATA *nlsData, NLS_KINSOL_DATA *kinsolData)
799797
{
800-
int retValue, i, retValue2=0;
798+
int retValue, i, retValue2=0, flag;
801799
double fNorm;
802800
double *xStart = NV_DATA_S(kinsolData->initialGuess);
803801
double *xScaling = NV_DATA_S(kinsolData->xScale);
@@ -830,7 +828,6 @@ int nlsKinsolErrorHandler(int errorCode, DATA *data, NONLINEAR_SYSTEM_DATA *nlsD
830828
kinsolData->retries--;
831829
return 1;
832830
/* maybe happened because of an out-dated factorization, so just retry */
833-
case KIN_LSETUP_FAIL:
834831
case KIN_LSOLVE_FAIL:
835832
warningStreamPrint(LOG_NLS, 0, "kinsols matrix need new factorization. Try again.\n");
836833
if (nlsData->isPatternAvailable){
@@ -842,6 +839,20 @@ int nlsKinsolErrorHandler(int errorCode, DATA *data, NONLINEAR_SYSTEM_DATA *nlsD
842839
warningStreamPrint(LOG_NLS, 0, "kinsols runs into issues retry with different configuration.\n");
843840
retValue = 1;
844841
break;
842+
case KIN_LSETUP_FAIL:
843+
/* in case of something goes wrong with the symbolic jacobian try the numerical */
844+
if ( kinsolData->linearSolverMethod == 3 && nlsData->isPatternAvailable && nlsData->analyticalJacobianColumn != NULL){
845+
flag = KINSlsSetSparseJacFn(kinsolData->kinsolMemory, nlsSparseJac);
846+
}
847+
if (checkReturnFlag(flag)){
848+
errorStreamPrint(LOG_STDOUT, 0, "##KINSOL## Something goes wrong while initialize KINSOL Sparse Solver!");
849+
return flag;
850+
}
851+
else
852+
{
853+
retValue = 1;
854+
}
855+
break;
845856
default:
846857
errorStreamPrint(LOG_STDOUT, 0, "kinsol has a serious solving issue ERROR %d\n", errorCode);
847858
return errorCode;

0 commit comments

Comments
 (0)