23 #ifndef O2SCL_ODE_IV_SOLVE_H 24 #define O2SCL_ODE_IV_SOLVE_H 32 #include <boost/numeric/ublas/vector.hpp> 33 #include <boost/numeric/ublas/matrix.hpp> 34 #include <boost/numeric/ublas/matrix_proxy.hpp> 36 #include <o2scl/astep.h> 37 #include <o2scl/astep_gsl.h> 39 #ifndef DOXYGEN_NO_O2NS 115 #ifndef DOXYGEN_INTERNAL 121 vec_t vtemp, vtemp2, vtemp3, vtemp4;
132 std::cout <<
type() <<
" x: " << x <<
" y: ";
133 for(
size_t i=0;i<nv;i++) std::cout << y[i] <<
" ";
134 std::cout << std::endl;
204 vec_t &ystart, vec_t ¥d, func_t &derivs) {
227 vec_t &ystart, vec_t ¥d, vec_t &yerr,
262 vec_t &ystart, vec_t ¥d, vec_t &yerr,
263 vec_t &dydx_end, func_t &derivs) {
265 if ((x1>x0 && h<=0.0) || (x0>x1 && h>=0.0)) {
266 std::string str=
"Interval direction (x1-x0="+
o2scl::dtos(x1-x0)+
267 ") does not match step direction (h="+
o2scl::dtos(h)+
268 " in ode_iv_solve::solve_final_value().";
272 O2SCL_ERR2(
"Starting and final endpoints identical in ",
273 "ode_iv_solve::solve_final_value().",
exc_einval);
283 double x=x0, xverb=0.0, dxverb=0.0, xnext;
284 int ret=0, first_ret=0;
289 vec_t &dydx_start=vtemp;
309 for(
size_t i=0;i<n;i++) yend[i]=ystart[i];
312 derivs(x,n,yend,dydx_start);
315 while (done==
false) {
318 ret=astp->
astep_full(x,x1,xnext,h,n,ystart,dydx_start,
319 yend,yerr,dydx_end,derivs);
324 "ode_iv_solve::solve_final_value()",ret);
325 }
else if (first_ret!=0) {
331 if (
verbose>0 && xnext>xverb) {
343 std::string str=
"Too many steps required (nsteps="+
345 ", x="+
o2scl::dtos(x)+
") in ode_iv_solve::solve_final_value().";
353 if (xnext>=x1) done=
true;
355 if (xnext<=x1) done=
true;
362 ret=astp->
astep_full(xnext,x1,x,h,n,yend,dydx_end,ystart,yerr,
368 "ode_iv_solve::solve_final_value()",ret);
369 }
else if (first_ret!=0) {
387 std::string str=
"Too many steps required (ntrial="+
itos(
ntrial)+
388 ", x="+
o2scl::dtos(x)+
") in ode_iv_solve::solve_final_value().";
398 for(
size_t j=0;j<n;j++) {
400 dydx_end[j]=dydx_start[j];
406 for(
size_t j=0;j<n;j++) {
408 dydx_end[j]=dydx_start[j];
457 template<
class mat_t>
459 vec_t &x_sol, mat_t &y_sol, mat_t &yerr_sol,
460 mat_t &dydx_sol, func_t &derivs,
size_t istart=0) {
469 double dx_verb=(x1-x0)/((
double)(
nsteps_out-1));
471 double dx_tab=(x1-x0)/((
double)(n_sol-istart-1));
473 double x_verb=x0+dx_verb;
474 double x_tab=x0+dx_tab;
481 vec_t &ystart=vtemp4;
482 vec_t &dydx_start=vtemp2;
485 for(
size_t j=0;j<n;j++) ystart[j]=y_sol(istart,j);
497 derivs(x0,n,ystart,dydx_start);
501 for(
size_t j=0;j<n;j++) {
502 dydx_sol(istart,j)=dydx_start[j];
503 yerr_sol(istart,j)=0.0;
507 size_t icurr=istart+1;
509 for(
size_t j=0;j<n;j++) y_sol(icurr,j)=ystart[j];
514 while (done==
false) {
519 vec_t &dydx_out=vtemp3;
524 for(
size_t i=0;i<n;i++) yrow[i]=y_sol(icurr,i);
525 ret=astp->
astep_full(x0,x1,xnext,h,n,yrow,dydx_start,ystart,yerr,
532 O2SCL_ERR2(
"Adaptive stepper returned non-zero in ",
534 }
else if (first_ret==0) {
542 std::string str=
"Too many steps required (ntrial="+
itos(
ntrial)+
543 ", x="+
o2scl::dtos(x0)+
") in ode_iv_solve::solve_store().";
548 if (xnext>=x_verb &&
verbose>0) {
565 for(
size_t j=0;j<n;j++) {
566 y_sol(icurr,j)=ystart[j];
567 dydx_sol(icurr,j)=dydx_out[j];
568 yerr_sol(icurr,j)=yerr[j];
576 if (xnext>=x_tab && icurr<nmax) {
580 for(
size_t j=0;j<n;j++) {
581 y_sol(icurr,j)=ystart[j];
582 dydx_sol(icurr,j)=dydx_out[j];
583 yerr_sol(icurr,j)=yerr[j];
586 y_sol(icurr+1,j)=ystart[j];
587 dydx_start[j]=dydx_out[j];
600 for(
size_t j=0;j<n;j++) {
601 dydx_start[j]=dydx_out[j];
602 y_sol(icurr,j)=ystart[j];
631 template<
class mat_t,
class mat_row_t>
633 mat_t &ysol, mat_t &err_sol, mat_t &dydx_sol,
637 double x1=xsol[nsol-1];
640 int ret=0, first_ret=0;
652 vec_t &dydx_start=vtemp2;
655 for(
size_t j=0;j<n;j++) {
663 derivs(x0,n,ystart,dydx_start);
666 for(
size_t j=0;j<n;j++) {
667 dydx_sol(0,j)=dydx_start[j];
671 for(
size_t i=1;i<nsol && ret==0;i++) {
673 mat_row_t y_row(ysol,i);
674 mat_row_t dydx_row(dydx_sol,i);
675 mat_row_t yerr_row(err_sol,i);
679 while(done==
false && ret==0) {
681 ret=astp->
astep_full(x,xsol[i],xnext,h,n,ystart,dydx_start,
682 y_row,yerr_row,dydx_row,derivs);
688 "ode_iv_solve::solve_grid()",ret);
689 }
else if (first_ret!=0) {
695 std::string str=
"Too many steps required (ntrial="+
itos(
ntrial)+
696 ", x="+
o2scl::dtos(x)+
") in ode_iv_solve::solve_grid().";
703 for(
size_t j=0;j<n;j++) {
705 dydx_start[j]=dydx_row[j];
710 if (x>=xsol[i]) done=
true;
712 if (x<=xsol[i]) done=
true;
760 virtual const char *
type() {
return "ode_iv_solve"; }
764 #ifndef DOXYGEN_NO_O2NS Solve an initial-value ODE problems given an adaptive ODE stepper.
astep_base< vec_t, vec_t, vec_t, func_t > * astp
The adaptive stepper.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
astep_gsl< vec_t, vec_t, vec_t, func_t > gsl_astp
The default adaptive stepper.
#define O2SCL_CONV_RET(d, n, b)
Set a "convergence" error and return the error value.
invalid argument supplied by user
virtual const char * type()
Return the type, "ode_iv_solve".
size_t nsteps
Number of adaptive ste!ps employed.
exceeded max number of iterations
bool exit_on_fail
If true, stop the solution if the adaptive stepper fails (default true)
int verbose
Set output level.
int solve_final_value(double x0, double x1, double h, size_t n, vec_t &ystart, vec_t ¥d, vec_t &yerr, func_t &derivs)
Solve the initial-value problem to get the final value with errors.
int solve_final_value(double x0, double x1, double h, size_t n, vec_t &ystart, vec_t ¥d, vec_t &yerr, vec_t &dydx_end, func_t &derivs)
Solve the initial-value problem to get the final value, derivatives, and errors.
int solve_final_value(double x0, double x1, double h, size_t n, vec_t &ystart, vec_t ¥d, func_t &derivs)
Solve the initial-value problem to get the final value.
std::function< int(double, size_t, const boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &)> ode_funct
Ordinary differential equation function.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
virtual int astep_full(double x, double xlimit, double &x_out, double &h, size_t n, vec_y_t &y, vec_dydx_t &dydx, vec_y_t &yout, vec_yerr_t &yerr, vec_dydx_t &dydx_out, func_t &derivs)=0
Make an adaptive integration step of the system derivs with derivatives.
size_t ntrial
Maximum number of applications of the adaptive stepper (default 1000)
int set_astep(astep_base< vec_t, vec_t, vec_t, func_t > &as)
Set the adaptive stepper to use.
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.
void allocate(size_t n)
Allocate space for temporary vectors.
virtual int print_iter(double x, size_t nv, vec_t &y)
Print out iteration information.
void free()
Free allocated memory.
int solve_grid(double h, size_t n, size_t nsol, vec_t &xsol, mat_t &ysol, mat_t &err_sol, mat_t &dydx_sol, func_t &derivs)
Solve the initial-value problem from x0 to x1 over a grid storing derivatives and errors...
bool err_nonconv
If true, call the error handler if the solution does not converge (default true)
size_t nsteps_out
Number of output points for verbose output (default 10)
size_t mem_size
The size of the temporary vectors.
std::string itos(int x)
Convert an integer to a string.
static const double x1[5]
std::string szttos(size_t x)
Convert a size_t to a string.