libode
Easy to compile, fast ODE integrators as C++ classes
Loading...
Searching...
No Matches
ode_embedded.cc
Go to the documentation of this file.
1
2
3#include "ode_embedded.h"
4
5namespace ode {
6
7OdeEmbedded::OdeEmbedded (unsigned long neq, bool need_jac, int lowerord) :
8 OdeAdaptive (neq, need_jac) {
9
10 //order of the LOWER order solution used for error estimation
11 lowerord_ = lowerord;
12 //variables for adaptive time stepping and step rejection
13 facsafe_ = 0.9; //fraction to multiply facopt by to be cautious
14 facmin_ = 1e-2; //minumum time step reduction factor
15 facmax_ = 1e1; //maximum time step increase factor
16 //lower order solution
17 solemb_ = new double[neq];
18}
19
20//destructor
22 delete [] solemb_;
23}
24
25double OdeEmbedded::error (double abstol, double reltol) {
26
27 //index
28 unsigned long i;
29 //storage for evaluating error terms
30 double sc, d, g, err=0.0;
31
32 //compute the Inf norm of the difference between higher and lower order steps
33 //with a scaling factor that combines relative and absolute error
34 for (i=0; i<neq_; i++) {
35 d = fabs(solemb_[i] - sol_[i]);
36 sc = abstol + reltol*fabs(sol_[i]);
37 g = d/sc;
38 if ( g > err ) err = g;
39 }
40
41 return(err);
42}
43
44double OdeEmbedded::facopt (double err) {
45
46 //compute the "optimal" adjustment factor for the time step
47 double fac = facsafe_*pow(1.0/err, 1.0/(lowerord_ + 1.0));
48 //keep the factor within limits
49 fac = ode_max2(facmin_, fac);
50 fac = ode_min2(facmax_, fac);
51
52 return(fac);
53}
54
55void OdeEmbedded::adapt (double abstol, double reltol) {
56
57 //compute the error estimate
58 double err = error(abstol, reltol);
59 //determine if the step should be rejected
60 isrej_ = ( err >= 1.0 ) ? true : false;
61 //determine the next time step
62 dtopt_ = facopt(err)*dt_;
63}
64
66 //simply return the value set in the adapt() function
67 return( isrej_ );
68}
69
71 //simply return the value set in the adapt() function
72 return( dtopt_ );
73}
74
75} // namespace ode
Base class implementing solver functions with adaptive time steps.
unsigned long neq_
number of equations in the system of ODEs
Definition ode_base.h:327
double dt_
time step is stored and updated during solves
Definition ode_base.h:331
double * sol_
array for the solution, changing over time
Definition ode_base.h:333
OdeEmbedded(unsigned long neq, bool need_jac, int lowerord)
constructs
virtual double dt_adapt()
simply returns dtopt
double * solemb_
embedded solution array
double facmin_
minimum allowable fraction change in time step
double error(double abstol, double reltol)
calculates error estimate with lower and higher order solutions
virtual ~OdeEmbedded()
destructs
double facmax_
maximum allowable fraction change in time step
double facopt(double err)
calculates factor for "optimal" next time step
virtual bool is_rejected()
simply returns isrej
virtual void adapt(double abstol, double reltol)
does the calculations to determine isrej and dtopt
double facsafe_
safety factor applied to time step selection
double ode_min2(double a, double b)
Simple minimum of two doubles.
Definition ode_util.cc:12
double ode_max2(double a, double b)
Simple maximum of two doubles.
Definition ode_util.cc:7