DOLFIN
DOLFIN C++ interface
CVode.h
1 // Copyright (C) 2017 Chris Richardson and Chris Hadjigeorgiou
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __DOLFIN_C_VODE_H
19 #define __DOLFIN_C_VODE_H
20 
21 #ifdef HAS_SUNDIALS
22 
23 #include <dolfin/la/SUNDIALSNVector.h>
24 
25 #include <cvode/cvode.h>
26 #include <cvode/cvode_impl.h>
27 #include <sunlinsol/sunlinsol_spgmr.h>
28 #include <sundials/sundials_dense.h>
29 #include <sundials/sundials_types.h>
30 #include <sundials/sundials_iterative.h>
31 
32 namespace dolfin
33 {
35  class CVode
36  {
37  public:
38 
39  // These enums are used by PYBIND11 to map the definitions from C
40  enum LMM { cv_bdf = CV_BDF, cv_adams = CV_ADAMS };
41 
42  enum ITER { cv_functional = CV_FUNCTIONAL, cv_newton = CV_NEWTON };
43 
49  CVode(LMM cv_lmm, ITER cv_iter);
50 
52  virtual ~CVode();
53 
63  void init(std::shared_ptr<GenericVector> u0, double atol, double rtol, long int mxsteps = 0);
64 
70  double step(double dt);
71 
75  double get_time() const;
76 
80  void set_time(double t0);
81 
90  virtual void derivs(double t, std::shared_ptr<GenericVector> u,
91  std::shared_ptr<GenericVector> udot);
92 
106  virtual int jacobian(std::shared_ptr<const GenericVector> v,
107  std::shared_ptr<GenericVector> Jv,
108  double t, std::shared_ptr<const GenericVector> y,
109  std::shared_ptr<const GenericVector> fy);
110 
122  virtual int jacobian_setup(double t,
123  std::shared_ptr<GenericVector> Jv,
124  std::shared_ptr<GenericVector> y);
125 
145  virtual int psolve(double tn, std::shared_ptr<const GenericVector>y,
146  std::shared_ptr<const GenericVector> fy,
147  std::shared_ptr<const GenericVector> r,
148  std::shared_ptr<GenericVector> z,
149  double gamma, double delta, int lr);
150 
155  std::map<std::string,double> statistics();
156 
157  private:
160  static int f(realtype t, N_Vector u, N_Vector udot, void *user_data);
161 
164  static int f_jac_setup(double t, N_Vector y, N_Vector fy, void *user_data);
165 
168  static int f_jac(N_Vector u, N_Vector fu, double t, N_Vector y, N_Vector fy, void* , N_Vector tmp);
169 
172  static int prec_solve(double, N_Vector, N_Vector, N_Vector, N_Vector, double, double, int, void*);
173 
174  // Vector of values - wrapper around dolfin::GenericVector
175  std::shared_ptr<SUNDIALSNVector> _u;
176 
177  // SUNDIALS Linear Solver
178  SUNLinearSolver _ls;
179 
180  // Current time
181  double _t;
182 
183  // Pointer to CVode memory struct
184  void* _cvode_mem;
185 
186  // Remember iter method between constructor and init
187  ITER _cv_iter;
188  };
189 
190 }
191 
192 #endif
193 
194 #endif
double step(double dt)
Definition: CVode.cpp:100
Definition: adapt.h:29
virtual int psolve(double tn, std::shared_ptr< const GenericVector >y, std::shared_ptr< const GenericVector > fy, std::shared_ptr< const GenericVector > r, std::shared_ptr< GenericVector > z, double gamma, double delta, int lr)
Definition: CVode.cpp:149
virtual int jacobian_setup(double t, std::shared_ptr< GenericVector > Jv, std::shared_ptr< GenericVector > y)
Definition: CVode.cpp:138
double get_time() const
Definition: CVode.cpp:109
CVode(LMM cv_lmm, ITER cv_iter)
Definition: CVode.cpp:47
std::map< std::string, double > statistics()
Definition: CVode.cpp:223
void init(std::shared_ptr< GenericVector > u0, double atol, double rtol, long int mxsteps=0)
Definition: CVode.cpp:66
void set_time(double t0)
Definition: CVode.cpp:114
virtual int jacobian(std::shared_ptr< const GenericVector > v, std::shared_ptr< GenericVector > Jv, double t, std::shared_ptr< const GenericVector > y, std::shared_ptr< const GenericVector > fy)
Definition: CVode.cpp:127
virtual void derivs(double t, std::shared_ptr< GenericVector > u, std::shared_ptr< GenericVector > udot)
Definition: CVode.cpp:119
Wrapper class to SUNDIALS CVODE.
Definition: CVode.h:35
virtual ~CVode()
Destructor.
Definition: CVode.cpp:59