LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
example_integrator_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 EXAMPLE_INTEGRATOR_TMPL_HPP
18 #define EXAMPLE_INTEGRATOR_TMPL_HPP
19 #include "example_integrator.h"
20 #include "types/float_traits.hpp"
21 //It seems that it would be easier to use dynamic types, since we have a problem
22 //where the type is decided at runtime. However, that murders performance terribly
23 //so we do compile-time type selection and then instantiate the right type at runtime from
24 //a list of types with the type_constructor class
25 template<class T>
28 
45  typedef typename float_traits<T>::type real_type;
47 
48  //Why don't we use the real_type to represent the integer types?
49  //floating point values can be very, very bad at representing large integer numbers.
50  size_t unsigned_var;
51  int rval2;
53  public:
55  int integrate(ptr_passer u, double t0, double tf);
56  std::string type() const{
57  return std::string("euler_sde_tmpl<")+typeid(T).name()+">";
58  }
59  const std::type_info& vtype() const{
60  //This gives the type ofthe template
61  return typeid(T);
62  }
63  void postprocess(input& in);
64  //This is called if a variable that this class references changes
65  //Hence why this must be passed to each retireve call-so the engine knows who
66  //needs updating
67  //
68  //Don't use this for IO utilities, and prefer to move logic into the controller class.
69  //The c_elegans rhs class is a bad example, but it needed to be done right away and was quicker than
70  //re-architecting parts of the engine for native mutable lists
71  void update();
72 };
73 
74 template<class T>
75 int example_integrator_tmpl<T>::integrate(ptr_passer _u, double t0, double tf){
76  //First, get the actual pointer out of the holder
77  //The holder helps to ensure type-safety
78  //The restr declaration tells the compiler that nothing else points at this location
79  //That is a lie!!! But nothing will be modifying this location during the function call
80  //so it can be delcared restr. Even if this were multithreaded, it would be incorrect to access this pointer
81  //from another thread and would have meaningless results anyways
82  T* restr u = _u;
83 
84  //This declaration will tell the compiler that the resulting pointer is aligned in memory.
85  //If you use the mempool and align to 16 bytes or more (32, 64, etc) This will be true
86  ALIGNED(u);
87 
88  //The input array will be of length dimension, unless you use a simulation class that doesn't
89  //enforce dimension-correctness with the items that it holds.
90  for(size_t i = 0; i < this->dimension; i++){
91  //Some magical integration function
92  u[i] = (rval1 * rval2) + (calc_val * unsigned_var) * u[i];
93  }
94  return 0;
95 }
96 
97 template<class T>
99  //perform postprocessing of integrator class
100  //note how we skip example_integrator in this chain since example integrator is
101  //only a proxy that performs type erasure
103  item* some_class;
104  in.retrieve(some_class, "test_class", this);
105  //this allows us to have a consistent representation in the input file.
106  //If the variable in question is not part of the hardcore numerical analysis code,
107  //it may be better to just use a double and not deal with this in the postprocessing
108  double _rval1 = 0;
109  in.retrieve(_rval1, "rval1", this);
110  rval1=_rval1;
111 
112  //note how we pass a default parameter since rval2 isn't necesarily going to exist
113  in.retrieve(rval2, "rval2", this, 0);
114  in.retrieve(unsigned_var, "unsigned_var", this);
115  in.retrieve(something, "something", this);
116  update();
117 }
118 
119 template<class T>
121  calc_val = rval1*3+rval2;
122 }
123 #endif