@@ -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
797795static
798796int 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