LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #pragma once
18 #ifndef ODE_HPP
19 #define ODE_HPP
20 #include "ode.h"
21 #include "utils/comp_funcs.hpp"
22 #include "utils/item_heads.hpp"
23 #include "writer/writer.h"
24 #include "engine/engineimp.h"
25 #include <limits>
26 #include <time.h>
27 template<class T>
28 class ode_tmpl:public ode{
29  void integrate_with_writes();
30  void write_t(double tcur);
33  T* sol;
35  double t0;
37  double tf;
39  double tw;
41  double w_step;
42  public:
43  std::string type() const;
44  const std::type_info& vtype() const;
45  void postprocess(input& in);
46  double simulate();
47 };
48 template<class T>
49 void ode_tmpl<T>::write_t(double tcur){
50  std::shared_ptr<writer> dat_writer=std::shared_ptr<writer>(new writer(true));
51  dat_writer->add_data(
53  dat_writer->add_data(
54  data::create("Func", sol, dimension), writer::INTERMEDIATE_FUNC);
55  holder->add_writer(dat_writer);
56 }
57 template<class T>
59  double tcur = tw;
60  while(tcur <= (tf-w_step)){
61  write_t(tcur);
62  inter->integrate(sol, tcur, tcur + w_step);
63  tcur += w_step;
64  }
65  write_t(tcur);
66  inter->integrate(sol, tcur, tf);
67 }
68 
69 template<class T>
73  in.retrieve(inter, "integrator", this);
74  if(!inter->compare<T>()){
75  err(this->name() + " is of type " + this->vname() + ", while the integrator inter \
76  is of type " + inter->vname(), "ode_tmpl::postprocess", "simulation/ode_tmpl.hpp",
77  FATAL_ERROR);
78  }
79  this->add_as_parent(inter);
80  in.retrieve(tf, "tf", this);
81  in.retrieve(t0, "t0", this, 0.0);
82  if(t0 >= tf){
83  err("t0 is >= than tf", "ode_tmpl::postprocess",
84  "simulation/ode_tmpl.hpp", FATAL_ERROR);
85  }
86 
87  in.retrieve(tw, "tw", this, t0);
88  in.retrieve(w_step, "w_step",this, -1.0);
89  if(tw >= tf){
90  err("tw is >= than tf, not writing intermittent data", "ode_tmpl::postprocess",
91  "simulation/ode_tmpl.hpp", WARNING);
92  w_step=-1;
93  }
94  if(tw < t0){
95  tw = t0;
96  }
97  this->memp.create(this->dimension, &sol);
98 }
99 template<class T>
101  inter->initial_condition(sol);
102  clock_t cval = clock();
103  if(w_step < 0 || abs(w_step) <= std::numeric_limits<double>::epsilon()){
104  //don't do any intermittent recording
105  inter->integrate(sol, t0, tf);
106  }
107  else{
108  if(tw > t0){
109  inter->integrate(sol, t0, tw);
110  }
111  integrate_with_writes();
112  }
113  write_t(tf);
114  std::cout << "Simulation took " << ((double)(clock()-cval))/CLOCKS_PER_SEC << " seconds" << std::endl;
115  return 0;
116 }
117 template<class T>
119  return "ode_tmpl<"+this->vname() + ">";
120 }
121 template<class T>
122 const std::type_info& ode_tmpl<T>::vtype() const{
123  return typeid(T);
124 }
125 #endif