Minsky
schema2.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2017
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 #include "schema2.h"
20 #include "sheet.h"
21 #include "minsky_epilogue.h"
22 
23 
24 namespace schema2
25 {
26  void unpack(classdesc::pack_t& b, civita::XVector& a)
27  {
28  size_t size;
29  std::string x;
30  a.clear();
31  b>>a.name>>a.dimension>>size;
32  for (size_t i=0; i<size; ++i)
33  {
34  b>>x;
35  a.push_back(x);
36  }
37  }
38 
39  void unpack(classdesc::pack_t& b, civita::TensorVal& a)
40  {
41  vector<unsigned> dims; // ignored, because info carried with xvectors
42  vector<double> data;
43  b>>dims>>data;
44 
45  civita::Hypercube hc;
46  size_t sz;
47  b>>sz;
48  for (size_t i=0; i<sz; ++i)
49  {
50  civita::XVector xv;
51  unpack(b,xv);
52  hc.xvectors.push_back(xv);
53  }
54 
55  a.hypercube(hc); //dimension data
56  assert(a.size()==data.size());
57  memcpy(a.begin(),data.data(),data.size()*sizeof(data[0]));
58  }
59 
61  {
62  map<int,schema1::UnionLayout> layout;
63  Schema1Layout(const vector<shared_ptr<schema1::Layout>>& x) {
64  for (auto& i: x)
65  {
66  if (auto ul=dynamic_cast<schema1::UnionLayout*>(i.get()))
67  layout[i->id]=*ul;
68  else
69  {
70  // TODO: I don't think this code will ever be executed?
71  // serialise to XML, then deserialise to a UnionLayout
72  ostringstream is;
73  xml_pack_t xmlPackBuf(is);
74  i->xml_pack(xmlPackBuf,"");
75  istringstream os(is.str());
76  xml_unpack_t xmlUnpackBuf(os);
77  xml_unpack(xmlUnpackBuf,"",layout[i->id]);
78  }
79  }
80  }
81  template <class V, class O>
82  void addItem(V& vec, const O& item) {
83  vec.emplace_back(item);
84  if (layout.contains(item.id))
85  vec.back().addLayout(layout[item.id]);
86  }
87  };
88 
90  ItemBase(it, "GodleyIcon"),
91  name(it.name), data(it.data), assetClasses(it.assetClasses)
92  {
93  typedef minsky::GodleyAssetClass::AssetClass AssetClass;
94  ports=it.ports;
95  // strip off leading :, since in schema 1, all godley tables are global
96  for (auto& i: *data)
97  for (auto& j: i)
98  {
99  minsky::FlowCoef fc(j);
100  if (fc.coef==0)
101  j="";
102  else
103  {
104  if (fc.name.length()>0 && fc.name[0]==':')
105  fc.name=fc.name.substr(1);
106  j=fc.str();
107  }
108  }
109 
110  // in doubleEntry mode, schema1 got the accounting equation wrong.
111  if (it.doubleEntryCompliant)
112  {
113  for (size_t r=1; r<data->size(); ++r)
114  for (size_t c=1; c<(*data)[r].size(); ++c)
115  if (!(*data)[r][c].empty() &&
116  ((*assetClasses)[c]==AssetClass::liability || (*assetClasses)[c]==AssetClass::equity))
117  {
118  minsky::FlowCoef fc((*data)[r][c]);
119  fc.coef*=-1;
120  (*data)[r][c]=fc.str();
121  }
122  }
123  else
124  {
125  // make all columns assets
126  assetClasses->resize((*data)[0].size());
127  for (auto& i: *assetClasses) i=AssetClass::asset;
128  }
129  }
130 
132  {
133  // read in layout into a map indexed by id
134  Schema1Layout layout(m.layout);
135 
136  for (auto& i: m.model.wires)
137  layout.addItem(wires,i);
138  for (auto& i: m.model.notes)
139  layout.addItem(items,i);
140  for (auto& i: m.model.operations)
141  layout.addItem(items,i);
142 
143  // save a mapping of ports to variables for later use by group
144  map<int,pair<int,bool>> portToVar;
145  for (auto& i: m.model.variables)
146  {
147  layout.addItem(items,i);
148  for (size_t j=0; j<i.ports.size(); ++j)
149  portToVar[i.ports[j]]=make_pair(i.id,j>0);
150  }
151 
152  for (auto& i: m.model.plots)
153  layout.addItem(items,i);
154  for (auto& i: m.model.switches)
155  layout.addItem(items,i);
156 
157  for (auto& i: m.model.godleys)
158  {
159  layout.addItem(items,i);
160  // override any height specifcation with a legacy iconScale parameter
161  items.back().height.reset();
162  items.back().iconScale=i.zoomFactor/m.zoomFactor;
163  }
164  for (auto& i: m.model.groups)
165  {
166  layout.addItem(groups,i);
167  for (auto p: i.ports)
168  {
169  auto v=portToVar.find(p);
170  if (v!=portToVar.end())
171  {
172  if (v->second.second)
173  groups.back().inVariables->push_back(v->second.first);
174  else
175  groups.back().outVariables->push_back(v->second.first);
176  }
177  }
178  }
179 
182  }
183 
184 }
185 
186 
void xml_unpack(classdesc::xml_unpack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
vector< Plot > plots
Definition: schema1.h:330
vector< shared_ptr< Layout > > layout
Definition: schema1.h:342
void addItem(V &vec, const O &item)
Definition: schema2.cc:82
vector< Group > groups
Definition: schema2.h:203
std::string name
Definition: flowCoef.h:30
vector< Wire > wires
Definition: schema2.h:201
minsky::Simulation rungeKutta
Definition: schema1.h:334
std::string str() const
Definition: flowCoef.cc:81
Optional< std::vector< minsky::GodleyAssetClass::AssetClass > > assetClasses
Definition: schema2.h:112
vector< Godley > godleys
Definition: schema1.h:333
MinskyModel model
Definition: schema1.h:341
minsky::Simulation rungeKutta
Definition: schema2.h:204
bool doubleEntryCompliant
Definition: schema1.h:188
vector< Group > groups
Definition: schema1.h:331
vector< Operation > operations
Definition: schema1.h:328
map< int, schema1::UnionLayout > layout
Definition: schema2.cc:62
std::vector< int > ports
Definition: schema2.h:75
void unpack(classdesc::pack_t &b, civita::XVector &a)
Definition: schema2.cc:26
represents a numerical coefficient times a variable (a "flow")
Definition: flowCoef.h:27
vector< int > ports
Definition: schema1.h:187
vector< Item > items
Definition: schema2.h:202
vector< Variable > variables
Definition: schema1.h:329
vector< Item > notes
descriptive notes
Definition: schema1.h:327
vector< Wire > wires
Definition: schema1.h:326
vector< Switch > switches
Definition: schema1.h:332
Optional< std::vector< std::vector< std::string > > > data
Definition: schema2.h:111
double zoomFactor
Definition: schema1.h:343
double zoomFactor
Definition: schema2.h:205
Schema1Layout(const vector< shared_ptr< schema1::Layout >> &x)
Definition: schema2.cc:63