Skip to content

Commit

Permalink
Improve handling of potentialRoot (#9217)
Browse files Browse the repository at this point in the history
- Mark the priority argument as structural and evaluate it.
- Check that the priority argument is actually a parameter expression.

Fixes #9216
  • Loading branch information
perost committed Jul 8, 2022
1 parent 3369011 commit 73b3e51
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
11 changes: 10 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo
Expand Up @@ -1452,6 +1452,7 @@ protected
Function fn;
Integer args_len;
String name;
Variability arg_var;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;

Expand Down Expand Up @@ -1483,13 +1484,21 @@ protected

if args_len == 2 then
arg2 := listHead(args);
(arg2, ty) := Typing.typeExp(arg2, context, info);
(arg2, ty, arg_var) := Typing.typeExp(arg2, context, info);

if not Type.isInteger(ty) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"2", ComponentRef.toString(fn_ref), "", Expression.toString(arg2),
Type.toString(ty), "Integer"}, info);
end if;

if arg_var > Variability.PARAMETER then
Error.addSourceMessageAndFail(Error.INVALID_ARGUMENT_VARIABILITY,
{"2", ComponentRef.toString(fn_ref), Prefixes.variabilityString(Variability.PARAMETER),
Expression.toString(arg2), Prefixes.variabilityString(arg_var)}, info);
end if;

Structural.markExp(arg2);
else
arg2 := Expression.INTEGER(0);
end if;
Expand Down
16 changes: 8 additions & 8 deletions OMCompiler/Compiler/NFFrontEnd/NFOCConnectionGraph.mo
Expand Up @@ -82,6 +82,7 @@ protected
import Absyn;
import NFBuiltin;
import Call = NFCall;
import Ceval = NFCeval;
import Class = NFClass;
import Dimension = NFDimension;
import NFFunction.Function;
Expand Down Expand Up @@ -175,7 +176,7 @@ protected
Call call;
list<Expression> lst;
Integer priority;
Expression root, msg;
Expression root, msg, arg1, arg2;
Connector c1, c2;
list<ComponentRef> lhs_crefs, rhs_crefs;
Boolean print_trace = Flags.isSet(Flags.CGRAPH);
Expand Down Expand Up @@ -210,13 +211,12 @@ algorithm

case ConnectionsOperator.POTENTIAL_ROOT
algorithm
graph := match lst
case {Expression.CREF(cref = cref)}
then addPotentialRoot(cref, 0, print_trace, graph);
case {Expression.CREF(cref = cref), Expression.INTEGER(priority)}
then addPotentialRoot(cref, priority, print_trace, graph);
end match;
then eql;
{arg1, arg2} := lst;
Expression.CREF(cref = cref) := arg1;
Expression.INTEGER(value = priority) := Ceval.evalExp(arg2);
graph := addPotentialRoot(cref, priority, print_trace, graph);
then
eql;

case ConnectionsOperator.UNIQUE_ROOT
algorithm
Expand Down
@@ -0,0 +1,48 @@
// name: FuncBuiltinPotentialRoot1
// keywords:
// status: correct
// cflags: -d=newInst
//

type OC
extends Real;

function equalityConstraint
input OC oc1;
input OC oc2;
output Real residue[0];
end equalityConstraint;
end OC;

connector C
Real e;
flow Real f;
OC oc;
end C;

model FuncBuiltinPotentialRoot1
C c1, c2;
parameter Integer p = 0;
equation
Connections.potentialRoot(c1.oc, p);
Connections.potentialRoot(c2.oc);
c1.f = 0;
c2.f = 0;
end FuncBuiltinPotentialRoot1;

// Result:
// class FuncBuiltinPotentialRoot1
// Real c1.e;
// Real c1.f;
// Real c1.oc;
// Real c2.e;
// Real c2.f;
// Real c2.oc;
// final parameter Integer p = 0;
// equation
// c1.f = 0.0;
// c2.f = 0.0;
// c1.f = 0.0;
// c2.f = 0.0;
// end FuncBuiltinPotentialRoot1;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -538,6 +538,7 @@ FuncBuiltinNdims.mo \
FuncBuiltinNoClock1.mo \
FuncBuiltinOnes.mo \
FuncBuiltinOuterProduct.mo \
FuncBuiltinPotentialRoot1.mo \
FuncBuiltinPre.mo \
FuncBuiltinPrevious1.mo \
FuncBuiltinPrevious2.mo \
Expand Down

0 comments on commit 73b3e51

Please sign in to comment.