/
SymbolicImplicitSolver.mo
325 lines (281 loc) · 12 KB
/
SymbolicImplicitSolver.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-2014, 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 SymbolicImplicitSolver
" file: SymbolicImplicitSolver.mo
package: SymbolicImplicitSolver
description: SymbolicImplicitSolver: der(x) is replaced with difference quotient so more symbolic
optimization is possible. After removeSimpleEquation, before tearing.
Original system is not changed, new system is stored in shared.InlineSystems.
Flag --symSolver is needed
"
public import BackendDAE;
protected
import BackendDAEUtil;
import BackendDump;
import BackendEquation;
import BackendVariable;
import ComponentReference;
import Expression;
import ExpressionDump;
import Flags;
import List;
public function symSolver
input BackendDAE.BackendDAE inDAE;
output Option<BackendDAE.InlineData> inlineData;
algorithm
//print("*********************inDAE*********************");
//List.map_0(inDAE.eqs, BackendDump.printEqSystem);
//BackendDump.printBackendDAE(inDAE);
// generate inline solver
if Flags.getConfigEnum(Flags.SYM_SOLVER)>0 then
inlineData := SOME(symSolverWork(inDAE));
else
inlineData := NONE();
end if;
//print("*********************outDAE*********************");
//List.map_0(outDAE.eqs, BackendDump.printEqSystem);
//BackendDump.printBackendDAE(outDAE);
end symSolver;
protected function symSolverWork
input BackendDAE.BackendDAE inDAE;
output BackendDAE.InlineData inlineData;
protected
list<BackendDAE.EqSystem> osystlst = {};
BackendDAE.EqSystem syst_;
BackendDAE.Shared shared;
BackendDAE.Var tmpv;
DAE.ComponentRef cref;
BackendDAE.Shared sharedIn;
BackendDAE.EqSystems localInline;
BackendDAE.Variables knownVariables, saveKnGlobalVars;
BackendDAE.BackendDAE inlineBDAE;
Boolean execbool;
algorithm
// create InlineSolverData
// copy EqSystem in shared.inlineSystems, so original system is not changed
localInline := BackendDAEUtil.copyEqSystems(inDAE.eqs);
// create empty known inline variables
knownVariables := BackendVariable.emptyVars(BackendDAEUtil.daeSize(inDAE));
inlineData := BackendDAE.INLINE_DATA(localInline, knownVariables);
// make dt
cref := ComponentReference.makeCrefIdent(BackendDAE.symSolverDT, DAE.T_REAL_DEFAULT, {});
tmpv := BackendVariable.makeVar(cref);
//tmpv := BackendVariable.setVarKind(tmpv, BackendDAE.PARAM());
tmpv := BackendVariable.setBindExp(tmpv, SOME(DAE.RCONST(0.0)));
inlineData.knownVariables := BackendVariable.addVars({tmpv}, inlineData.knownVariables);
// call symSolverUpdateSyst for every equation system in localInline
knownVariables := inlineData.knownVariables;
for syst in inlineData.inlineSystems loop
(syst_, knownVariables) := symSolverUpdateSyst(syst, knownVariables);
// add every result equation system to osystlst (list of equation systems)
osystlst := syst_ :: osystlst;
end for;
inlineData.knownVariables := knownVariables;
shared := inDAE.shared;
// push known variables from shared to local known variables to provide full shared object
saveKnGlobalVars := shared.globalKnownVars;
knownVariables := BackendVariable.addVariables(shared.globalKnownVars, knownVariables);
shared.globalKnownVars := knownVariables;
//set backenddae type inline in shared
shared.backendDAEType := BackendDAE.INLINESYSTEM();
inlineBDAE := BackendDAE.DAE(osystlst, shared);
execbool := Flags.disableDebug(Flags.EXEC_STAT);
if Flags.isSet(Flags.DUMP_INLINE_SOLVER) then
BackendDump.bltdump("Generated inline system:",inlineBDAE);
end if;
inlineBDAE := BackendDAEUtil.getSolvedSystemforJacobians(inlineBDAE,
{"removeEqualRHS",
"removeSimpleEquations",
"evalFunc"},
NONE(),
NONE(),
{
//"wrapFunctionCalls",
"inlineArrayEqn",
"constantLinearSystem",
"solveSimpleEquations",
"tearingSystem",
"calculateStrongComponentJacobians",
"removeConstants",
"simplifyTimeIndepFuncCalls"});
_ := Flags.set(Flags.EXEC_STAT, execbool);
if Flags.isSet(Flags.DUMP_INLINE_SOLVER) then
BackendDump.bltdump("Final inline systems:", inlineBDAE);
end if;
if (Flags.isSet(Flags.DUMP_BACKENDDAE_INFO) or Flags.isSet(Flags.DUMP_STATESELECTION_INFO) or Flags.isSet(Flags.DUMP_DISCRETEVARS_INFO)) then
BackendDump.dumpCompShort(inlineBDAE);
end if;
BackendDAE.DAE(localInline, _) := inlineBDAE;
inlineData.inlineSystems := localInline;
shared.globalKnownVars := saveKnGlobalVars;
end symSolverWork;
protected function symSolverUpdateSyst
input BackendDAE.EqSystem iSyst;
input BackendDAE.Variables inKnVars;
output BackendDAE.EqSystem oSyst;
output BackendDAE.Variables oKnVars = inKnVars;
protected
array<Option<BackendDAE.Equation>> equOptArr;
BackendDAE.Equation eqn;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<DAE.ComponentRef> crlst;
algorithm
oSyst := match iSyst
local
BackendDAE.EqSystem syst;
case syst as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns)
algorithm
crlst := {};
// for every equation in the input equation system
for i in 1:ExpandableArray.getLastUsedIndex(eqns) loop
if ExpandableArray.occupied(i, eqns) then
eqn := ExpandableArray.get(i, eqns);
// traverse all expression of the equation and replace der(x)
(eqn, (crlst, _)) := BackendEquation.traverseExpsOfEquation(eqn, symSolverUpdateEqn, (crlst, syst.orderedVars));
ExpandableArray.update(i, eqn, eqns);
end if;
end for;
// change state variables to algebraic variables since der(x) is replaced by the difference quotient
(vars, oKnVars) := symSolverState(vars, inKnVars, crlst);
syst.orderedVars := vars;
syst.orderedEqs := eqns;
then BackendDAEUtil.clearEqSyst(syst);
end match;
end symSolverUpdateSyst;
// function changes every state variable to algebraic variable
protected function symSolverState
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
input list<DAE.ComponentRef> crlst;
output BackendDAE.Variables ovars = vars;
output BackendDAE.Variables oknvars = knvars;
protected
Integer idx;
DAE.ComponentRef oldCref;
BackendDAE.Var var;
algorithm
for cref in crlst loop
// change former kind of from STATE to ALG_STATE
(var, idx) := BackendVariable.getVar2(cref, ovars);
ovars := BackendVariable.setVarKindForVar(idx, BackendDAE.ALG_STATE(), ovars);
// create an old variable as known input
oldCref := ComponentReference.appendStringLastIdent("$Old", cref);
var := BackendVariable.copyVarNewName(oldCref, var);
var := BackendVariable.setVarKind(var, BackendDAE.ALG_STATE_OLD());
oknvars := BackendVariable.addVars({var}, oknvars);
end for;
end symSolverState;
protected function symSolverUpdateEqn
input DAE.Exp inExp;
input tuple<list<DAE.ComponentRef>, BackendDAE.Variables> inTl;
output DAE.Exp outExp;
output tuple<list<DAE.ComponentRef>, BackendDAE.Variables> outTpl;
protected
BackendDAE.Variables orderedVars;
list<DAE.ComponentRef> inTpl;
algorithm
(inTpl, orderedVars) := inTl;
if (Flags.getConfigEnum(Flags.SYM_SOLVER) > 1) then
// explicit euler
(outExp, (inTpl, orderedVars)) := Expression.traverseExpTopDown(inExp, symSolverUpdateStates, (inTpl, orderedVars));
else
// implicit euler
(outExp, inTpl) := Expression.traverseExpTopDown(inExp, symSolverUpdateDer, inTpl);
end if;
outTpl := (inTpl, orderedVars);
end symSolverUpdateEqn;
protected function symSolverUpdateStates
input DAE.Exp inExp;
input tuple<list<DAE.ComponentRef>, BackendDAE.Variables> inTl;
output DAE.Exp outExp;
output Boolean cont=true;
output tuple<list<DAE.ComponentRef>, BackendDAE.Variables> outTl;
protected
list<DAE.ComponentRef> inTpl;
BackendDAE.Variables orderedVars;
algorithm
(inTpl, orderedVars) := inTl;
(outExp, outTl) := match (inTpl, inExp)
local
DAE.Exp e, e1, e2, e3;
DAE.Type tp;
list<DAE.ComponentRef> cr_lst;
DAE.ComponentRef cr;
case (cr_lst, DAE.CALL(path=Absyn.IDENT(name="der"), expLst={e1 as DAE.CREF(ty=tp, componentRef = cr)}))
equation
e2 = Expression.crefExp(ComponentReference.appendStringLastIdent("$Old", cr));
e3 = Expression.crefExp(ComponentReference.makeCrefIdent(BackendDAE.symSolverDT, DAE.T_REAL_DEFAULT, {}));
cont = false;
then (DAE.BINARY(DAE.BINARY(e1, DAE.SUB(tp), e2), DAE.DIV(tp), e3), (List.unionElt(cr,cr_lst), orderedVars));
case (cr_lst, DAE.CREF(ty=_, componentRef=cr))
equation
(e, cr_lst) = symSolverAppendStringToStates(cr, cr_lst, orderedVars);
then (e, (cr_lst, orderedVars));
else (inExp, inTl);
end match;
end symSolverUpdateStates;
protected function symSolverAppendStringToStates
input DAE.ComponentRef inCr;
input list<DAE.ComponentRef> incr_lst;
input BackendDAE.Variables orderedVars;
output DAE.Exp outExp = Expression.crefExp(inCr);
output list<DAE.ComponentRef> outcr_lst = incr_lst;
algorithm
if (BackendVariable.isState(inCr, orderedVars)) then
outExp := Expression.crefExp(ComponentReference.appendStringLastIdent("$Old", inCr));
outcr_lst := List.unionElt(inCr, incr_lst);
end if;
end symSolverAppendStringToStates;
// function changes call "der" to difference quotient
protected function symSolverUpdateDer
input DAE.Exp inExp;
input list<DAE.ComponentRef> inTpl;
output DAE.Exp outExp;
output Boolean cont=true;
output list<DAE.ComponentRef> outTpl;
algorithm
(outExp, outTpl) := match (inTpl, inExp)
local
DAE.Exp e1, e2, e3;
DAE.Type tp;
list<DAE.ComponentRef> cr_lst;
DAE.ComponentRef cr;
case (cr_lst, DAE.CALL(path=Absyn.IDENT(name="der"), expLst={e1 as DAE.CREF(ty=tp, componentRef = cr)}))
equation
e2 = Expression.crefExp(ComponentReference.appendStringLastIdent("$Old", cr));
e3 = Expression.crefExp(ComponentReference.makeCrefIdent(BackendDAE.symSolverDT, DAE.T_REAL_DEFAULT, {}));
then (DAE.BINARY(DAE.BINARY(e1, DAE.SUB(tp), e2), DAE.DIV(tp), e3), List.unionElt(cr,cr_lst));
else (inExp, inTpl);
end match;
end symSolverUpdateDer;
annotation(__OpenModelica_Interface="backend");
end SymbolicImplicitSolver;