-
Notifications
You must be signed in to change notification settings - Fork 305
/
NBCausalize.mo
325 lines (290 loc) · 13.9 KB
/
NBCausalize.mo
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2020, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package NBCausalize
"file: NBCausalize.mo
package: NBCausalize
description: This file contains the functions which perform the causalization process;
"
public
import Module = NBModule;
protected
// NF imports
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import Expression = NFExpression;
import NFFlatten.{FunctionTree, FunctionTreeImpl};
import InstNode = NFInstNode.InstNode;
import Prefixes = NFPrefixes;
import Subscript = NFSubscript;
import Type = NFType;
import Variable = NFVariable;
import NFArrayConnections.NameVertexTable;
// Backend imports
import Adjacency = NBAdjacency;
import BackendDAE = NBackendDAE;
import BEquation = NBEquation;
import Differentiate = NBDifferentiate;
import NBEquation.{Equation, EquationPointers, EqData, EquationAttributes};
import Matching = NBMatching;
import Sorting = NBSorting;
import StrongComponent = NBStrongComponent;
import System = NBSystem;
import BVariable = NBVariable;
import NBVariable.{VariablePointers, VarData};
// util imports
import BackendUtil = NBBackendUtil;
import Error;
import List;
import StringUtil;
import UnorderedSet;
// ############################################################
// Main Functions
// ############################################################
public
function main extends Module.wrapper;
input System.SystemType systemType;
protected
Module.causalizeInterface func = getModule();
algorithm
bdae := match (systemType, bdae)
local
list<System.System> systems;
VarData varData;
EqData eqData;
FunctionTree funcTree;
case (System.SystemType.ODE, BackendDAE.MAIN(ode = systems, varData = varData, eqData = eqData, funcTree = funcTree))
algorithm
(systems, varData, eqData, funcTree) := applyModule(systems, systemType, varData, eqData, funcTree, func);
bdae.ode := systems;
bdae.varData := varData;
bdae.eqData := eqData;
bdae.funcTree := funcTree;
then bdae;
case (System.SystemType.INI, BackendDAE.MAIN(init = systems, varData = varData, eqData = eqData, funcTree = funcTree))
algorithm
if Flags.isSet(Flags.INITIALIZATION) then
print(StringUtil.headline_1("Balance Initialization") + "\n");
end if;
(systems, varData, eqData, funcTree) := applyModule(systems, systemType, varData, eqData, funcTree, func);
bdae.init := systems;
if Util.isSome(bdae.init_0) then
(systems, varData, eqData, funcTree) := applyModule(Util.getOption(bdae.init_0), systemType, varData, eqData, funcTree, func);
bdae.init_0 := SOME(systems);
end if;
bdae.varData := varData;
bdae.eqData := eqData;
bdae.funcTree := funcTree;
then bdae;
case (System.SystemType.DAE, BackendDAE.MAIN(dae = SOME(systems), varData = varData, eqData = eqData, funcTree = funcTree))
algorithm
(systems, varData, eqData, funcTree) := applyModule(systems, systemType, varData, eqData, funcTree, causalizeDAEMode);
bdae.dae := SOME(systems);
bdae.varData := varData;
bdae.eqData := eqData;
bdae.funcTree := funcTree;
then bdae;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed with system type " + System.System.systemTypeString(systemType) + "!"});
then fail();
end match;
end main;
function applyModule
input list<System.System> systems;
input System.SystemType systemType;
output list<System.System> new_systems = {};
input output VarData varData;
input output EqData eqData;
input output FunctionTree funcTree;
input Module.causalizeInterface func;
protected
System.System new_system;
Boolean violated = false "true if any system violated variability consistency";
algorithm
for system in systems loop
(new_system, varData, eqData, funcTree) := func(system, varData, eqData, funcTree);
new_systems := new_system :: new_systems;
end for;
new_systems := listReverse(new_systems);
if systemType <> System.SystemType.INI then
for system in new_systems loop
violated := checkSystemVariabilities(system) or violated;
end for;
if violated then fail(); end if;
end if;
end applyModule;
function checkSystemVariabilities
"checks whether variability is valid. Prevents things like `Integer i = time;`"
input System.System system;
output Boolean violated = false;
algorithm
if isSome(system.strongComponents) then
for scc in Util.getOption(system.strongComponents) loop
() := match scc
local
Type ty1, ty2;
case StrongComponent.SINGLE_COMPONENT() algorithm
ty1 := Type.removeSizeOneArrays(Variable.typeOf(Pointer.access(scc.var)));
ty2 := Type.removeSizeOneArrays(Equation.getType(Pointer.access(scc.eqn)));
if not Type.isEqual(ty1, ty2) then
// The variability of the equation must be greater or equal to that of the variable it solves.
// See MLS section 3.8 Variability of Expressions
Error.addMessage(Error.COMPILER_ERROR, {getInstanceName() + " failed. The following strong component has conflicting types: "
+ Type.toString(ty1) + " != " + Type.toString(ty2)
+ "\n" + StrongComponent.toString(scc)});
violated := true;
end if;
then ();
/* TODO case StrongComponent.MULTI_COMPONENT() */
else ();
end match;
end for;
end if;
end checkSystemVariabilities;
function simple
input VariablePointers vars;
input EquationPointers eqns;
input Adjacency.MatrixStrictness st = NBAdjacency.MatrixStrictness.MATCHING;
output Matching matching;
output list<StrongComponent> comps;
protected
Adjacency.Matrix full, adj;
algorithm
// create full matrix
full := Adjacency.Matrix.createFull(vars, eqns);
// create solvable adjacency matrix for matching
adj := Adjacency.Matrix.fromFull(full, vars.map, eqns.map, eqns, st);
matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj);
// create all occurence adjacency matrix for sorting, upgrading the matching matrix
adj := Adjacency.Matrix.upgrade(adj, full, vars.map, eqns.map, eqns, NBAdjacency.MatrixStrictness.SORTING);
comps := Sorting.tarjan(adj, matching, vars, eqns);
end simple;
function getModule
"Returns the module function that was chosen by the user."
output Module.causalizeInterface func;
protected
String flag = Flags.getConfigString(Flags.MATCHING_ALGORITHM);
algorithm
(func) := match flag
case "PFPlusExt" then causalizePseudoArray;
case "pseudo" then causalizePseudoArray;
/* ... New causalize modules have to be added here */
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for unknown option: " + flag});
then fail();
end match;
end getModule;
// ############################################################
// Protected Functions and Types
// ############################################################
protected
function causalizePseudoArray extends Module.causalizeInterface;
protected
VariablePointers variables;
EquationPointers equations;
Adjacency.Matrix full, adj_matching, adj_sorting;
Matching matching;
list<StrongComponent> comps;
algorithm
(variables, equations, full, matching, comps) := match system.systemType
local
list<Pointer<Variable>> fixable, unfixable;
list<Pointer<Equation>> initials, simulation;
UnorderedMap<ComponentRef, Integer> vo, vn, eo, en;
case NBSystem.SystemType.INI algorithm
// compress the arrays to remove gaps
system.unknowns := VariablePointers.compress(system.unknowns);
system.equations := EquationPointers.compress(system.equations);
// split the variables and equations
(fixable, unfixable) := List.splitOnTrue(VariablePointers.toList(system.unknowns), BVariable.isFixable);
(initials, simulation) := List.splitOnTrue(EquationPointers.toList(system.equations), Equation.isInitial);
// create full matrix
full := Adjacency.Matrix.createFull(system.unknowns, system.equations);
// do not resolve potential singular systems in Phase I or II! -> regular matching
// #################################################
// Phase I: match initial equations <-> unfixable vars
// #################################################
vn := UnorderedMap.subMap(system.unknowns.map, list(BVariable.getVarName(var) for var in unfixable));
en := UnorderedMap.subMap(system.equations.map, list(Equation.getEqnName(eqn) for eqn in initials));
adj_matching := Adjacency.Matrix.fromFull(full, vn, en, system.equations, NBAdjacency.MatrixStrictness.MATCHING);
matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj_matching, true, true);
// #################################################
// Phase II: match all equations <-> unfixables
// #################################################
vo := vn;
eo := en;
vn := UnorderedMap.new<Integer>(ComponentRef.hash, ComponentRef.isEqual);
en := UnorderedMap.subMap(system.equations.map, list(Equation.getEqnName(eqn) for eqn in simulation));
(adj_matching, full) := Adjacency.Matrix.expand(adj_matching, full, vo, vn, eo, en, system.unknowns, system.equations);
matching := Matching.regular(matching, adj_matching, true, true);
// #################################################
// Phase III: match all equations <-> all vars
// #################################################
vo := UnorderedMap.merge(vo, vn, sourceInfo());
eo := UnorderedMap.merge(eo, en, sourceInfo());
vn := UnorderedMap.subMap(system.unknowns.map, list(BVariable.getVarName(var) for var in fixable));
en := UnorderedMap.new<Integer>(ComponentRef.hash, ComponentRef.isEqual);
(adj_matching, full) := Adjacency.Matrix.expand(adj_matching, full, vo, vn, eo, en, system.unknowns, system.equations);
(matching, adj_matching, full, variables, equations, funcTree, varData, eqData) := Matching.singular(matching, adj_matching, full, system.unknowns, system.equations, funcTree, varData, eqData, system.systemType, false, true, false);
// create all occurence adjacency matrix for sorting, upgrading the matching matrix
adj_sorting := Adjacency.Matrix.upgrade(adj_matching, full, variables.map, equations.map, equations, NBAdjacency.MatrixStrictness.SORTING);
comps := Sorting.tarjan(adj_sorting, matching, variables, equations);
then (variables, equations, full, matching, comps);
else algorithm
// compress the arrays to remove gaps
variables := VariablePointers.compress(system.unknowns);
equations := EquationPointers.compress(system.equations);
// create full matrix
full := Adjacency.Matrix.createFull(variables, equations);
// create solvable adjacency matrix for matching
adj_matching := Adjacency.Matrix.fromFull(full, variables.map, equations.map, equations, NBAdjacency.MatrixStrictness.MATCHING);
(matching, adj_matching, full, variables, equations, funcTree, varData, eqData) := Matching.singular(NBMatching.EMPTY_MATCHING, adj_matching, full, variables, equations, funcTree, varData, eqData, system.systemType, false, true);
// create all occurence adjacency matrix for sorting, upgrading the matching matrix
adj_sorting := Adjacency.Matrix.upgrade(adj_matching, full, variables.map, equations.map, equations, NBAdjacency.MatrixStrictness.SORTING);
comps := Sorting.tarjan(adj_sorting, matching, variables, equations);
then (variables, equations, full, matching, comps);
end match;
system.unknowns := variables;
system.equations := equations;
system.adjacencyMatrix := SOME(full);
system.matching := SOME(matching);
system.strongComponents := SOME(listArray(comps));
end causalizePseudoArray;
function causalizeDAEMode extends Module.causalizeInterface;
protected
Pointer<list<StrongComponent>> acc = Pointer.create({});
algorithm
// create all components as residuals for now
// ToDo: use tearing to get inner/tmp equations
EquationPointers.mapPtr(system.equations, function StrongComponent.makeDAEModeResidualTraverse(acc = acc));
system.strongComponents := SOME(List.listArrayReverse(Pointer.access(acc)));
end causalizeDAEMode;
annotation(__OpenModelica_Interface="backend");
end NBCausalize;