Skip to content

Commit

Permalink
Fix flattening of if-equations (#12373)
Browse files Browse the repository at this point in the history
- Flatten the condition of an if-equation branch after evaluating it.

Fixes #12368
  • Loading branch information
perost committed May 3, 2024
1 parent a382649 commit 42f1a15
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -1820,6 +1820,7 @@ algorithm
// happen if scalarization is turned off and for-loops aren't unrolled.
if settings.scalarize or not Expression.contains(cond, Expression.isIterator) then
cond := Ceval.evalExp(cond, target);
cond := flattenExp(cond, prefix);
end if;
end if;

Expand Down Expand Up @@ -1861,6 +1862,7 @@ algorithm
algorithm
if var <= Variability.STRUCTURAL_PARAMETER then
cond := Ceval.evalExp(cond, target);
cond := flattenExp(cond, prefix);
end if;

if not Expression.isFalse(cond) then
Expand Down
45 changes: 45 additions & 0 deletions testsuite/flattening/modelica/scodeinst/IfEquation9.mo
@@ -0,0 +1,45 @@
// name: IfEquation9
// keywords:
// status: correct
// cflags: -d=newInst
//

model A
Real x;
parameter Boolean b;
equation
if b then
x = 2;
else
x = 3;
end if;
end A;

model IfEquation9
parameter Boolean barr[6] = {false, false, true, false, true, false};
A a[4](b = barr[1:4]);
end IfEquation9;

// Result:
// class IfEquation9
// parameter Boolean barr[1] = false;
// parameter Boolean barr[2] = false;
// parameter Boolean barr[3] = true;
// parameter Boolean barr[4] = false;
// parameter Boolean barr[5] = true;
// parameter Boolean barr[6] = false;
// Real a[1].x;
// final parameter Boolean a[1].b = false;
// Real a[2].x;
// final parameter Boolean a[2].b = false;
// Real a[3].x;
// final parameter Boolean a[3].b = true;
// Real a[4].x;
// final parameter Boolean a[4].b = false;
// equation
// a[1].x = 3.0;
// a[2].x = 3.0;
// a[3].x = 2.0;
// a[4].x = 3.0;
// end IfEquation9;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -727,6 +727,7 @@ IfEquation5.mo \
IfEquation6.mo \
IfEquation7.mo \
IfEquation8.mo \
IfEquation9.mo \
IfEquationEval1.mo \
IfEquationEval2.mo \
IfEquationEval3.mo \
Expand Down

0 comments on commit 42f1a15

Please sign in to comment.