@@ -110,6 +110,122 @@ algorithm
110110 outDAE. shared := shared ;
111111end 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//
0 commit comments