LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
toroidal.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 */
17 #include "engine/item.h"
18 #include "toroidal.h"
19 #include "comp_funcs.hpp"
20 #include "types/type_register.hpp"
21 #include "engine/engineimp.h"
22 #include <algorithm>
23 template class type_register<toroidal>;
24 /*
25  * This function returns the dependencies of the toroidal class
26  * This class has the same dependencies as the controller class, along with
27  *
28  * - int iterations: The number of iterations the controller will perform
29  * - double initial_inc: The stepsize of the first variable
30  * - double mul_fac: The multiplying factor to find the increment of the next variable, inc2=mul_fac*inc1, inc3=mul_fac*inc2, etc.
31  *
32  *\sa controller::dependencies, item_dim::dependencies
33  */
34 std::vector<std::string> toroidal::dependencies() const{
35  std::string deps[] = {"iterations", "initial_inc", "mul_fac"};
36  return make_append(deps, controller::dependencies());
37 }
38 
39 
48  num_int=0;
49  int _iterations;
50 
51 
52 
53  dat.retrieve(_iterations, "iterations", this);
54  dat.retrieve(initial_inc, "initial_inc", this);
55  dat.retrieve(mul_fac, "mul_fac", this);
56 
57 
58  iterations = _iterations;
59  if(mul_fac==0){
60  err("Multiply factor, mul_fac, must not be equal to zero",
61  "toroidal::postprocess", "controller/toroidal.cpp",
62  FATAL_ERROR);
63  }
64  if(initial_inc==0){
65  err("The initial increment, initial_inc, must not be equal to zero",
66  "toroidal::postprocess", "controller/toroidal.cpp",
67  FATAL_ERROR);
68  }
69  //temporary hack to help with c_elegans
71  //find the controllers place in the number of iterations
72 }
73 
79  return "toroidal";
80 }
81 
83  double curinc = initial_inc;
84  num_int++;
85  num_cont++;
86  if(vars.size() == 4){
87  //hard coded to test against matlab code
88  vars[0].lock()->inc(sqrt(0.11)*100.0*PI/1000.0);
89  vars[1].lock()->inc(sqrt(0.13)*100.0*PI/1000.0);
90  vars[2].lock()->inc(sqrt(0.17)*100.0*PI/1000.0);
91  vars[3].lock()->inc(sqrt(0.19)*100.0*PI/1000.0);
92  return;
93  }
94  vars.erase(std::remove_if(vars.begin(), vars.end(), [](std::weak_ptr<variable>& inval){
95  return inval.expired();
96  }), vars.end());
97 
98  //still resume loack here in case a multithreaded engine appears in the future
99  for(auto _cvar : vars){
100  std::shared_ptr<variable> cvar = _cvar.lock();
101  if(cvar){
102  cvar->inc(curinc);
103  curinc *= mul_fac;
104  }
105  }
106 }
107 
109 void toroidal::addvar(std::weak_ptr<variable> v){
110  vars.push_back(v);
111 }
114  return num_int < iterations;
115 }
118  for(size_t i = 0; i < index*iterations; i++){
119  double curinc=initial_inc;
120  for(auto _val : vars){
121  std::shared_ptr<variable> var = _val.lock();
122  if(var.use_count()){
123  var->inc(curinc);
124  curinc *= mul_fac;
125  }
126  }
127  }
128  return index*iterations;
129 }