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

Commit 1c86d4e

Browse files
vrugeOpenModelica-Hudson
authored andcommitted
improved inStream
use positiveMax like proposed in see. https://trac.openmodelica.org/OpenModelica/ticket/3885#comment:12 refs ticket:3885, ticket:4441 Belonging to [master]: - #2319 - OpenModelica/OpenModelica-testsuite#900
1 parent de9ded9 commit 1c86d4e

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,122 @@ algorithm
110110
outDAE.shared := shared;
111111
end simplifyAllExpressions;
112112

113+
// =============================================================================
114+
// simplifyInStream
115+
//
116+
// OM introduces $OMC$PosetiveMax which can simplified using min or max attribute
117+
// see Modelica spec for inStream
118+
// author: Vitalij Ruge
119+
// see. #3885, 4441
120+
// =============================================================================
121+
122+
public function simplifyInStream
123+
input output BackendDAE.BackendDAE dae;
124+
protected
125+
BackendDAE.Shared shared = dae.shared;
126+
BackendDAE.EqSystems eqs = dae.eqs;
127+
list<BackendDAE.Variables> vars = list(eq.orderedVars for eq in eqs);
128+
algorithm
129+
// from the description inputs are part of globalKnownVars and localKnownVars :(
130+
vars := shared.globalKnownVars :: vars; // need inputs with min = 0 or max = 0
131+
vars := shared.localKnownVars :: vars;
132+
_ := BackendDAEUtil.traverseBackendDAEExpsNoCopyWithUpdate(dae, simplifyInStreamWork, vars);
133+
end simplifyInStream;
134+
135+
136+
protected function simplifyInStreamWork
137+
input DAE.Exp inExp;
138+
input list<BackendDAE.Variables> inVars;
139+
output DAE.Exp outExp;
140+
output list<BackendDAE.Variables> outVars = inVars;
141+
algorithm
142+
outExp := Expression.traverseExpBottomUp(inExp, simplifyInStreamWork2, outVars);
143+
end simplifyInStreamWork;
144+
145+
protected function simplifyInStreamWork2
146+
input DAE.Exp inExp;
147+
input list<BackendDAE.Variables> inVars;
148+
output DAE.Exp outExp;
149+
output list<BackendDAE.Variables> outVars = inVars;
150+
algorithm
151+
outExp := match(inExp)
152+
local DAE.Type tp;
153+
DAE.ComponentRef cr;
154+
DAE.Exp e, expr;
155+
Option<DAE.Exp> eMin;
156+
Option<DAE.Exp> eMax;
157+
Boolean isZero;
158+
159+
160+
case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e as DAE.CREF(componentRef=cr), expr})
161+
algorithm
162+
(eMin, eMax) := simplifyInStreamWorkExpresion(cr, outVars);
163+
isZero := simplifyInStreamWorkSimplify(eMin, false);
164+
tp := ComponentReference.crefTypeFull(cr);
165+
then
166+
if isZero then e else Expression.makePureBuiltinCall("max", {e, expr}, tp);
167+
168+
case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e as DAE.UNARY(DAE.UMINUS(tp), DAE.CREF(componentRef=cr)), expr})
169+
algorithm
170+
(eMin, eMax) := simplifyInStreamWorkExpresion(cr, outVars);
171+
isZero := simplifyInStreamWorkSimplify(eMax, true);
172+
then
173+
if isZero then Expression.createZeroExpression(tp) else Expression.makePureBuiltinCall("max", {e, expr}, tp);
174+
175+
case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e, expr})
176+
guard Expression.isZero(e)
177+
then e;
178+
179+
case DAE.CALL(path=Absyn.IDENT("$OMC$PositiveMax"),expLst={e, expr})
180+
//algorithm
181+
//print("\nsimplifyInStreamWork: ");
182+
//print(ExpressionDump.printExpStr(inExp));
183+
//print(" <-> ");
184+
//print(ExpressionDump.printExpStr(e));
185+
186+
then Expression.makePureBuiltinCall("max", {e, expr}, Expression.typeof(e));
187+
188+
case _
189+
then inExp;
190+
end match;
191+
192+
end simplifyInStreamWork2;
193+
194+
protected function simplifyInStreamWorkExpresion
195+
input DAE.ComponentRef cr;
196+
input list<BackendDAE.Variables> inVars;
197+
output Option<DAE.Exp> outMin = NONE();
198+
output Option<DAE.Exp> outMax = NONE();
199+
protected
200+
BackendDAE.Var v;
201+
algorithm
202+
for vars in inVars loop
203+
try
204+
(v, _) := BackendVariable.getVarSingle(cr, vars);
205+
(outMin, outMax) := BackendVariable.getMinMaxAttribute(v);
206+
break;
207+
else
208+
// search
209+
end try;
210+
end for;
211+
212+
end simplifyInStreamWorkExpresion;
213+
214+
protected function simplifyInStreamWorkSimplify
215+
input Option<DAE.Exp> bound;
216+
input Boolean neg;
217+
output Boolean isZero;
218+
algorithm
219+
isZero := match bound
220+
local Real r;
221+
case SOME(DAE.RCONST(r))
222+
then if neg then r<= 0.0 else r >= 0.0;
223+
case _
224+
then false;
225+
end match;
226+
end simplifyInStreamWorkSimplify;
227+
228+
113229
// =============================================================================
114230
// simplify time independent function calls
115231
//

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7495,6 +7495,7 @@ public function allPreOptimizationModules
74957495
(BackendDAEOptimize.removeUnusedVariables, "removeUnusedVariables"),
74967496
(BackendDAEOptimize.residualForm, "residualForm"),
74977497
(BackendDAEOptimize.simplifyAllExpressions, "simplifyAllExpressions"),
7498+
(BackendDAEOptimize.simplifyInStream, "simplifyInStream"),
74987499
(BackendDump.dumpDAE, "dumpDAE"),
74997500
(XMLDump.dumpDAEXML, "dumpDAEXML"),
75007501
// This should indeed be at the end
@@ -7621,6 +7622,10 @@ algorithm
76217622
preOptModules := getPreOptModulesString();
76227623
preOptModules := Util.getOptionOrDefault(inPreOptModules, preOptModules);
76237624

7625+
if isSome(getGlobalRoot(Global.isInStream)) then
7626+
enabledModules := "simplifyInStream"::enabledModules;
7627+
end if;
7628+
76247629
if Flags.getConfigBool(Flags.DEFAULT_OPT_MODULES_ORDERING) then
76257630
// handle special flags, which enable modules
76267631

Compiler/FrontEnd/ConnectUtil.mo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import PrefixUtil;
7474
import System;
7575
import Types;
7676
import Util;
77+
import Global;
7778

7879
// Import some types from Connect.
7980
import Connect.Face;
@@ -1408,6 +1409,7 @@ protected
14081409
Boolean has_stream, has_expandable, has_cardinality;
14091410
ConnectionGraph.DaeEdges broken, connected;
14101411
algorithm
1412+
setGlobalRoot(Global.isInStream, NONE());
14111413
if not topScope then
14121414
return;
14131415
end if;
@@ -2321,7 +2323,7 @@ algorithm
23212323
end if;
23222324

23232325
positiveMaxCall :=
2324-
DAE.CALL(Absyn.IDENT("max"), {flowExp, flow_threshold},
2326+
DAE.CALL(Absyn.IDENT("$OMC$PositiveMax"), {flowExp, flow_threshold},
23252327
DAE.CALL_ATTR(
23262328
ty,
23272329
false,
@@ -2330,6 +2332,8 @@ algorithm
23302332
false,
23312333
DAE.NO_INLINE(),
23322334
DAE.NO_TAIL()));
2335+
2336+
setGlobalRoot(Global.isInStream, SOME(true));
23332337
end makePositiveMaxCall;
23342338

23352339
protected function evaluateConnectionOperators

Compiler/Global/Global.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ constant Integer currentInstVar = 22;
6464
constant Integer operatorOverloadingCache = 23;
6565
constant Integer optionSimCode = 24;
6666
constant Integer interactiveCache = 25;
67+
constant Integer isInStream = 26;
6768

6869
// indexes in System.tick
6970
// ----------------------

0 commit comments

Comments
 (0)