Skip to content

Commit

Permalink
- add heuristic state selction rule "state=var+param"
Browse files Browse the repository at this point in the history
- Expression.mo: add function traverseCrefsFromExp
- fix testsuite

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8787 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed May 3, 2011
1 parent 81e3f49 commit 66c19a3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 16 deletions.
71 changes: 57 additions & 14 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -2050,16 +2050,16 @@ algorithm
local
DAE.ComponentRef s;
BackendDAE.Value sn;
BackendDAE.Variables vars;
BackendDAE.Variables vars,knvars;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
BackendDAE.EquationArray eqns;
list<tuple<DAE.ComponentRef,Integer,Real>> prioTuples;
BackendDAE.BackendDAE dae;

case (varCrefs,varIndices,BackendDAE.DAE(orderedVars=vars,orderedEqs = eqns),m,mt)
case (varCrefs,varIndices,BackendDAE.DAE(orderedVars=vars,knownVars=knvars,orderedEqs = eqns),m,mt)
equation
prioTuples = calculateVarPriorities(varCrefs,varIndices,vars,eqns,m,mt);
prioTuples = calculateVarPriorities(varCrefs,varIndices,vars,knvars,eqns,m,mt);
//print("priorities:");print(Util.stringDelimitList(Util.listMap(prioTuples,printPrioTuplesStr),","));print("\n");
(s,sn) = selectMinPrio(prioTuples);
then (s,sn);
Expand Down Expand Up @@ -2119,23 +2119,24 @@ protected function calculateVarPriorities
input list<DAE.ComponentRef> varCrefs;
input list<Integer> varIndices;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
input BackendDAE.EquationArray eqns;
input BackendDAE.IncidenceMatrix m;
input BackendDAE.IncidenceMatrixT mt;
output list<tuple<DAE.ComponentRef,Integer,Real>> tuples;
algorithm
tuples := match(varCrefs,varIndices,vars,eqns,m,mt)
tuples := match(varCrefs,varIndices,vars,knvars,eqns,m,mt)
local DAE.ComponentRef varCref;
Integer varIndx;
BackendDAE.Var v;
Real prio,prio1,prio2;
list<tuple<DAE.ComponentRef,Integer,Real>> prios;
case({},{},_,_,_,_) then {};
case (varCref::varCrefs,varIndx::varIndices,vars,eqns,m,mt) equation
prios = calculateVarPriorities(varCrefs,varIndices,vars,eqns,m,mt);
case({},{},_,_,_,_,_) then {};
case (varCref::varCrefs,varIndx::varIndices,vars,knvars,eqns,m,mt) equation
prios = calculateVarPriorities(varCrefs,varIndices,vars,knvars,eqns,m,mt);
(v::_,_) = BackendVariable.getVar(varCref,vars);
prio1 = varStateSelectPrio(v);
prio2 = varStateSelectHeuristicPrio(v,vars,eqns,m,mt);
prio2 = varStateSelectHeuristicPrio(v,vars,knvars,eqns,m,mt);
prio = prio1 +. prio2;
Debug.fcall("dummyselect",BackendDump.debugStrCrefStrRealStrRealStrRealStr,("Calc Prio for ",varCref,"\n Prio StateSelect : ",prio1,"\n Prio Heuristik : ",prio2,"\n ### Prio Result : ",prio,"\n"));
then ((varCref,varIndx,prio)::prios);
Expand All @@ -2160,6 +2161,7 @@ protected function varStateSelectHeuristicPrio
then sd.s_rel should have lower priority than the others."
input BackendDAE.Var v;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
input BackendDAE.EquationArray eqns;
input BackendDAE.IncidenceMatrix m;
input BackendDAE.IncidenceMatrixT mt;
Expand All @@ -2173,7 +2175,7 @@ algorithm
(_,vindx::_) := BackendVariable.getVar(BackendVariable.varCref(v),vars); // Variable index not stored in var itself => lookup required
vEqns := BackendDAEUtil.eqnsForVarWithStates(mt,vindx);
vCr := BackendVariable.varCref(v);
prio1 := varStateSelectHeuristicPrio1(vCr,vEqns,vars,eqns);
prio1 := varStateSelectHeuristicPrio1(vCr,vEqns,vars,knvars,eqns);
Debug.fcall("dummyselect",print," Prio 1 : " +& realString(prio1) +& "\n");
prio2 := varStateSelectHeuristicPrio2(vCr,vars);
Debug.fcall("dummyselect",print," Prio 2 : " +& realString(prio2) +& "\n");
Expand Down Expand Up @@ -2312,25 +2314,31 @@ protected function varStateSelectHeuristicPrio1
input DAE.ComponentRef cr;
input list<Integer> eqnLst;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
input BackendDAE.EquationArray eqns;
output Real prio;
algorithm
prio := matchcontinue(cr,eqnLst,vars,eqns)
prio := matchcontinue(cr,eqnLst,vars,knvars,eqns)
local Integer e; BackendDAE.Equation eqn;
case(cr,{},_,_) then 0.0;
case(cr,e::eqnLst,vars,eqns)
case(cr,{},_,_,_) then 0.0;
case(cr,e::eqnLst,vars,_,eqns)
equation
eqn = BackendDAEUtil.equationNth(eqns,e-1);
true = isStateConstraintEquation(cr,eqn,vars);
then -1.0;
case(cr,_::eqnLst,vars,eqns) then varStateSelectHeuristicPrio1(cr,eqnLst,vars,eqns);
case(cr,e::eqnLst,vars,knvars,eqns)
equation
eqn = BackendDAEUtil.equationNth(eqns,e-1);
true = isStateAssignEquation(cr,eqn,vars,knvars);
then -0.5;
case(cr,_::eqnLst,vars,knvars,eqns) then varStateSelectHeuristicPrio1(cr,eqnLst,vars,knvars,eqns);
end matchcontinue;
end varStateSelectHeuristicPrio1;

protected function isStateConstraintEquation
"function isStateConstraintEquation
author: PA
Help function to varStateSelectHeuristicPrio2
Help function to varStateSelectHeuristicPrio1
Returns true if an equation is on the form cr = expr(s1,s2...sn) for states cr, s1,s2..,sn"
input DAE.ComponentRef cr;
input BackendDAE.Equation eqn;
Expand Down Expand Up @@ -2367,6 +2375,41 @@ algorithm
end matchcontinue;
end isStateConstraintEquation;

protected function isStateAssignEquation
"function isStateAssignEquation
author: Frenkel TUD 2011-04
Help function to varStateSelectHeuristicPrio1
Returns true if an equation is on the form cr = expr(s1,s2...sn,pv1,...,pvn) for states cr, s1,s2..,sn, and parameters pv1,...,pvn "
input DAE.ComponentRef cr;
input BackendDAE.Equation eqn;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
output Boolean res;
algorithm
res := matchcontinue(cr,eqn,vars,knvars)
local
DAE.ComponentRef cr2;
list<DAE.ComponentRef> crs;
list<list<BackendDAE.Var>> crVars,prVars;
DAE.Exp e2;
Boolean b1,b2,b;

case(cr,BackendDAE.EQUATION(exp = DAE.CREF(cr2,_), scalar = e2),vars,knvars)
equation
true = ComponentReference.crefEqualNoStringCompare(cr,cr2);
_::_::_ = Expression.terms(e2);
then true;

case(cr,BackendDAE.EQUATION(exp = e2, scalar = DAE.CREF(cr2,_)),vars,knvars)
equation
true = ComponentReference.crefEqualNoStringCompare(cr,cr2);
_::_::_ = Expression.terms(e2);
then true;

else false;
end matchcontinue;
end isStateAssignEquation;

protected function varStateSelectPrio
"function varStateSelectPrio
Helper function to calculateVarPriorities.
Expand Down
56 changes: 54 additions & 2 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -4081,10 +4081,9 @@ algorithm
Type ty;
DAE.Exp e;

case((DAE.CREF(cr,ty), crefs))
case((e as DAE.CREF(cr,ty), crefs))
equation
crefs = Util.listUnionEltOnTrue(cr,crefs,ComponentReference.crefEqual);
e = makeCrefExp(cr,ty);
then
((e, crefs ));

Expand All @@ -4093,6 +4092,59 @@ algorithm
end matchcontinue;
end traversingComponentRefFinder;

public function traverseCrefsFromExp "
Author: Frenkel TUD 2011-05, traverses all ComponentRef from an Expression."
input DAE.Exp inExp;
input FuncCrefTypeA inFunc;
input Type_a inArg;
output Type_a outArg;
partial function FuncCrefTypeA
input ComponentRef inCref;
input Type_a inArg;
output Type_a outArg;
end FuncCrefTypeA;
replaceable type Type_a subtypeof Any;
algorithm
outArg := match(inExp,inFunc,inArg)
local Type_a arg;
case(inExp,inFunc,inArg)
equation
((_,(_,arg))) = traverseExp(inExp, traversingCrefFinder, (inFunc,inArg));
then
arg;
end match;
end traverseCrefsFromExp;

protected function traversingCrefFinder "
Author: Frenkel TUD 2011-05"
input tuple<DAE.Exp, tuple<FuncCrefTypeA,Type_a> > inExp;
output tuple<DAE.Exp, tuple<FuncCrefTypeA,Type_a> > outExp;
partial function FuncCrefTypeA
input ComponentRef inCref;
input Type_a inArg;
output Type_a outArg;
end FuncCrefTypeA;
replaceable type Type_a subtypeof Any;
algorithm
outExp := matchcontinue(inExp)
local
Type_a arg,arg1;
FuncCrefTypeA func;
ComponentRef cr;
Type ty;
DAE.Exp e;

case((e as DAE.CREF(cr,ty),(func,arg)))
equation
arg1 = func(cr,arg);
then
((e, (func,arg1) ));

case(inExp) then inExp;

end matchcontinue;
end traversingCrefFinder;

public function extractDivExpFromExp "
Author: Frenkel TUD 2010-02, Extracts all Division DAE.Exp from an Expression."
input DAE.Exp inExp;
Expand Down

0 comments on commit 66c19a3

Please sign in to comment.