Minsky
evalGodley.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 EVALGODLEY_H
20 #define EVALGODLEY_H
21 
22 #include "schema/assetClass.h"
23 #include "classdesc_access.h"
24 #include "constMap.h"
25 #include "variableValues.h"
26 #include "str.h"
27 #include "flowCoef.h"
28 
29 #include <ecolab.h>
30 #include <arrays.h>
31 
32 namespace minsky
33 {
34  using namespace ecolab;
35 
36  class EvalGodley
37  {
39  ecolab::array<int> sidx, fidx;
40  ecolab::array<double> m;
41 
43  ecolab::array<int> initIdx;
44 
46  public:
47 
49  template <class GodleyIterator>
50  void initialiseGodleys
51  (const GodleyIterator& begin, const GodleyIterator& end,
52  const VariableValues& values);
53 
58  void eval(double* sv, const double* fv) const;
59 
60  EvalGodley(): compatibility(false) {}
64 
65  };
66 
68  template <class T> class GodleyIteratorAdaptor
69  {
70  T it;
71  public:
72  GodleyIteratorAdaptor(const T& it): it(it) {}
73  void operator++() {++it;}
74  bool operator!=(const GodleyIteratorAdaptor& x) const
75  {return x.it!=it;}
76 
77  int id() const {return it->id();}
78 
80  const std::vector<std::vector<std::string> >& data() const;
81  GodleyAssetClass::AssetClass assetClass(std::size_t col) const;
82  bool signConventionReversed(int col) const;
83  bool initialConditionRow(int row) const;
85  // TODO: this should be refactored to a more central location
86  string valueId(const std::string& x) const {return it->valueId(x);}
87  };
88 
89  template <class T> GodleyIteratorAdaptor<T> makeGodleyIt(const T& it)
90  {return GodleyIteratorAdaptor<T>(it);}
91 
94  {
96 
98  std::map<string, AssetClass> colAssetType;
100  std::set<string> sharedCol;
101 
104  void checkShared(const string& name, AssetClass ac);
105 
110 
113  bool updateColDefs(const string& col, const FlowCoef& fc);
114 
116  void checkSharedColDefs() const;
117  };
118 
119 
120  template <class GodleyIterator> void EvalGodley::initialiseGodleys
121  (const GodleyIterator& begin, const GodleyIterator& end,
122  const VariableValues& values)
123  {
124  SharedColumnCheck scCheck;
125  sidx.resize(0);
126  fidx.resize(0);
127  m.resize(0);
128 
129  std::set<int> iidx;
130 
131  for (GodleyIterator g=begin; g!=end; ++g)
132  {
133  if (g.data().empty()) continue;
134  // check for shared columns
135  if (!compatibility)
136  for (std::size_t col=1; col<g.data()[0].size(); ++col)
137  if (!trimWS(g.data()[0][col]).empty())
138  scCheck.checkShared(g.valueId(trimWS(g.data()[0][col])), g.assetClass(col));
139 
140  for (std::size_t row=1; row<g.data().size(); ++row)
141  if (!g.initialConditionRow(row))
142  for (std::size_t col=1; col<g.data()[row].size(); ++col)
143  {
144  FlowCoef fvc(g.data()[row][col]);
145  auto svName=col<g.data()[0].size()? trimWS(g.data()[0][col]): "";
146  if (fvc.name.empty() || svName.empty()) continue;
147  fvc.name=g.valueId(fvc.name);
148  svName=g.valueId(svName);
149 
150  auto fv=values.find(fvc.name);
151  auto sv=values.find(svName);
152  if (fv!=values.end() && sv!=values.end() &&
153  fv->second->idx()>=0 && sv->second->idx()>=0)
154  {
155  // check for compatible column definitions
156  if (!compatibility &&
157  scCheck.updateColDefs(svName, fvc))
158  continue;
159 
160  iidx.insert(sv->second->idx());
161  sidx<<=sv->second->idx();
162  fidx<<=fv->second->idx();
163  m<<=fvc.coef;
164  }
165  }
166  }
167 
168  initIdx.clear();
169  for (auto i: iidx)
170  initIdx<<=i;
171 
172  if (!compatibility)
173  scCheck.checkSharedColDefs();
174  }
175 
176 }
177 
178 #include "evalGodley.cd"
179 #include "evalGodley.xcd"
180 #endif
bool operator!=(const GodleyIteratorAdaptor &x) const
Definition: evalGodley.h:74
bool updateColDefs(const string &col, const FlowCoef &fc)
update col defs, give column name, and flow variable entry
Definition: evalGodley.cc:68
ecolab::array< int > sidx
representation of matrix connecting flow variables to stock variables
Definition: evalGodley.h:39
std::map< string, AssetClass > colAssetType
asset type of previously seen column of this name
Definition: evalGodley.h:98
bool compatibility
if compatibility is true, then consttrainst between Godley tables is not applied, and shared columns ...
Definition: evalGodley.h:63
void checkShared(const string &name, AssetClass ac)
check whether column name has already been seen, and if it has, whether it is allowed to be shared by...
Definition: evalGodley.cc:41
for checking shared columns between tables
Definition: evalGodley.h:93
GodleyIteratorAdaptor< T > makeGodleyIt(const T &it)
Definition: evalGodley.h:89
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
void initialiseGodleys(const GodleyIterator &begin, const GodleyIterator &end, const VariableValues &values)
initialise this to a sequence of Godley tables - adaptors provided below
Definition: evalGodley.h:121
std::string trimWS(const std::string &s)
Definition: str.h:49
void checkSharedColDefs() const
check shared columns are equivalently defined
Definition: evalGodley.cc:78
GodleyAssetClass::AssetClass AssetClass
Definition: evalGodley.h:95
std::set< string > sharedCol
indicates a column is shared between Godley tables
Definition: evalGodley.h:100
#define CLASSDESC_ACCESS(type)
string valueId(const std::string &x) const
returns valueid for variable reference in table
Definition: evalGodley.h:86
represents a numerical coefficient times a variable (a "flow")
Definition: flowCoef.h:27
ecolab::array< int > initIdx
index of stock variables that need to be zeroed at start of eval
Definition: evalGodley.h:43
ecolab::array< double > m
Definition: evalGodley.h:40
GodleyIteratorAdaptor(const T &it)
Definition: evalGodley.h:72
adaptor suitable for initialiseGodleys
Definition: evalGodley.h:68
ConstMap< string, std::map< string, double > > colDef
store the sum of flow var contributions for each colum here. Used for checking that shared column def...
Definition: evalGodley.h:109