-
Notifications
You must be signed in to change notification settings - Fork 297
/
NBSystem.mo
342 lines (310 loc) · 11.6 KB
/
NBSystem.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* 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 NBSystem
"file: NBSystem.mo
package: NBSystem
description: This file contains the data-types used to process individual
systems of equations.
"
public
import Adjacency = NBAdjacency;
import Matching = NBMatching;
import StrongComponent = NBStrongComponent;
protected
// NF imports
import Variable = NFVariable;
import Expression = NFExpression;
// Backend Imports
import BackendDAE = NBackendDAE;
import BEquation = NBEquation;
import BJacobian = NBJacobian;
import NBEquation.EquationPointers;
import Jacobian = NBackendDAE.BackendDAE;
import BVariable = NBVariable;
import NBVariable.VariablePointers;
// Util imports
import DoubleEnded;
import StringUtil;
public
// ToDo: Expand with Jacobian and Hessian later on
type SystemType = enumeration(ODE, ALG, ODE_EVT, ALG_EVT, INI, DAE, JAC);
type PartitionKind = enumeration(UNKNOWN, UNSPECIFIED, CLOCKED, CONTINUOUS);
uniontype System
record SYSTEM
SystemType systemType "Type of system";
VariablePointers unknowns "Variable array of unknowns, subset of full variable array";
Option<VariablePointers> daeUnknowns "Variable array of unknowns in the case of dae mode";
EquationPointers equations "Equations array, subset of the full equation array";
Option<Adjacency.Matrix> adjacencyMatrix "Adjacency matrix with all additional information";
Option<Matching> matching "Matching (see 2.5)";
Option<array<StrongComponent>> strongComponents "Strong Components";
PartitionKind partitionKind "Clocked/Continuous partition kind";
Integer partitionIndex "For clocked partitions";
Option<Jacobian> jacobian "Analytic jacobian for the integrator";
end SYSTEM;
function toString
input System system;
input Integer level = 0;
output String str;
algorithm
str := StringUtil.headline_2(partitionKindString(system.partitionKind) + " " + intString(system.partitionIndex) + " " + systemTypeString(system.systemType) + " System") + "\n";
str := match system.strongComponents
local
array<StrongComponent> comps;
case SOME(comps)
algorithm
for i in 1:arrayLength(comps) loop
str := str + StrongComponent.toString(comps[i], i) + "\n";
end for;
then str;
else
algorithm
str := str + VariablePointers.toString(system.unknowns, "Unknown") + "\n" +
EquationPointers.toString(system.equations, "Equations") + "\n";
then str;
end match;
if level == 1 or level == 3 then
if isSome(system.adjacencyMatrix) then
str := str + Adjacency.Matrix.toString(Util.getOption(system.adjacencyMatrix)) + "\n";
end if;
if isSome(system.matching) then
str := str + Matching.toString(Util.getOption(system.matching)) + "\n";
end if;
end if;
if level == 2 then
if isSome(system.jacobian) then
str := str + BJacobian.toString(Util.getOption(system.jacobian), "SIM") + "\n";
else
str := str + StringUtil.headline_2("NO JACOBIAN") + "\n";
end if;
end if;
end toString;
function toStringList
input list<System> systems;
input String header = "";
output String str = "";
algorithm
if not listEmpty(systems) then
if header <> "" then
str := StringUtil.headline_1(header) + "\n";
end if;
for syst in systems loop
str := str + System.toString(syst);
end for;
end if;
end toStringList;
function sort
input output System system;
algorithm
system.unknowns := VariablePointers.sort(system.unknowns);
system.equations := EquationPointers.sort(system.equations);
end sort;
function isEmpty
"returns true if the system is empty.
maybe check more than only equations?"
input System system;
output Boolean b = EquationPointers.size(system.equations) == 0;
end isEmpty;
function isAlgebraicContinuous
input System syst;
output Boolean alg = true;
output Boolean con = true;
algorithm
for var in VariablePointers.toList(syst.unknowns) loop
alg := if alg then not BVariable.isStateDerivative(var) else false;
con := if con then not BVariable.isDiscrete(var) else false;
// stop searching if both
if not (alg or con) then
break;
end if;
end for;
end isAlgebraicContinuous;
function categorize
input System system;
input DoubleEnded.MutableList<System> ode;
input DoubleEnded.MutableList<System> alg;
input DoubleEnded.MutableList<System> ode_evt;
input DoubleEnded.MutableList<System> alg_evt;
protected
Boolean algebraic, continuous;
System cont_syst, disc_syst;
algorithm
(algebraic, continuous) := isAlgebraicContinuous(system);
() := match (algebraic, continuous)
case (true, true) algorithm
// algebraic continuous
system.systemType := SystemType.ALG;
DoubleEnded.push_back(alg, system);
then ();
case (false, true) algorithm
// differential continuous
system.systemType := SystemType.ODE;
DoubleEnded.push_back(ode, system);
then ();
case (true, false) algorithm
// algebraic discrete
system.systemType := SystemType.ALG_EVT;
DoubleEnded.push_back(alg_evt, system);
then ();
case (false, false) algorithm
// differential discrete
system.systemType := SystemType.ODE_EVT;
DoubleEnded.push_back(ode_evt, system);
then ();
else fail();
end match;
end categorize;
function getLoopResiduals
input System syst;
output list<Pointer<Variable>> residuals = {};
algorithm
if Util.isSome(syst.strongComponents) then
for comp in Util.getOption(syst.strongComponents) loop
residuals := listAppend(StrongComponent.getLoopResiduals(comp), residuals);
end for;
end if;
end getLoopResiduals;
function mapEqn
input output System system;
input MapFunc func;
partial function MapFunc
input output BEquation.Equation e;
end MapFunc;
algorithm
system.equations := EquationPointers.map(system.equations, func);
end mapEqn;
function mapExp
input output System system;
input MapFunc func;
partial function MapFunc
input output Expression e;
end MapFunc;
algorithm
system.equations := EquationPointers.mapExp(system.equations, func);
end mapExp;
function mapStrongComponents
input output System system;
input MapFunc func;
partial function MapFunc
input output StrongComponent comp;
end MapFunc;
protected
array<StrongComponent> comps;
algorithm
if Util.isSome(system.strongComponents) then
SOME(comps) := system.strongComponents;
for i in 1:arrayLength(comps) loop
comps[i] := func(comps[i]);
end for;
system.strongComponents := SOME(comps);
end if;
end mapStrongComponents;
function systemTypeString
input SystemType systemType;
output String str = "";
algorithm
str := match systemType
case SystemType.ODE then "ODE";
case SystemType.ALG then "ALG";
case SystemType.ODE_EVT then "ODE_EVT";
case SystemType.ALG_EVT then "ALG_EVT";
case SystemType.INI then "INI";
case SystemType.DAE then "DAE";
case SystemType.JAC then "JAC";
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed. Unknown system type in match."});
then fail();
end match;
end systemTypeString;
function systemTypeInteger
input SystemType systemType;
output Integer i;
algorithm
i := match systemType
case SystemType.ODE then 0;
case SystemType.ALG then 1;
case SystemType.ODE_EVT then 2;
case SystemType.ALG_EVT then 3;
case SystemType.INI then 4;
case SystemType.DAE then 5;
case SystemType.JAC then 6;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed. Unknown system type in match."});
then fail();
end match;
end systemTypeInteger;
function clone
"only clones equations."
input output System sys;
input Boolean shallow = true;
algorithm
sys.equations := EquationPointers.clone(sys.equations, shallow);
// these are partially pointer based and have to be recomputed if not shallow
if not shallow then
sys.adjacencyMatrix := NONE();
sys.matching := NONE();
sys.strongComponents := NONE();
sys.jacobian := NONE();
end if;
end clone;
function removeAlias
"removes alias strong components and replaces it with their original strong components.
used before differentiating for jacobians."
input output System sys;
protected
array<StrongComponent> comps;
algorithm
if Util.isSome(sys.strongComponents) then
// no need to override comps afterwards since arrays are mutable
comps := Util.getOption(sys.strongComponents);
for i in 1:arrayLength(comps) loop
comps[i] := StrongComponent.removeAlias(comps[i]);
end for;
end if;
end removeAlias;
protected
function partitionKindString
input PartitionKind partitionKind;
output String str;
algorithm
str := match partitionKind
case PartitionKind.UNKNOWN then "UNKNOWN";
case PartitionKind.UNSPECIFIED then "UNSPECIFIED";
case PartitionKind.CLOCKED then "CLOCKED";
case PartitionKind.CONTINUOUS then "CONTINUOUS";
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed. Unknown partition kind in match."});
then fail();
end match;
end partitionKindString;
end System;
annotation(__OpenModelica_Interface="backend");
end NBSystem;