4747/** \brief Result code indicating failure in setting the preconditioner.
4848 */
4949#define PMC_CONDENSE_SOLVER_LINSOL_PREC 10
50+ /** \brief Result code indicating failure to allocate the SUNDIALS Context.
51+ */
52+ #define PMC_CONDENSE_SOLVER_INIT_SUNDIALS 11
5053
5154static int condense_vf (realtype t , N_Vector y , N_Vector ydot , void * user_data );
5255
@@ -88,11 +91,26 @@ int condense_solver(int neq, double *x_f, double *abstol_f, double reltol_f,
8891 y = abstol = NULL ;
8992 cvode_mem = NULL ;
9093
94+ #if SUNDIALS_VERSION_MAJOR >= 6
95+ SUNContext sunctx = NULL ;
96+ flag = SUNContext_Create (NULL , & sunctx );
97+ if (condense_check_flag (& flag , "SUNContext_Create" , 1 ))
98+ return PMC_CONDENSE_SOLVER_INIT_SUNDIALS ;
99+ #endif
100+
101+ #if SUNDIALS_VERSION_MAJOR >= 6
102+ y = N_VNew_Serial (neq , sunctx );
103+ #else
91104 y = N_VNew_Serial (neq );
105+ #endif
92106 if (condense_check_flag ((void * )y , "N_VNew_Serial" , 0 ))
93107 return PMC_CONDENSE_SOLVER_INIT_Y ;
94108
109+ #if SUNDIALS_VERSION_MAJOR >= 6
110+ abstol = N_VNew_Serial (neq , sunctx );
111+ #else
95112 abstol = N_VNew_Serial (neq );
113+ #endif
96114 if (condense_check_flag ((void * )abstol , "N_VNew_Serial" , 0 ))
97115 return PMC_CONDENSE_SOLVER_INIT_ABSTOL ;
98116
@@ -107,7 +125,11 @@ int condense_solver(int neq, double *x_f, double *abstol_f, double reltol_f,
107125 t_initial = t_initial_f ;
108126 t_final = t_final_f ;
109127
128+ #if SUNDIALS_VERSION_MAJOR >= 6
129+ cvode_mem = CVodeCreate (CV_BDF , sunctx );
130+ #else
110131 cvode_mem = CVodeCreate (CV_BDF );
132+ #endif
111133 if (condense_check_flag ((void * )cvode_mem , "CVodeCreate" , 0 ))
112134 return PMC_CONDENSE_SOLVER_INIT_CVODE_MEM ;
113135
@@ -124,14 +146,18 @@ int condense_solver(int neq, double *x_f, double *abstol_f, double reltol_f,
124146 return PMC_CONDENSE_SOLVER_SET_MAX_STEPS ;
125147
126148
127- SUNLinearSolver LS = SUNLinSol_SPGMR (y , PREC_LEFT , 0 );
128- if (condense_check_flag ((void * )LS , "SUNLinSol_SPGMR" , 0 ))
149+ #if SUNDIALS_VERSION_MAJOR >= 6
150+ SUNLinearSolver LS = SUNLinSol_SPGMR (y , PREC_LEFT , 0 , sunctx );
151+ #else
152+ SUNLinearSolver LS = SUNLinSol_SPGMR (y , PREC_LEFT , 0 );
153+ #endif
154+ if (condense_check_flag ((void * )LS , "SUNLinSol_SPGMR" , 0 ))
129155 return PMC_CONDENSE_SOLVER_LINSOL_CTOR ;
130156 flag = CVodeSetLinearSolver (cvode_mem , LS , NULL );
131- if (condense_check_flag (& flag , "CVodeSetLinearSolver" , 1 ))
157+ if (condense_check_flag (& flag , "CVodeSetLinearSolver" , 1 ))
132158 return PMC_CONDENSE_SOLVER_LINSOL_SET ;
133159 flag = CVodeSetPreconditioner (cvode_mem , NULL , condense_solver_Solve );
134- if (condense_check_flag (& flag , "CVodeSetPreconditioner" , 1 ))
160+ if (condense_check_flag (& flag , "CVodeSetPreconditioner" , 1 ))
135161 return PMC_CONDENSE_SOLVER_LINSOL_PREC ;
136162
137163 t = t_initial ;
@@ -146,6 +172,9 @@ int condense_solver(int neq, double *x_f, double *abstol_f, double reltol_f,
146172 N_VDestroy_Serial (y );
147173 N_VDestroy_Serial (abstol );
148174 CVodeFree (& cvode_mem );
175+ #if SUNDIALS_VERSION_MAJOR >= 6
176+ SUNContext_Free (& sunctx );
177+ #endif
149178 return PMC_CONDENSE_SOLVER_SUCCESS ;
150179}
151180
0 commit comments