Skip to content

Commit

Permalink
improved inStream
Browse files Browse the repository at this point in the history
use positiveMax like proposed in
see. https://trac.openmodelica.org/OpenModelica/ticket/3885#comment:12

refs ticket:3885, ticket:4441

Belonging to [master]:
  - OpenModelica/OMCompiler#2319
  - OpenModelica/OpenModelica-testsuite#900
  • Loading branch information
vruge authored and OpenModelica-Hudson committed Mar 26, 2018
1 parent de9ded9 commit 1c86d4e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
116 changes: 116 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -110,6 +110,122 @@ algorithm
outDAE.shared := shared;
end simplifyAllExpressions;

// =============================================================================
// simplifyInStream
//
// OM introduces $OMC$PosetiveMax which can simplified using min or max attribute
// see Modelica spec for inStream
// author: Vitalij Ruge
// see. #3885, 4441
// =============================================================================

public function simplifyInStream
input output BackendDAE.BackendDAE dae;
protected
BackendDAE.Shared shared = dae.shared;
BackendDAE.EqSystems eqs = dae.eqs;
list<BackendDAE.Variables> vars = list(eq.orderedVars for eq in eqs);
algorithm
// from the description inputs are part of globalKnownVars and localKnownVars :(
vars := shared.globalKnownVars :: vars; // need inputs with min = 0 or max = 0
vars := shared.localKnownVars :: vars;
_ := BackendDAEUtil.traverseBackendDAEExpsNoCopyWithUpdate(dae, simplifyInStreamWork, vars);
end simplifyInStream;


protected function simplifyInStreamWork
input DAE.Exp inExp;
input list<BackendDAE.Variables> inVars;
output DAE.Exp outExp;
output list<BackendDAE.Variables> outVars = inVars;
algorithm
outExp := Expression.traverseExpBottomUp(inExp, simplifyInStreamWork2, outVars);
end simplifyInStreamWork;

protected function simplifyInStreamWork2
input DAE.Exp inExp;
input list<BackendDAE.Variables> inVars;
output DAE.Exp outExp;
output list<BackendDAE.Variables> outVars = inVars;
algorithm
outExp := match(inExp)
local DAE.Type tp;
DAE.ComponentRef cr;
DAE.Exp e, expr;
Option<DAE.Exp> eMin;
Option<DAE.Exp> eMax;
Boolean isZero;


case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e as DAE.CREF(componentRef=cr), expr})
algorithm
(eMin, eMax) := simplifyInStreamWorkExpresion(cr, outVars);
isZero := simplifyInStreamWorkSimplify(eMin, false);
tp := ComponentReference.crefTypeFull(cr);
then
if isZero then e else Expression.makePureBuiltinCall("max", {e, expr}, tp);

case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e as DAE.UNARY(DAE.UMINUS(tp), DAE.CREF(componentRef=cr)), expr})
algorithm
(eMin, eMax) := simplifyInStreamWorkExpresion(cr, outVars);
isZero := simplifyInStreamWorkSimplify(eMax, true);
then
if isZero then Expression.createZeroExpression(tp) else Expression.makePureBuiltinCall("max", {e, expr}, tp);

case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e, expr})
guard Expression.isZero(e)
then e;

case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e, expr})
//algorithm
//print("\nsimplifyInStreamWork: ");
//print(ExpressionDump.printExpStr(inExp));
//print(" <-> ");
//print(ExpressionDump.printExpStr(e));

then Expression.makePureBuiltinCall("max", {e, expr}, Expression.typeof(e));

case _
then inExp;
end match;

end simplifyInStreamWork2;

protected function simplifyInStreamWorkExpresion
input DAE.ComponentRef cr;
input list<BackendDAE.Variables> inVars;
output Option<DAE.Exp> outMin = NONE();
output Option<DAE.Exp> outMax = NONE();
protected
BackendDAE.Var v;
algorithm
for vars in inVars loop
try
(v, _) := BackendVariable.getVarSingle(cr, vars);
(outMin, outMax) := BackendVariable.getMinMaxAttribute(v);
break;
else
// search
end try;
end for;

end simplifyInStreamWorkExpresion;

protected function simplifyInStreamWorkSimplify
input Option<DAE.Exp> bound;
input Boolean neg;
output Boolean isZero;
algorithm
isZero := match bound
local Real r;
case SOME(DAE.RCONST(r))
then if neg then r<= 0.0 else r >= 0.0;
case _
then false;
end match;
end simplifyInStreamWorkSimplify;


// =============================================================================
// simplify time independent function calls
//
Expand Down
5 changes: 5 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7495,6 +7495,7 @@ public function allPreOptimizationModules
(BackendDAEOptimize.removeUnusedVariables, "removeUnusedVariables"),
(BackendDAEOptimize.residualForm, "residualForm"),
(BackendDAEOptimize.simplifyAllExpressions, "simplifyAllExpressions"),
(BackendDAEOptimize.simplifyInStream, "simplifyInStream"),
(BackendDump.dumpDAE, "dumpDAE"),
(XMLDump.dumpDAEXML, "dumpDAEXML"),
// This should indeed be at the end
Expand Down Expand Up @@ -7621,6 +7622,10 @@ algorithm
preOptModules := getPreOptModulesString();
preOptModules := Util.getOptionOrDefault(inPreOptModules, preOptModules);

if isSome(getGlobalRoot(Global.isInStream)) then
enabledModules := "simplifyInStream"::enabledModules;
end if;

if Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) then
// handle special flags, which enable modules

Expand Down
6 changes: 5 additions & 1 deletion Compiler/FrontEnd/ConnectUtil.mo
Expand Up @@ -74,6 +74,7 @@ import PrefixUtil;
import System;
import Types;
import Util;
import Global;

// Import some types from Connect.
import Connect.Face;
Expand Down Expand Up @@ -1408,6 +1409,7 @@ protected
Boolean has_stream, has_expandable, has_cardinality;
ConnectionGraph.DaeEdges broken, connected;
algorithm
setGlobalRoot(Global.isInStream, NONE());
if not topScope then
return;
end if;
Expand Down Expand Up @@ -2321,7 +2323,7 @@ algorithm
end if;

positiveMaxCall :=
DAE.CALL(Absyn.IDENT("max"), {flowExp, flow_threshold},
DAE.CALL(Absyn.IDENT("$OMC$PositiveMax"), {flowExp, flow_threshold},
DAE.CALL_ATTR(
ty,
false,
Expand All @@ -2330,6 +2332,8 @@ algorithm
false,
DAE.NO_INLINE(),
DAE.NO_TAIL()));

setGlobalRoot(Global.isInStream, SOME(true));
end makePositiveMaxCall;

protected function evaluateConnectionOperators
Expand Down
1 change: 1 addition & 0 deletions Compiler/Global/Global.mo
Expand Up @@ -64,6 +64,7 @@ constant Integer currentInstVar = 22;
constant Integer operatorOverloadingCache = 23;
constant Integer optionSimCode = 24;
constant Integer interactiveCache = 25;
constant Integer isInStream = 26;

// indexes in System.tick
// ----------------------
Expand Down

0 comments on commit 1c86d4e

Please sign in to comment.