Skip to content

Commit

Permalink
- initialization: bugfix inline inaktive whenstatment
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Jens Frenkel committed Dec 31, 2012
1 parent 7da6cfa commit 2d6d9f0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
12 changes: 6 additions & 6 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -390,12 +390,12 @@ algorithm
list<tuple<DAE.ComponentRef,Integer>> 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
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
42 changes: 41 additions & 1 deletion Compiler/BackEnd/SimCodeUtil.mo
Expand Up @@ -2259,7 +2259,7 @@ algorithm
list<SimCode.SimEqSystem> resEqs;
list<BackendDAE.WhenClause> wcl;
DAE.ComponentRef left,varOutput;
DAE.Exp e1,e2,varexp,exp_,right,cond;
DAE.Exp e1,e2,varexp,exp_,right,cond,prevarexp;
list<tuple<DAE.Exp, Integer>> conditionsWithHindex;
BackendDAE.WhenEquation whenEquation,elseWhen;
String algStr,message,eqStr;
Expand Down Expand Up @@ -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 (_, _,
Expand Down Expand Up @@ -2454,6 +2474,26 @@ algorithm
end matchcontinue;
end createEquation;

protected function replaceIFBrancheswithoutVar
input tuple<DAE.Exp,tuple<DAE.Exp,DAE.Exp>> inExp;
output tuple<DAE.Exp,tuple<DAE.Exp,DAE.Exp>> 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<DAE.Statement> inStmts;
input list<BackendDAE.Var> solveFor;
Expand Down

0 comments on commit 2d6d9f0

Please sign in to comment.