hips
Loading...
Searching...
No Matches
integrator_cvode.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include <cvode/cvode.h> // prototypes for CVODE fcts., consts.
6#include <nvector/nvector_serial.h> // access to serial N_Vector
7#include <sunmatrix/sunmatrix_dense.h> // access to dense SUNMatrix
8#include <sunlinsol/sunlinsol_dense.h> // access to dense SUNLinearSolver
9
16
18
19public:
20
22
23 SUNContext sun;
24 void *cmem;
25 N_Vector vars;
26 N_Vector atol;
27 sunrealtype rtol;
28 unsigned nvar;
29 SUNMatrix J;
30 SUNLinearSolver LS;
31 int rv;
32
34
45
47 int (*Func)(sunrealtype, N_Vector, N_Vector, void*),
48 void * _user_data,
49 const int _nvar,
50 const double _rtol,
51 const std::vector<double> &_atol) :
52 nvar(_nvar),
53 rtol(_rtol) {
54
55 rv = SUNContext_Create(SUN_COMM_NULL, &sun);
56
57 vars = N_VNew_Serial(nvar, sun);
58 atol = N_VNew_Serial(nvar, sun);
59 for(int k=0; k<nvar; ++k)
60 NV_Ith_S(atol, k) = _atol[k];
61
62 cmem = CVodeCreate(CV_BDF, sun);
63 rv = CVodeSetUserData(cmem, _user_data);
64 rv = CVodeInit(cmem, Func, 0.0, vars);
65 rv = CVodeSVtolerances(cmem, rtol, atol);
66 rv = CVodeSetMaxNumSteps(cmem, 5000);
67
68 J = SUNDenseMatrix(nvar, nvar, sun); // linear solver matrix J
69 LS = SUNLinSol_Dense(vars, J, sun); // set linear solver
70 rv = CVodeSetLinearSolver(cmem, LS, J); // associate matrix J and solver LS
71}
72
73//--------------
74
82
83int integrate(std::vector<double> &y, const sunrealtype dt) {
84
85 for(int k=0; k<nvar; ++k)
86 NV_Ith_S(vars, k) = y[k];
87
88 sunrealtype t;
89
90 rv = CVodeReInit(cmem, 0.0, vars);
91 rv = CVode(cmem, dt, vars, &t, CV_NORMAL);
92
93 for(int k=0; k<nvar; ++k)
94 y[k] = NV_Ith_S(vars,k);
95
96 return rv;
97}
98
104
106
107 N_VDestroy(vars);
108 N_VDestroy(atol);
109 CVodeFree(&cmem);
110 SUNLinSolFree(LS);
111 SUNMatDestroy(J);
112 SUNContext_Free(&sun);
113}
114};
unsigned nvar
number of equations being solved
N_Vector atol
vector atol (for each variable)
int integrate(std::vector< double > &y, const sunrealtype dt)
SUNContext sun
sundials object
N_Vector vars
vector of variables being solved
SUNMatrix J
matrix for linear solver
SUNLinearSolver LS
linear solver
integrator_cvode(int(*Func)(sunrealtype, N_Vector, N_Vector, void *), void *_user_data, const int _nvar, const double _rtol, const std::vector< double > &_atol)
int rv
return value: checking status of calls
sunrealtype rtol
scalar rtol
void * cmem
cvode object