LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
example_rhs.cpp
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 */
18 
22 #include "example_rhs.h"
23 #include "utils/item_heads.hpp"//various headers important to dealing with items, see header for current content
24 #include "comp_funcs.hpp" //includes various functions for complex variables and some template math ones
25 #include "types/type_register.hpp"//include file for the type_register class;
26 //****IMPORTANT*****IMPORTANT*****IMPORTANT*****IMPORTANT*****IMPORTANT*
27 //To have code that compiles with the engine, you need to edit the makefile in
28 //the rhs directory. It is the file that controls when pieces of code are built.
29 //Alhough it is possible to simply recompile everything every time, it is incredibly ineffecient
30 //and wastes and incredible amount of time.
31 //
32 //An example can be found, for this file, in the makefile in rhs.
33 //You pretty much just need to copy and paste a version with your file names
34 //instead of the example file names. Also, please don't remove the example build commands
35 //
36 //
37 //*************************
38 //IMPORTANT
39 //the line of code below makes it so that you can create an instance of the class
40 //through the input scripts
41 template class type_register<example_rhs>;
42 //If you don't do this, then you can't create this in the input file
43 //
44 //That is desirable behavior, for example it is used by the template backends of the integrators
45 
46 
48 
55  return "example_rhs";
56 }
57 
59 
69 std::vector<std::string> example_rhs::dependencies() const{
70  std::string deps[] = {"val1", "val2", "random_info"};
71  //this array holds the dependencies
72 
73  //This function appenps an array of strings and a vector of strings,
74  //returning a vector of the results
75  return make_append(deps, rhs::dependencies());
76  //return the dependencies for this class along with any dependencies the parent classes have
77 }
78 
80 
86  //warning so nobody ever actually constructs one of these
87  //please don't have warnings that always throw in your code ever
88  err("Example_rhs created, mysteriously fails on some architectures seemingly from AVX instructions. Also does nothing useful", "rhs::create", "rhs/rhs.cpp", WARNING);
89 
90  rhs::postprocess(dat);//always postprocess the parent class first
91  //Any class that inherits from rhs, or item_dim, has access to a variable
92  //called dimension, which represents the dimension of the problem at hand
93  //dimension is initialized in the item_dep, which is why the parent postprocessing
94  //must be called first
95 
96 
97  //There are two types of errors, warning and fatal errors.
98  //Warnings print something to the screen to warn the user,
99  //but do not stop the program. A fatal error causes an immediate exit
100  //These are caused by the function err. Details can be found in the documentation
101  //
102  //Lets throw a fatal error if the dimension is less than 10
103  if(dimension > 10){
104  err("dimension > 10, which is required for example_rhs", "example_rhs::postprocess",
105  "rhs/example_rhs.cpp", FATAL_ERROR);
106  }
107 
108  //Lets also throw an error if the dimension is equal to 5
109  if(dimension==5){
110  err("dimension=5, which gets a warning from example_rhs", "example_rhs::postprocess",
111  "rhs/example_rhs.cpp", WARNING);
112  }
113  //The map contains item*, which point to an item of arbitrary type.
114  //I plan to implement type checking of the dependencies.
115  //But sometime later
116  //
117  //To retrieve a value, you can call the retrieve function of the variable at hand
118  //you just pass the name of parameter that is being retrieved to the map, which returns
119  //an item*. Then this pointer is called to retrieve the value, and is passed the address
120  //of val1
121 
122  dat.retrieve(val1, "val1", this);
123  dat.retrieve(val2, "val2", this);
124  dat.retrieve(random_info, "random_info", this);
125 
126  //now, we are going to allocate some memory to something
127  //This may be useful for storing temporary calculations during the RHS.
128  //Inheriting from item_dim provides access to a memory pool, memp.
129  //just pass memp.create the dimension of the problem and the
130  //addresses of the pointers and the alignment, allocation, and deallocation is
131  //taken care of!
132  //
133  //memp.create(dimension, &ptr1, &ptr2, &ptr3, etc)
134  //for custom alignment(standard is 32 byte)
135  //memp.create(alignment, dimension, &ptr1, &prt2, etc)
136  //
138  for(size_t i = 0; i < dimension; i++){
139  value_holder[i] = Id*(double)i*val1 + (dimension-i)*val2;
140  }
141 }
143 
152  for(size_t i = 0; i < dimension; i++){
153  value_holder[i] = Id*(double)i*val1 + (dimension-i)*val2;
154  }
155 }
156 
158 
164  //this->free_various_resources();
165  //Seriously though try to use resources which destruct automatically
166  //Through their own destructor
167 }
168 
170 
178 int example_rhs::dxdt(ptr_passer _x, ptr_passer _dx, double t){
179  //This pulls the pointer out of the ptr_passer object.
180  //The conversion is implicit, but will throw a fatal error if the
181  //types do not match.
182  comp* restr x = _x;
183  comp* restr dx = _dx;
184  //This doesn't really do anything useful
185  //but showcases some of the compex variable functions
186  //See detailed documentation for more
187  //But is a nice example
188  for(size_t i = 0; i < dimension; i++){
189  dx[i] = (_real(x[i])*_imag(value_holder[i])) * (val2/val1);
190  }
191  return 0;
192 }