OGS 6.2.1-499-g3b941532c.dirty.20191012113459
MathLib::ODE::CVodeSolverImpl Class Referencefinal

## Detailed Description

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 83 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

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...

## ◆ 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 130 of file CVodeSolver.cpp.

132 {
133  if (auto const param =
135  config.getConfigParameterOptional<std::string>("linear_multistep_method"))
136  {
137  DBUG("setting linear multistep method (config: %s)", param->c_str());
138
140  {
142  }
143  else if (*param == "BDF")
144  {
145  _linear_multistep_method = CV_BDF;
146  }
147  else
148  {
149  OGS_FATAL("unknown linear multistep method: %s", param->c_str());
150  }
151  }
152
153  if (auto const param =
155  config.getConfigParameterOptional<std::string>("nonlinear_solver_iteration"))
156  {
157  DBUG("setting nonlinear solver iteration (config: %s)", param->c_str());
158
159  if (*param == "Functional")
160  {
161  _nonlinear_solver_iteration = CV_FUNCTIONAL;
162  }
163  else if (*param == "Newton")
164  {
165  _nonlinear_solver_iteration = CV_NEWTON;
166  }
167  else
168  {
169  OGS_FATAL("unknown nonlinear solver iteration: %s", param->c_str());
170  }
171  }
172
173  _y = N_VNew_Serial(num_equations);
174  _abstol = N_VNew_Serial(num_equations);
175  _num_equations = num_equations;
176
177  _cvode_mem =
179
180  if (_cvode_mem == nullptr || _y == nullptr || _abstol == nullptr)
181  {
182  OGS_FATAL("couldn't allocate storage for CVode solver.");
183  }
184
185  auto f_wrapped = [](const realtype t, const N_Vector y, N_Vector ydot,
186  void* function_handles) -> int
187  {
188  bool successful =
189  static_cast<detail::FunctionHandles*>(function_handles)
190  ->call(t, NV_DATA_S(y), NV_DATA_S(ydot));
191  return successful ? 0 : 1;
192  };
193
194  check_error("CVodeInit", CVodeInit(_cvode_mem, f_wrapped, 0.0, _y));
195 }
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:64
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:32
int _linear_multistep_method
The multistep method used for solving the ODE.

## ◆ ~CVodeSolverImpl()

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

Definition at line 298 of file CVodeSolver.cpp.

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

Referenced by getTime().

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

## ◆ getSolution()

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

Definition at line 97 of file CVodeSolver.cpp.

References _y.

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

## ◆ getTime()

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

Definition at line 98 of file CVodeSolver.cpp.

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

98 { return _t; }

## ◆ getYDot()

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

Definition at line 291 of file CVodeSolver.cpp.

References _f.

Referenced by getTime().

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

## ◆ preSolve()

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

Definition at line 233 of file CVodeSolver.cpp.

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

## ◆ setFunction()

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

Definition at line 217 of file CVodeSolver.cpp.

References _f, and _num_equations.

218 {
219  _f = std::move(f);
220  assert(_num_equations == _f->getNumberOfEquations());
221 }
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 223 of file CVodeSolver.cpp.

References _num_equations, _t, and _y.

Referenced by getTime().

224 {
225  for (unsigned i = 0; i < _num_equations; ++i)
226  {
227  NV_Ith_S(_y, i) = y0[i];
228  }
229
230  _t = t0;
231 }
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 197 of file CVodeSolver.cpp.

References _abstol, _num_equations, and _reltol.

Referenced by getTime().

198 {
199  for (unsigned i = 0; i < _num_equations; ++i)
200  {
201  NV_Ith_S(_abstol, i) = abstol[i];
202  }
203
204  _reltol = reltol;
205 }
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 207 of file CVodeSolver.cpp.

References _abstol, _num_equations, and _reltol.

208 {
209  for (unsigned i = 0; i < _num_equations; ++i)
210  {
211  NV_Ith_S(_abstol, i) = abstol;
212  }
213
214  _reltol = reltol;
215 }
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 278 of file CVodeSolver.cpp.

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

279 {
280  realtype t_reached;
281  check_error("CVode solve",
282  CVode(_cvode_mem, t_end, _y, &t_reached, CV_NORMAL));
283  _t = t_reached;
284
285  // check_error asserts that t_end == t_reached and that solving the ODE
286  // went fine. Otherwise the program will be aborted. Therefore, we don't
287  // have to check manually for errors here and can always savely return true.
288  return true;
289 }
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:32

## ◆ _abstol

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

current time

Array of absolute tolerances.

Definition at line 111 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 115 of file CVodeSolver.cpp.

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

## ◆ _f

 std::unique_ptr MathLib::ODE::CVodeSolverImpl::_f
private

Function handles that compute and .

Definition at line 119 of file CVodeSolver.cpp.

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

## ◆ _linear_multistep_method

private

The multistep method used for solving the ODE.

Definition at line 122 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 125 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 114 of file CVodeSolver.cpp.

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

## ◆ _reltol

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

Relative tolerance.

Definition at line 112 of file CVodeSolver.cpp.

Referenced by preSolve(), and setTolerance().

## ◆ _t

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

Definition at line 109 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 107 of file CVodeSolver.cpp.

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

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