LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rk4_tmpl.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #include "integrator.h"
18 #include "rk4.h"
19 #include "item_heads.hpp"
20 #include "types/float_traits.hpp"
21 template<class T>
22 class rk4_tmpl:public rk4 {
23  typedef typename float_traits<T>::type real_type;
24  protected:
25  T* restr f0, * restr f1, * restr f2, * restr f3;
26  T* restr u_calc;
29  public:
30  virtual const std::type_info& vtype() const;
31  void postprocess(input& dat);
32  std::string type() const;
33  int integrate(ptr_passer u, double t0, double tf);
34  virtual ~rk4_tmpl();
35 };
36 template<class T>
37 const std::type_info& rk4_tmpl<T>::vtype()const{
38  return typeid(T);
39 }
40 template<class T>
42  }
43 
44 template<class T>
45 int rk4_tmpl<T>::integrate(ptr_passer _u0, double t0, double tf)
46  // Using code from John Burkardt as a guideline
47 {
48  T* restr u0 = _u0;
49  ALIGNED(u0);
50  ALIGNED(f0);
51  ALIGNED(f1);
52  ALIGNED(f2);
53  ALIGNED(f3);
54  ALIGNED(u0);
55  ALIGNED(u_calc);
56  const size_t m = dimension;
57  const double t_diff = tf-t0;
58  const size_t steps = ceil(t_diff/stepsize);
59  const real_type dt = t_diff/steps;
60  //do steps integrations
61  //each loop iteration is a runge_kutta timestep
62  for(size_t j = 0; j < steps; j++){
63  size_t i;
64  real_type t1=t0+dt/2.0;
65  real_type t2=t0+dt;
66  //
67  // Get four sample values of the derivative.
68  //
69  func->dxdt(u0, f0, t0);
70 
71  for ( i = 0; i < m; i++ ){
72  u_calc[i] = u0[i] + dt * f0[i] / 2.0;
73  }
74 
75  func->dxdt(u_calc, f1, t1);
76  for ( i = 0; i < m; i++ ){
77  u_calc[i] = u0[i] + dt * f1[i] / 2.0;
78  }
79 
80  func->dxdt(u_calc, f2, t1);
81  for ( i = 0; i < m; i++ ){
82  u_calc[i] = u0[i] + dt * f2[i];
83 
84  }
85 
86  func->dxdt(u_calc, f3, t2);
87  for ( i = 0; i < m; i++ ){
88  u0[i] = u0[i] + (dt * ( f0[i] + 2.0 * f1[i] + 2.0 * f2[i] + f3[i] ) / 6.0);
89  }
90 
91  t0 += dt;
92  }
93  return 0;
94 }
95 template<class T>
97  return std::string("rk4_tmpl<") + this->vname() + ">";
98 }
99 template<class T>
102  if(!rh_val->compare<T>()){
103  err("Bad rhs type passed to rk4 integrator", "rk4_tmpl::postprocess",
104  "integrator/rk4_tmpl.h", FATAL_ERROR);
105  }
106  this->add_as_parent(rh_val);
107  dat.retrieve(stepsize, "stepsize", this);
108  if(stepsize <= 0){
109  err("stepsize is invalid, must be >= 0", "rk4_tmpl::postprocess",
110  "integrator/rk4.hpp", dat["stepsize"], FATAL_ERROR);
111  }
112  memp.create(dimension, &f0, &f1, &f2, &f3, &u_calc);
113 }
114