4949#include "nonlinearSolverHomotopy.h"
5050#include "nonlinearSolverHybrd.h"
5151
52+ #ifdef __cplusplus
53+ extern "C" {
54+ #endif
55+
56+ extern int dgesv_ (int * n , int * nrhs , doublereal * a , int * lda , int * ipiv , doublereal * b , int * ldb , int * info );
57+
58+ #ifdef __cplusplus
59+ }
60+ #endif
61+
5262/*! \typedef DATA_HOMOTOPY
5363 * define memory structure for nonlinear system solver
5464 * \author bbachmann
@@ -953,6 +963,7 @@ static int wrapper_fvec_homotopy_fixpoint_der(DATA_HOMOTOPY* solverData, double*
953963 return 0 ;
954964}
955965
966+
956967/*! \fn getIndicesOfPivotElement for calculating pivot element
957968 *
958969 * \author bbachmann
@@ -976,6 +987,7 @@ static int wrapper_fvec_homotopy_fixpoint_der(DATA_HOMOTOPY* solverData, double*
976987 }
977988}
978989
990+
979991/*! \fn solveSystemWithTotalPivotSearch for solution of overdetermined linear system
980992 * used for the homotopy solver, for calculating the direction
981993 * used for the newton solver, for calculating the Newton step
@@ -994,6 +1006,7 @@ int solveSystemWithTotalPivotSearch(int n, double* x, double* A, int* indRow, in
9941006 double * res ;
9951007
9961008 debugMatrixDouble (LOG_NLS_JAC ,"Linear System Matrix [Jac res]:" ,A , n , m );
1009+ debugVectorDouble (LOG_NLS_JAC ,"vector b:" , A + n * n , n );
9971010
9981011 /* assume full rank of matrix [n x (n+1)] */
9991012 * rank = n ;
@@ -1077,20 +1090,85 @@ int solveSystemWithTotalPivotSearch(int n, double* x, double* A, int* indRow, in
10771090 * pos = indCol [n ];
10781091 }
10791092
1093+ return 0 ;
1094+ }
1095+
1096+
1097+ /*! \fn linearSolverWrapper
1098+ */
1099+ int linearSolverWrapper (int n , double * x , double * A , int * indRow , int * indCol , int * pos , int * rank , int method )
1100+ {
1101+ /* First try to use lapack and if it fails then
1102+ * use solveSystemWithTotalPivotSearch */
1103+ int returnValue = -1 ;
1104+ int solverinfo ;
1105+ int nrhs = 1 ;
1106+ int lda = n ;
1107+
1108+ debugMatrixDouble (LOG_NLS_JAC ,"Linear System Matrix [Jac res]:" , A , n , n + 1 );
1109+ debugVectorDouble (LOG_NLS_JAC ,"vector b:" , x , n );
1110+
1111+ switch (method ){
1112+ case (1 ): /* NLS_LS_TOTALPIVOT */
1113+
1114+ solverinfo = solveSystemWithTotalPivotSearch (n , x , A , indRow , indCol , pos , rank );
1115+ /* in case of failing */
1116+ if (solverinfo != 0 )
1117+ {
1118+ /* debug information */
1119+ debugString (LOG_NLS_V , "Linear total pivot solver failed!!!" );
1120+ debugString (LOG_NLS_V , "******************************************************" );
1121+ }
1122+ else
1123+ {
1124+ returnValue = 0 ;
1125+ }
1126+ break ;
1127+ case 2 : /* NLS_LS_LAPACK */
1128+ /* Solve system with lapack */
1129+ dgesv_ ((int * ) & n ,
1130+ (int * ) & nrhs ,
1131+ A ,
1132+ (int * ) & lda ,
1133+ indRow ,
1134+ x ,
1135+ (int * ) & n ,
1136+ & solverinfo );
1137+
1138+ debugMatrixDouble (LOG_NLS_JAC ,"Linear system matrix [Jac res] after decomposition:" , A , n , n + 1 );
1139+ /* in case of failing */
1140+ if (solverinfo != 0 )
1141+ {
1142+ /* debug information */
1143+ debugString (LOG_NLS_V , "Linear lapack solver failed!!!" );
1144+ debugString (LOG_NLS_V , "******************************************************" );
1145+ }
1146+ else
1147+ {
1148+ vecScalarMult (n , x , -1 , x );
1149+ returnValue = 0 ;
1150+ }
1151+ break ;
1152+ default :
1153+ warningStreamPrint (LOG_STDOUT , 0 , "Non-Linear solver try to run with a unknown linear solver." );
1154+ }
1155+
10801156 /* Debugging error of linear system */
10811157 if (ACTIVE_STREAM (LOG_NLS_JAC ))
10821158 {
1083- res = (double * ) calloc (n ,sizeof (double ));
1084- debugVectorDouble (LOG_NLS_JAC ,"solution:" , x , m );
1085- matVecMult (n , m , A , x , res );
1159+ double * res = (double * ) calloc (n ,sizeof (double ));
1160+ debugVectorDouble (LOG_NLS_JAC ,"solution:" , x , n );
1161+ matVecMult (n , n , A , x , res );
10861162 debugVectorDouble (LOG_NLS_JAC ,"test solution:" , res , n );
10871163 debugDouble (LOG_NLS_JAC ,"error of linear system = " , vec2Norm (n , res ));
10881164 free (res );
10891165 messageClose (LOG_NLS_JAC );
10901166 }
10911167
1092- return 0 ;
1168+ return returnValue ;
10931169}
1170+
1171+
10941172/*! \fn solve system with damped Newton-Raphson
10951173 *
10961174 * \author bbachmann
@@ -1114,6 +1192,7 @@ static int newtonAlgorithm(DATA_HOMOTOPY* solverData, double* x)
11141192 int assert = 1 ;
11151193 threadData_t * threadData = solverData -> threadData ;
11161194 NONLINEAR_SYSTEM_DATA * nonlinsys = & (solverData -> data -> simulationInfo -> nonlinearSystemData [solverData -> data -> simulationInfo -> currentNonlinearSystemIndex ]);
1195+ int linearSolverMethod = solverData -> data -> simulationInfo -> nlsLinearSolver ;
11171196
11181197 /* debug information */
11191198 debugString (LOG_NLS_V , "******************************************************" );
@@ -1135,7 +1214,7 @@ static int newtonAlgorithm(DATA_HOMOTOPY* solverData, double* x)
11351214 debugInt (LOG_NLS_V , "Iteration:" , numberOfIterations );
11361215
11371216 /* solve jacobian and function value (both stored in hJac, last column is fvec), side effects: jacobian matrix is changed */
1138- if ((numberOfIterations > 1 ) && ( solveSystemWithTotalPivotSearch ( solverData -> n , solverData -> dy0 , solverData -> fJac , solverData -> indRow , solverData -> indCol , & pos , & rank ) != 0 ) )
1217+ if ((numberOfIterations > 1 ) && linearSolverWrapper ( solverData -> n , solverData -> dy0 , solverData -> fJac , solverData -> indRow , solverData -> indCol , & pos , & rank , linearSolverMethod ) != 0 )
11391218 {
11401219 /* report solver abortion */
11411220 solverData -> info = -1 ;
@@ -1392,6 +1471,7 @@ static int newtonAlgorithm(DATA_HOMOTOPY* solverData, double* x)
13921471 matVecMultAbsBB (solverData -> n , solverData -> fJac , solverData -> ones , solverData -> resScaling );
13931472 debugVectorDouble (LOG_NLS_JAC , "residuum scaling:" , solverData -> resScaling , solverData -> n );
13941473 scaleMatrixRows (solverData -> n , solverData -> m , solverData -> fJac );
1474+ vecCopy (n , solverData -> fJac + n * n , solverData -> dy0 );
13951475 }
13961476 return 0 ;
13971477}
0 commit comments