astep_nonadapt.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 
24 #ifndef O2SCL_ASTEP_NONADAPT_H
25 #define O2SCL_ASTEP_NONADAPT_H
26 
27 /** \file astep_nonadapt.h
28  \brief File defining \ref o2scl::astep_nonadapt
29 */
30 
31 #include <gsl/gsl_math.h>
32 #include <gsl/gsl_odeiv.h>
33 
34 #include <boost/numeric/ublas/vector.hpp>
35 
36 #include <o2scl/astep.h>
37 
38 #ifndef DOXYGEN_NO_O2NS
39 namespace o2scl {
40 #endif
41 
42  /** \brief An non-adaptive stepper implementation of \ref astep_base.
43 
44  This class simply calls the specified ODE stepper without any
45  attempt to modify the size of the step. It is primarily useful
46  to allow for simple comparisons between adaptive and
47  non-adaptive solution or to proceed with a solution in places
48  where an adaptive stepper is failing.
49 
50  To modify the ODE stepper which is used, use the function
51  astep_base::set_step().
52  */
53  template<class vec_y_t=boost::numeric::ublas::vector<double>,
54  class vec_dydx_t=vec_y_t, class vec_yerr_t=vec_y_t,
55  class func_t=ode_funct11 > class astep_nonadapt :
56  public astep_base<vec_y_t,vec_dydx_t,vec_yerr_t,func_t> {
57 
58 #ifndef DOXYGEN_INTERNAL
59 
60  protected:
61 
62  /// The allocated vector size
63  size_t msize;
64 
65  /** \brief Internal storage for dydx
66 
67  \comment
68  This isn't named dydx so that we can name function arguments
69  dydx in astep_derivs().
70  \endcomment
71  */
72  vec_dydx_t dydx_int;
73 
74 #endif
75 
76  public:
77 
78  astep_nonadapt() {
79  msize=0;
80  }
81 
82  virtual ~astep_nonadapt() {
83  if (msize>0) {
84  dydx_int.clear();
85  }
86  }
87 
88  /** \brief Make an adaptive integration step of the system
89  \c derivs
90 
91  This attempts to take a step of size \c h from the point \c x of
92  an \c n-dimensional system \c derivs starting with \c y. On
93  exit, \c x and \c y contain the new values at the end of the
94  step, \c h contains the size of the step, \c dydx_out contains
95  the derivative at the end of the step, and \c yerr contains the
96  estimated error at the end of the step.
97 
98  If the base stepper returns a non-zero value, that value is
99  passed on as the return value of <tt>astep()</tt>, though the
100  input \c y may still have been modified by the base stepper.
101 
102  */
103  virtual int astep(double &x, double xlimit, double &h,
104  size_t n, vec_y_t &y, vec_dydx_t &dydx_out,
105  vec_yerr_t &yerr, func_t &derivs) {
106 
107  if (n!=msize) {
108  dydx_int.resize(n);
109  }
110 
111  derivs(x,n,y,dydx_int);
112  if (h>0) {
113  if (x+h>xlimit) h=xlimit-x;
114  } else {
115  if (x+h<xlimit) h=xlimit-x;
116  }
117  int ret=this->stepp->step(x,h,n,y,dydx_int,y,yerr,dydx_out,derivs);
118  x+=h;
119 
120  return ret;
121  }
122 
123  /** \brief Make an adaptive integration step of the system
124  \c derivs with derivatives
125 
126  This attempts to take a step of size \c h from the point \c x of
127  an \c n-dimensional system \c derivs starting with \c y and
128  given the initial derivatives \c dydx. On exit, \c x, \c y and
129  \c dydx contain the new values at the end of the step, \c h
130  contains the size of the step, \c dydx contains the derivative
131  at the end of the step, and \c yerr contains the estimated error
132  at the end of the step.
133 
134  If the base stepper returns a non-zero value, that value is
135  passed on as the return value of <tt>astep()</tt>, though the
136  inputs \c y and \c dydx may still have been modified by the
137  base stepper.
138  */
139  virtual int astep_derivs(double &x, double xlimit, double &h,
140  size_t n, vec_y_t &y, vec_dydx_t &dydx,
141  vec_yerr_t &yerr, func_t &derivs) {
142 
143  if (h>0) {
144  if (x+h>xlimit) h=xlimit-x;
145  } else {
146  if (x+h<xlimit) h=xlimit-x;
147  }
148  int ret=this->stepp->step(x,h,n,y,dydx,y,yerr,dydx,derivs);
149  x+=h;
150 
151  return ret;
152  }
153 
154  /** \brief Make an adaptive integration step of the system
155  \c derivs
156 
157  This function performs an adaptive integration step with the
158  \c n-dimensional system \c derivs and parameter \c pa. It
159  Begins at \c x with initial stepsize \c h, ensuring that the
160  step goes no farther than \c xlimit. At the end of the step,
161  the size of the step taken is \c h and the new value of \c x
162  is in \c x_out. Initially, the function values and
163  derivatives should be specified in \c y and \c dydx. The
164  function values, derivatives, and the error at the end of
165  the step are given in \c yout, \c yerr, and \c dydx_out.
166  Unlike in \c ode_step objects, the objects \c y, \c yout,
167  \c dydx, and \c dydx_out must all be distinct.
168 
169  If the base stepper returns a non-zero value, that value is
170  passed on as the return value of <tt>astep()</tt>, though the
171  output parameters may still have been modified by the
172  base stepper.
173  */
174  virtual int astep_full(double x, double xlimit, double &x_out, double &h,
175  size_t n, vec_y_t &y, vec_dydx_t &dydx,
176  vec_y_t &yout, vec_yerr_t &yerr,
177  vec_dydx_t &dydx_out, func_t &derivs) {
178 
179  if (h>0) {
180  if (x+h>xlimit) h=xlimit-x;
181  } else {
182  if (x+h<xlimit) h=xlimit-x;
183  }
184  int ret=this->stepp->step(x,h,n,y,dydx,yout,yerr,dydx_out,derivs);
185  x_out=x+h;
186 
187  return ret;
188  }
189 
190  };
191 
192 #ifndef DOXYGEN_NO_O2NS
193 }
194 #endif
195 
196 #endif
An non-adaptive stepper implementation of astep_base.
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
ode_step< vec_y_t, vec_dydx_t, vec_yerr_t, func_t > * stepp
Pointer to the stepper being used.
Definition: astep.h:131
virtual int astep(double &x, double xlimit, double &h, size_t n, vec_y_t &y, vec_dydx_t &dydx_out, vec_yerr_t &yerr, func_t &derivs)
Make an adaptive integration step of the system derivs.
Adaptive stepper [abstract base].
Definition: astep.h:49
vec_dydx_t dydx_int
Internal storage for dydx.
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)
Make an adaptive integration step of the system derivs.
virtual int astep_derivs(double &x, double xlimit, double &h, size_t n, vec_y_t &y, vec_dydx_t &dydx, vec_yerr_t &yerr, func_t &derivs)
Make an adaptive integration step of the system derivs with derivatives.
size_t msize
The allocated vector size.

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).