accelerInt  v0.1
cvodes_init.c
Go to the documentation of this file.
1 
9 #include "header.h"
10 #include "solver_options.h"
11 #include "cvodes_dydt.h"
12 
13 #ifndef FINITE_DIFFERENCE
14  #include "cvodes_jac.h"
15 #endif
16 
17 /* CVODES INCLUDES */
18 #include "sundials/sundials_types.h"
19 #include "sundials/sundials_math.h"
20 #include "sundials/sundials_nvector.h"
21 #include "nvector/nvector_serial.h"
22 #include "cvodes/cvodes.h"
23 #include "cvodes/cvodes_lapack.h"
24 
25 #ifdef GENERATE_DOCS
26 namespace cvode {
27 #endif
28 
30 N_Vector *y_locals;
34 void** integrators;
35 
49  void initialize_solver(int num_threads) {
50  y_locals = (N_Vector*)malloc(num_threads * sizeof(N_Vector));
51  y_local_vectors = (double*)calloc(num_threads * NSP, sizeof(double));
52  integrators = (void**)malloc(num_threads * sizeof(void*));
53 
54  for (int i = 0; i < num_threads; i++)
55  {
56  integrators[i] = CVodeCreate(CV_BDF, CV_NEWTON);
57  y_locals[i] = N_VMake_Serial(NSP, &y_local_vectors[i * NSP]);
58  if (integrators[i] == NULL)
59  {
60  printf("Error creating CVodes Integrator\n");
61  exit(-1);
62  }
63 
64  //initialize
65  int flag = CVodeInit(integrators[i], dydt_cvodes, 0, y_locals[i]);
66  if (flag != CV_SUCCESS) {
67  if (flag == CV_MEM_FAIL) {
68  printf("Memory allocation failed.\n");
69  } else if (flag == CV_ILL_INPUT) {
70  printf("Illegal value for CVodeInit input argument.\n");
71  } else if (flag == CV_MEM_NULL) {
72  printf("CVODEs Memory was not initialized with CVodeInit!\n");
73  }
74  exit(flag);
75  }
76 
77  //set tolerances
78  flag = CVodeSStolerances(integrators[i], RTOL, ATOL);
79  if (flag != CV_SUCCESS) {
80  if (flag == CV_NO_MALLOC) {
81  printf("CVODE memory block not initialized by CVodeCreate.\n");
82  } else if (flag == CV_ILL_INPUT) {
83  printf("Illegal value for CVodeInit input argument.\n");
84  } else if (flag == CV_MEM_NULL) {
85  printf("CVODEs Memory was not initialized with CVodeInit!\n");
86  }
87  exit(flag);
88  }
89 
90  //setup the solver
91  flag = CVLapackDense(integrators[i], NSP);
92  if (flag != CVDLS_SUCCESS) {
93  if (flag == CVDLS_MEM_FAIL) {
94  printf("CVODE memory block not initialized by CVodeCreate.\n");
95  } else if (flag == CVDLS_ILL_INPUT) {
96  printf("Illegal value for CVodeInit input argument.\n");
97  } else if (flag == CVDLS_MEM_NULL) {
98  printf("CVODEs Memory was not initialized with CVodeInit!\n");
99  }
100  exit(flag);
101  }
102 
103  #ifndef FINITE_DIFFERENCE
104  flag = CVDlsSetDenseJacFn(integrators[i], eval_jacob_cvodes);
105  if (flag != CV_SUCCESS) {
106  printf("Error setting analytic jacobian\n");
107  exit(flag);
108  }
109  #endif
110 
111  #ifdef CV_MAX_ORD
112  CVodeSetMaxOrd(integrators[i], CV_MAX_ORD);
113  if (flag != CV_SUCCESS) {
114  printf("Error setting max order\n");
115  exit(flag);
116  }
117  #endif
118 
119  #ifdef CV_MAX_STEPS
120  CVodeSetMaxNumSteps(integrators[i], CV_MAX_STEPS);
121  if (flag != CV_SUCCESS) {
122  printf("Error setting max steps\n");
123  exit(flag);
124  }
125  #endif
126 
127  #ifdef CV_HMAX
128  CVodeSetMaxStep(integrators[i], CV_HMAX);
129  if (flag != CV_SUCCESS) {
130  printf("Error setting max timestep\n");
131  exit(flag);
132  }
133  #endif
134  #ifdef CV_HMIN
135  CVodeSetMinStep(integrators[i], CV_HMIN);
136  if (flag != CV_SUCCESS) {
137  printf("Error setting min timestep\n");
138  exit(flag);
139  }
140  #endif
141  #ifdef CV_MAX_ERRTEST_FAILS
142  CVodeSetMaxErrTestFails(integrators[i], CV_MAX_ERRTEST_FAILS);
143  if (flag != CV_SUCCESS) {
144  printf("Error setting max error test fails\n");
145  exit(flag);
146  }
147  #endif
148  #ifdef CV_MAX_HNIL
149  CVodeSetMaxHnilWarns(integrators[i], CV_MAX_HNIL);
150  if (flag != CV_SUCCESS) {
151  printf("Error setting max hnil warnings\n");
152  exit(flag);
153  }
154  #endif
155  }
156  }
157 
165  void cleanup_solver(int num_threads) {
166  //free the integrators and nvectors
167  for (int i = 0; i < num_threads; i++)
168  {
169  CVodeFree(&integrators[i]);
170  N_VDestroy(y_locals[i]);
171  }
172  free(y_locals);
173  free(y_local_vectors);
174  free(integrators);
175  }
176 
181  const char* solver_name() {
182 #ifdef SUNDIALS_ANALYTIC_JACOBIAN
183  const char* name = "cvodes-analytic-int";
184 #else
185  const char* name = "cvodes-int";
186 #endif
187  return name;
188  }
189 
191  }
192  void solver_log() {
193  }
194 
195 #ifdef GENERATE_DOCS
196 }
197 #endif
Header file for CVODEs interface to RHS of ODEs.
An example header file that defines system size and other required methods for integration of the van...
void solver_log()
Definition: cvodes_init.c:192
#define NSP
The IVP system size.
Definition: header.cuh:20
A file generated by Scons that specifies various options to the solvers.
void ** integrators
Definition: cvodes_init.c:34
const char * solver_name()
Returns a descriptive solver name.
Definition: cvodes_init.c:181
N_Vector * y_locals
Definition: cvodes_init.c:30
int dydt_cvodes(realtype t, N_Vector y, N_Vector ydot, void *f)
Definition: cvodes_dydt.c:16
#define RTOL
void initialize_solver(int num_threads)
Initializes the solver.
Definition: cvodes_init.c:49
#define ATOL
#define CV_MAX_HNIL
void cleanup_solver(int num_threads)
Cleans up the created solvers.
Definition: cvodes_init.c:165
int eval_jacob_cvodes(long int N, double t, N_Vector y, N_Vector ydot, DlsMat jac, void *f, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
The CVODEs Jacobian interface for a direct dense Jacobian.
Definition: cvodes_jac.c:13
A simple wrapper, allowing for use of the analytical jacobian w/ CVODES.
#define CV_MAX_ERRTEST_FAILS
#define CV_MAX_STEPS
double * y_local_vectors
Definition: cvodes_init.c:32
void init_solver_log()
Definition: cvodes_init.c:190