forked from OpenModelica/OpenModelica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodegenUtilSimulation.tpl
434 lines (397 loc) · 13.5 KB
/
CodegenUtilSimulation.tpl
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// This file defines util functions for templates for transforming Modelica/MetaModelica code to C like
// code. They are used in the code generator phase of the compiler to write
// target code.
//
// There are two root templates intended to be called from the code generator:
// translateModel and translateFunctions. These templates do not return any
// result but instead write the result to files. All other templates return
// text and are used by the root templates (most of them indirectly).
//
// To future maintainers of this file:
//
// - A line like this
// # var = "" /*BUFD*/
// declares a text buffer that you can later append text to. It can also be
// passed to other templates that in turn can append text to it. In the new
// version of Susan it should be written like this instead:
// let &var = buffer ""
//
// - A line like this
// ..., Text var /*BUFP*/, ...
// declares that a template takes a text buffer as input parameter. In the
// new version of Susan it should be written like this instead:
// ..., Text &var, ...
//
// - A line like this:
// ..., var /*BUFC*/, ...
// passes a text buffer to a template. In the new version of Susan it should
// be written like this instead:
// ..., &var, ...
//
// - Style guidelines:
//
// - Try (hard) to limit each row to 80 characters
//
// - Code for a template should be indented with 2 spaces
//
// - Exception to this rule is if you have only a single case, then that
// single case can be written using no indentation
//
// This single case can be seen as a clarification of the input to the
// template
//
// - Code after a case should be indented with 2 spaces if not written on the
// same line
package CodegenUtilSimulation
import interface SimCodeTV;
import ExpressionDumpTpl.*;
import CodegenUtil.*;
template modelNamePrefix(SimCode simCode)
::=
match simCode
case simCode as SIMCODE(__) then makeC89Identifier(fileNamePrefix)
end modelNamePrefix;
template fileNamePrefix(SimCode simCode)
::=
match simCode
case simCode as SIMCODE(__) then fileNamePrefix
end fileNamePrefix;
template fullPathPrefix(SimCode simCode)
::=
match simCode
case simCode as SIMCODE(__) then fullPathPrefix
end fullPathPrefix;
/********* Equation Dumps *****************************/
template equationIndex(SimEqSystem eq)
"Generates an equation."
::=
match eq
case SES_RESIDUAL(__)
case SES_FOR_RESIDUAL(__)
case SES_GENERIC_RESIDUAL(__)
case SES_SIMPLE_ASSIGN(__)
case SES_SIMPLE_ASSIGN_CONSTRAINTS(__)
case SES_ARRAY_CALL_ASSIGN(__)
case SES_GENERIC_ASSIGN(__)
case SES_ENTWINED_ASSIGN(__)
case SES_IFEQUATION(__)
case SES_ALGORITHM(__)
then index
case SES_INVERSE_ALGORITHM(__)
then index
case SES_LINEAR(lSystem=ls as LINEARSYSTEM(__))
then ls.index
case SES_NONLINEAR(nlSystem=nls as NONLINEARSYSTEM(__))
then nls.index
case SES_MIXED(__)
case SES_WHEN(__)
case SES_FOR_LOOP(__)
then index
case SES_ALIAS(__)
then aliasOf
case SES_ALGEBRAIC_SYSTEM(__)
then index
else error(sourceInfo(), "equationIndex failed")
end equationIndex;
template equationIndexAlternativeTearing(SimEqSystem eq)
"Generates an equation."
::=
match eq
case SES_LINEAR(alternativeTearing=SOME(at as LINEARSYSTEM(__)))
then at.index
case SES_NONLINEAR(alternativeTearing=SOME(at as NONLINEARSYSTEM(__)))
then at.index
end equationIndexAlternativeTearing;
template dumpEqs(list<SimEqSystem> eqs)
::= match getConfigString(OBFUSCATE)
case "none" then dumpEqsWork(eqs)
end dumpEqs;
template dumpEqsWork(list<SimEqSystem> eqs)
::= eqs |> eq hasindex i0 =>
match eq
case e as SES_RESIDUAL(__) then
<<
equation index: <%equationIndex(eq)%>
type: RESIDUAL
<%escapeCComments(dumpExp(e.exp,"\""))%>
>>
case e as SES_FOR_RESIDUAL(__) then
<<
equation index: <%equationIndex(eq)%>
type: FOR_RESIDUAL
<%escapeCComments(dumpExp(e.exp,"\""))%>
>>
case e as SES_GENERIC_RESIDUAL(__) then
<<
equation index: <%equationIndex(eq)%>
type: GENERIC_RESIDUAL
<%escapeCComments(dumpExp(e.exp,"\""))%>
>>
case e as SES_SIMPLE_ASSIGN(__) then
<<
equation index: <%equationIndex(eq)%>
type: SIMPLE_ASSIGN
<%escapeCComments(dumpCref(e.cref))%> = <%escapeCComments(dumpExp(e.exp,"\""))%>
>>
case e as SES_SIMPLE_ASSIGN_CONSTRAINTS(__) then
<<
equation index: <%equationIndex(eq)%>
type: SIMPLE_ASSIGN_CONSTRAINTS
<%escapeCComments(dumpCref(e.cref))%> = <%escapeCComments(dumpExp(e.exp,"\""))%>
constraints: <%escapeCComments(dumpConstraints(e.cons))%>
>>
case e as SES_ARRAY_CALL_ASSIGN(lhs=lhs as CREF(__)) then
<<
equation index: <%equationIndex(eq)%>
type: ARRAY_CALL_ASSIGN
<%escapeCComments(dumpCref(lhs.componentRef))%> = <%escapeCComments(dumpExp(e.exp,"\""))%>
>>
case e as SES_GENERIC_ASSIGN(__) then
<<
equation index: <%equationIndex(eq)%>
type: SES_GENERIC_ASSIGN call index: <%e.call_index%>
>>
case e as SES_ALGORITHM(statements={}) then
<<
empty algorithm
>>
case e as SES_ALGORITHM(statements=first::_) then
<<
equation index: <%equationIndex(eq)%>
type: ALGORITHM
<%e.statements |> stmt => escapeCComments(ppStmtStr(stmt,2))%>
>>
case e as SES_INVERSE_ALGORITHM(statements=first::_) then
<<
equation index: <%equationIndex(eq)%>
type: INVERSE ALGORITHM
<%e.statements |> stmt => escapeCComments(ppStmtStr(stmt,2))%>
>>
case e as SES_LINEAR(lSystem=ls as LINEARSYSTEM(__)) then
<<
equation index: <%equationIndex(eq)%>
type: LINEAR
<%ls.vars |> SIMVAR(name=cr) => '<var><%escapeCComments(dumpCref(cr))%></var>' ; separator = "\n" %>
<row>
<%ls.beqs |> exp => '<cell><%escapeCComments(dumpExp(exp,"\""))%></cell>' ; separator = "\n" %><%\n%>
</row>
<matrix>
<%ls.simJac |> (i1,i2,eq) =>
<<
<cell row="<%i1%>" col="<%i2%>">
<%match eq
case e as SES_RESIDUAL(__) then
<<
<residual><%escapeCComments(dumpExp(e.exp,"\""))%></residual>
>>
case e as SES_FOR_RESIDUAL(__) then
<<
<residual><%escapeCComments(dumpExp(e.exp,"\""))%></residual>
>>
case e as SES_GENERIC_RESIDUAL(__) then
<<
<residual><%escapeCComments(dumpExp(e.exp,"\""))%></residual>
>>
%>
</cell>
>>
%>
</matrix>
>>
case e as SES_NONLINEAR(nlSystem=nls as NONLINEARSYSTEM(__)) then
<<
equation index: <%equationIndex(eq)%>
indexNonlinear: <%nls.indexNonLinearSystem%>
type: NONLINEAR
vars: {<%nls.crefs |> cr => '<%escapeCComments(dumpCref(cr))%>' ; separator = ", "%>}
eqns: {<%nls.eqs |> eq => '<%equationIndex(eq)%>' ; separator = ", "%>}
>>
case e as SES_MIXED(__) then
<<
equation index: <%equationIndex(eq)%>
type: MIXED
<%dumpEqs(fill(e.cont,1))%>
<%dumpEqs(e.discEqs)%><%\n%>
<mixed>
<continuous index="<%equationIndex(e.cont)%>" />
<%e.discVars |> SIMVAR(name=cr) => '<var><%escapeCComments(dumpCref(cr))%></var>' ; separator = ","%>
<%e.discEqs |> eq => '<discrete index="<%equationIndex(eq)%>" />'%>
</mixed>
>>
case e as SES_ALGEBRAIC_SYSTEM(residual=residual as OMSI_FUNCTION(__)) then
let detailedDescription = dumpAlgSystemOps(matrix)
<<
equation index: <%equationIndex(eq)%>
type: ALGEBRAIC_SYSTEM
is linear: <%e.linearSystem%>
depending functions indices: <%residual.equations |> eq => '<%equationIndex(eq)%>' ; separator = ", "%>
dimension: <%listLength(residual.equations)%>
<%detailedDescription%>
>>
case e as SES_WHEN(__) then
let body = dumpWhenOps(whenStmtLst)
<<
equation index: <%equationIndex(eq)%>
type: WHEN
when {<%conditions |> cond => '<%escapeCComments(dumpCref(cond))%>' ; separator=", " %>} then
<%body%>
end when;
>>
case e as SES_IFEQUATION(__) then
let branches = ifbranches |> (_,eqs) => dumpEqs(eqs)
let elsebr = dumpEqs(elsebranch)
<<
equation index: <%equationIndex(eq)%>
type: IFEQUATION
<%branches%>
<%elsebr%>
>>
case e as SES_FOR_LOOP(__) then
let &forstatement = buffer ""
let &forstatement += 'for ' + escapeCComments(dumpExp(e.iter,"\"")) + ' in ' + escapeCComments(dumpExp(e.startIt,"\""))
let &forstatement += ' : ' + escapeCComments(dumpExp(e.endIt,"\"")) + ' loop<%\n%>'
let &forstatement += ' <%escapeCComments(dumpCref(e.cref))%> = <%escapeCComments(dumpExp(e.exp,"\""))%>; '
let &forstatement += 'end for'
<<
equation index: <%equationIndex(e)%>
type: FOR_LOOP
<%forstatement%>
>>
else
<<
unknown equation
>>
end dumpEqsWork;
template dumpAlgSystemOps(Option<DerivativeMatrix> derivativeMatrix)
"dumps description of eqations of algebraic system.
Helper function for dumpEqs."
::=
let &varsBuffer = buffer ""
let &columnBuffer = buffer ""
match derivativeMatrix
case SOME(matrix as DERIVATIVE_MATRIX(__)) then
let _ = (matrix.columns |> column =>
dumpAlgSystemColumn(column, &columnBuffer, &varsBuffer)
)
<<
iteration vars: <%varsBuffer%>
<%columnBuffer%>
>>
end dumpAlgSystemOps;
template dumpAlgSystemColumn(OMSIFunction column ,Text &columnBuffer, Text &varsBuffer)
"dumps equation description for one OMSIFunction"
::=
match column
case OMSI_FUNCTION(__) then
let &varsBuffer += (inputVars |> var as SIMVAR(__) =>
<<
<%dumpCref(name)%>
>>
; separator=", "
)
let _ = (equations |> equation as SES_SIMPLE_ASSIGN(__) =>
let &columnBuffer += dumpCref(equation.cref) + " = " + escapeCComments(dumpExp(equation.exp,"\"")) + "\n" // "
<<>>
)
<<>>
end dumpAlgSystemColumn;
template dumpWhenOps(list<BackendDAE.WhenOperator> whenOps)
::=
match whenOps
case ({}) then <<>>
case ((e as BackendDAE.ASSIGN(left=left as CREF(__)))::rest) then
let restbody = dumpWhenOps(rest)
<<
<%dumpCref(left.componentRef)%> = <%escapeCComments(dumpExp(e.right,"\""))%>;
<%restbody%>
>>
case ((e as BackendDAE.ASSIGN(left=left))::rest) then
let restbody = dumpWhenOps(rest)
<<
<%dumpExp(e.left,"\"")%> = <%escapeCComments(dumpExp(e.right,"\""))%>;
<%restbody%>
>>
case ((e as BackendDAE.REINIT(__))::rest) then
let restbody = dumpWhenOps(rest)
<<
reinit(<%dumpCref(e.stateVar)%>, <%escapeCComments(dumpExp(e.value,"\""))%>);
<%restbody%>
>>
case ((e as BackendDAE.ASSERT(__))::rest) then
let restbody = dumpWhenOps(rest)
<<
assert(<%escapeCComments(dumpExp(e.condition,"\""))%>, <%escapeCComments(dumpExp(e.message,"\""))%>, <%escapeCComments(dumpExp(e.level,"\""))%>);
<%restbody%>
>>
case ((e as BackendDAE.TERMINATE(__))::rest) then
let restbody = dumpWhenOps(rest)
<<
terminate(<%escapeCComments(dumpExp(e.message,"\""))%>)%>);
<%restbody%>
>>
case ((e as BackendDAE.NORETCALL(__))::rest) then
let restbody = dumpWhenOps(rest)
<<
noReturnCall(<%escapeCComments(dumpExp(e.exp,"\""))%>)%>);
<%restbody%>
>>
else error(sourceInfo(),"dumpEqs: Unknown equation")
end dumpWhenOps;
template dumpEqsAlternativeTearing(list<SimEqSystem> eqs)
::= match getConfigString(OBFUSCATE)
case "none" then dumpEqsAlternativeTearingWork(eqs)
end dumpEqsAlternativeTearing;
template dumpEqsAlternativeTearingWork(list<SimEqSystem> eqs)
::= eqs |> eq hasindex i0 =>
match eq
case e as SES_LINEAR(alternativeTearing=SOME(at as LINEARSYSTEM(__))) then
<<
equation index: <%equationIndexAlternativeTearing(eq)%>
type: LINEAR
<%at.vars |> SIMVAR(name=cr) => '<var><%dumpCref(cr)%></var>' ; separator = "\n" %>
<row>
<%at.beqs |> exp => '<cell><%escapeCComments(dumpExp(exp,"\""))%></cell>' ; separator = "\n" %><%\n%>
</row>
<matrix>
<%at.simJac |> (i1,i2,eq) =>
<<
<cell row="<%i1%>" col="<%i2%>">
<%match eq
case e as SES_RESIDUAL(__) then
<<
<residual><%escapeCComments(dumpExp(e.exp,"\""))%></residual>
>>
case e as SES_FOR_RESIDUAL(__) then
<<
<residual><%escapeCComments(dumpExp(e.exp,"\""))%></residual>
>>
case e as SES_GENERIC_RESIDUAL(__) then
<<
<residual><%escapeCComments(dumpExp(e.exp,"\""))%></residual>
>>
%>
</cell>
>>
%>
</matrix>
This is the alternative tearing set with casual solvability rules.
If it fails, this function will call the strict tearing set.
>>
case e as SES_NONLINEAR(alternativeTearing=SOME(at as NONLINEARSYSTEM(__))) then
<<
equation index: <%equationIndexAlternativeTearing(eq)%>
indexNonlinear: <%at.indexNonLinearSystem%>
type: NONLINEAR
vars: {<%at.crefs |> cr => '<%dumpCref(cr)%>' ; separator = ", "%>}
eqns: {<%at.eqs |> eq => '<%equationIndex(eq)%>' ; separator = ", "%>}
This is the alternative tearing set with casual solvability rules.
If it fails, this function will call the strict tearing set.
>>
else
<<
unknown equation
>>
end dumpEqsAlternativeTearingWork;
annotation(__OpenModelica_Interface="backend");
end CodegenUtilSimulation;
// vim: filetype=susan sw=2 sts=2