Minsky: 3.17.0
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 <arrays.h>
30 
31 namespace minsky
32 {
33  using namespace ecolab;
34 
35  class EvalGodley
36  {
38  ecolab::array<int> sidx, fidx;
39  ecolab::array<double> m;
40 
42  ecolab::array<int> initIdx;
43 
45  public:
46 
48  template <class GodleyIterator>
49  void initialiseGodleys
50  (const GodleyIterator& begin, const GodleyIterator& end,
51  const VariableValues& values);
52 
57  void eval(double* sv, const double* fv) const;
58 
59  EvalGodley(): compatibility(false) {}
63 
64  };
65 
67  template <class T> class GodleyIteratorAdaptor
68  {
69  T it;
70  public:
71  GodleyIteratorAdaptor(const T& it): it(it) {}
72  void operator++() {++it;}
73  bool operator!=(const GodleyIteratorAdaptor& x) const
74  {return x.it!=it;}
75 
76  int id() const {return it->id();}
77 
79  const std::vector<std::vector<std::string> >& data() const;
80  GodleyAssetClass::AssetClass assetClass(std::size_t col) const;
81  bool signConventionReversed(int col) const;
82  bool initialConditionRow(int row) const;
84  // TODO: this should be refactored to a more central location
85  string valueId(const std::string& x) const {return it->valueId(x);}
86  };
87 
88  template <class T> GodleyIteratorAdaptor<T> makeGodleyIt(const T& it)
89  {return GodleyIteratorAdaptor<T>(it);}
90 
93  {
95 
97  std::map<string, AssetClass> colAssetType;
99  std::set<string> sharedCol;
100 
103  void checkShared(const string& name, AssetClass ac);
104 
109 
112  bool updateColDefs(const string& col, const FlowCoef& fc);
113 
115  void checkSharedColDefs() const;
116  };
117 
118 
119  template <class GodleyIterator> void EvalGodley::initialiseGodleys
120  (const GodleyIterator& begin, const GodleyIterator& end,
121  const VariableValues& values)
122  {
123  SharedColumnCheck scCheck;
124  sidx.resize(0);
125  fidx.resize(0);
126  m.resize(0);
127 
128  std::set<int> iidx;
129 
130  for (GodleyIterator g=begin; g!=end; ++g)
131  {
132  if (g.data().empty()) continue;
133  // check for shared columns
134  if (!compatibility)
135  for (std::size_t col=1; col<g.data()[0].size(); ++col)
136  if (!trimWS(g.data()[0][col]).empty())
137  scCheck.checkShared(g.valueId(trimWS(g.data()[0][col])), g.assetClass(col));
138 
139  for (std::size_t row=1; row<g.data().size(); ++row)
140  if (!g.initialConditionRow(row))
141  for (std::size_t col=1; col<g.data()[row].size(); ++col)
142  {
143  FlowCoef fvc(g.data()[row][col]);
144  auto svName=col<g.data()[0].size()? trimWS(g.data()[0][col]): "";
145  if (fvc.name.empty() || svName.empty()) continue;
146  fvc.name=g.valueId(fvc.name);
147  svName=g.valueId(svName);
148 
149  auto fv=values.find(fvc.name);
150  auto sv=values.find(svName);
151  if (fv!=values.end() && sv!=values.end() &&
152  fv->second->idx()>=0 && sv->second->idx()>=0)
153  {
154  // check for compatible column definitions
155  if (!compatibility &&
156  scCheck.updateColDefs(svName, fvc))
157  continue;
158 
159  iidx.insert(sv->second->idx());
160  sidx<<=sv->second->idx();
161  fidx<<=fv->second->idx();
162  m<<=fvc.coef;
163  }
164  }
165  }
166 
167  initIdx.clear();
168  for (auto i: iidx)
169  initIdx<<=i;
170 
171  if (!compatibility)
172  scCheck.checkSharedColDefs();
173  }
174 
175 }
176 
177 #include "evalGodley.cd"
178 #include "evalGodley.xcd"
179 #endif
bool operator!=(const GodleyIteratorAdaptor &x) const
Definition: evalGodley.h:73
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:38
std::map< string, AssetClass > colAssetType
asset type of previously seen column of this name
Definition: evalGodley.h:97
bool compatibility
if compatibility is true, then consttrainst between Godley tables is not applied, and shared columns ...
Definition: evalGodley.h:62
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:92
GodleyIteratorAdaptor< T > makeGodleyIt(const T &it)
Definition: evalGodley.h:88
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:120
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:94
std::set< string > sharedCol
indicates a column is shared between Godley tables
Definition: evalGodley.h:99
#define CLASSDESC_ACCESS(type)
string valueId(const std::string &x) const
returns valueid for variable reference in table
Definition: evalGodley.h:85
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:42
ecolab::array< double > m
Definition: evalGodley.h:39
GodleyIteratorAdaptor(const T &it)
Definition: evalGodley.h:71
adaptor suitable for initialiseGodleys
Definition: evalGodley.h:67
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:108