diff --git a/Compiler/BackEnd/SymbolicJacobian.mo b/Compiler/BackEnd/SymbolicJacobian.mo index f1c8e92e4c..d80ad04119 100644 --- a/Compiler/BackEnd/SymbolicJacobian.mo +++ b/Compiler/BackEnd/SymbolicJacobian.mo @@ -1451,7 +1451,7 @@ protected function getSparsePattern2 protected list localList; algorithm - localList := getSparsePatternHelp(inInputVars, invarSparse, inMark, inUsed, inmarkValue, {}); + localList := getSparsePatternHelp(inInputVars, invarSparse, inMark, inUsed, inmarkValue); List.map2_0(inSolvedVars, Array.updateIndexFirst, localList, invarSparse); List.map2_0(inEqns, Array.updateIndexFirst, localList, ineqnSparse); end getSparsePattern2; @@ -1462,47 +1462,32 @@ protected function getSparsePatternHelp input array inMark; input array inUsed; input Integer inmarkValue; - input list inLocalList; - output list outLocalList; -algorithm - outLocalList := match (inInputVars, invarSparse, inMark, inUsed, inmarkValue, inLocalList) - local - list localList, varSparse, rest; - Integer arrayElement, var; - case ({},_,_,_,_,_) then inLocalList; - case (var::rest,_,_,_,_,_) - equation - arrayElement = arrayGet(inUsed, var); - localList = if intEq(1, arrayElement) then getSparsePatternHelp2(var, inMark, inmarkValue, inLocalList) else inLocalList; + output list outLocalList = {}; +protected + Integer arrayElement; + list varSparse; +algorithm + for var in inInputVars loop + arrayElement := arrayGet(inUsed, var); + if intEq(1, arrayElement) then + arrayElement := arrayGet(inMark, var); + if not intEq(inmarkValue, arrayElement) then + arrayUpdate(inMark, var, inmarkValue); + outLocalList := var::outLocalList; + end if; + end if; - varSparse = arrayGet(invarSparse, var); - localList = List.fold2(varSparse, getSparsePatternHelp2, inMark, inmarkValue, localList); - localList = getSparsePatternHelp(rest, invarSparse, inMark, inUsed, inmarkValue, localList); - then localList; - end match; + varSparse := arrayGet(invarSparse, var); + for v in varSparse loop + arrayElement := arrayGet(inMark, v); + if not intEq(inmarkValue, arrayElement) then + arrayUpdate(inMark, v, inmarkValue); + outLocalList := v::outLocalList; + end if; + end for; + end for; end getSparsePatternHelp; -protected function getSparsePatternHelp2 - input Integer inInputVar; - input array inMark; - input Integer inmarkValue; - input list inLocalList; - output list outLocalList; -algorithm - outLocalList := matchcontinue(inInputVar, inMark, inmarkValue, inLocalList) - local - Integer arrayElement; - case (_,_,_,_) - equation - arrayElement = arrayGet(inMark, inInputVar); - false = intEq(inmarkValue, arrayElement); - arrayUpdate(inMark, inInputVar, inmarkValue); - then inInputVar::inLocalList; - else - then inLocalList; - end matchcontinue; -end getSparsePatternHelp2; - public function transposeSparsePattern input list> inSparsePattern; input array> inAccumList; diff --git a/Compiler/FrontEnd/ExpressionSimplify.mo b/Compiler/FrontEnd/ExpressionSimplify.mo index 177733d17a..ca6192595c 100644 --- a/Compiler/FrontEnd/ExpressionSimplify.mo +++ b/Compiler/FrontEnd/ExpressionSimplify.mo @@ -2864,15 +2864,17 @@ protected function simplifyAddJoinTerms output list> outTplExpRealLst = {}; protected list> tplExpRealLst = inTplExpRealLst; + tuple t; DAE.Exp e; Real coeff, coeff2; algorithm while not listEmpty(tplExpRealLst) loop - (e, coeff) :: tplExpRealLst := tplExpRealLst; + t :: tplExpRealLst := tplExpRealLst; + (e, coeff) := t; (coeff2, tplExpRealLst) := simplifyAddJoinTermsFind(e, tplExpRealLst); coeff := coeff + coeff2; - outTplExpRealLst := (e, coeff) :: outTplExpRealLst; + outTplExpRealLst := (if coeff2==0 then t else (e, coeff)) :: outTplExpRealLst; end while; //outTplExpRealLst := listReverse(outTplExpRealLst); end simplifyAddJoinTerms; @@ -2882,31 +2884,21 @@ protected function simplifyAddJoinTermsFind Helper function to simplifyAddJoinTerms, finds all occurences of Expression." input DAE.Exp inExp; input list> inTplExpRealLst; - output Real outReal; - output list> outTplExpRealLst; + output Real outReal = 0.0; + output list> outTplExpRealLst = {}; +protected + DAE.Exp e; + Real coeff; algorithm - (outReal,outTplExpRealLst) := matchcontinue (inExp,inTplExpRealLst) - local - Real coeff2,coeff3,coeff; - list> res,rest; - DAE.Exp e,e2; - - case (_,{}) then (0.0,{}); - - case (e,((e2,coeff) :: rest)) - equation - true = Expression.expEqual(e, e2); - (coeff2,res) = simplifyAddJoinTermsFind(e, rest); - coeff3 = coeff + coeff2; - then - (coeff3,res); - - case (e,((e2,coeff) :: rest)) /* not Expression.expEqual */ - equation - (coeff2,res) = simplifyAddJoinTermsFind(e, rest); - then - (coeff2,((e2,coeff) :: res)); - end matchcontinue; + for t in inTplExpRealLst loop + (e,coeff) := t; + if Expression.expEqual(inExp, e) then + outReal := outReal + coeff; + else + outTplExpRealLst := t::outTplExpRealLst; + end if; + end for; + outTplExpRealLst := Dangerous.listReverseInPlace(outTplExpRealLst); end simplifyAddJoinTermsFind; protected function simplifyAddMakeMul @@ -2919,7 +2911,6 @@ protected tuple tplExpReal; algorithm for tplExpReal in inTplExpRealLst loop - outExpLst := matchcontinue (tplExpReal) local DAE.Exp e; @@ -2951,14 +2942,14 @@ protected function simplifyBinaryAddCoeff2 input DAE.Exp inExp; output tuple outRes; algorithm - outRes := matchcontinue (inExp) + outRes := match (inExp) local DAE.Exp exp,e1,e2,e; Real coeff,coeff_1; Integer icoeff; Type tp; - case ((exp as DAE.CREF())) then ((exp,1.0)); + case (DAE.CREF()) then ((inExp,1.0)); case (DAE.UNARY(operator = DAE.UMINUS(ty = DAE.T_REAL()), exp = exp)) equation @@ -2985,14 +2976,14 @@ algorithm ((e1,coeff_1)); case (DAE.BINARY(exp1 = e1,operator = DAE.ADD(),exp2 = e2)) - equation - true = Expression.expEqual(e1, e2); + guard + Expression.expEqual(e1, e2) then ((e1,2.0)); else ((inExp,1.0)); - end matchcontinue; + end match; end simplifyBinaryAddCoeff2; protected function simplifyBinaryMulCoeff2