OGS 6.2.0-97-g4a610c866
MathLib::ODE::CVodeSolverImpl Class Referencefinal

Detailed Description

This class provides concrete access to Sundials' CVode solver.

This class is the implementation part in the pimpl idiom used by the CVodeSolver class. Therefore all if this classes methods are only forwarded from CVodeSolver.

Definition at line 82 of file CVodeSolver.cpp.

Public Member Functions

 CVodeSolverImpl (BaseLib::ConfigTree const &config, unsigned const num_equations)
 
void setFunction (std::unique_ptr< detail::FunctionHandles > &&f)
 
void preSolve ()
 
bool solve (const double t_end)
 
double const * getSolution () const
 
double getTime () const
 
void getYDot (const double t, double const *const y, double *const y_dot)
 
void setTolerance (const double *abstol, const double reltol)
 
void setTolerance (const double abstol, const double reltol)
 
void setIC (const double t0, double const *const y0)
 
 ~CVodeSolverImpl ()
 

Private Attributes

N_Vector _y = nullptr
 The solution vector. More...
 
realtype _t
 
N_Vector _abstol = nullptr
 current time More...
 
realtype _reltol
 Relative tolerance. More...
 
unsigned _num_equations
 Number of equations in the ODE system. More...
 
void * _cvode_mem
 CVode's internal memory. More...
 
std::unique_ptr< detail::FunctionHandles_f
 
int _linear_multistep_method = CV_ADAMS
 The multistep method used for solving the ODE. More...
 
int _nonlinear_solver_iteration = CV_FUNCTIONAL
 Either solve via fixed-point iteration or via Newton-Raphson method. More...
 

Constructor & Destructor Documentation

◆ CVodeSolverImpl()

MathLib::ODE::CVodeSolverImpl::CVodeSolverImpl ( BaseLib::ConfigTree const &  config,
unsigned const  num_equations 
)
Input File Parameter:
ode_solver__CVODE__linear_multistep_method
Input File Parameter:
ode_solver__CVODE__nonlinear_solver_iteration

Definition at line 129 of file CVodeSolver.cpp.

References _abstol, _cvode_mem, _linear_multistep_method, _nonlinear_solver_iteration, _num_equations, _y, check_error(), BaseLib::ConfigTree::getConfigParameterOptional(), and OGS_FATAL.

131 {
132  if (auto const param =
134  config.getConfigParameterOptional<std::string>("linear_multistep_method"))
135  {
136  DBUG("setting linear multistep method (config: %s)", param->c_str());
137 
138  if (*param == "Adams")
139  {
140  _linear_multistep_method = CV_ADAMS;
141  }
142  else if (*param == "BDF")
143  {
144  _linear_multistep_method = CV_BDF;
145  }
146  else
147  {
148  OGS_FATAL("unknown linear multistep method: %s", param->c_str());
149  }
150  }
151 
152  if (auto const param =
154  config.getConfigParameterOptional<std::string>("nonlinear_solver_iteration"))
155  {
156  DBUG("setting nonlinear solver iteration (config: %s)", param->c_str());
157 
158  if (*param == "Functional")
159  {
160  _nonlinear_solver_iteration = CV_FUNCTIONAL;
161  }
162  else if (*param == "Newton")
163  {
164  _nonlinear_solver_iteration = CV_NEWTON;
165  }
166  else
167  {
168  OGS_FATAL("unknown nonlinear solver iteration: %s", param->c_str());
169  }
170  }
171 
172  _y = N_VNew_Serial(num_equations);
173  _abstol = N_VNew_Serial(num_equations);
174  _num_equations = num_equations;
175 
176  _cvode_mem =
178 
179  if (_cvode_mem == nullptr || _y == nullptr || _abstol == nullptr)
180  {
181  OGS_FATAL("couldn't allocate storage for CVode solver.");
182  }
183 
184  auto f_wrapped = [](const realtype t, const N_Vector y, N_Vector ydot,
185  void* function_handles) -> int
186  {
187  bool successful =
188  static_cast<detail::FunctionHandles*>(function_handles)
189  ->call(t, NV_DATA_S(y), NV_DATA_S(ydot));
190  return successful ? 0 : 1;
191  };
192 
193  check_error("CVodeInit", CVodeInit(_cvode_mem, f_wrapped, 0.0, _y));
194 }
void * _cvode_mem
CVode&#39;s internal memory.
int _nonlinear_solver_iteration
Either solve via fixed-point iteration or via Newton-Raphson method.
N_Vector _y
The solution vector.
N_Vector _abstol
current time
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
unsigned _num_equations
Number of equations in the ODE system.
void check_error(std::string const &f_name, int const error_flag)
Definition: CVodeSolver.cpp:31
int _linear_multistep_method
The multistep method used for solving the ODE.

◆ ~CVodeSolverImpl()

MathLib::ODE::CVodeSolverImpl::~CVodeSolverImpl ( )

Definition at line 297 of file CVodeSolver.cpp.

References _abstol, _cvode_mem, _y, and printStats().

Referenced by getTime().

298 {
300 
301  N_VDestroy_Serial(_y);
302  N_VDestroy_Serial(_abstol);
303  CVodeFree(&_cvode_mem);
304 }
void * _cvode_mem
CVode&#39;s internal memory.
void printStats(void *cvode_mem)
Prints some statistics about an ODE solver run.
Definition: CVodeSolver.cpp:41
N_Vector _y
The solution vector.
N_Vector _abstol
current time

Member Function Documentation

◆ getSolution()

double const* MathLib::ODE::CVodeSolverImpl::getSolution ( ) const
inline

Definition at line 96 of file CVodeSolver.cpp.

References _y.

96 { return NV_DATA_S(_y); }
N_Vector _y
The solution vector.

◆ getTime()

double MathLib::ODE::CVodeSolverImpl::getTime ( ) const
inline

Definition at line 97 of file CVodeSolver.cpp.

References _t, getYDot(), setIC(), setTolerance(), and ~CVodeSolverImpl().

97 { return _t; }

◆ getYDot()

void MathLib::ODE::CVodeSolverImpl::getYDot ( const double  t,
double const *const  y,
double *const  y_dot 
)

Definition at line 290 of file CVodeSolver.cpp.

References _f.

Referenced by getTime().

292 {
293  assert(_f != nullptr);
294  _f->call(t, y, y_dot);
295 }
std::unique_ptr< detail::FunctionHandles > _f

◆ preSolve()

void MathLib::ODE::CVodeSolverImpl::preSolve ( )

Definition at line 232 of file CVodeSolver.cpp.

References _abstol, _cvode_mem, _f, _num_equations, _reltol, _t, _y, MathLib::ODE::detail::FunctionHandles::callJacobian(), and check_error().

233 {
234  assert(_f != nullptr && "ode function handle was not provided");
235 
236  // sets initial conditions
237  check_error("CVodeReInit", CVodeReInit(_cvode_mem, _t, _y));
238 
239  check_error("CVodeSetUserData",
240  CVodeSetUserData(_cvode_mem, static_cast<void*>(_f.get())));
241 
242  /* Call CVodeSVtolerances to specify the scalar relative tolerance
243  * and vector absolute tolerances */
244  check_error("CVodeSVtolerances",
245  CVodeSVtolerances(_cvode_mem, _reltol, _abstol));
246 
247  /* Call CVDense to specify the CVDENSE dense linear solver */
248  check_error("CVDense", CVDense(_cvode_mem, _num_equations));
249 
250  if (_f->hasJacobian())
251  {
252  auto df_wrapped = [](const long N, const realtype t, const N_Vector y,
253  const N_Vector ydot, const DlsMat jac,
254  void* function_handles, N_Vector /*tmp1*/,
255  N_Vector /*tmp2*/, N_Vector /*tmp3*/
256  ) -> int
257  {
258  (void)N; // prevent warnings during non-debug build
259  auto* fh = static_cast<detail::FunctionHandles*>(function_handles);
260  assert(N == fh->getNumberOfEquations());
261 
262  // Caution: by calling the DENSE_COL() macro we assume that matrices
263  // are stored contiguously in memory!
264  // See also the header files sundials_direct.h and cvode_direct.h in
265  // the Sundials source code. The comments about the macro DENSE_COL
266  // in those files indicate that matrices are stored column-wise.
267  bool successful = fh->callJacobian(t, NV_DATA_S(y), NV_DATA_S(ydot),
268  DENSE_COL(jac, 0));
269  return successful ? 0 : 1;
270  };
271 
272  check_error("CVDlsSetDenseJacFn",
273  CVDlsSetDenseJacFn(_cvode_mem, df_wrapped));
274  }
275 }
void * _cvode_mem
CVode&#39;s internal memory.
N_Vector _y
The solution vector.
realtype _reltol
Relative tolerance.
std::unique_ptr< detail::FunctionHandles > _f
N_Vector _abstol
current time
unsigned _num_equations
Number of equations in the ODE system.
void check_error(std::string const &f_name, int const error_flag)
Definition: CVodeSolver.cpp:31

◆ setFunction()

void MathLib::ODE::CVodeSolverImpl::setFunction ( std::unique_ptr< detail::FunctionHandles > &&  f)

Definition at line 216 of file CVodeSolver.cpp.

References _f, and _num_equations.

217 {
218  _f = std::move(f);
219  assert(_num_equations == _f->getNumberOfEquations());
220 }
std::unique_ptr< detail::FunctionHandles > _f
unsigned _num_equations
Number of equations in the ODE system.

◆ setIC()

void MathLib::ODE::CVodeSolverImpl::setIC ( const double  t0,
double const *const  y0 
)

Definition at line 222 of file CVodeSolver.cpp.

References _num_equations, _t, and _y.

Referenced by getTime().

223 {
224  for (unsigned i = 0; i < _num_equations; ++i)
225  {
226  NV_Ith_S(_y, i) = y0[i];
227  }
228 
229  _t = t0;
230 }
N_Vector _y
The solution vector.
unsigned _num_equations
Number of equations in the ODE system.

◆ setTolerance() [1/2]

void MathLib::ODE::CVodeSolverImpl::setTolerance ( const double *  abstol,
const double  reltol 
)

Definition at line 196 of file CVodeSolver.cpp.

References _abstol, _num_equations, and _reltol.

Referenced by getTime().

197 {
198  for (unsigned i = 0; i < _num_equations; ++i)
199  {
200  NV_Ith_S(_abstol, i) = abstol[i];
201  }
202 
203  _reltol = reltol;
204 }
realtype _reltol
Relative tolerance.
N_Vector _abstol
current time
unsigned _num_equations
Number of equations in the ODE system.

◆ setTolerance() [2/2]

void MathLib::ODE::CVodeSolverImpl::setTolerance ( const double  abstol,
const double  reltol 
)

Definition at line 206 of file CVodeSolver.cpp.

References _abstol, _num_equations, and _reltol.

207 {
208  for (unsigned i = 0; i < _num_equations; ++i)
209  {
210  NV_Ith_S(_abstol, i) = abstol;
211  }
212 
213  _reltol = reltol;
214 }
realtype _reltol
Relative tolerance.
N_Vector _abstol
current time
unsigned _num_equations
Number of equations in the ODE system.

◆ solve()

bool MathLib::ODE::CVodeSolverImpl::solve ( const double  t_end)

Definition at line 277 of file CVodeSolver.cpp.

References _cvode_mem, _t, _y, and check_error().

278 {
279  realtype t_reached;
280  check_error("CVode solve",
281  CVode(_cvode_mem, t_end, _y, &t_reached, CV_NORMAL));
282  _t = t_reached;
283 
284  // check_error asserts that t_end == t_reached and that solving the ODE
285  // went fine. Otherwise the program will be aborted. Therefore, we don't
286  // have to check manually for errors here and can always savely return true.
287  return true;
288 }
void * _cvode_mem
CVode&#39;s internal memory.
N_Vector _y
The solution vector.
void check_error(std::string const &f_name, int const error_flag)
Definition: CVodeSolver.cpp:31

Member Data Documentation

◆ _abstol

N_Vector MathLib::ODE::CVodeSolverImpl::_abstol = nullptr
private

current time

Array of absolute tolerances.

Definition at line 110 of file CVodeSolver.cpp.

Referenced by CVodeSolverImpl(), preSolve(), setTolerance(), and ~CVodeSolverImpl().

◆ _cvode_mem

void* MathLib::ODE::CVodeSolverImpl::_cvode_mem
private

CVode's internal memory.

Definition at line 114 of file CVodeSolver.cpp.

Referenced by CVodeSolverImpl(), preSolve(), solve(), and ~CVodeSolverImpl().

◆ _f

std::unique_ptr<detail::FunctionHandles> MathLib::ODE::CVodeSolverImpl::_f
private

Function handles that compute $\partial \dot y/\partial y$ and $\dot y$.

Definition at line 118 of file CVodeSolver.cpp.

Referenced by getYDot(), preSolve(), and setFunction().

◆ _linear_multistep_method

int MathLib::ODE::CVodeSolverImpl::_linear_multistep_method = CV_ADAMS
private

The multistep method used for solving the ODE.

Definition at line 121 of file CVodeSolver.cpp.

Referenced by CVodeSolverImpl().

◆ _nonlinear_solver_iteration

int MathLib::ODE::CVodeSolverImpl::_nonlinear_solver_iteration = CV_FUNCTIONAL
private

Either solve via fixed-point iteration or via Newton-Raphson method.

Definition at line 124 of file CVodeSolver.cpp.

Referenced by CVodeSolverImpl().

◆ _num_equations

unsigned MathLib::ODE::CVodeSolverImpl::_num_equations
private

Number of equations in the ODE system.

Definition at line 113 of file CVodeSolver.cpp.

Referenced by CVodeSolverImpl(), preSolve(), setFunction(), setIC(), and setTolerance().

◆ _reltol

realtype MathLib::ODE::CVodeSolverImpl::_reltol
private

Relative tolerance.

Definition at line 111 of file CVodeSolver.cpp.

Referenced by preSolve(), and setTolerance().

◆ _t

realtype MathLib::ODE::CVodeSolverImpl::_t
private

Definition at line 108 of file CVodeSolver.cpp.

Referenced by getTime(), preSolve(), setIC(), and solve().

◆ _y

N_Vector MathLib::ODE::CVodeSolverImpl::_y = nullptr
private

The solution vector.

Definition at line 106 of file CVodeSolver.cpp.

Referenced by CVodeSolverImpl(), getSolution(), preSolve(), setIC(), solve(), and ~CVodeSolverImpl().


The documentation for this class was generated from the following file: