@@ -116,7 +116,7 @@ end simplifyAllExpressions;
116116// OM introduces $OMC$PosetiveMax which can simplified using min or max attribute
117117// see Modelica spec for inStream
118118// author: Vitalij Ruge
119- // see. #3885, 4441
119+ // see. #3885, 4441, 5104
120120// =============================================================================
121121
122122public function simplifyInStream
@@ -152,26 +152,40 @@ algorithm
152152 local
153153 DAE . Type tp;
154154 DAE . ComponentRef cr;
155- DAE . Exp e, expr, a, b ;
155+ DAE . Exp e, expr, ret ;
156156 Option < DAE . Exp > eMin, eMax;
157- Boolean simplifyToZero;
158157
159158 // positiveMax(cref, eps) = 0 if variable(cref).max <= 0
159+ // positiveMax(cref, eps) = cref if variable(cref).min >= 0
160160 case DAE . CALL (path= Absyn . IDENT ("$OMC$PositiveMax" ),expLst= {e as DAE . CREF (componentRef= cr), expr})
161161 algorithm
162162 (eMin, eMax) := simplifyInStreamWorkExpresion(cr, outVars);
163- simplifyToZero := simplifyInStreamWorkSimplify(eMax, true ); // var.max <= 0.0
164- tp := ComponentReference . crefTypeFull(cr);
163+ if simplifyInStreamWorkSimplify(eMax, true ) then // var.max <= 0.0
164+ tp := ComponentReference . crefTypeFull(cr);
165+ ret := Expression . createZeroExpression(tp);
166+ elseif simplifyInStreamWorkSimplify(eMin, false ) then // var.min >= 0.0
167+ ret := e;
168+ else
169+ tp := ComponentReference . crefTypeFull(cr);
170+ ret := Expression . makePureBuiltinCall("max" , {e, expr}, tp);
171+ end if ;
165172 then
166- if simplifyToZero then Expression . createZeroExpression(tp) else Expression . makePureBuiltinCall( "max" , {e, expr}, tp) ;
173+ ret ;
167174
168175 // positiveMax(-cref, eps) = 0 if variable(cref).min >= 0
176+ // positiveMax(-cref, eps) = -cref if variable(cref).max <= 0
169177 case DAE . CALL (path= Absyn . IDENT ("$OMC$PositiveMax" ),expLst= {e as DAE . UNARY (DAE . UMINUS (tp), DAE . CREF (componentRef= cr)), expr})
170178 algorithm
171179 (eMin, eMax) := simplifyInStreamWorkExpresion(cr, outVars);
172- simplifyToZero := simplifyInStreamWorkSimplify(eMin, false ); // var.min >= 0.0
180+ if simplifyInStreamWorkSimplify(eMin, false ) then // var.min >= 0.0
181+ ret := Expression . createZeroExpression(tp);
182+ elseif simplifyInStreamWorkSimplify(eMax, true ) then // var.max <= 0.0
183+ ret := e;
184+ else
185+ ret := Expression . makePureBuiltinCall("max" , {e, expr}, tp);
186+ end if ;
173187 then
174- if simplifyToZero then Expression . createZeroExpression(tp) else Expression . makePureBuiltinCall( "max" , {e, expr}, tp) ;
188+ ret ;
175189
176190 // positiveMax(cref, eps) = cref where cref >= 0
177191 case DAE . CALL (path= Absyn . IDENT ("$OMC$PositiveMax" ),expLst= {e, expr}) guard Expression . isPositiveOrZero(e)
@@ -189,7 +203,9 @@ algorithm
189203 case DAE . CALL (path= Absyn . IDENT ("$OMC$inStreamDiv" ),expLst= {e, expr})
190204 algorithm
191205 e := ExpressionSimplify . simplify(e);
192- expr := match(e)
206+ ret := match(e)
207+ local
208+ DAE . Exp a,b;
193209
194210 case DAE . BINARY (a, DAE . DIV (), b)
195211 guard Expression . isZero(a) and Expression . isZero(b)
@@ -199,7 +215,7 @@ algorithm
199215
200216 end match;
201217 then
202- expr ;
218+ ret ;
203219
204220 else inExp;
205221 end match;
0 commit comments