-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathschema1.cc
More file actions
180 lines (158 loc) · 5.6 KB
/
schema1.cc
File metadata and controls
180 lines (158 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
@copyright Steve Keen 2012
@author Russell Standish
This file is part of Minsky.
Minsky is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Minsky is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Minsky. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cairoItems.h"
#include "schema1.h"
#include "schemaHelper.h"
#include "factory.h"
#include "str.h"
#include "minsky_epilogue.h"
using namespace ecolab;
namespace schema1
{
const int Minsky::version;
namespace
{
struct IsOrphan
{
set<int> ids;
void insert(int id) {ids.insert(id);}
bool operator()(const Variable& v) const {
return ids.contains(v.id);
}
bool operator()(const shared_ptr<Layout>& l) const {
return l && ids.contains(l->id);
}
};
}
void Minsky::removeIntVarOrphans()
{
set<string> intNames;
for (vector<Operation>::const_iterator o=model.operations.begin();
o!=model.operations.end(); ++o)
if (o->type==minsky::OperationType::integrate)
intNames.insert(o->name);
IsOrphan isOrphan;
for (vector<Variable>::const_iterator v=model.variables.begin();
v!=model.variables.end(); ++v)
// an orphaned variable is an integral variable not attached to an integral and without
if (v->type==minsky::VariableType::integral && intNames.contains(v->name)==0)
isOrphan.insert(v->id);
model.variables.erase
(remove_if(model.variables.begin(), model.variables.end(), isOrphan),
model.variables.end());
layout.erase
(remove_if(layout.begin(), layout.end(), isOrphan), layout.end());
}
Minsky::Minsky(const schema0::Minsky& m)
{
// uniquify all IDs
int nextId=0;
map<int,int> newPortIds;
map<int,int> newOpIds;
map<int,int> newVarIds;
for (auto& i: m.ports)
newPortIds[i.first]=nextId++;
for (auto& i: m.wires)
{
model.wires.emplace_back(nextId, newPortIds[i.second.from], newPortIds[i.second.to]);
layout.emplace_back(new WireLayout(nextId++, i.second));
}
for (auto v: m.variables) //NOLINT - index var mutated in loop body
{
newVarIds[v.first]=nextId;
v.second.m_outPort=newPortIds[v.second.m_outPort];
if (v.second.m_inPort>=0)
v.second.m_inPort=newPortIds[v.second.m_inPort];
// values init field overrides that of the variable's
auto value=m.variables.values.find(v.second.name);
if (value!=m.variables.values.end())
v.second.init=value->second.init;
model.variables.emplace_back(nextId, v.second);
layout.emplace_back(new ItemLayout(nextId++, v.second));
}
for (auto i: m.operations)
{
newOpIds[i.first]=nextId;
for (auto& j: i.second.m_ports)
j=newPortIds[j];
if (i.second.intVar>-1) i.second.intVar=newVarIds[i.second.intVar];
model.operations.emplace_back(nextId, i.second);
layout.emplace_back(new ItemLayout(nextId++, i.second));
}
for (auto i: m.plots.plots)
{
for (auto& j: i.second.ports)
j=newPortIds[j];
// insert another 6 ports for axis ranges that weren't there in schema 0
// move first 4 ports (x ports) to end, and add anothe 4 x ports
i.second.ports=(pcoord(6)+nextId)<<
i.second.ports[pcoord(8)+4]<<
i.second.ports[pcoord(4)]<<
(pcoord(4)+nextId+6);
nextId+=10;
model.plots.emplace_back(nextId, i.second);
layout.emplace_back(new PlotLayout(nextId++, i.second));
}
for (auto g: m.groupItems)
{
for (auto& i: g.second.operations)
i=newOpIds[i];
for (auto& i: g.second.variables)
i=newVarIds[i];
for (auto& j: g.second.m_ports)
j=newPortIds[j];
// wires not used in new schema, no need to renumber
model.groups.emplace_back(nextId, g.second);
layout.emplace_back(new GroupLayout(nextId++, g.second));
}
for (auto g: m.godleyItems)
{
// need to renumber ports in variables
for (auto& v: g.second.flowVars)
v.m_inPort=newPortIds[v.m_inPort];
for (auto& v: g.second.stockVars)
v.m_outPort=newPortIds[v.m_outPort];
model.godleys.emplace_back(nextId, g.second);
layout.emplace_back(new PositionLayout(nextId++, g.second.x, g.second.y));
}
model.rungeKutta.stepMin=m.stepMin;
model.rungeKutta.stepMax=m.stepMax;
model.rungeKutta.nSteps=m.nSteps;
model.rungeKutta.epsAbs=m.epsAbs;
model.rungeKutta.epsRel=m.epsRel;
removeIntVarOrphans();
}
}
namespace classdesc
{
template<> Factory<schema1::Item,string>::Factory() {}
template<> Factory<schema1::Layout,string>::Factory()
{enumerateRegisterLayout(*this);}
}
namespace schema1
{
using classdesc::Factory;
struct ItemFactory: public Factory<schema1::Item,string>
{
ItemFactory() {enumerateRegisterItems(*this);}
using Factory<schema1::Item,string>::registerType;
// define our own registerType that avoids calling type()
template <class T> void registerType()
{registerType<T>(classdesc::typeName<T>());}
};
ItemFactory itemFactory;
Factory<schema1::Layout,string> factoryForLayout;
}