Skip to content

Commit

Permalink
- BackendDAEOptimize.getConditionList1: report an error message
Browse files Browse the repository at this point in the history
- minor changes to statesorting
- do not prever internal variables
- add only used prevariables from not discrete variables to initial system

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14887 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jan 22, 2013
1 parent 5253aba commit f4fef63
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
24 changes: 17 additions & 7 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -10836,19 +10836,29 @@ algorithm
list<DAE.ComponentRef> conditionVarList;
Boolean initialCall;
DAE.ComponentRef componentRef;
DAE.Exp exp;
String msg;

case ({}, conditionVarList, initialCall)
then (conditionVarList, initialCall);
case ({}, _, _)
then (inConditionVarList, inInitialCall);

case (DAE.CALL(path = Absyn.IDENT(name = "initial"))::conditionList, conditionVarList, _) equation
(conditionVarList, initialCall) = getConditionList1(conditionList, conditionVarList, true);
case (DAE.CALL(path = Absyn.IDENT(name = "initial"))::conditionList, _, _) equation
(conditionVarList, initialCall) = getConditionList1(conditionList, inConditionVarList, true);
then (conditionVarList, initialCall);

case (DAE.CREF(componentRef=componentRef)::conditionList, conditionVarList, initialCall) equation
(conditionVarList, initialCall) = getConditionList1(conditionList, componentRef::conditionVarList, initialCall);
case (DAE.CREF(componentRef=componentRef)::conditionList, _, _) equation
(conditionVarList, initialCall) = getConditionList1(conditionList, componentRef::inConditionVarList, inInitialCall);
then (conditionVarList, initialCall);

case (DAE.BCONST(_)::conditionList, _, _) equation
(conditionVarList, initialCall) = getConditionList1(conditionList, inConditionVarList, inInitialCall);
then (conditionVarList, initialCall);

else then fail();
case (exp::_,_,_)
equation
msg = "./Compiler/BackEnd/BackendDAEOptimize.mo: function getConditionList1 failed for " +& ExpressionDump.printExpStr(exp) +& "\n";
Error.addMessage(Error.INTERNAL_ERROR, {msg});
then fail();
end matchcontinue;
end getConditionList1;

Expand Down
61 changes: 31 additions & 30 deletions Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -2670,7 +2670,7 @@ algorithm
m1 = incidenceMatrixfromEnhanced2(me,hovvars);
mT1 = BackendDAEUtil.transposeMatrix(m1,nfreeStates);
// BackendDump.printEqSystem(syst);
hovvars = sortStateCandidatesVars(hovvars,BackendVariable.daeVars(isyst),so,SOME(mT1));
hovvars = sortStateCandidatesVars(hovvars,BackendVariable.daeVars(isyst),SOME(mT1));
Debug.fcall(Flags.BLT_DUMP, print, "highest Order Derivatives:\n");
Debug.fcall(Flags.BLT_DUMP, BackendDump.printVariables, hovvars);
Debug.fcall(Flags.BLT_DUMP, BackendDump.printEquationList, eqnslst);
Expand Down Expand Up @@ -3107,7 +3107,7 @@ algorithm

// sort vars with heuristic
vars = BackendVariable.listVar1(varlst);
vars = sortStateCandidatesVars(vars,BackendVariable.daeVars(isyst),so,NONE());
vars = sortStateCandidatesVars(vars,BackendVariable.daeVars(isyst),NONE());
(vars,_) = BackendVariable.traverseBackendDAEVarsWithUpdate(vars,setVarKind,BackendDAE.VARIABLE());

eqns = BackendEquation.listEquation(eqnslst);
Expand Down Expand Up @@ -4266,7 +4266,7 @@ algorithm
Debug.fcall(Flags.BLT_DUMP, print, "try to select dummy vars with natural matching\n");

// sort vars with heuristic
vars1 = sortStateCandidatesVars(vars,BackendVariable.daeVars(isyst),so,NONE());
vars1 = sortStateCandidatesVars(vars,BackendVariable.daeVars(isyst),NONE());

(vars1,_) = BackendVariable.traverseBackendDAEVarsWithUpdate(vars1,setVarKind,BackendDAE.VARIABLE());
syst = BackendDAE.EQSYSTEM(vars1,eqns,NONE(),NONE(),BackendDAE.NO_MATCHING(),{});
Expand Down Expand Up @@ -4316,23 +4316,22 @@ protected function sortStateCandidatesVars
sort the state candidates"
input BackendDAE.Variables inVars;
input BackendDAE.Variables allVars;
input BackendDAE.StateOrder so;
input Option<BackendDAE.IncidenceMatrix> m;
output BackendDAE.Variables outStates;
algorithm
outStates:=
matchcontinue (inVars,allVars,so,m)
matchcontinue (inVars,allVars,m)
local
Integer varsize;
list<Integer> varIndices;
BackendDAE.Variables vars;
list<tuple<DAE.ComponentRef,Integer,Real>> prioTuples;
list<BackendDAE.Var> vlst;

case (_,_,_,_)
case (_,_,_)
equation
varsize = BackendVariable.varsSize(inVars);
prioTuples = calculateVarPriorities(1,inVars,varsize,allVars,so,m,{});
prioTuples = calculateVarPriorities(1,inVars,varsize,allVars,m,{});
prioTuples = List.sort(prioTuples,sortprioTuples);
varIndices = List.map(prioTuples,Util.tuple32);
vlst = List.map1r(varIndices,BackendVariable.getVarAt,inVars);
Expand Down Expand Up @@ -4366,29 +4365,28 @@ protected function calculateVarPriorities
input BackendDAE.Variables vars;
input Integer varsSize;
input BackendDAE.Variables allVars;
input BackendDAE.StateOrder so;
input Option<BackendDAE.IncidenceMatrix> m;
input list<tuple<DAE.ComponentRef,Integer,Real>> iTuples;
output list<tuple<DAE.ComponentRef,Integer,Real>> tuples;
algorithm
tuples := matchcontinue(index,vars,varsSize,allVars,so,m,iTuples)
tuples := matchcontinue(index,vars,varsSize,allVars,m,iTuples)
local
DAE.ComponentRef varCref;
BackendDAE.Var v;
Real prio,prio1,prio2;

case (_,_,_,_,_,_,_)
case (_,_,_,_,_,_)
equation
true = intLe(index,varsSize);
v = BackendVariable.getVarAt(vars,index);
varCref = BackendVariable.varCref(v);
prio1 = varStateSelectPrio(v);
prio2 = varStateSelectHeuristicPrio(v,allVars,so,index,m);
prio2 = varStateSelectHeuristicPrio(v,allVars,index,m);
prio = prio1 +. prio2;
Debug.fcall(Flags.DUMMY_SELECT,BackendDump.debugStrCrefStrRealStrRealStrRealStr,("Calc Prio for ",varCref,"\n Prio StateSelect : ",prio1,"\n Prio Heuristik : ",prio2,"\n ### Prio Result : ",prio,"\n"));
then
calculateVarPriorities(index+1,vars,varsSize,allVars,so,m,(varCref,index,prio)::iTuples);
case (_,_,_,_,_,_,_)
calculateVarPriorities(index+1,vars,varsSize,allVars,m,(varCref,index,prio)::iTuples);
else
equation
false = intLe(index,varsSize);
then
Expand All @@ -4401,7 +4399,6 @@ protected function varStateSelectHeuristicPrio
author: Frenkel TUD 2012-08"
input BackendDAE.Var v;
input BackendDAE.Variables vars;
input BackendDAE.StateOrder so;
input Integer index;
input Option<BackendDAE.IncidenceMatrix> m;
output Real prio;
Expand All @@ -4411,7 +4408,7 @@ algorithm
prio1 := varStateSelectHeuristicPrio1(v);
prio2 := varStateSelectHeuristicPrio2(v);
prio3 := varStateSelectHeuristicPrio3(v);
prio4 := varStateSelectHeuristicPrio4(v,so,vars);
prio4 := varStateSelectHeuristicPrio4(v,vars);
prio5 := varStateSelectHeuristicPrio5(v);
prio6 := varStateSelectHeuristicPrio6(v,index,m);
prio:= prio1 +. prio2 +. prio3 +. prio4 +. prio5 +. prio6;
Expand Down Expand Up @@ -4485,18 +4482,21 @@ protected function varStateSelectHeuristicPrio4
author: Frenkel TUD 2012-08
Helper function to varStateSelectHeuristicPrio.
added prio for states/variables wich are derivatives of deselected states"
input BackendDAE.Var v;
input BackendDAE.StateOrder so;
input BackendDAE.Var inVar;
input BackendDAE.Variables vars;
output Real prio;
algorithm
prio := matchcontinue(v,so,vars)
local DAE.ComponentRef cr,pcr;
case(BackendDAE.VAR(varName=cr),_,_)
prio := matchcontinue(inVar,vars)
local
DAE.ComponentRef cr;
BackendDAE.Var v;
Boolean b;
case(BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(cr))),_)
equation
pcr::_ = BackendDAETransform.getDerStateOrder(cr, so);
(BackendDAE.VAR(varKind=BackendDAE.DUMMY_STATE())::{},_) = BackendVariable.getVar(pcr, vars);
then -1.0;
({v},_) = BackendVariable.getVar(cr, vars);
b = BackendVariable.isDummyStateVar(v);
prio = Util.if_(b,-1.0,3.0);
then prio;
else then 0.0;
end matchcontinue;
end varStateSelectHeuristicPrio4;
Expand All @@ -4505,17 +4505,19 @@ protected function varStateSelectHeuristicPrio3
"function varStateSelectHeuristicPrio3
author: Frenkel TUD 2012-04
Helper function to varStateSelectHeuristicPrio.
added prio for variables with $_DER. name. Thouse are dummy_states
added prio for variables with $DER. name. Thouse are dummy_states
added by index reduction from normal variables"
input BackendDAE.Var v;
output Real prio;
algorithm
prio := matchcontinue(v)
local DAE.ComponentRef cr,pcr;
local
DAE.ComponentRef cr;
DAE.Ident id;
case(BackendDAE.VAR(varName=cr))
equation
pcr = ComponentReference.crefFirstCref(cr);
true = ComponentReference.crefEqual(pcr,ComponentReference.makeCrefIdent("$DER",DAE.T_REAL_DEFAULT,{}));
id = ComponentReference.crefFirstIdent(cr);
true = stringEq(id,"$DER");
then -100.0;
else then 0.0;
end matchcontinue;
Expand All @@ -4533,7 +4535,7 @@ algorithm
case _
equation
true = BackendVariable.varFixed(v);
then 1.0;
then 3.0;
else then 0.0;
end matchcontinue;
end varStateSelectHeuristicPrio2;
Expand All @@ -4552,8 +4554,7 @@ algorithm
case _
equation
e = BackendVariable.varStartValueFail(v);
true = Expression.isZero(e);
then -0.1;
then 1.0;
else then 0.0;
end matchcontinue;
end varStateSelectHeuristicPrio1;
Expand Down
3 changes: 1 addition & 2 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -1770,8 +1770,7 @@ algorithm

vars = Debug.bcallret2(not b, BackendVariable.addVar, var, vars, vars);
fixvars = Debug.bcallret2(b, BackendVariable.addVar, var, fixvars, fixvars);
vars = Debug.bcallret2(preused and not isFixed, BackendVariable.addVar, preVar, vars, vars);
fixvars = Debug.bcallret2(preused and isFixed, BackendVariable.addVar, preVar, fixvars, fixvars);
vars = Debug.bcallret2(preused, BackendVariable.addVar, preVar, vars, vars);
then ((var, (vars, fixvars, hs)));

case ((var, _)) equation
Expand Down
17 changes: 13 additions & 4 deletions Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -73,6 +73,7 @@ protected import HashSet;
protected import Inline;
protected import List;
protected import Util;
protected import System;

protected type EquationAttributes = tuple<DAE.ElementSource,Boolean> "eqnAttributes";

Expand Down Expand Up @@ -1344,6 +1345,7 @@ algorithm
case((e as DAE.CALL(path = Absyn.IDENT(name = "change")), (_,vars,knvars,b1,b2,ilst))) then ((e,false,(true,vars,knvars,b1,b2,ilst) ));
case((e as DAE.CALL(path = Absyn.IDENT(name = "edge")), (_,vars,knvars,b1,b2,ilst))) then ((e,false,(true,vars,knvars,b1,b2,ilst) ));
case((e as DAE.CALL(path = Absyn.IDENT(name = "delay")), (_,vars,knvars,b1,b2,ilst))) then ((e,false,(true,vars,knvars,b1,b2,ilst) ));
case((e as DAE.CALL(path = Absyn.IDENT(name = "terminal")), (_,vars,knvars,b1,b2,ilst))) then ((e,false,(true,vars,knvars,b1,b2,ilst) ));
// case for finding simple equation in jacobians
// there are all known variables mark as input
// and they are all time-depending
Expand Down Expand Up @@ -2023,8 +2025,6 @@ algorithm
_= arrayUpdate(simpleeqnsarr,r,setVisited(mark,s));
(v as BackendDAE.VAR(varName=cr)) = BackendVariable.getVarAt(iVars,i);
(replacable,replaceble1) = replaceableAlias(v,unreplacable);
// set fixed=true if replacable
v = Debug.bcallret2(replacable,BackendVariable.setVarFixed,v,true,v);
(vars,shared,isState,eqnslst) = optMoveVarShared(replacable,v,i,eqnAttributes,exp,BackendVariable.addKnVarDAE,iMT,iVars,ishared,iEqnslst);
constExp = Expression.isConst(exp);
// add to replacements if constant
Expand Down Expand Up @@ -3566,19 +3566,20 @@ author: Frenkel TUD 2010-12"
input tuple<DAE.Exp,HashSet.HashSet> inExp;
output tuple<DAE.Exp,HashSet.HashSet> outExp;
algorithm
outExp := match(inExp)
outExp := matchcontinue(inExp)
local
HashSet.HashSet hs;
DAE.ComponentRef cr;
DAE.Exp e;
case((e as DAE.CREF(componentRef=cr), hs))
equation
true = isNotWhen(cr);
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr,hs);
then
((e, hs ));
case _ then inExp;
end match;
end matchcontinue;
end addUnreplacableFromEqnsExp;

protected function addUnreplacableFromWhensSystem
Expand Down Expand Up @@ -3752,13 +3753,21 @@ algorithm
equation
crlst = List.flatten(List.map(explst,Expression.extractCrefsFromExp));
crlst = List.map(crlst,ComponentReference.crefStripLastSubs);
crlst = List.select(crlst, isNotWhen);
unreplacable = List.fold(crlst,BaseHashSet.add,unreplacable);
then
((e, unreplacable));
case _ then inExp;
end matchcontinue;
end traverserExpUnreplacable;

protected function isNotWhen
input DAE.ComponentRef iCr;
output Boolean b;
algorithm
b := not intEq(System.strncmp(ComponentReference.crefFirstIdent(iCr),"$when",5),0);
end isNotWhen;

protected function traverseCrefUnreplacable
"@author: Frenkel TUD 2012-12"
input DAE.ComponentRef inCref;
Expand Down

0 comments on commit f4fef63

Please sign in to comment.