From 2d6d9f071455a1e6404e52845d523d9a18dbeaeb Mon Sep 17 00:00:00 2001 From: Jens Frenkel Date: Mon, 31 Dec 2012 00:55:23 +0000 Subject: [PATCH] - initialization: bugfix inline inaktive whenstatment - SimCode: hack for if-equation to if-exp when variable to solve for is not present in all branches git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14596 f25d12d1-65f4-0310-ae8a-bbce733d8d8e --- Compiler/BackEnd/Initialization.mo | 12 ++++----- Compiler/BackEnd/SimCodeUtil.mo | 42 +++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Compiler/BackEnd/Initialization.mo b/Compiler/BackEnd/Initialization.mo index d482bba5b84..4f7370e7f73 100644 --- a/Compiler/BackEnd/Initialization.mo +++ b/Compiler/BackEnd/Initialization.mo @@ -390,12 +390,12 @@ algorithm list> crintLst; case ({},_,_,_) then (listReverse(inAcc),iLeftCrs); // single inactive when equation during initialization - case ((stmt as DAE.STMT_WHEN(exp=condition, elseWhen=NONE()))::{},true,_,_) + case ((stmt as DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=NONE()))::{},true,_,_) equation false = Expression.containsInitialCall(condition, false); - crefLst = CheckModel.algorithmStatementListOutputs({stmt}); + crefLst = CheckModel.algorithmStatementListOutputs(stmts); crintLst = List.map1(crefLst,Util.makeTuple,1); - leftCrs = List.fold(crintLst,BaseHashTable.add,iLeftCrs); + leftCrs = List.fold(crefLst,addWhenLeftCr,iLeftCrs); then ({},leftCrs); // when equation during initialization @@ -1012,6 +1012,7 @@ algorithm /* print("Trying to fix over-determined initial system Variables " +& intString(nVars) +& " Equations " +& intString(nEqns) +& "... [not implemented yet!]"); (system,m,mt,_,mapIncRowEqn) = BackendDAEUtil.getIncidenceMatrixScalar(isyst,BackendDAE.NORMAL()); + BackendDump.printEqSystem(system); vec1 = arrayCreate(nVars,-1); vec2 = arrayCreate(nEqns,-1); Matching.matchingExternalsetIncidenceMatrix(nVars,nEqns,m); @@ -1022,10 +1023,9 @@ print("Trying to fix over-determined initial system Variables " +& intString(nVa BackendDump.dumpMatching(vec2); system = BackendDAEUtil.setEqSystemMatching(system,BackendDAE.MATCHING(vec1,vec2,{})); BackendDump.printEqSystem(system); - - +*/ Debug.fcall2(Flags.PEDANTIC, BackendDump.dumpEqSystem, system, "Trying to fix over-determined initial system"); -*/ msg = "Trying to fix over-determined initial system Variables " +& intString(nVars) +& " Equations " +& intString(nEqns) +& "... [not implemented yet!]"; + msg = "Trying to fix over-determined initial system Variables " +& intString(nVars) +& " Equations " +& intString(nEqns) +& "... [not implemented yet!]"; Error.addCompilerWarning(msg); then fail(); diff --git a/Compiler/BackEnd/SimCodeUtil.mo b/Compiler/BackEnd/SimCodeUtil.mo index 254c023d5bb..86a844b9acf 100644 --- a/Compiler/BackEnd/SimCodeUtil.mo +++ b/Compiler/BackEnd/SimCodeUtil.mo @@ -2259,7 +2259,7 @@ algorithm list resEqs; list wcl; DAE.ComponentRef left,varOutput; - DAE.Exp e1,e2,varexp,exp_,right,cond; + DAE.Exp e1,e2,varexp,exp_,right,cond,prevarexp; list> conditionsWithHindex; BackendDAE.WhenEquation whenEquation,elseWhen; String algStr,message,eqStr; @@ -2339,6 +2339,26 @@ algorithm (resEqs,uniqueEqIndex) = addAssertEqn(asserts,{SimCode.SES_SIMPLE_ASSIGN(iuniqueEqIndex,cr, exp_, source)},iuniqueEqIndex+1); then (resEqs,uniqueEqIndex,itempvars); + + // single equation from if-equation -> 0.0 = if .. then bla else lbu and var is not in all branches + // change branches without variable to var - pre(var) + case (_, _, + BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns),_, + _, false, _,_,_) + equation + BackendDAE.EQUATION(exp= e1 as DAE.RCONST(_), scalar=e2 as DAE.IFEXP(expCond=_), source=source) = BackendDAEUtil.equationNth(eqns, eqNum-1); + (v as BackendDAE.VAR(varName = cr, values=values)) = BackendVariable.getVarAt(vars,varNum); + varexp = Expression.crefExp(cr); + varexp = Debug.bcallret1(BackendVariable.isStateVar(v), Expression.expDer, varexp, varexp); + failure((_,_) = ExpressionSolve.solve(e1, e2, varexp)); + prevarexp = Expression.makeBuiltinCall("pre",{varexp},Expression.typeof(varexp)); + prevarexp = Expression.expSub(varexp,prevarexp); + ((e2,_)) = Expression.traverseExp(e2,replaceIFBrancheswithoutVar,(varexp,prevarexp)); + eqn = BackendDAE.EQUATION(e1,e2,source,false); + (resEqs,uniqueEqIndex,tempvars) = createNonlinearResidualEquations({eqn},iuniqueEqIndex,itempvars); + cr = Debug.bcallret1(BackendVariable.isStateVar(v), ComponentReference.crefPrefixDer, cr, cr); + then + ({SimCode.SES_NONLINEAR(uniqueEqIndex, resEqs, {cr}, 0, NONE())},uniqueEqIndex+1,tempvars); // non-linear case (_, _, @@ -2454,6 +2474,26 @@ algorithm end matchcontinue; end createEquation; +protected function replaceIFBrancheswithoutVar + input tuple> inExp; + output tuple> outExp; +algorithm + outExp := match(inExp) + local + DAE.Exp exp,crexp,cond,e1,e2; + Boolean b; + case((DAE.IFEXP(cond,e1,e2),(crexp,exp))) + equation + b = Expression.expContains(e1,crexp); + e1 = Util.if_(b,e1,exp); + b = Expression.expContains(e2,crexp); + e2 = Util.if_(b,e2,exp); + then + ((DAE.IFEXP(cond,e1,e2),(crexp,exp))); + else then inExp; + end match; +end replaceIFBrancheswithoutVar; + protected function solveAlgorithmInverse input list inStmts; input list solveFor;