LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stable_ode_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 #ifndef STABLE_ODE_TMPL_HPP
18 #define STABLE_ODE_TMPL_HPP
19 #include "stable.h"
20 #include "item_heads.hpp"
21 template<class T>
23  protected:
24  double tcur;
25  T* restr ucur, * restr ulast;
27  double int_len;
29  double t0;
33  virtual void pre_integration_operations();
35  virtual void post_integration_operations();
36  virtual void iterate_system();
37  virtual double get_change();
38  public:
39  double score();
40  const std::type_info& vtype() const;
41  virtual void postprocess(input& invals);
42  virtual std::string type() const;
43  virtual ~stable_ode_tmpl();
44  friend class stable;
45 };
46 
47 
48 //stable_ode_tmpl functions
49 
56 template <class T>
58  for(size_t i = 0; i < this->dimension; i++){
59  ulast[i] = ucur[i];
60  }
61  this->pre_integration_operations();
62  int res = inter->integrate(ucur, tcur, tcur+int_len);
63  tcur += int_len;
64  this->post_integration_operations();
65  if(res < 0){
66  this->bad_res=1;
67  }
68 }
69 
70 template <class T>
72  stable::postprocess(invals);
73  invals.retrieve(inter, "integrator", this);
74  if(!inter->compare<T>()){
75  err("Bad integrator type passed to stable_ode_tmpl", "stable_ode_tmpl:postprocess",
76  "simulation/stable_ode_tmpl.hpp", FATAL_ERROR);
77  }
78  this->add_as_parent(inter);
79  invals.retrieve(t0, "t0", this, 0.0);
80  tcur = t0;
81  invals.retrieve(int_len, "int_len", this);
82  const double eps_val = 1e-12;
83  if(int_len < eps_val){
84  err("Variable int_len is too small, int_len must be greater than 1e-12",
85  "stable_ode::postprocess", "system/stable.cpp", FATAL_ERROR);
86  }
87  memp.add(dimension, &ucur, &ulast);
88 }
89 
90 template <class T>
92 template <class T>
94 }
95 template <class T>
97 }
98 template <class T>
100  std::string val = "stable_ode_tmpl";
101  val.append(typeid(T).name());
102  return val;
103 }
104 template <class T>
106  return this->obj->score((comp*)ucur);
107 }
108 
109 template<class T>
110 const std::type_info& stable_ode_tmpl<T>::vtype() const{
111  return typeid(T);
112 }
113 template<class T>
115  //standard difference is the L2 norm
116  double sabs=0;
117  for(size_t i = 0; i < this->dimension; i++){
118  double aval = abs(ucur[i]-ulast[i]);
119  sabs+= aval*aval;
120  }
121  return sqrt(sabs);
122 }
123 #endif