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

Commit b8d7e26

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] More fixes for checking when-clauses.
- Handle all kinds of equations when checking when-clauses. - Make the componentref set check more robust. - Fix variability check for reinit. - Do model verification after scalarization, it takes a negligible amount of time anyway. Belonging to [master]: - #3011 - OpenModelica/OpenModelica-testsuite#1151
1 parent adba422 commit b8d7e26

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ algorithm
161161
flat_model := SimplifyModel.simplify(flat_model);
162162
funcs := Flatten.collectFunctions(flat_model, name);
163163

164-
VerifyModel.verify(flat_model);
165-
166164
// Collect package constants that couldn't be substituted with their values
167165
// (e.g. because they where used with non-constant subscripts), and add them
168166
// to the model.
@@ -176,6 +174,8 @@ algorithm
176174
flat_model.variables := List.filterOnFalse(flat_model.variables, Variable.isEmptyArray);
177175
end if;
178176

177+
VerifyModel.verify(flat_model);
178+
179179
// Convert the flat model to a DAE.
180180
(dae, daeFuncs) := ConvertDAE.convert(flat_model, funcs, name, InstNode.info(inst_cls));
181181

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,8 +2943,8 @@ algorithm
29432943

29442944
// The first argument must be a continuous time variable.
29452945
// Check the variability of the cref instead of the variability returned by
2946-
// typeExp, since expressions in when-equation count as discrete.
2947-
if ComponentRef.nodeVariability(cref) <> Variability.CONTINUOUS then
2946+
// typeExp, since expressions in when-equations count as discrete.
2947+
if ComponentRef.nodeVariability(cref) < Variability.IMPLICITLY_DISCRETE then
29482948
Error.addSourceMessage(Error.REINIT_MUST_BE_VAR,
29492949
{Expression.toString(crefExp),
29502950
Prefixes.variabilityString(ComponentRef.nodeVariability(cref))}, info);

Compiler/NFFrontEnd/NFVerifyModel.mo

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected
4141
import ComponentRef = NFComponentRef;
4242
import Equation = NFEquation;
4343
import Expression = NFExpression;
44+
import ExpandExp = NFExpandExp;
4445

4546
public
4647
function verify
@@ -89,11 +90,7 @@ protected
8990
Equation.Branch.BRANCH(body = body) := branch;
9091
crefs2 := whenEquationBranchCrefs(body);
9192

92-
if not List.isEqualOnTrue(crefs1, crefs2, ComponentRef.isEqual) then
93-
Error.addSourceMessage(Error.DIFFERENT_VARIABLES_SOLVED_IN_ELSEWHEN,
94-
{}, ElementSource.getInfo(source));
95-
fail();
96-
end if;
93+
checkCrefSetEquality(crefs1, crefs2, Error.DIFFERENT_VARIABLES_SOLVED_IN_ELSEWHEN, source);
9794
end for;
9895
end verifyWhenEquation;
9996

@@ -105,8 +102,12 @@ protected
105102
algorithm
106103
for eq in eql loop
107104
crefs := match eq
108-
case Equation.EQUALITY() then whenEquationEqualityCrefs(eq.lhs, crefs);
109-
case Equation.IF() then whenEquationIfCrefs(eq.branches, eq.source, crefs);
105+
case Equation.EQUALITY() then whenEquationEqualityCrefs(eq.lhs, crefs);
106+
case Equation.CREF_EQUALITY() then eq.lhs :: crefs;
107+
case Equation.ARRAY_EQUALITY() then whenEquationEqualityCrefs(eq.lhs, crefs);
108+
case Equation.REINIT() then whenEquationEqualityCrefs(eq.cref, crefs);
109+
case Equation.IF() then whenEquationIfCrefs(eq.branches, eq.source, crefs);
110+
else crefs;
110111
end match;
111112
end for;
112113

@@ -144,15 +145,56 @@ protected
144145
crefs2 := whenEquationBranchCrefs(body);
145146

146147
// All the branches must have the same set of crefs on the lhs.
147-
if not List.isEqualOnTrue(crefs1, crefs2, ComponentRef.isEqual) then
148-
Error.addSourceMessage(Error.WHEN_IF_VARIABLE_MISMATCH,
149-
{}, ElementSource.getInfo(source));
150-
fail();
151-
end if;
148+
checkCrefSetEquality(crefs1, crefs2, Error.WHEN_IF_VARIABLE_MISMATCH, source);
152149
end for;
153150

154151
crefs := listAppend(crefs1, crefs);
155152
end whenEquationIfCrefs;
156153

154+
function checkCrefSetEquality
155+
input list<ComponentRef> crefs1;
156+
input list<ComponentRef> crefs2;
157+
input Error.Message errMsg;
158+
input DAE.ElementSource source;
159+
algorithm
160+
// Assume the user isn't mixing different ways of subscripting array
161+
// varibles in the different branches, and just check the sets as is.
162+
if List.isEqualOnTrue(crefs1, crefs2, ComponentRef.isEqual) then
163+
return;
164+
end if;
165+
166+
// If the sets didn't match, expand arrays into scalars and try again.
167+
if List.isEqualOnTrue(expandCrefSet(crefs1), expandCrefSet(crefs2), ComponentRef.isEqual) then
168+
return;
169+
end if;
170+
171+
// Couldn't get the sets to match, print an error and fail.
172+
Error.addSourceMessage(errMsg, {}, ElementSource.getInfo(source));
173+
fail();
174+
end checkCrefSetEquality;
175+
176+
function expandCrefSet
177+
input list<ComponentRef> crefs;
178+
output list<ComponentRef> outCrefs = {};
179+
protected
180+
Expression exp;
181+
list<Expression> expl;
182+
algorithm
183+
for cref in crefs loop
184+
exp := Expression.fromCref(cref);
185+
exp := ExpandExp.expandCref(exp);
186+
187+
if Expression.isArray(exp) then
188+
expl := Expression.arrayElements(exp);
189+
outCrefs := listAppend(list(Expression.toCref(e) for e in expl), outCrefs);
190+
else
191+
outCrefs := cref :: outCrefs;
192+
end if;
193+
end for;
194+
195+
outCrefs := List.sort(outCrefs, ComponentRef.isGreater);
196+
outCrefs := List.sortedUnique(outCrefs, ComponentRef.isEqual);
197+
end expandCrefSet;
198+
157199
annotation(__OpenModelica_Interface="frontend");
158200
end NFVerifyModel;

0 commit comments

Comments
 (0)