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
19
class
integrator_cvode
{
20
21
public
:
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
51
integrator_cvode
(
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
91
int
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
113
~integrator_cvode
() {
114
115
N_VDestroy(
vars
);
116
N_VDestroy(
atol
);
117
CVodeFree(&
cmem
);
118
SUNLinSolFree(
LS
);
119
SUNMatDestroy(
J
);
120
SUNContext_Free(&
sun
);
121
}
122
};
integrator_cvode
Definition
integrator_cvode.h:19
integrator_cvode::nvar
unsigned nvar
number of equations being solved
Definition
integrator_cvode.h:30
integrator_cvode::atol
N_Vector atol
vector atol (absolute tolerance, for each variable)
Definition
integrator_cvode.h:28
integrator_cvode::integrate
int integrate(std::vector< double > &y, const realtype dt)
Definition
integrator_cvode.h:91
integrator_cvode::integrator_cvode
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)
Definition
integrator_cvode.h:51
integrator_cvode::sun
SUNContext sun
sundials object
Definition
integrator_cvode.h:25
integrator_cvode::rtol
realtype rtol
scalar rtol (relative tolerance)
Definition
integrator_cvode.h:29
integrator_cvode::vars
N_Vector vars
vector of variables being solved
Definition
integrator_cvode.h:27
integrator_cvode::J
SUNMatrix J
matrix for linear solver
Definition
integrator_cvode.h:31
integrator_cvode::LS
SUNLinearSolver LS
linear solver
Definition
integrator_cvode.h:32
integrator_cvode::~integrator_cvode
~integrator_cvode()
Definition
integrator_cvode.h:113
integrator_cvode::rv
int rv
return value: checking status of calls
Definition
integrator_cvode.h:33
integrator_cvode::cmem
void * cmem
cvode object
Definition
integrator_cvode.h:26
src
integrator_cvode.h