Skip to content

Commit

Permalink
[NF] Fix actualStream evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Jul 8, 2020
1 parent 1d71cfe commit a1be2e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
27 changes: 12 additions & 15 deletions OMCompiler/Compiler/NFFrontEnd/NFConnectEquations.mo
Expand Up @@ -137,7 +137,10 @@ algorithm
case Absyn.IDENT("inStream")
then evaluateInStream(Expression.toCref(listHead(call.arguments)), sets, setsArray, ctable);
case Absyn.IDENT("actualStream")
then evaluateActualStream(Expression.toCref(listHead(call.arguments)), sets, setsArray, ctable);
algorithm
evalExp := evaluateActualStream(Expression.toCref(listHead(call.arguments)), sets, setsArray, ctable);
then
evalExp;
case Absyn.IDENT("cardinality")
then CardinalityTable.evaluateCardinality(listHead(call.arguments), ctable);
else Expression.mapShallow(exp,
Expand Down Expand Up @@ -825,16 +828,15 @@ protected function evaluateActualStream
input ConnectionSets.Sets sets;
input array<list<Connector>> setsArray;
input CardinalityTable.Table ctable;
input Option<ComponentRef> mulCref = NONE();
output Expression exp;
output ComponentRef flowCref;
protected
Integer flow_dir;
ComponentRef flow_cr;
Expression flow_exp, stream_exp, instream_exp;
Operator op;
algorithm
flow_cr := associatedFlowCref(streamCref);
flow_dir := evaluateFlowDirection(flow_cr);
flowCref := associatedFlowCref(streamCref);
flow_dir := evaluateFlowDirection(flowCref);

// Select a branch if we know the flow direction, otherwise generate the whole
// if-equation.
Expand All @@ -843,21 +845,16 @@ algorithm
elseif flow_dir == -1 then
exp := Expression.fromCref(streamCref);
else
// actualStream(stream_var) = smooth(0, if flow_var > 0 then inStream(stream_var)
// else stream_var);
flow_exp := Expression.fromCref(flow_cr);
// actualStream(stream_var) = if flow_var > 0 then inStream(stream_var) else stream_var);
flow_exp := Expression.fromCref(flowCref);
stream_exp := Expression.fromCref(streamCref);
instream_exp := evaluateInStream(streamCref, sets, setsArray, ctable);
op := Operator.makeGreater(ComponentRef.nodeType(flow_cr));
op := Operator.makeGreater(ComponentRef.nodeType(flowCref));

exp := Expression.IF(
Type.REAL(),
Expression.RELATION(flow_exp, op, Expression.REAL(0.0)),
instream_exp, stream_exp);

if isNone(mulCref) or not ComponentRef.isEqual(flow_cr, Util.getOption(mulCref)) then
exp := makeSmoothCall(exp, 0);
end if;
end if;
end evaluateActualStream;

Expand All @@ -876,12 +873,12 @@ protected
ComponentRef cr, flow_cr;
algorithm
e1 as Expression.CREF(cref = cr) := evaluateOperators(crefExp, sets, setsArray, ctable);
e2 := evaluateActualStream(Expression.toCref(actualStreamArg), sets, setsArray, ctable, SOME(cr));
(e2, flow_cr) := evaluateActualStream(Expression.toCref(actualStreamArg), sets, setsArray, ctable);
outExp := Expression.BINARY(e1, op, e2);

// Wrap the expression in smooth if the result would be flow_cr * (if flow_cr > 0 then ...)
outExp := match e2
case Expression.IF() then makeSmoothCall(outExp, 0);
case Expression.IF() guard ComponentRef.isEqual(cr, flow_cr) then makeSmoothCall(outExp, 0);
else outExp;
end match;
end evaluateActualStreamMul;
Expand Down
4 changes: 2 additions & 2 deletions testsuite/flattening/modelica/scodeinst/ActualStream.mo
Expand Up @@ -39,7 +39,7 @@ end ActualStream;
// equation
// a.s1.r = a.s2.r;
// a.s2.f + a.s1.f = 0.0;
// actual_stream_s1 = smooth(0, if a.s1.f > 0.0 then a.s2.s else a.s1.s);
// actual_stream_s2 = smooth(0, if a.s2.f > 0.0 then a.s1.s else a.s2.s);
// actual_stream_s1 = if a.s1.f > 0.0 then a.s2.s else a.s1.s;
// actual_stream_s2 = if a.s2.f > 0.0 then a.s1.s else a.s2.s;
// end ActualStream;
// endResult
4 changes: 2 additions & 2 deletions testsuite/flattening/modelica/scodeinst/ActualStreamMinMax.mo
Expand Up @@ -58,7 +58,7 @@ end ActualStreamMinMax;
// a.s4.f + a.s3.f = 0.0;
// actual_stream_s1 = a.s2.s;
// actual_stream_s2 = a.s2.s;
// actual_stream_s3 = smooth(0, if a.s3.f > 0.0 then a.s4.s else a.s3.s);
// actual_stream_s4 = smooth(0, if a.s4.f > 0.0 then a.s3.s else a.s4.s);
// actual_stream_s3 = if a.s3.f > 0.0 then a.s4.s else a.s3.s;
// actual_stream_s4 = if a.s4.f > 0.0 then a.s3.s else a.s4.s;
// end ActualStreamMinMax;
// endResult

0 comments on commit a1be2e6

Please sign in to comment.