-
Notifications
You must be signed in to change notification settings - Fork 296
/
NBModule.mo
297 lines (264 loc) · 12.8 KB
/
NBModule.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
/*
* 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 NBModule
" file: NBModule.mo
package: NBModule
description: This file contains all functions and structures regarding
generic backend modules and interfaces.
This file contains following module wrappers:
*** PRE (Mandatory)
- eventsInterface
- detectStatesInterface
- detectContinuousStatesInterface
- detectDiscreteStatesInterface
*** PRE (Optional)
- aliasInterface
*** MAIN
- partitioningInterface
- causalizeInterface
- daeModeInterface
*** POST (Mandatory)
- jacobianInterface
*** POST (Optional)
- tearingInterface
"
public
import BackendDAE = NBackendDAE;
protected
// OF imports
import DAE;
// NF imports
import NFFlatten.FunctionTree;
// Backend imports
import Adjacency = NBAdjacency;
import BEquation = NBEquation;
import NBEquation.{Equation, EquationPointers, EqData};
import Jacobian = NBackendDAE;
import NBJacobian.JacobianType;
import StrongComponent = NBStrongComponent;
import System = NBSystem;
import BVariable = NBVariable;
import NBVariable.{VariablePointers, VarData};
import NBEvents.EventInfo;
import Inline = NBInline;
// Util imports
import BuiltinSystem = System;
import StringUtil;
public
partial function wrapper
input output BackendDAE bdae;
end wrapper;
function moduleClockString
input tuple<String, Real> name_clock;
output String str;
protected
String name;
Real clck;
algorithm
(name, clck) := name_clock;
str := "\t" + name + StringUtil.repeat(".", 50 - stringLength(name)) + BuiltinSystem.sprintff("%.4g", clck);
end moduleClockString;
// =========================================================================
// MAIN MODULES
// =========================================================================
// PARTITIONING
// *************************************************************************
partial function partitioningInterface
"Partitioning
This function is only allowed to create systems of specialized SystemType
by creating an adjacency matrix using provided variables and equations."
input System.SystemType systemType;
input VariablePointers variables;
input EquationPointers equations;
output list<System.System> systems;
end partitioningInterface;
// CAUSALIZE
// *************************************************************************
partial function causalizeInterface
"Causalize
This function is allowed to add variables, equations and manipulate the
function tree (index reduction)."
input output System.System system;
input output VarData varData;
input output EqData eqData;
input output FunctionTree funcTree;
end causalizeInterface;
// RESOLVING SINGULARITIES
// Index Reduction + Balance Initialization
// *************************************************************************
partial function resolveSingularitiesInterface
input output VariablePointers variables;
input output EquationPointers equations;
input output VarData varData;
input output EqData eqData;
input output FunctionTree funcTree;
input Option<Adjacency.Mapping> mapping_opt;
input Adjacency.MatrixType matrixType;
output Boolean changed;
end resolveSingularitiesInterface;
// DAEMODE
// *************************************************************************
partial function daeModeInterface
"DAEMode
This function is only allowed to create a list of new systems for dae Mode."
input output list<System.System> systems;
end daeModeInterface;
// =========================================================================
// MANDATORY PRE-OPT MODULES
// =========================================================================
// COLLECT EVENTS
// *************************************************************************
partial function eventsInterface
"Events
This function is only allowed to read and change equations and create new
discrete zero crossing equations and variables. ($TEV, $SEV)
It also fills the EventInfo object."
input output VarData varData "Data containing variable pointers";
input output EqData eqData "Data containing equation pointers";
input output EventInfo eventInfo "object containing all zero crossings";
input FunctionTree funcTree "function tree for differentiation (solve)";
end eventsInterface;
// DETECT STATES
// *************************************************************************
partial function detectStatesInterface
"DetectStates
This function is only allowed to read and change equations, change algebraic
variables to states and create state derivatives. It also detects der() and
pre() calls and replaces them with $DER and $PRE.
Sub-Modules:
- DetectContinuousStates
- DetectDiscreteStates"
input output VarData varData "Data containing variable pointers";
input output EqData eqData "Data containing equation pointers";
input detectContinuousStatesInterface continuousFunc "Subroutine for continuous states";
input detectDiscreteStatesInterface discreteFunc "Subroutine for discrete states";
end detectStatesInterface;
partial function detectContinuousStatesInterface
"DetectContinuousStates
This function is only allowed to read and change equations, change algebraic
variables to states and create state derivatives."
input output VariablePointers variables "All variables";
input output VariablePointers unknowns "Unknowns";
input output VariablePointers knowns "Knowns";
input output VariablePointers initials "Initial unknowns";
input output VariablePointers states "States";
input output VariablePointers derivatives "State derivatives (der(x) -> $DER.x)";
input output VariablePointers algebraics "Algebraic variables";
input EquationPointers equations "System equations";
output list<Pointer<Equation>> aux_eqns "New auxiliary equations";
end detectContinuousStatesInterface;
partial function detectDiscreteStatesInterface
"DetectDiscreteStates
This function is only allowed to read and change equations, change algebraic
variables to discrete and create previous discrete variables."
input output VariablePointers variables "All variables";
input output EquationPointers equations "ONLY discrete or initial equations!";
input output VariablePointers knowns "Knowns";
input output VariablePointers initials "Initial unknowns";
input output VariablePointers discretes "Discrete variables";
input output VariablePointers discrete_states "Discrete variables";
input output VariablePointers previous "Previous discrete variables (pre(d) -> $PRE.d)";
input String context "only for debugging";
end detectDiscreteStatesInterface;
// =========================================================================
// Optional PRE-OPT MODULES
// =========================================================================
// ALIAS
// *************************************************************************
partial function functionAliasInterface
"Alias
This module is allowed to read and remove equations and move variables from
unknowns to knowns. Since this can also affect all other pointer arrays, the
full variable data is needed. All things that are allowed to be changed
are pointers, so no return value."
input output VarData varData "Data containing variable pointers";
input output EqData eqData "Data containing equation pointers";
end functionAliasInterface;
partial function aliasInterface
"Alias
This module is allowed to read and remove equations and move variables from
unknowns to knowns. Since this can also affect all other pointer arrays, the
full variable data is needed. All things that are allowed to be changed
are pointers, so no return value."
input output VarData varData "Data containing variable pointers";
input output EqData eqData "Data containing equation pointers";
end aliasInterface;
// INLINE
// *************************************************************************
partial function inlineInterface
"Inline
This module is allowed to read, change and add equations. It uses the
function tree to evaluate and inline functions."
input output EqData eqData "Data containing equation pointers";
input VarData varData "Data containing equation pointers, for lowering purposes";
input FunctionTree funcTree "function tree for differentiation (solve)";
input list<DAE.InlineType> inline_types "Inline types for which to inline at the current state";
end inlineInterface;
// =========================================================================
// MANDATORY POST-OPT MODULES
// =========================================================================
// JACOBIAN
// *************************************************************************
partial function jacobianInterface
"The jacobian is only allowed to read the variables and equations of current
system and additionally the global known variables. It needs a unique name
and is allowed to manipulate the function tree.
[!] This function can not only be used as an optimization module but also for
nonlinear systems, state sets, linearization and dynamic optimization."
input String name "Name of jacobian";
input JacobianType jacType "Type of jacobian (sim/nonlin)";
input VariablePointers seedCandidates "differentiate by these";
input VariablePointers partialCandidates "solve the equations for these";
input EquationPointers equations "Equations array";
input VariablePointers knowns "Variable array of knowns";
input Option<array<StrongComponent>> strongComponents "Strong Components";
output Option<Jacobian> jacobian "Resulting jacobian";
input output FunctionTree funcTree "Function call bodies";
end jacobianInterface;
// =========================================================================
// Optional POST-OPT MODULES
// =========================================================================
// TEARING
// *************************************************************************
partial function tearingInterface
"Tearing
The tearing module analyzes each strong component and applies tearing if
necessary. Only has access to the strong component itself, everything else
accessable with pointers."
input output StrongComponent comp "the suspected algebraic loop.";
input output FunctionTree funcTree "Function call bodies";
input output Integer index "current unique loop index";
input VariablePointers variables "all variables";
input Pointer<Integer> eq_index "equation index";
input System.SystemType systemType = NBSystem.SystemType.ODE "system type";
end tearingInterface;
annotation(__OpenModelica_Interface="backend");
end NBModule;