-
Notifications
You must be signed in to change notification settings - Fork 296
/
NBAlias.mo
707 lines (639 loc) · 29.1 KB
/
NBAlias.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2021, 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 NBAlias
"file: NBAlias.mo
package: NBAlias
description: This file contains the functions for the alias elimination module.
It eliminates alias variables (ToDo: and resolves simple index reduction problems).
"
// ToDo:
// 1. simple state rules (with derivative replacement)
// - state = state
// - state = alg
// - state = time
// - state = const
// 2. write rateVar() and decide if we want an auxiliary for each set
// - rateVar() --> mergeAttributes()
// 3. post causalize alias elimination
// - for the ODE
// - for jacobians/hessians (once we got hessians)
// - for strong components in general
// 4. simplify only replaced equations and remove simplify2 module
// - probably not that trivial
// - Equation mapExp function that returns true if something was replaced
// - EquationArray map function that accumulates pointers if function returns true
// - simplify all equations in pointer list
// 5. trivial solution a = b; a = -b; (or other cyclic sets)
// - take an equation from the set, get both crefs in it (a,b)
// - solve for a -> set a as known
// - solve the rest of the set with causalize
// - replacements a -> what it solves for in eq1 and apply on all eq in set
// - find equation that solves b, and solve for b. add to replacements
// - apply replacements on all eq
public
import Module = NBModule;
protected
// OF imports
import DAE;
// NF imports
import BackendExtension = NFBackendExtension;
import ComponentRef = NFComponentRef;
import Expression = NFExpression;
import Operator = NFOperator;
import Variable = NFVariable;
import NFPrefixes.Variability;
// Backend imports
import BackendDAE = NBackendDAE;
import BEquation = NBEquation;
import BVariable = NBVariable;
import Causalize = NBCausalize;
import NBEquation.{Equation, EquationPointers, EqData};
import Replacements = NBReplacements;
import Solve = NBSolve;
import StrongComponent = NBStrongComponent;
import NBVariable.{VariablePointers, VarData};
// Util imports
import MetaModelica.Dangerous;
import StringUtil;
import UnorderedMap;
import UnorderedSet;
public
function main
"Wrapper function for any alias removal function. This will be
called during simulation and gets the corresponding subfunction from
Config."
extends Module.wrapper;
protected
Module.aliasInterface func;
algorithm
(func) := getModule();
bdae := match bdae
local
VarData varData "Data containing variable pointers";
EqData eqData "Data containing equation pointers";
case BackendDAE.MAIN(varData = varData, eqData = eqData)
algorithm
(varData, eqData) := func(varData, eqData);
bdae.varData := varData;
bdae.eqData := eqData;
then bdae;
case BackendDAE.HESSIAN(varData = varData, eqData = eqData)
algorithm
(varData, eqData) := func(varData, eqData);
bdae.varData := varData;
bdae.eqData := eqData;
then bdae;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
then fail();
end match;
end main;
function getModule
"Returns the module function that was chosen by the user."
output Module.aliasInterface func;
protected
String flag = "default"; //Flags.getConfigString(Flags.REMOVE_SIMPLE_EQUATIONS)
algorithm
func := match flag
case "default" then aliasDefault;
/* ... New alias modules have to be added here */
else fail();
end match;
end getModule;
protected
uniontype AliasSet "gets accumulated to find sets of alias equations and solve them"
record ALIAS_SET
list<ComponentRef> simple_variables "list of all variables in this set";
list<Pointer<Equation>> simple_equations "list of all equations in this set";
Option<Pointer<Equation>> const_opt "optional constant binding of one variable";
end ALIAS_SET;
function toString
input AliasSet set;
output String str;
algorithm
if isSome(set.const_opt) then
str := "\tConstant/Parameter Binding: "
+ Equation.toString(Pointer.access(Util.getOption(set.const_opt))) + "\n";
else
str := "\t<No Constant/Parameter Binding>\n";
end if;
if listEmpty(set.simple_equations) then
str := str + "\t###<No Set Equations>\n";
else
str := str + "\t### Set Equations:\n";
for eq in set.simple_equations loop
str := str + Equation.toString(Pointer.access(eq), "\t") + "\n";
end for;
end if;
end toString;
end AliasSet;
constant AliasSet EMPTY_ALIAS_SET = ALIAS_SET({}, {}, NONE());
// needed for unordered map
type SetPtr = Pointer<AliasSet>;
uniontype CrefTpl "used for findCrefs()"
record CREF_TPL
Boolean cont "false if search already resulted in non simple structure";
Integer varCount "variable count";
Integer paramCount "parameter/constant count";
list<ComponentRef> cr_lst "list of found variables for replacement";
end CREF_TPL;
end CrefTpl;
constant CrefTpl EMPTY_CREF_TPL = CREF_TPL(true, 0, 0, {});
constant CrefTpl FAILED_CREF_TPL = CREF_TPL(false, 0, 0, {});
function aliasDefault
"STEPS:
1. collect alias sets (variables, equations, optional constant binding)
2. balance sets - choose variable to keep if necessary
3. match/sort set (linear w.r.t. unknowns since all equations contain two crefs at max and are simple/linear)
4. apply replacements
5. save replacements in bindings of alias variables
"
extends Module.aliasInterface;
algorithm
(varData, eqData) := match (varData, eqData)
local
UnorderedMap<ComponentRef, Expression> replacements;
EquationPointers newEquations;
list<Pointer<Variable>> alias_vars, const_vars, non_trivial_alias;
list<Pointer<Equation>> non_trivial_eqs;
case (BVariable.VAR_DATA_SIM(), BEquation.EQ_DATA_SIM())
algorithm
// -----------------------------------
// 1. 2. 3.
// -----------------------------------
(replacements, newEquations) := aliasCausalize(varData.unknowns, eqData.simulation);
// -----------------------------------
// 4. apply replacements
// 5. save replacements in bindings of alias variables
// -----------------------------------
(eqData, varData) := Replacements.applySimple(eqData, varData, replacements);
alias_vars := list(BVariable.getVarPointer(cref) for cref in UnorderedMap.keyList(replacements));
// save new equations and compress affected arrays(some might have been removed)
eqData.simulation := EquationPointers.compress(newEquations);
eqData.equations := EquationPointers.compress(eqData.equations);
eqData.continuous := EquationPointers.compress(eqData.continuous);
eqData.discretes := EquationPointers.compress(eqData.discretes);
// remove alias vars from all relevant arrays after splitting off non trivial alias vars
varData.variables := VariablePointers.removeList(alias_vars, varData.variables);
varData.unknowns := VariablePointers.removeList(alias_vars, varData.unknowns);
varData.algebraics := VariablePointers.removeList(alias_vars, varData.algebraics);
varData.states := VariablePointers.removeList(alias_vars, varData.states);
varData.discretes := VariablePointers.removeList(alias_vars, varData.discretes);
varData.initials := VariablePointers.removeList(alias_vars, varData.initials);
// categorize alias vars and sort them to the correct arrays
(non_trivial_alias, alias_vars) := List.splitOnTrue(alias_vars, BVariable.hasNonTrivialAliasBinding);
// split off constant alias
// update constant start values and add to parameters
// otherwise they would not show in the result file
(const_vars, alias_vars) := List.splitOnTrue(alias_vars, BVariable.hasConstOrParamAliasBinding);
const_vars := list(BVariable.setVarKind(var, BackendExtension.VariableKind.PARAMETER()) for var in const_vars);
const_vars := list(BVariable.setBindingAsStartAndFix(var) for var in const_vars);
varData.parameters := VariablePointers.addList(const_vars, varData.parameters);
varData.knowns := VariablePointers.addList(const_vars, varData.knowns);
// add only the actual 1/-1 alias vars to alias vars
varData.aliasVars := VariablePointers.addList(alias_vars, varData.aliasVars);
varData.nonTrivialAlias := VariablePointers.addList(non_trivial_alias, varData.nonTrivialAlias);
// add non trivial alias to removed
non_trivial_eqs := list(Equation.generateBindingEquation(var, eqData.uniqueIndex, false) for var in non_trivial_alias);
eqData.removed := EquationPointers.addList(non_trivial_eqs, eqData.removed);
//eqData.equations := EquationPointers.addList(non_trivial_eqs, eqData.equations);
then (varData, eqData);
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed."});
then fail();
end match;
end aliasDefault;
function aliasCausalize
"STEPS:
1. collect alias sets (variables, equations, optional constant binding)
2. balance sets - choose variable to keep if necessary
3. match/sort set (linear w.r.t. since all equations contain two unknown crefs at max and are simple/linear)
"
input VariablePointers variables;
input EquationPointers equations;
output UnorderedMap<ComponentRef, Expression> replacements;
output EquationPointers newEquations;
protected
Integer size, setIdx = 1;
UnorderedMap<ComponentRef, SetPtr> map;
list<AliasSet> sets;
algorithm
// ------------------------------------------------------------------------------
// 1. collect alias sets (variables, equations, optional constant binding)
// ------------------------------------------------------------------------------
// collect (cref) -> (simpleSet) hashtable
size := VariablePointers.size(variables);
map := UnorderedMap.new<SetPtr>(ComponentRef.hash, ComponentRef.isEqual, size);
(newEquations, map) := NBEquation.EquationPointers.foldRemovePtr(equations, findSimpleEquation, map);
sets := getSimpleSets(map, size);
if Flags.isSet(Flags.DUMP_REPL) then
print(StringUtil.headline_2("[dumprepl] Alias Sets:") + "\n");
if listEmpty(sets) then
print("<No Alias Sets>\n\n");
else
for set in sets loop
print(StringUtil.headline_4("Alias Set " + intString(setIdx) + ":") + AliasSet.toString(set) + "\n");
setIdx := setIdx + 1;
end for;
end if;
end if;
// --------------------------------------------------------------------------------------------------------
// 2. balance sets - choose variable to keep if necessary
// 3. match/sort set (linear w.r.t. vars since all equations contain two crefs at max and are simple/linear)
// --------------------------------------------------------------------------------------------------------
replacements := UnorderedMap.new<Expression>(ComponentRef.hash, ComponentRef.isEqual, size);
for set in sets loop
replacements := createReplacementRules(set, replacements);
end for;
if Flags.isSet(Flags.DUMP_REPL) then
print(Replacements.simpleToString(replacements) + "\n");
end if;
end aliasCausalize;
function findSimpleEquation
"Checks if the equation is simple and adds it to the correct set in the hashTable."
input Pointer<Equation> eq_ptr;
input output UnorderedMap<ComponentRef, SetPtr> map;
output Boolean delete = false;
protected
Equation eq;
CrefTpl crefTpl = EMPTY_CREF_TPL;
algorithm
eq := Pointer.access(eq_ptr);
crefTpl := match eq
case BEquation.SCALAR_EQUATION() guard(isSimpleExp(eq.lhs) and isSimpleExp(eq.rhs)) algorithm
crefTpl := Expression.fold(eq.rhs, findCrefs, crefTpl);
crefTpl := Expression.fold(eq.lhs, findCrefs, crefTpl);
then crefTpl;
// ToDo: ARRAY_EQUATION RECORD_EQUATION (AUX_EQUATION?)
else crefTpl;
end match;
(map, delete) := match crefTpl
local
SetPtr set_ptr, set1_ptr, set2_ptr;
AliasSet set, set1, set2;
ComponentRef cr1, cr2;
// one variable is connected to a parameter or constant
case CREF_TPL(cr_lst = {cr1}) algorithm
if not UnorderedMap.contains(cr1, map) then
// the variable does not belong to a set -> create new one
set := EMPTY_ALIAS_SET;
set.simple_variables := {cr1};
set.const_opt := SOME(Pointer.create(eq));
UnorderedMap.add(cr1, Pointer.create(set), map);
else
// it already belongs to a set, try to update it and throw error if there already is a const binding
set_ptr := UnorderedMap.getSafe(cr1, map, sourceInfo());
set := Pointer.access(set_ptr);
if isSome(set.const_opt) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to add Equation:\n"
+ Equation.toString(eq) + "\n because the set already contains a constant binding.
Overdetermined Set!:" + AliasSet.toString(set)});
fail();
else
set.const_opt := SOME(Pointer.create(eq));
Pointer.update(set_ptr, set);
end if;
end if;
then (map, true);
// two variable crefs are connected by a simple equation
case CREF_TPL(cr_lst = {cr1, cr2}) algorithm
if (UnorderedMap.contains(cr1, map) and UnorderedMap.contains(cr2, map)) then
// Merge sets
set1_ptr := UnorderedMap.getSafe(cr1, map, sourceInfo());
set2_ptr := UnorderedMap.getSafe(cr2, map, sourceInfo());
set1 := Pointer.access(set1_ptr);
set2 := Pointer.access(set2_ptr);
set := EMPTY_ALIAS_SET;
if referenceEq(set1_ptr, set2_ptr) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to merge following sets " +
"because they would create a loop. This would create an underdetermined Set!:\n\n" +
"Trying to merge: " + Equation.toString(eq) + "\n\n" +
AliasSet.toString(set1) + "\n" + AliasSet.toString(set2)});
fail();
elseif (isSome(set1.const_opt) and isSome(set2.const_opt)) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to merge following sets " +
"because both have a constant binding. This would create an overdetermined Set!:\n\n" +
AliasSet.toString(set1) + "\n" + AliasSet.toString(set2)});
fail();
elseif isSome(set1.const_opt) then
set.const_opt := set1.const_opt;
elseif isSome(set2.const_opt) then
set.const_opt := set2.const_opt;
end if;
// try to append the shorter to the longer lists
if listLength(set1.simple_equations) > listLength(set2.simple_equations) then
set.simple_equations := Pointer.create(eq) :: Dangerous.listAppendDestroy(set2.simple_equations, set1.simple_equations);
else
set.simple_equations := Pointer.create(eq) :: Dangerous.listAppendDestroy(set1.simple_equations, set2.simple_equations);
end if;
// try to change as few pointer entries as possible
if listLength(set1.simple_variables) > listLength(set2.simple_variables) then
set.simple_variables := Dangerous.listAppendDestroy(set2.simple_variables, set1.simple_variables);
Pointer.update(set1_ptr, set);
for cr in set2.simple_variables loop
UnorderedMap.add(cr, set1_ptr, map);
end for;
else
set.simple_variables := Dangerous.listAppendDestroy(set2.simple_variables, set1.simple_variables);
Pointer.update(set2_ptr, set);
for cr in set1.simple_variables loop
UnorderedMap.add(cr, set2_ptr, map);
end for;
end if;
elseif UnorderedMap.contains(cr1, map) then
// Update set
set_ptr := UnorderedMap.getSafe(cr1, map, sourceInfo());
set := Pointer.access(set_ptr);
// add cr2 to variables and add new equation pointer
set.simple_variables := cr2 :: set.simple_variables;
set.simple_equations := Pointer.create(eq) :: set.simple_equations;
Pointer.update(set_ptr, set);
// add new hash entry for c2
UnorderedMap.add(cr2, set_ptr, map);
elseif UnorderedMap.contains(cr2, map) then
// Update set
set_ptr := UnorderedMap.getSafe(cr2, map, sourceInfo());
set := Pointer.access(set_ptr);
// add cr1 to variables and add new equation pointer
set.simple_variables := cr1 :: set.simple_variables;
set.simple_equations := Pointer.create(eq) :: set.simple_equations;
Pointer.update(set_ptr, set);
// add new hash entry for c1
UnorderedMap.add(cr1, set_ptr, map);
else
// create new set
set := EMPTY_ALIAS_SET;
// add both variables and add new equation pointer
set.simple_variables := {cr1, cr2};
set.simple_equations := {Pointer.create(eq)};
set_ptr := Pointer.create(set);
// add new hash entry for both variables
UnorderedMap.add(cr1, set_ptr, map);
UnorderedMap.add(cr2, set_ptr, map);
end if;
then (map, true);
// no replacements can be done with this equation
else (map, false);
end match;
end findSimpleEquation;
function findCrefs "BB, kabdelhak
looks for variable crefs in Expressions, if more than 2 are found stop searching
also stop if complex structures appear, e.g. IFEXP
"
input Expression exp;
input output CrefTpl tpl;
algorithm
tpl := match exp
case _ guard(not tpl.cont) then FAILED_CREF_TPL;
// time, parameter or constant found (nothing happens)
case Expression.CREF()
guard(BVariable.isParamOrConst(BVariable.getVarPointer(exp.cref)) or ComponentRef.isTime(exp.cref))
then tpl;
// fail for multidimensional crefs and record elements for now
case Expression.CREF()
guard(BVariable.size(BVariable.getVarPointer(exp.cref)) > 1 or Util.isSome(BVariable.getParent(BVariable.getVarPointer(exp.cref))))
then FAILED_CREF_TPL;
// variable found
// 1. not time and not param or const
// 2. less than two previous variables
// 3. if it is an array, it has to be the full array. no slice replacement here
case Expression.CREF()
guard((tpl.varCount < 2) and not ComponentRef.hasSubscripts(exp.cref))
algorithm
// add the variable to the list and bump var count
tpl.cr_lst := exp.cref :: tpl.cr_lst;
tpl.varCount := tpl.varCount + 1;
then tpl;
// set the continue attribute to false if any fail case is met
case _ guard(findCrefsFail(exp)) then FAILED_CREF_TPL;
else tpl;
end match;
end findCrefs;
function findCrefsFail
"finds all failing cases to stop searching for simple crefs.
also fails for crefs because viable cases have to be caught
before invoking this function in findCrefs().
ToDo: Discuss and find all failing cases"
input Expression exp;
output Boolean cont;
algorithm
cont := match exp
case Expression.CREF() then true;
case Expression.RELATION() then true;
case Expression.IF() then true;
case Expression.CALL() then true;
case Expression.RECORD() then true;
else false;
end match;
end findCrefsFail;
function isSimpleExp
"checks if an expression can be considered simple."
input Expression exp;
input output Boolean simple = true;
output Integer num_cref = 0;
algorithm
if not simple then return; end if;
(simple, num_cref) := match exp
local
Integer num_cref_tmp;
Operator.Op op;
case Expression.INTEGER() then (true, 0);
case Expression.REAL() then (true, 0);
case Expression.BOOLEAN() then (true, 0);
case Expression.STRING() then (true, 0);
case Expression.CREF() then (true, 1);
// TODO what about parameters in the denominator, they could be zero, (alias strictness?)
//case Expression.CREF() then (true, if ComponentRef.variability(exp.cref) > Variability.NON_STRUCTURAL_PARAMETER then 1 else 0);
case Expression.CAST() then isSimpleExp(exp.exp);
case Expression.UNARY() algorithm
(simple, num_cref) := isSimpleExp(exp.exp);
simple := if simple then checkOp(exp.operator, num_cref) else false;
then (simple, num_cref);
case Expression.LUNARY() algorithm
(simple, num_cref) := isSimpleExp(exp.exp);
simple := if simple then checkOp(exp.operator, num_cref) else false;
then (simple, num_cref);
case Expression.BINARY(operator = Operator.OPERATOR(op = op)) algorithm
(simple, num_cref) := isSimpleExp(exp.exp2);
// 1/x is not considered simple
if op == NFOperator.Op.DIV and num_cref <> 0 then simple := false; return; end if;
(simple, num_cref_tmp) := isSimpleExp(exp.exp1, simple);
num_cref := num_cref + num_cref_tmp;
simple := if simple then checkOp(exp.operator, num_cref) else false;
then (simple, num_cref);
case Expression.LBINARY() algorithm
(simple, num_cref) := isSimpleExp(exp.exp1);
(simple, num_cref_tmp) := isSimpleExp(exp.exp2, simple);
num_cref := num_cref + num_cref_tmp;
simple := if simple then checkOp(exp.operator, num_cref) else false;
then (simple, num_cref);
case Expression.MULTARY(operator = Operator.OPERATOR(op = op)) algorithm
for arg in exp.inv_arguments loop
(simple, num_cref_tmp) := isSimpleExp(arg, simple);
if not simple then return; end if;
num_cref := num_cref + num_cref_tmp;
end for;
// 1/x is not considered simple
if op == NFOperator.Op.MUL and num_cref <> 0 then simple := false; return; end if;
for arg in exp.arguments loop
(simple, num_cref_tmp) := isSimpleExp(arg, simple);
if not simple then return; end if;
num_cref := num_cref + num_cref_tmp;
end for;
simple := if simple then checkOp(exp.operator, num_cref) else false;
then (simple, num_cref);
else (false, num_cref);
end match;
end isSimpleExp;
function checkOp
"BB"
input Operator op;
input Integer cref_num;
output Boolean b;
algorithm
b := match(op)
case Operator.OPERATOR(op = NFOperator.Op.ADD) then true;
case Operator.OPERATOR(op = NFOperator.Op.SUB) then true;
case Operator.OPERATOR(op = NFOperator.Op.UMINUS) then true;
case Operator.OPERATOR(op = NFOperator.Op.NOT) then true;
case Operator.OPERATOR(op = NFOperator.Op.MUL) then cref_num < 2;
case Operator.OPERATOR(op = NFOperator.Op.DIV) then cref_num < 2;
else cref_num == 0;
end match;
end checkOp;
function getSimpleSets
"extracts all simple sets from the hashTable and avoids duplicates by marking variables"
input UnorderedMap<ComponentRef, SetPtr> map;
input Integer size;
output list<AliasSet> sets = {};
protected
UnorderedSet<ComponentRef> cref_marks = UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual, size);
list<tuple<ComponentRef, SetPtr>> entry_lst;
ComponentRef simple_cref;
SetPtr set_ptr;
AliasSet set;
algorithm
entry_lst := UnorderedMap.toList(map);
for entry in entry_lst loop
(simple_cref, set_ptr) := entry;
if not UnorderedSet.contains(simple_cref, cref_marks) then
set := Pointer.access(set_ptr);
sets := set :: sets;
for cr in set.simple_variables loop
try
UnorderedSet.addUnique(cr, cref_marks);
else
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because the set for " + ComponentRef.toString(cr) + " was already added."});
end try;
end for;
end if;
end for;
end getSimpleSets;
function createReplacementRules
"creates replacement rules from a simple set by causalizing it and replacing the expressions in order"
input AliasSet set;
input output UnorderedMap<ComponentRef, Expression> replacements;
algorithm
// ToDo: fix variable attributes to keep
// report errors/warnings
replacements := match set.const_opt
local
Pointer<Equation> const_eq;
list<Pointer<Variable>> alias_vars;
VariablePointers vars;
EquationPointers eqs;
list<StrongComponent> comps;
case SOME(const_eq) algorithm
// there is a constant binding -> no variable will be kept and all will be replaced by a constant
vars := VariablePointers.fromList(list(BVariable.getVarPointer(cr) for cr in set.simple_variables), true);
eqs := EquationPointers.fromList(const_eq :: set.simple_equations);
// causalize the system
comps := Causalize.simple(vars, eqs);
// create replacements from strong components
Replacements.simple(comps, replacements);
then replacements;
else algorithm
// there is no constant binding -> all others will be replaced by one variable
alias_vars := chooseVariableToKeep(list(BVariable.getVarPointer(cr) for cr in set.simple_variables));
vars := VariablePointers.fromList(alias_vars);
eqs := EquationPointers.fromList(set.simple_equations);
// causalize the system
comps := Causalize.simple(vars, eqs);
// create replacements from strong components
Replacements.simple(comps, replacements);
then replacements;
end match;
end createReplacementRules;
function chooseVariableToKeep
"choose a variable from a list to keep. returns all variables but the one with the highest rating"
input list<Pointer<Variable>> tail;
input Pointer<Pointer<Variable>> var_to_keep = Pointer.create(Pointer.create(NBVariable.DUMMY_VARIABLE));
input Integer max_rating = -1;
output list<Pointer<Variable>> acc;
algorithm
acc := match tail
local
list<Pointer<Variable>> rest;
Pointer<Variable> var, new_alias;
Integer cur_rating, new_max_rating;
case var :: rest guard(max_rating == -1) algorithm
// this is the entry point. update the variable to keep with the very first of the list
Pointer.update(var_to_keep, var);
new_max_rating := rateVar(var);
then chooseVariableToKeep(rest, var_to_keep, new_max_rating);
case var :: rest algorithm
// check if new rating is better than old
cur_rating := rateVar(var);
if cur_rating > max_rating then
// put the currently held variable back to the list and update the new "variable to keep"
new_alias := Pointer.access(var_to_keep);
Pointer.update(var_to_keep, var);
new_max_rating := cur_rating;
else
// do not change anything and just keep the variable and max_rating
new_alias := var;
new_max_rating := max_rating;
end if;
then new_alias :: chooseVariableToKeep(rest, var_to_keep, new_max_rating);
else {};
end match;
end chooseVariableToKeep;
function rateVar
"Rates a variable based on attributes"
input Pointer<Variable> var_ptr;
output Integer rating;
algorithm
// ToDo: put acutal rating algorithm here
rating := if BVariable.isFixed(var_ptr) then 1 else 0;
rating := if BVariable.isFunctionAlias(var_ptr) then rating - 5 else rating;
end rateVar;
annotation(__OpenModelica_Interface="backend");
end NBAlias;