Skip to content

Commit

Permalink
- fix tests, switch of tearing for some models because nonlinear solv…
Browse files Browse the repository at this point in the history
…er has trouble see Ticket #1804

- improve adiacency matrix, check if expressions branch variables
- fix bug for determinant calculation in dynamic state selection
- fix bug for temp variables in simcode

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12896 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Sep 12, 2012
1 parent 7ee040b commit f77d75f
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 14 deletions.
1 change: 0 additions & 1 deletion Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -68,7 +68,6 @@ protected import SCode;
protected import System;
protected import Util;


public function lower
"function: lower
This function translates a DAE, which is the result from instantiating a
Expand Down
172 changes: 171 additions & 1 deletion Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -6088,7 +6088,7 @@ algorithm
Boolean b,bs;
Integer mark;
array<Integer> rowmark;

BackendDAE.BinTree bt;
case ((e as DAE.LUNARY(exp = e1),(vars,bs,(mark,rowmark),pa)))
equation
((_,(vars,_,_,pa))) = Expression.traverseExpTopDown(e1, traversingadjacencyRowExpSolvableEnhancedFinder, (vars,true,(mark,rowmark),pa));
Expand All @@ -6108,6 +6108,10 @@ algorithm
((_,(vars,_,_,pa))) = Expression.traverseExpTopDown(e1, traversingadjacencyRowExpSolvableEnhancedFinder, (vars,bs,(mark,rowmark),pa));
((_,(vars,_,_,pa))) = Expression.traverseExpTopDown(e2, traversingadjacencyRowExpSolvableEnhancedFinder, (vars,bs,(mark,rowmark),pa));
((_,(vars,_,_,pa))) = Expression.traverseExpTopDown(e3, traversingadjacencyRowExpSolvableEnhancedFinder, (vars,true,(mark,rowmark),pa));
// mark all vars which are not in alle branches unsolvable
((_,bt)) = Expression.traverseExpTopDown(e,getIfExpBranchVarOccurency,BackendDAE.emptyBintree);
((_,(_,_,_,_))) = Expression.traverseExp(e1,markBranchVars,(mark,rowmark,vars,bt));
((_,(_,_,_,_))) = Expression.traverseExp(e2,markBranchVars,(mark,rowmark,vars,bt));
then
((e,false,(vars,bs,(mark,rowmark),pa)));
case ((e as DAE.RANGE(start = e1,step=NONE(),stop=e2),(vars,bs,(mark,rowmark),pa)))
Expand Down Expand Up @@ -6155,7 +6159,173 @@ algorithm
case ((e,(vars,bs,(mark,rowmark),pa))) then ((e,true,(vars,bs,(mark,rowmark),pa)));
end matchcontinue;
end traversingadjacencyRowExpSolvableEnhancedFinder;

protected function markBranchVars
"Author: Frenkel TUD 2012-09
mark all vars of a if expression which are not in all branches as unsolvable"
input tuple<DAE.Exp, tuple<Integer,array<Integer>,BackendDAE.Variables,BackendDAE.BinTree>> inTuple;
output tuple<DAE.Exp, tuple<Integer,array<Integer>,BackendDAE.Variables,BackendDAE.BinTree>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e;
BackendDAE.Variables vars;
DAE.ComponentRef cr;
BackendDAE.BinTree bt;
list<Integer> ilst;
Integer mark;
array<Integer> rowmark;
list<BackendDAE.Var> backendVars;

// special case for time, it is never part of the equation system
case ((e as DAE.CREF(componentRef = DAE.CREF_IDENT(ident="time")),(mark,rowmark,vars,bt)))
then ((e, (mark,rowmark,vars,bt)));

// case for functionpointers
case ((e as DAE.CREF(ty=DAE.T_FUNCTION_REFERENCE_FUNC(builtin=_)),(mark,rowmark,vars,bt)))
then
((e, (mark,rowmark,vars,bt)));

// mark if not in bt
case ((e as DAE.CREF(componentRef = cr),(mark,rowmark,vars,bt)))
equation
(backendVars,ilst) = BackendVariable.getVar(cr, vars);
markBranchVars1(backendVars,ilst,mark,rowmark,bt);
then
((e, (mark,rowmark,vars,bt)));

case inTuple then inTuple;
end matchcontinue;
end markBranchVars;

protected function markBranchVars1
"Author: Frenkel TUD 2012-09
Helper for markBranchVars"
input list<BackendDAE.Var> varlst;
input list<Integer> iIlst;
input Integer mark;
input array<Integer> rowmark;
input BackendDAE.BinTree bt;
algorithm
_ := matchcontinue(varlst,iIlst,mark,rowmark,bt)
local
DAE.ComponentRef cr;
list<BackendDAE.Var> vlst;
Integer i;
list<Integer> ilst;
case({},_,_,_,_) then ();
case(BackendDAE.VAR(varName=cr)::vlst,_::ilst,_,_,_)
equation
_ = treeGet(bt,cr);
markBranchVars1(vlst,ilst,mark,rowmark,bt);
then
();
case(_::vlst,i::ilst,_,_,_)
equation
_ = arrayUpdate(rowmark,i,-mark);
markBranchVars1(vlst,ilst,mark,rowmark,bt);
then
();
end matchcontinue;
end markBranchVars1;

protected function getIfExpBranchVarOccurency
"Author: Frenkel TUD 2012-09
Helper for getIfExpBranchVarOccurency"
input tuple<DAE.Exp, BackendDAE.BinTree> inTpl "(exp,allbranchvars)";
output tuple<DAE.Exp, Boolean, BackendDAE.BinTree> outTpl;
algorithm
outTpl := match(inTpl)
local
DAE.ComponentRef cr;
DAE.Exp e,e1,e2;
BackendDAE.BinTree bt,bt_then,bt_else;
Boolean b;
list<DAE.Exp> elst;
case ((e as DAE.IFEXP(expThen = e1,expElse = e2),bt))
equation
((_,bt_then)) = Expression.traverseExpTopDown(e1,getIfExpBranchVarOccurency,BackendDAE.emptyBintree);
((_,bt_else)) = Expression.traverseExpTopDown(e2,getIfExpBranchVarOccurency,BackendDAE.emptyBintree);
bt = binTreeintersection(bt_then,bt_else,bt);
then
((e,false,bt));
// skip relations,ranges,asubs
case ((e as DAE.LUNARY(exp = _),bt))
then ((e,false,bt));
case ((e as DAE.LBINARY(exp1 = _),bt))
then ((e,false,bt));
case ((e as DAE.RELATION(exp1 = _),bt))
then ((e,false,bt));
case ((e as DAE.RANGE(start = _),bt))
then ((e,false,bt));
case ((e as DAE.RANGE(start = _),bt))
then ((e,false,bt));
case ((e as DAE.ASUB(exp = e1,sub=elst),bt))
equation
((_,bt)) = Expression.traverseExpTopDown(e1, getIfExpBranchVarOccurency, bt);
then ((e,false,bt));
// add crefs
case ((e as DAE.CREF(componentRef = cr),bt))
equation
bt = treeAdd(bt,cr,0);
then
((e,false,bt));
case ((e as DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}),bt))
equation
bt = treeAdd(bt,cr,0);
then
((e,false,bt));
case ((e as DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}),bt))
equation
bt = treeAdd(bt,cr,0);
then
((e,false,bt));
// pre(v) is considered a known variable
case ((e as DAE.CALL(path = Absyn.IDENT(name = "pre"),expLst = {DAE.CREF(componentRef = cr)}),bt)) then ((e,false,bt));
// delay(e) can be used to break algebraic loops given some solver options
case ((e as DAE.CALL(path = Absyn.IDENT(name = "delay"),expLst = {_,_,e1,e2}),bt))
equation
b = Flags.isSet(Flags.DELAY_BREAK_LOOP) and Expression.expEqual(e1,e2);
then ((e,not b,bt));
case ((e,bt)) then ((e,true,bt));
end match;
end getIfExpBranchVarOccurency;

protected function binTreeintersection
"Author: Frenkel TUD 2012-09
at all key member of bt1 and bt2 to iBt"
input BackendDAE.BinTree bt1;
input BackendDAE.BinTree bt2;
input BackendDAE.BinTree iBt;
output BackendDAE.BinTree oBt;
protected
list<DAE.ComponentRef> keys;
algorithm
(keys,_) := bintreeToList(bt1);
oBt := List.fold1(keys, binTreeintersection1, bt2,iBt);
end binTreeintersection;

protected function binTreeintersection1
"Author: Frenkel TUD 2012-09
Helper for binTreeintersection1"
input DAE.ComponentRef key;
input BackendDAE.BinTree bt2;
input BackendDAE.BinTree iBt;
output BackendDAE.BinTree oBt;
algorithm
oBt := matchcontinue(key,bt2,iBt)
local
BackendDAE.BinTree bt;
case(_,_,_)
equation
_ = treeGet(bt2,key);
bt = treeAdd(iBt,key,0);
then
bt;
else then iBt;
end matchcontinue;
end binTreeintersection1;

protected function adjacencyRowExpEnhanced1
"function: adjacencyRowExpEnhanced1
author: Frenkel TUD 2012-05
Expand Down
15 changes: 14 additions & 1 deletion Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -222,6 +222,19 @@ algorithm
(_,(_,outVars)) := traverseBackendDAEExpsEqn(inEquation,checkEquationsVars,(inVars,outVars));
end equationVars;

public function expressionVars
"function: equationVars
author: Frenkel TUD 2012-03
From the expression and a variable array return all
variables in the expression."
input DAE.Exp inExp;
input BackendDAE.Variables inVars;
output BackendDAE.Variables outVars;
algorithm
outVars := BackendDAEUtil.emptyVars();
((_,(_,outVars))) := Expression.traverseExp(inExp,checkEquationsVarsExp,(inVars,outVars));
end expressionVars;

protected function checkEquationsVars
input tuple<DAE.Exp, tuple<BackendDAE.Variables,BackendDAE.Variables>> inTpl;
output tuple<DAE.Exp, tuple<BackendDAE.Variables,BackendDAE.Variables>> outTpl;
Expand Down Expand Up @@ -1097,7 +1110,7 @@ algorithm
case (_,BackendDAE.EQUATION_ARRAY(size=size,numberOfElement = n,arrSize = arrsize,equOptArr = arr))
equation
print("- BackendEquation.equationAdd failed\nArraySize: " +& intString(arrsize) +&
"\nnumberOfElement " +& intString(n) +& "\nSize " +& intString(size));
"\nnumberOfElement " +& intString(n) +& "\nSize " +& intString(size) +& "\narraySize " +& intString(arrayLength(arr)));
then
fail();
end matchcontinue;
Expand Down
8 changes: 4 additions & 4 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2358,7 +2358,7 @@ public function removeVar
"function: removeVar
author: Frenkel TUD 2011-04
Removes a var from the vararray but does not scaling down the array"
input Integer inVarPos;
input Integer inVarPos "1 based index";
input BackendDAE.Variables inVariables;
output BackendDAE.Variables outVariables;
output BackendDAE.Var outVar;
Expand Down Expand Up @@ -2411,14 +2411,14 @@ algorithm

case (BackendDAE.VARIABLE_ARRAY(numberOfElements = n,arrSize = size,varOptArr = arr),pos)
equation
(pos < size) = true;
(pos <= size) = true;
SOME(v) = arr[pos];
arr_1 = arrayUpdate(arr, pos, NONE());
then
(v,BackendDAE.VARIABLE_ARRAY(n,size,arr_1));
case (_,_)
case (BackendDAE.VARIABLE_ARRAY(numberOfElements = n,arrSize = size,varOptArr = arr),_)
equation
print("- BackendVariable.removeVar1 failed\n");
print("- BackendVariable.removeVar1 failed\n Pos " +& intString(inInteger) +& " numberOfElements " +& intString(n) +& " size " +& intString(size) +& " arraySize " +& intString(arrayLength(arr)) +& "\n");
then
fail();
end matchcontinue;
Expand Down
3 changes: 2 additions & 1 deletion Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -2388,7 +2388,8 @@ algorithm
acc = getDeterminants1(rest,jac,unassigned-1,size+1,digraphLst,select,unusedStates,iAcc);
_ = arrayUpdate(select,i,-1);
then
getDeterminants(rest,jac,unassigned,size,digraphLst,select,i::unusedStates,acc);
acc;
//getDeterminants(rest,jac,unassigned,size,digraphLst,select,i::unusedStates,acc);
case ((cr,i)::rest,_,_,_,_,_,_,_)
equation
false = intGe(listLength(rest),unassigned);
Expand Down
12 changes: 6 additions & 6 deletions Compiler/BackEnd/SimCodeUtil.mo
Expand Up @@ -3943,7 +3943,7 @@ algorithm
//true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
(tp as DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(path))) = Expression.typeof(e1);
// tmp
ident = Absyn.pathString(path);
ident = System.stringReplace(Absyn.pathString(path),".","_");
crtmp = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
tempvars = greateTempVars(varLst,crtmp,itempvars);
// 0 = a - tmp
Expand Down Expand Up @@ -3971,7 +3971,7 @@ algorithm
//((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
(tp as DAE.T_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(path))) = Expression.typeof(e2);
// tmp
ident = Absyn.pathString(path);
ident = System.stringReplace(Absyn.pathString(path),".","_");
crtmp = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
tempvars = greateTempVars(varLst,crtmp,itempvars);
// 0 = a - tmp
Expand All @@ -3998,7 +3998,7 @@ algorithm
((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
//true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = Absyn.pathString(path);
ident = System.stringReplace(Absyn.pathString(path),".","_");
cr = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e1_1 = Expression.crefExp(cr);
stms = DAE.STMT_ASSIGN(tp,e1_1,e2_1,source);
Expand Down Expand Up @@ -4026,7 +4026,7 @@ algorithm
((e1_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
//true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = Absyn.pathString(path);
ident = System.stringReplace(Absyn.pathString(path),".","_");
cr = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e2_1 = Expression.crefExp(cr);
stms = DAE.STMT_ASSIGN(tp,e2_1,e1_1,source);
Expand Down Expand Up @@ -5622,7 +5622,7 @@ algorithm
((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
//true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = Absyn.pathString(path);
ident = System.stringReplace(Absyn.pathString(path),".","_");
cr1 = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e1_1 = Expression.crefExp(cr1);
stms = DAE.STMT_ASSIGN(tp,e1_1,e2_1,source);
Expand All @@ -5647,7 +5647,7 @@ algorithm
((e1_1,(_,_))) = BackendDAEUtil.extendArrExp((e1,(NONE(),false)));
//true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
// tmp = f()
ident = Absyn.pathString(path);
ident = System.stringReplace(Absyn.pathString(path),".","_");
cr1 = ComponentReference.makeCrefIdent("$TMP_" +& ident +& intString(iuniqueEqIndex), tp, {});
e2_1 = Expression.crefExp(cr1);
stms = DAE.STMT_ASSIGN(tp,e2_1,e1_1,source);
Expand Down

0 comments on commit f77d75f

Please sign in to comment.