libode
Easy to compile, fast ODE integrators as C++ classes
Loading...
Searching...
No Matches
ode::OdeAdaptive Class Reference

Base class implementing solver functions with adaptive time steps. More...

#include <ode_adaptive.h>

Inheritance diagram for ode::OdeAdaptive:
ode::OdeBase ode::OdeBackwardEuler ode::OdeEmbedded ode::OdeEuler ode::OdeGauss6 ode::OdeGeng5 ode::OdeLobattoIIIC6 ode::OdeRK4 ode::OdeROW6A ode::OdeRadauIIA5 ode::OdeSsp3 ode::OdeTrapz

Public Member Functions

 OdeAdaptive (unsigned long neq, bool need_jac)
 constructs
 
virtual ~OdeAdaptive ()
 destructs
 
long unsigned get_nrej ()
 gets the count of rejected steps
 
double get_abstol ()
 gets the absolute error tolerance
 
double get_reltol ()
 gets the relative error tolerance
 
double get_dtmax ()
 gets the maximum allowable time step
 
void set_abstol (double tol)
 sets the absolute error tolerance
 
void set_reltol (double tol)
 sets the relative error tolerance
 
void set_tol (double tol)
 sets the absolute and relative error tolerance to the same value
 
void set_dtmax (double dtmax)
 sets the maximum allowable time step
 
void solve_adaptive (double tint, double dt0, bool extras=true)
 integrates for a specified duration of independent variable without output
 
void solve_adaptive (double tint, double dt0, const char *dirout, int inter)
 lots of output, solves and stores every "inter" point along the way
 
void solve_adaptive (double tint, double dt0, unsigned long nsnap, const char *dirout)
 solves and writes evenly spaced snapshots
 
void solve_adaptive (double dt0, double *tsnap, unsigned long nsnap, const char *dirout)
 solves and writes snapshots at times specified in the tsnap array
 
- Public Member Functions inherited from ode::OdeBase
 OdeBase (unsigned long neq, bool need_jac)
 constructs
 
virtual ~OdeBase ()
 destructs
 
const char * get_name ()
 gets the name of the ODE system
 
const char * get_method ()
 gets the name of the solver/method
 
const char * get_dirout ()
 gets output directory string if one has been set
 
bool get_quiet ()
 gets the boolean determining if updates are printed during solves
 
bool get_silent_snap ()
 gets whether to skip writing the solution vector to file when snapping
 
unsigned long get_neq ()
 gets the size of the ODE system
 
double get_t ()
 gets the current value of the independent variable
 
double get_dt ()
 gets the most recent or current time step size
 
double * get_sol ()
 gets a pointer to the whole solution array
 
double get_sol (unsigned long i)
 gets an element of the solution array
 
long unsigned get_nstep ()
 gets the total number of steps taken
 
long unsigned get_neval ()
 gets the total number of ODE system evaluation
 
long unsigned get_icheck ()
 gets the number of steps after which the solution is checked for integrity
 
long unsigned get_nJac ()
 gets the total number of Jacobian evaluations performed
 
void set_t (double t)
 sets the "time," or independent variable used to track progress
 
void set_sol (unsigned long i, double x)
 sets an element of the solution array
 
void set_sol (double *sol)
 copies an array into the solution array
 
void set_name (std::string name)
 sets the name of the ODE system
 
void set_name (const char *name)
 sets the name of the ODE system
 
void set_quiet (bool quiet)
 sets the boolean determining if updates are printed during solves
 
void set_silent_snap (bool silent_snap)
 sets whether to skip writing the solution vector to file when snapping
 
void set_icheck (unsigned long icheck)
 sets the number of steps after which the solution is checked for integrity
 
void step (double dt, bool extra=true)
 increments the step counter and the time, checks the solution integrity if needed, stores the time step in the object, and executes after_step() if extra is true
 
void solve_fixed (double tint, double dt, bool extras=true)
 integrates for a specified duration of independent variable without output
 
void solve_fixed (double tint, double dt, const char *dirout, int inter=1)
 lots of output, solves and stores every "inter" point along the way
 
void solve_fixed (double tint, double dt, unsigned long nsnap, const char *dirout)
 solves and writes evenly spaced snapshots
 
void solve_fixed (double dt, double *tsnap, unsigned long nsnap, const char *dirout)
 solves and writes snapshots at times specified in the tsnap array
 
void reset (double t, double *sol)
 reset to a specified time and initial condition array
 

Protected Member Functions

void solve_adaptive_ (double tint, double dt0, bool extra=true)
 integrates without output or any counters, trackers, extra functions...
 
virtual void adapt (double abstol, double reltol)
 executes whatever calculations need to be performed for adapting, including a determination of whether a step is rejected and a calculation of the next time step size
 
virtual bool is_rejected ()
 retreives a bool determining whether a step is accepted/rejected, false by default
 
virtual double dt_adapt ()
 retrieves the best time step for the next step
 
bool solve_done_adaptive (double tend)
 determines whether an adaptive solve is finished
 
bool step_adaptive_ (double dt, bool extra=true)
 executes a single time and calls all necessary adapting functions
 
double dt_adapt_ (double tend)
 wrapper around dt_adapt() to perform additional checks
 
- Protected Member Functions inherited from ode::OdeBase
void solve_fixed_ (double tint, double dt, bool extra=true)
 integrates without output or any counters, trackers, extra functions...
 
virtual void ode_fun (double *solin, double *fout)=0
 evaluates the system of ODEs in autonomous form and must be defined by a derived class
 
virtual void ode_jac (double *solin, double **Jout)
 evaluates the system's Jacobian matrix, also in autonomous form, and can either be defined in a derived class or left to numerical approximation
 
virtual void step_ (double dt)=0
 advances a single time step (without changing counters or the time) and must be defined in the derived class implementing the solver/method
 
void ode_fun_ (double *solin, double *fout)
 wrapper, calls ode_fun() and increases the neval counter by one
 
void ode_jac_ (double *solin, double **Jout)
 wrapper, calls ode_jac() and increments nJac;
 
virtual void before_solve ()
 does any extra stuff before starting a solve
 
virtual void after_step (double t)
 does any extra stuff after each step
 
virtual void after_capture (double t)
 does any extra stuff only when a step is captured
 
virtual void after_snap (std::string dirout, long isnap, double t)
 does any extra stuff after each snap
 
virtual void after_solve ()
 does any extra stuff after completing a solve
 
void snap (std::string dirout, long isnap, double tsnap)
 writes the current value of the solution to a binary file
 
bool solve_done (double dt, double tend)
 checks if the solution is within a single time step of the end point
 
void check_sol_integrity ()
 checks solution for nans and infs, exiting the program if they're found
 
void check_pre_solve (double tint, double dt)
 checks that a solve can be performed with given tend and dt values
 
void check_pre_snaps (double dt, double *tsnap, unsigned long nsnap)
 checks that snap times are monotonically increasing and > current time
 

Protected Attributes

long unsigned nrej_
 counter for rejected steps
 
double abstol_
 absolute error tolerance
 
double reltol_
 absolute error tolerance
 
double dtmax_
 maximum allowable time step
 
- Protected Attributes inherited from ode::OdeBase
std::string name_
 the "name" of the system, which is used for output
 
std::string method_
 the "name" of the solver/method, as in "Euler" or "RK4"
 
std::string dirout_
 output directory if one is being used by a solver
 
bool quiet_
 whether stuff should be printed during a solve
 
bool silent_snap_
 whether to skip writing the solution vector to file when snapping but still execute after_snap()
 
unsigned long neq_
 number of equations in the system of ODEs
 
double t_
 time, initialized to zero
 
double dt_
 time step is stored and updated during solves
 
double * sol_
 array for the solution, changing over time
 
long unsigned nstep_
 number of time steps
 
long unsigned neval_
 function evaluation counter, must be incremented in step() when defined
 
long unsigned icheck_
 interval of steps after which to check for nans and infs (zero to ignore)
 
double ** Jac_
 storage for the ODE system's Jacobian matrix, only allocated for the methods that need it
 
long unsigned nJac_
 counter for jacobian evaluations
 
double absjacdel_
 absolute adjustment fraction for numerical Jacobian, if needed
 
double reljacdel_
 relative adjustment fraction for numerical Jacobian, if needed
 

Detailed Description

Base class implementing solver functions with adaptive time steps.

The OdeAdaptive class is a base for any integrator that can modify its own time step as it integrates and reject steps. The class inherits from the deepest base class, ode_base. It implements three virtual functions to enable time step adaptation:

  1. The adapt function is meant to execute whatever calculations are needed to subsequently assess whether the current time step should be rejected and what the next time step should be.
  2. The is_rejected function simply furnishes a boolean answering the question, "should the step just taken be rejected?"
  3. The dt_adapt function furnishes the next time step, whether the step current step was rejected or not. These functions are used with local error tolerances to integrate with an efficiently adaptive time step. Specifically, they are used in the step_adaptive_ method of this class, which is called in each of the solve_adaptive methods.

Definition at line 30 of file ode_adaptive.h.

Constructor & Destructor Documentation

◆ OdeAdaptive()

ode::OdeAdaptive::OdeAdaptive ( unsigned long neq,
bool need_jac )

constructs

Parameters
[in]neqnumber of equations in ODE system
[in]need_jacflag signaling whether the Jacobian of the system is needed

Definition at line 7 of file ode_adaptive.cc.

◆ ~OdeAdaptive()

ode::OdeAdaptive::~OdeAdaptive ( )
virtual

destructs

Definition at line 21 of file ode_adaptive.cc.

Member Function Documentation

◆ adapt()

void ode::OdeAdaptive::adapt ( double abstol,
double reltol )
protectedvirtual

executes whatever calculations need to be performed for adapting, including a determination of whether a step is rejected and a calculation of the next time step size

Reimplemented in ode::OdeEmbedded.

Definition at line 176 of file ode_adaptive.cc.

◆ dt_adapt()

double ode::OdeAdaptive::dt_adapt ( )
protectedvirtual

retrieves the best time step for the next step

Reimplemented in ode::OdeEmbedded.

Definition at line 183 of file ode_adaptive.cc.

◆ dt_adapt_()

double ode::OdeAdaptive::dt_adapt_ ( double tend)
protected

wrapper around dt_adapt() to perform additional checks

Definition at line 230 of file ode_adaptive.cc.

◆ get_abstol()

double ode::OdeAdaptive::get_abstol ( )
inline

gets the absolute error tolerance

Definition at line 48 of file ode_adaptive.h.

◆ get_dtmax()

double ode::OdeAdaptive::get_dtmax ( )
inline

gets the maximum allowable time step

Definition at line 52 of file ode_adaptive.h.

◆ get_nrej()

long unsigned ode::OdeAdaptive::get_nrej ( )
inline

gets the count of rejected steps

Definition at line 46 of file ode_adaptive.h.

◆ get_reltol()

double ode::OdeAdaptive::get_reltol ( )
inline

gets the relative error tolerance

Definition at line 50 of file ode_adaptive.h.

◆ is_rejected()

bool ode::OdeAdaptive::is_rejected ( )
protectedvirtual

retreives a bool determining whether a step is accepted/rejected, false by default

Reimplemented in ode::OdeEmbedded.

Definition at line 181 of file ode_adaptive.cc.

◆ set_abstol()

void ode::OdeAdaptive::set_abstol ( double tol)
inline

sets the absolute error tolerance

Definition at line 55 of file ode_adaptive.h.

◆ set_dtmax()

void ode::OdeAdaptive::set_dtmax ( double dtmax)
inline

sets the maximum allowable time step

Definition at line 61 of file ode_adaptive.h.

◆ set_reltol()

void ode::OdeAdaptive::set_reltol ( double tol)
inline

sets the relative error tolerance

Definition at line 57 of file ode_adaptive.h.

◆ set_tol()

void ode::OdeAdaptive::set_tol ( double tol)
inline

sets the absolute and relative error tolerance to the same value

Definition at line 59 of file ode_adaptive.h.

◆ solve_adaptive() [1/4]

void ode::OdeAdaptive::solve_adaptive ( double dt0,
double * tsnap,
unsigned long nsnap,
const char * dirout )

solves and writes snapshots at times specified in the tsnap array

Parameters
[in]dt0initial time step size
[in]tsnaparray of desired snapshot times
[in]nsnapnumber of snapshots (length of tsnap)
[in]diroutoutput directory (must already exist)

Definition at line 140 of file ode_adaptive.cc.

◆ solve_adaptive() [2/4]

void ode::OdeAdaptive::solve_adaptive ( double tint,
double dt0,
bool extras = true )

integrates for a specified duration of independent variable without output

Parameters
[in]tinttotal integration time
[in]dt0initial time step size
[in]extraswhether to call all the extra functions (before_solve, after_step, ...)

Definition at line 45 of file ode_adaptive.cc.

◆ solve_adaptive() [3/4]

void ode::OdeAdaptive::solve_adaptive ( double tint,
double dt0,
const char * dirout,
int inter )

lots of output, solves and stores every "inter" point along the way

Parameters
[in]tinttotal integration time
[in]dt0initial time step size
[in]diroutoutput directory (must already exist)
[in]interinterval of steps to store and output

Definition at line 61 of file ode_adaptive.cc.

◆ solve_adaptive() [4/4]

void ode::OdeAdaptive::solve_adaptive ( double tint,
double dt0,
unsigned long nsnap,
const char * dirout )

solves and writes evenly spaced snapshots

Parameters
[in]tinttotal integration time
[in]dt0initial time step size
[in]nsnapnumber of snapshots to output
[in]diroutoutput directory (must already exist)

Definition at line 126 of file ode_adaptive.cc.

◆ solve_adaptive_()

void ode::OdeAdaptive::solve_adaptive_ ( double tint,
double dt0,
bool extra = true )
protected

integrates without output or any counters, trackers, extra functions...

Parameters
[in]tinttotal integration time
[in]dt0initial time step size
[in]extrawhether to call after_step()

Definition at line 28 of file ode_adaptive.cc.

◆ solve_done_adaptive()

bool ode::OdeAdaptive::solve_done_adaptive ( double tend)
protected

determines whether an adaptive solve is finished

Definition at line 188 of file ode_adaptive.cc.

◆ step_adaptive_()

bool ode::OdeAdaptive::step_adaptive_ ( double dt,
bool extra = true )
protected

executes a single time and calls all necessary adapting functions

Definition at line 198 of file ode_adaptive.cc.

Member Data Documentation

◆ abstol_

double ode::OdeAdaptive::abstol_
protected

absolute error tolerance

Definition at line 117 of file ode_adaptive.h.

◆ dtmax_

double ode::OdeAdaptive::dtmax_
protected

maximum allowable time step

Definition at line 121 of file ode_adaptive.h.

◆ nrej_

long unsigned ode::OdeAdaptive::nrej_
protected

counter for rejected steps

Definition at line 115 of file ode_adaptive.h.

◆ reltol_

double ode::OdeAdaptive::reltol_
protected

absolute error tolerance

Definition at line 119 of file ode_adaptive.h.


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