Minsky
evalOp.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2013
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef EVALOP_H
20 #define EVALOP_H
21 
22 #include <ecolab.h>
23 #include <xml_pack_base.h>
24 #include <xml_unpack_base.h>
25 
26 #include "variableValue.h"
27 #include "operation.h"
28 #include "group.h"
29 #include "polyPackBase.h"
30 
31 #include <vector>
32 #include <cairo/cairo.h>
33 
34 #include <arrays.h>
35 
36 // override EcoLab's default CLASSDESC_ACCESS macro
37 #include "classdesc_access.h"
38 
39 
40 namespace minsky
41 {
42  using namespace ecolab;
43  using namespace classdesc;
44  using namespace std;
45 
46  struct EvalOpBase: public classdesc::PolyBase<minsky::OperationType::Type>,
47  // virtual public classdesc::PolyPackBase,
48  public OperationType
49  {
51 
52  // value used for the time operator
53  static double t;
54  static std::string timeUnit;
55 
57  int out=-1;
64  std::vector<unsigned> in1;
65  struct Support
66  {
67  double weight;
68  unsigned idx;
69  Support() {}
70  Support(double weight, unsigned idx): weight(weight), idx(idx) {}
71  };
72  std::vector<std::vector<Support>> in2;
74  bool flow1=true, flow2=true, xflow=true;
75 
77  std::shared_ptr<OperationBase> state;
78  [[noreturn]] void throw_error(const std::string& msg) const {
79  if (state) state->throw_error(msg);
80  else throw std::runtime_error(msg);
81  }
82  virtual ~EvalOpBase() {}
83 
96  virtual void deriv(double df[], std::size_t n, const double ds[],
97  const double sv[], const double fv[])=0;
98 
102  virtual void eval(double fv[]=ValueVector::flowVars.data(), std::size_t n=ValueVector::flowVars.size(),
103  const double sv[]=ValueVector::stockVars.data())=0;
104 
105 
107  virtual void setTensorParams(const VariableValue&,const OperationBase&) {}
108  };
109 
111  struct ScalarEvalOp: public EvalOpBase
112  {
113  std::shared_ptr<VariableValue> result;
114  virtual int numArgs() const =0;
116 
118  static ScalarEvalOp* create(Type op/*=numOps*/, const ItemPtr& state);
119 
120  void deriv(double df[], std::size_t n, const double ds[],
121  const double sv[], const double fv[]) override;
122 
123  void eval(double fv[], std::size_t, const double sv[]) override;
124 
126  virtual double evaluate(double in1=0, double in2=0) const=0;
131  virtual double d1(double x1=0, double x2=0) const=0;
132  virtual double d2(double x1=0, double x2=0) const=0;
134  };
135 
137  template <minsky::OperationType::Type T>
138  struct EvalOp: public classdesc::Poly<EvalOp<T>, ScalarEvalOp>
139  {
140  OperationType::Type type() const override {return T;}
141  int numArgs() const override {
142  return OperationTypeInfo::numArguments<T>();
143  }
144  double evaluate(double in1=0, double in2=0) const override;
145  double d1(double x1=0, double x2=0) const override;
146  double d2(double x1=0, double x2=0) const override;
147  };
148 
149  struct ConstantEvalOp: public EvalOp<minsky::OperationType::constant>
150  {
151  double value;
152  double evaluate(double in1=0, double in2=0) const override;
153  };
154 
155  struct EvalOpPtr: public classdesc::shared_ptr<EvalOpBase>,
156  public OperationType
157  {
159  EvalOpPtr(EvalOpBase* e): classdesc::shared_ptr<EvalOpBase>(e) {}
161  classdesc::shared_ptr<EvalOpBase>(ScalarEvalOp::create(op,nullptr)) {}
164  const ItemPtr& state,
165  const std::shared_ptr<VariableValue>& to,
166  const VariableValue& from1={},
167  const VariableValue& from2={});
168  };
169 
170  struct EvalOpVector: public vector<EvalOpPtr>
171  {
172  // override push_back for diagnostic purposes
173 // void push_back(const EvalOpPtr& x) {
174 // vector<EvalOpPtr>::push_back(x);
175 // for (std::size_t i=0; i<x->in1.size(); ++i)
176 // if (auto s=dynamic_cast<ScalarEvalOp*>(x.get()))
177 // {
178 // cout << OperationType::typeName(s->type())<<"(";
179 // if (s->numArgs()>0)
180 // cout << s->in1[i]<<(s->flow1?"":"s");
181 // if (s->numArgs()>1)
182 // for (auto& i: s->in2[i])
183 // cout<<","<<i.idx<<"("<<i.weight<<")"<<(s->flow2?"":"s");
184 // cout <<")->"<<s->out+i<<endl;
185 // }
186 // }
187  };
188 
190  struct FlowVarsResized: public std::exception
191  {
192  const char* what() const noexcept {return "FlowVars unexpectedly resized";}
193  };
194 }
195 
196 #include "evalOp.cd"
197 #endif
std::vector< std::vector< Support > > in2
Definition: evalOp.h:72
represents the operation when evaluating the equations
Definition: evalOp.h:138
static std::vector< double, CIVITA_ALLOCATOR< double > > stockVars
vector of variables that are integrated via Runge-Kutta. These variables label the columns of the God...
static double t
Definition: evalOp.h:53
virtual void setTensorParams(const VariableValue &, const OperationBase &)
set additional tensor operation related parameters
Definition: evalOp.h:107
STL namespace.
const char * what() const noexcept
Definition: evalOp.h:192
std::shared_ptr< Item > ItemPtr
Definition: item.h:57
EvalOpPtr(EvalOpBase *e)
Definition: evalOp.h:159
Support(double weight, unsigned idx)
Definition: evalOp.h:70
Legacy EvalOp base interface.
Definition: evalOp.h:111
OperationType::Type Type
Definition: evalOp.h:50
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
OperationType::Type type() const override
Definition: evalOp.h:140
std::vector< unsigned > in1
Definition: evalOp.h:64
std::shared_ptr< VariableValue > result
lifetime management of the resultant variableValue number of arguments to this operation ...
Definition: evalOp.h:113
virtual ~EvalOpBase()
Definition: evalOp.h:82
static std::vector< double, CIVITA_ALLOCATOR< double > > flowVars
variables defined as a simple function of the stock variables, also known as lhs variables. These variables appear in the body of the Godley table
EvalOpPtr(OperationType::Type op)
Definition: evalOp.h:160
std::shared_ptr< OperationBase > state
state data (for those ops that need it)
Definition: evalOp.h:77
int numArgs() const override
Definition: evalOp.h:141
static std::string timeUnit
Definition: evalOp.h:54
void throw_error(const std::string &msg) const
Definition: evalOp.h:78
exception throw if flowVars vector unexpectedly resized during evalEquations
Definition: evalOp.h:190
float d2(float x0, float y0, float x1, float y1)
Definition: wire.cc:568