Minsky: 3.17.0
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 "variableValue.h"
23 #include "operation.h"
24 #include "group.h"
25 #include "polyPackBase.h"
26 
27 #include <vector>
28 #include <cairo/cairo.h>
29 
30 #include <arrays.h>
31 
32 // override EcoLab's default CLASSDESC_ACCESS macro
33 #include "classdesc_access.h"
34 
35 
36 namespace minsky
37 {
38  using namespace ecolab;
39  using namespace classdesc;
40  using namespace std;
41 
42  struct EvalOpBase: public classdesc::PolyBase<minsky::OperationType::Type>,
43  // virtual public classdesc::PolyPackBase,
44  public OperationType
45  {
47 
48  // value used for the time operator
49  static double t;
50  static std::string timeUnit;
51 
53  int out=-1;
60  std::vector<unsigned> in1;
61  struct Support
62  {
63  double weight;
64  unsigned idx;
65  Support() {}
66  Support(double weight, unsigned idx): weight(weight), idx(idx) {}
67  };
68  std::vector<std::vector<Support>> in2;
70  bool flow1=true, flow2=true, xflow=true;
71 
73  std::shared_ptr<OperationBase> state;
74  [[noreturn]] void throw_error(const std::string& msg) const {
75  if (state) state->throw_error(msg);
76  else throw std::runtime_error(msg);
77  }
78  virtual ~EvalOpBase() {}
79 
92  virtual void deriv(double df[], std::size_t n, const double ds[],
93  const double sv[], const double fv[])=0;
94 
98  virtual void eval(double fv[]=ValueVector::flowVars.data(), std::size_t n=ValueVector::flowVars.size(),
99  const double sv[]=ValueVector::stockVars.data())=0;
100 
101 
103  virtual void setTensorParams(const VariableValue&,const OperationBase&) {}
104  };
105 
107  struct ScalarEvalOp: public EvalOpBase
108  {
109  std::shared_ptr<VariableValue> result;
110  virtual int numArgs() const =0;
112 
114  static ScalarEvalOp* create(Type op/*=numOps*/, const ItemPtr& state);
115 
116  void deriv(double df[], std::size_t n, const double ds[],
117  const double sv[], const double fv[]) override;
118 
119  void eval(double fv[], std::size_t, const double sv[]) override;
120 
122  virtual double evaluate(double in1=0, double in2=0) const=0;
127  virtual double d1(double x1=0, double x2=0) const=0;
128  virtual double d2(double x1=0, double x2=0) const=0;
130  };
131 
133  template <minsky::OperationType::Type T>
134  struct EvalOp: public classdesc::Poly<EvalOp<T>, ScalarEvalOp>
135  {
136  OperationType::Type type() const override {return T;}
137  int numArgs() const override {
138  return OperationTypeInfo::numArguments<T>();
139  }
140  double evaluate(double in1=0, double in2=0) const override;
141  double d1(double x1=0, double x2=0) const override;
142  double d2(double x1=0, double x2=0) const override;
143  };
144 
145  struct ConstantEvalOp: public EvalOp<minsky::OperationType::constant>
146  {
147  double value;
148  double evaluate(double in1=0, double in2=0) const override;
149  };
150 
151  struct EvalOpPtr: public classdesc::shared_ptr<EvalOpBase>,
152  public OperationType
153  {
155  EvalOpPtr(EvalOpBase* e): classdesc::shared_ptr<EvalOpBase>(e) {}
157  classdesc::shared_ptr<EvalOpBase>(ScalarEvalOp::create(op,nullptr)) {}
160  const ItemPtr& state,
161  const std::shared_ptr<VariableValue>& to,
162  const VariableValue& from1={},
163  const VariableValue& from2={});
164  };
165 
166  struct EvalOpVector: public vector<EvalOpPtr>
167  {
168  // override push_back for diagnostic purposes
169 // void push_back(const EvalOpPtr& x) {
170 // vector<EvalOpPtr>::push_back(x);
171 // for (std::size_t i=0; i<x->in1.size(); ++i)
172 // if (auto s=dynamic_cast<ScalarEvalOp*>(x.get()))
173 // {
174 // cout << OperationType::typeName(s->type())<<"(";
175 // if (s->numArgs()>0)
176 // cout << s->in1[i]<<(s->flow1?"":"s");
177 // if (s->numArgs()>1)
178 // for (auto& i: s->in2[i])
179 // cout<<","<<i.idx<<"("<<i.weight<<")"<<(s->flow2?"":"s");
180 // cout <<")->"<<s->out+i<<endl;
181 // }
182 // }
183  };
184 
186  struct FlowVarsResized: public std::exception
187  {
188  const char* what() const noexcept {return "FlowVars unexpectedly resized";}
189  };
190 }
191 
192 #include "evalOp.cd"
193 #endif
std::vector< std::vector< Support > > in2
Definition: evalOp.h:68
represents the operation when evaluating the equations
Definition: evalOp.h:134
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:49
virtual void setTensorParams(const VariableValue &, const OperationBase &)
set additional tensor operation related parameters
Definition: evalOp.h:103
STL namespace.
const char * what() const noexcept
Definition: evalOp.h:188
std::shared_ptr< Item > ItemPtr
Definition: item.h:55
EvalOpPtr(EvalOpBase *e)
Definition: evalOp.h:155
Support(double weight, unsigned idx)
Definition: evalOp.h:66
Legacy EvalOp base interface.
Definition: evalOp.h:107
OperationType::Type Type
Definition: evalOp.h:46
OperationType::Type type() const override
Definition: evalOp.h:136
std::vector< unsigned > in1
Definition: evalOp.h:60
std::shared_ptr< VariableValue > result
lifetime management of the resultant variableValue number of arguments to this operation ...
Definition: evalOp.h:109
virtual ~EvalOpBase()
Definition: evalOp.h:78
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:156
std::shared_ptr< OperationBase > state
state data (for those ops that need it)
Definition: evalOp.h:73
int numArgs() const override
Definition: evalOp.h:137
static std::string timeUnit
Definition: evalOp.h:50
void throw_error(const std::string &msg) const
Definition: evalOp.h:74
exception throw if flowVars vector unexpectedly resized during evalEquations
Definition: evalOp.h:186
float d2(float x0, float y0, float x1, float y1)
Definition: wire.cc:564