Class ode_iv_solve (o2scl)

O2scl : Class List

template<class func_t = ode_funct, class vec_t = boost::numeric::ublas::vector<double>>
class ode_iv_solve

Solve an initial-value ODE problems given an adaptive ODE stepper.

This class gives several functions which solve an initial value ODE problem. The functions solve_final_value() gives only the final value of the functions at the end of the ODE integration and is relatively fast.

The function solve_store() stores the solution of the ODE over the full range into a set of vectors and matrices which are allocated and specified by the user. This function is designed to give exactly the same results (though this cannot be guaranteed) as solve_final_value() and additionally records some or all of the results from the adaptive steps which were taken.

All of these functions automatically evaluate the derivatives from the specified function at the initial point and user-specified initial derivatives are ignored. The total number of steps taken is limited by ntrial and nsteps stores the number of steps taken by the most recent solution. The variable nsteps_out is the maximum number of points in the interval for which verbose output will be given when o2scl::ode_iv_solve::verbose is greater than zero.

There is an example for the usage of this class in examples/ex_ode.cpp< documented in the Ordinary differential equations example.

Convergence error handling

There are two different convergence errors which can be controlled separately in this class.

  • The adaptive stepper may require too many steps. If this happens, then the solver immediately stops. The solver calls the error handler if err_nonconv is true, and otherwise it returns a non-zero value.

  • The adaptive stepper may fail. If exit_on_fail is true, then the error handler is called. Otherwise, the solver proceeds to continue computing the whole solution. So long as the number of adaptive steps required is less than ntrial, then the full solution is computed and a non-zero value is returned to indicate the accuracy of the solution may be impacted. If the number of adaptive steps required after a failure of the adaptive stepper is larger than ntrial, then the behavior of the solver is controlled by err_nonconv as described above.

Documentation links for default template arguments

  • func_t - ode_funct

  • vec_t - boost::numeric::ublas::vector < double >

The default adaptive stepper is an object of type astep_gsl.

Todo

Future: The form of solve_final_value() is very similar to that of astep_base::astep_full(), but not quite the same. Maybe these functions should be consistent with each other?

Subclassed by o2scl::ode_iv_table< func_t, vec_t, alloc_vec_t, alloc_t >

Vectors for temporary storage

vec_t vtemp
vec_t vtemp2
vec_t vtemp3
vec_t vtemp4
size_t mem_size

The size of the temporary vectors.

astep_base<vec_t, vec_t, vec_t, func_t, double> *astp

The adaptive stepper.

bool err_nonconv

If true, call the error handler if the solution does not converge (default true)

inline virtual int print_iter(double x, size_t nv, vec_t &y)

Print out iteration information.

inline void free()

Free allocated memory.

inline void allocate(size_t n)

Allocate space for temporary vectors.

inline ode_iv_solve()
inline virtual ~ode_iv_solve()

Main solver functions

int verbose

Set output level.

size_t nsteps_out

Number of output points for verbose output (default 10)

This is used in functions solve_store() and solve_final_value() to control how often steps are output when verbose is greater than zero.

size_t ntrial

Maximum number of applications of the adaptive stepper (default 1000)

size_t nsteps

Number of adaptive ste!ps employed.

inline int solve_final_value(double x0, double x1, double h, size_t n, vec_t &ystart, vec_t &yend, func_t &derivs)

Solve the initial-value problem to get the final value.

Given the n initial values of the functions in ystart, this function integrates the ODEs specified in derivs over the interval from x0 to x1 with an initial stepsize of h. The final values of the function are given in yend and the initial values of yend are ignored.

If verbose is greater than zero, The solution at less than or approximately equal to nsteps_out points will be written to std::cout. If verbose is greater than one, a character will be required after each selected point.

inline int solve_final_value(double x0, double x1, double h, size_t n, vec_t &ystart, vec_t &yend, vec_t &yerr, func_t &derivs)

Solve the initial-value problem to get the final value with errors.

Given the n initial values of the functions in ystart, this function integrates the ODEs specified in derivs over the interval from x0 to x1 with an initial stepsize of h. The final values of the function are given in yend and the associated errors are given in yerr. The initial values of yend and yerr are ignored.

If verbose is greater than zero, The solution at less than or approximately equal to nsteps_out points will be written to std::cout. If verbose is greater than one, a character will be required after each selected point.

inline int solve_final_value(double x0, double x1, double h, size_t n, vec_t &ystart, vec_t &yend, vec_t &yerr, vec_t &dydx_end, func_t &derivs)

Solve the initial-value problem to get the final value, derivatives, and errors.

Given the n initial values of the functions in ystart, this function integrates the ODEs specified in derivs over the interval from x0 to x1 with an initial stepsize of h. The final values of the function are given in yend, the derivatives in dydx_end, and the associated errors are given in yerr. The initial values of yend and yerr are ignored.

This function is designed to be relatively fast, avoiding extra copying of vectors back and forth.

If verbose is greater than zero, The solution at less than or approximately equal to nsteps_out points will be written to std::cout. If verbose is greater than one, a character will be required after each selected point.

This function computes dydx_start automatically and the values given by the user are ignored.

The solution fails if more than ntrial steps are required. This function will also fail if x1>x0 and h<0 or if x1-x0 and h>0 do not have the same sign.

template<class mat_t>
inline int solve_store(double x0, double x1, double h, size_t n, size_t &n_sol, vec_t &x_sol, mat_t &y_sol, mat_t &yerr_sol, mat_t &dydx_sol, func_t &derivs, size_t istart = 0)

Solve the initial-value problem and store the associated output.

Initially, x_sol should be a vector of size n_sol, and y_sol, dydx_sol, and yerr_sol should be matrices with size [n_sol][n]. On exit, n_sol will will be number of points store, less than or equal to the original value of n_sol. This function avoids performing extra calls to the adaptive stepper, and the solution will be approximately evenly spaced.

This function is also designed to give the exactly the same results as solve_final_value(). This cannot be strictly guaranteed, but will likely hold in most applications.

This template function works with any matrix class mat_t which can be accessed using successive applications of operator[] and which has an associated class mat_row_t which returns a row of a matrix of type mat_t as an object with type vec_t.

If verbose is greater than zero, The solution at each internal point will be written to std::cout. If verbose is greater than one, a character will be required after each point.

Todo:

Document istart

The adaptive stepper

bool exit_on_fail

If true, stop the solution if the adaptive stepper fails (default true)

astep_gsl<vec_t, vec_t, vec_t, func_t, double> gsl_astp

The default adaptive stepper.

inline int set_astep(astep_base<vec_t, vec_t, vec_t, func_t, double> &as)

Set the adaptive stepper to use.

inline virtual const char *type()

Return the type, "ode_iv_solve".

Public Types

typedef boost::numeric::ublas::vector<double> ubvector