Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 5e99170

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Evaluate if-conditions in more cases.
- Change the branches of if- and when-equation from tuples to uniontypes that also contains variability. - Evaluate structural if-conditions during model simplification. Belonging to [master]: - #2518 - OpenModelica/OpenModelica-testsuite#980
1 parent 05b3577 commit 5e99170

File tree

7 files changed

+278
-92
lines changed

7 files changed

+278
-92
lines changed

Compiler/NFFrontEnd/NFConvertDAE.mo

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -521,18 +521,24 @@ algorithm
521521
end convertEquation;
522522

523523
function convertIfEquation
524-
input list<tuple<Expression, list<Equation>>> ifBranches;
524+
input list<Equation.Branch> ifBranches;
525525
input DAE.ElementSource source;
526526
input Boolean isInitial;
527527
output DAE.Element ifEquation;
528528
protected
529-
list<Expression> conds;
530-
list<list<Equation>> branches;
529+
list<Expression> conds = {};
530+
list<list<Equation>> branches = {};
531531
list<DAE.Exp> dconds;
532532
list<list<DAE.Element>> dbranches;
533533
list<DAE.Element> else_branch;
534534
algorithm
535-
(conds, branches) := List.unzipReverse(ifBranches);
535+
for branch in ifBranches loop
536+
(conds, branches) := match branch
537+
case Equation.Branch.BRANCH()
538+
then (branch.condition :: conds, branch.body :: branches);
539+
end match;
540+
end for;
541+
536542
dbranches := if isInitial then
537543
list(convertInitialEquations(b) for b in branches) else
538544
list(convertEquations(b) for b in branches);
@@ -552,7 +558,7 @@ algorithm
552558
end convertIfEquation;
553559

554560
function convertWhenEquation
555-
input list<tuple<Expression, list<Equation>>> whenBranches;
561+
input list<Equation.Branch> whenBranches;
556562
input DAE.ElementSource source;
557563
output DAE.Element whenEquation;
558564
protected
@@ -561,9 +567,14 @@ protected
561567
Option<DAE.Element> when_eq = NONE();
562568
algorithm
563569
for b in listReverse(whenBranches) loop
564-
cond := Expression.toDAE(Util.tuple21(b));
565-
els := convertEquations(Util.tuple22(b));
566-
when_eq := SOME(DAE.Element.WHEN_EQUATION(cond, els, when_eq, source));
570+
when_eq := match b
571+
case Equation.Branch.BRANCH()
572+
algorithm
573+
cond := Expression.toDAE(b.condition);
574+
els := convertEquations(b.body);
575+
then
576+
SOME(DAE.Element.WHEN_EQUATION(cond, els, when_eq, source));
577+
end match;
567578
end for;
568579

569580
SOME(whenEquation) := when_eq;

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ encapsulated uniontype NFEquation
3636
import NFInstNode.InstNode;
3737
import DAE;
3838
import ComponentRef = NFComponentRef;
39+
import NFPrefixes.Variability;
3940

4041
protected
4142
import Equation = NFEquation;
@@ -44,6 +45,14 @@ protected
4445
import ElementSource;
4546

4647
public
48+
uniontype Branch
49+
record BRANCH
50+
Expression condition;
51+
Variability conditionVar;
52+
list<Equation> body;
53+
end BRANCH;
54+
end Branch;
55+
4756
record EQUALITY
4857
Expression lhs "The left hand side expression.";
4958
Expression rhs "The right hand side expression.";
@@ -78,14 +87,12 @@ public
7887
end FOR;
7988

8089
record IF
81-
list<tuple<Expression, list<Equation>>> branches
82-
"List of branches, where each branch is a tuple of a condition and a body.";
90+
list<Branch> branches;
8391
DAE.ElementSource source;
8492
end IF;
8593

8694
record WHEN
87-
list<tuple<Expression, list<Equation>>> branches
88-
"List of branches, where each branch is a tuple of a condition and a body.";
95+
list<Branch> branches;
8996
DAE.ElementSource source;
9097
end WHEN;
9198

@@ -112,8 +119,18 @@ public
112119
DAE.ElementSource source;
113120
end NORETCALL;
114121

122+
function makeBranch
123+
input Expression condition;
124+
input list<Equation> body;
125+
input Variability condVar = Variability.CONTINUOUS;
126+
output Branch branch;
127+
algorithm
128+
branch := Branch.BRANCH(condition, condVar, body);
129+
annotation(__OpenModelica_EarlyInline=true);
130+
end makeBranch;
131+
115132
function makeIf
116-
input list<tuple<Expression, list<Equation>>> branches;
133+
input list<Branch> branches;
117134
input DAE.ElementSource src;
118135
output Equation eq;
119136
algorithm
@@ -174,19 +191,33 @@ public
174191
case IF()
175192
algorithm
176193
for b in eq.branches loop
177-
for e in Util.tuple22(b) loop
178-
apply(e, func);
179-
end for;
194+
() := match b
195+
case Branch.BRANCH()
196+
algorithm
197+
for e in b.body loop
198+
apply(e, func);
199+
end for;
200+
then
201+
();
202+
else ();
203+
end match;
180204
end for;
181205
then
182206
();
183207

184208
case WHEN()
185209
algorithm
186210
for b in eq.branches loop
187-
for e in Util.tuple22(b) loop
188-
apply(e, func);
189-
end for;
211+
() := match b
212+
case Branch.BRANCH()
213+
algorithm
214+
for e in b.body loop
215+
apply(e, func);
216+
end for;
217+
then
218+
();
219+
else ();
220+
end match;
190221
end for;
191222
then
192223
();
@@ -214,15 +245,31 @@ public
214245

215246
case IF()
216247
algorithm
217-
eq.branches := list((Util.tuple21(b), list(map(e, func) for e in Util.tuple22(b)))
218-
for b in eq.branches);
248+
eq.branches := list(
249+
match b
250+
case Branch.BRANCH()
251+
algorithm
252+
b.body := list(map(e, func) for e in b.body);
253+
then
254+
b;
255+
else b;
256+
end match
257+
for b in eq.branches);
219258
then
220259
();
221260

222261
case WHEN()
223262
algorithm
224-
eq.branches := list((Util.tuple21(b), list(map(e, func) for e in Util.tuple22(b)))
225-
for b in eq.branches);
263+
eq.branches := list(
264+
match b
265+
case Branch.BRANCH()
266+
algorithm
267+
b.body := list(map(e, func) for e in b.body);
268+
then
269+
b;
270+
else b;
271+
end match
272+
for b in eq.branches);
226273
then
227274
();
228275

@@ -328,16 +375,22 @@ public
328375
end mapExp;
329376

330377
function mapExpBranch
331-
input output tuple<Expression, list<Equation>> branch;
378+
input output Branch branch;
332379
input MapExpFn func;
333380
protected
334381
Expression cond;
335382
list<Equation> eql;
336383
algorithm
337-
(cond, eql) := branch;
338-
cond := func(cond);
339-
eql := list(mapExp(e, func) for e in eql);
340-
branch := (cond, eql);
384+
branch := match branch
385+
case Branch.BRANCH()
386+
algorithm
387+
cond := func(branch.condition);
388+
eql := list(mapExp(e, func) for e in branch.body);
389+
then
390+
Branch.BRANCH(cond, branch.conditionVar, eql);
391+
392+
else branch;
393+
end match;
341394
end mapExpBranch;
342395

343396
function foldExpList<ArgT>
@@ -400,17 +453,33 @@ public
400453
case Equation.IF()
401454
algorithm
402455
for b in eq.branches loop
403-
arg := func(Util.tuple21(b), arg);
404-
arg := foldExpList(Util.tuple22(b), func, arg);
456+
() := match b
457+
case Branch.BRANCH()
458+
algorithm
459+
arg := func(b.condition, arg);
460+
arg := foldExpList(b.body, func, arg);
461+
then
462+
();
463+
464+
else ();
465+
end match;
405466
end for;
406467
then
407468
();
408469

409470
case Equation.WHEN()
410471
algorithm
411472
for b in eq.branches loop
412-
arg := func(Util.tuple21(b), arg);
413-
arg := foldExpList(Util.tuple22(b), func, arg);
473+
() := match b
474+
case Branch.BRANCH()
475+
algorithm
476+
arg := func(b.condition, arg);
477+
arg := foldExpList(b.body, func, arg);
478+
then
479+
();
480+
481+
else ();
482+
end match;
414483
end for;
415484
then
416485
();

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -706,29 +706,37 @@ algorithm
706706
end flattenEquation;
707707

708708
function flattenIfEquation
709-
input list<tuple<Expression, list<Equation>>> branches;
709+
input list<Equation.Branch> branches;
710710
input ComponentRef prefix;
711711
input DAE.ElementSource source;
712712
input output list<Equation> equations;
713713
protected
714-
list<tuple<Expression, list<Equation>>> bl = {};
714+
list<Equation.Branch> bl = {};
715715
Expression cond;
716716
list<Equation> eql;
717+
Variability var;
717718
algorithm
718719
for b in branches loop
719-
(cond, eql) := b;
720-
eql := flattenEquations(eql, prefix);
721-
722-
if Expression.isTrue(cond) and listEmpty(bl) then
723-
// If the condition is literal true and we haven't collected any other
724-
// branches yet, replace the if equation with this branch.
725-
equations := listAppend(eql, equations);
726-
return;
727-
elseif not Expression.isFalse(cond) then
728-
// Only add the branch to the list of branches if the condition is not
729-
// literal false, otherwise just drop it since it will never trigger.
730-
bl := (cond, eql) :: bl;
731-
end if;
720+
bl := match b
721+
case Equation.Branch.BRANCH(cond, var, eql)
722+
algorithm
723+
eql := flattenEquations(eql, prefix);
724+
725+
if Expression.isTrue(cond) and listEmpty(bl) then
726+
// If the condition is literal true and we haven't collected any other
727+
// branches yet, replace the if equation with this branch.
728+
equations := listAppend(eql, equations);
729+
return;
730+
elseif not Expression.isFalse(cond) then
731+
// Only add the branch to the list of branches if the condition is not
732+
// literal false, otherwise just drop it since it will never trigger.
733+
bl := Equation.makeBranch(cond, eql, var) :: bl;
734+
end if;
735+
then
736+
bl;
737+
738+
else b :: bl;
739+
end match;
732740
end for;
733741

734742
// Add the flattened if equation to the list of equations if we got this far,
@@ -739,16 +747,17 @@ algorithm
739747
end flattenIfEquation;
740748

741749
function flattenEqBranch
742-
input output tuple<Expression, list<Equation>> branch;
750+
input output Equation.Branch branch;
743751
input ComponentRef prefix;
744752
protected
745753
Expression exp;
746754
list<Equation> eql;
755+
Variability var;
747756
algorithm
748-
(exp, eql) := branch;
757+
Equation.Branch.BRANCH(exp, var, eql) := branch;
749758
exp := flattenExp(exp, prefix);
750759
eql := flattenEquations(eql, prefix);
751-
branch := (exp, listReverseInPlace(eql));
760+
branch := Equation.makeBranch(exp, listReverseInPlace(eql), var);
752761
end flattenEqBranch;
753762

754763
function unrollForLoop
@@ -1042,11 +1051,19 @@ algorithm
10421051
end collectEquationFuncs;
10431052

10441053
function collectEqBranchFuncs
1045-
input tuple<Expression, list<Equation>> branch;
1054+
input Equation.Branch branch;
10461055
input output FunctionTree funcs;
10471056
algorithm
1048-
funcs := collectExpFuncs(Util.tuple21(branch), funcs);
1049-
funcs := List.fold(Util.tuple22(branch), collectEquationFuncs, funcs);
1057+
() := match branch
1058+
case Equation.Branch.BRANCH()
1059+
algorithm
1060+
funcs := collectExpFuncs(branch.condition, funcs);
1061+
funcs := List.fold(branch.body, collectEquationFuncs, funcs);
1062+
then
1063+
();
1064+
1065+
else ();
1066+
end match;
10501067
end collectEqBranchFuncs;
10511068

10521069
function collectAlgorithmFuncs

0 commit comments

Comments
 (0)