Ignis
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#include <sunmatrix/sunmatrix_band.h> // access to dense SUNMatrix
10#include <sunlinsol/sunlinsol_band.h> // access to dense SUNLinearSolver
11
18
20
21public:
22
24
25 SUNContext sun;
26 void *cmem;
27 N_Vector vars;
28 N_Vector atol;
29 realtype rtol;
30 unsigned nvar;
31 SUNMatrix J;
32 SUNLinearSolver LS;
33 int rv;
34
36
50
52 int (*Func)(realtype, N_Vector, N_Vector, void*),
53 void * _user_data,
54 const int _nvar,
55 const double _rtol,
56 const std::vector<double> &_atol,
57 const int mu,
58 const int ml,
59 std::vector<double> &y) :
60 nvar(_nvar),
61 rtol(_rtol) {
62
63 rv = SUNContext_Create(NULL, &sun);
64
65 vars = N_VNew_Serial(nvar, sun);
66 atol = N_VNew_Serial(nvar, sun);
67 for(int k=0; k<nvar; ++k) {
68 NV_Ith_S(vars, k) = y[k];
69 NV_Ith_S(atol, k) = _atol[k];
70 }
71
72 cmem = CVodeCreate(CV_BDF, sun);
73 rv = CVodeSetUserData(cmem, _user_data);
74 rv = CVodeInit(cmem, Func, 0.0, vars);
75 rv = CVodeSVtolerances(cmem, rtol, atol);
76 rv = CVodeSetMaxNumSteps(cmem, 20000); //5000);
77
78 J = SUNBandMatrix(nvar, mu, ml, sun); // linear solver matrix J
79 LS = SUNLinSol_Band(vars, J, sun); // set linear solver
80 rv = CVodeSetLinearSolver(cmem, LS, J); // associate matrix J and solver LS
81}
82
90
91int integrate(std::vector<double> &y, const realtype dt) {
92
93 for(int k=0; k<nvar; ++k)
94 NV_Ith_S(vars, k) = y[k];
95
96 realtype t;
97
98 rv = CVodeReInit(cmem, 0.0, vars);
99 rv = CVode(cmem, dt, vars, &t, CV_NORMAL);
100
101 for(int k=0; k<nvar; ++k)
102 y[k] = NV_Ith_S(vars,k);
103
104 return rv;
105}
106
112
114
115 N_VDestroy(vars);
116 N_VDestroy(atol);
117 CVodeFree(&cmem);
118 SUNLinSolFree(LS);
119 SUNMatDestroy(J);
120 SUNContext_Free(&sun);
121}
122};
unsigned nvar
number of equations being solved
N_Vector atol
vector atol (absolute tolerance, for each variable)
int integrate(std::vector< double > &y, const realtype dt)
integrator_cvode(int(*Func)(realtype, N_Vector, N_Vector, void *), void *_user_data, const int _nvar, const double _rtol, const std::vector< double > &_atol, const int mu, const int ml, std::vector< double > &y)
SUNContext sun
sundials object
realtype rtol
scalar rtol (relative tolerance)
N_Vector vars
vector of variables being solved
SUNMatrix J
matrix for linear solver
SUNLinearSolver LS
linear solver
int rv
return value: checking status of calls
void * cmem
cvode object