Skip to content

Commit

Permalink
- add information to store how often a state are differentiated
Browse files Browse the repository at this point in the history
- bugfix BackendVariable.hasContinousVar

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14672 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jan 4, 2013
1 parent b011ca9 commit 5003b97
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 85 deletions.
4 changes: 3 additions & 1 deletion Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -86,7 +86,9 @@ end BackendDAEType;
public
uniontype VarKind "- Variabile kind"
record VARIABLE end VARIABLE;
record STATE end STATE; // we should also save information if it is a state with used derivative and the how often this states was differentiated
record STATE
Integer index "how often this states was differentiated";
end STATE;
record STATE_DER end STATE_DER;
record DUMMY_DER end DUMMY_DER;
record DUMMY_STATE end DUMMY_STATE;
Expand Down
12 changes: 6 additions & 6 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -768,10 +768,10 @@ algorithm
(outVarKind) := matchcontinue (inVarKind,inType,inComponentRef,inVarDirection,inConnectorType,daeAttr)
// variable -> state if have stateSelect=StateSelect.always
case (DAE.VARIABLE(),_,_,_,_,SOME(DAE.VAR_ATTR_REAL(stateSelectOption = SOME(DAE.ALWAYS()))))
then (BackendDAE.STATE());
then (BackendDAE.STATE(1));
// variable -> state if have stateSelect=StateSelect.prefer
case (DAE.VARIABLE(),_,_,_,_,SOME(DAE.VAR_ATTR_REAL(stateSelectOption = SOME(DAE.PREFER()))))
then (BackendDAE.STATE());
then (BackendDAE.STATE(1));

case (DAE.VARIABLE(),DAE.T_BOOL(varLst = _),_,_,_,_)
equation
Expand Down Expand Up @@ -2485,7 +2485,7 @@ algorithm
Boolean b,b1,b2;
DAE.Exp e,ae;
// state variable
case (BackendDAE.VAR(varKind=BackendDAE.STATE()),_,1,_,
case (BackendDAE.VAR(varKind=BackendDAE.STATE(_)),_,1,_,
BackendDAE.VAR(varName=cr2),_,1,_,_,_,_,_,_,_)
equation
// check if replacable
Expand All @@ -2510,7 +2510,7 @@ algorithm
(vars,iKnVars,iExtVars,avars,repl);
// state variable
case (BackendDAE.VAR(varName=cr1),_,1,_,
BackendDAE.VAR(varKind=BackendDAE.STATE()),_,1,_,_,_,_,_,_,_)
BackendDAE.VAR(varKind=BackendDAE.STATE(_)),_,1,_,_,_,_,_,_,_)
equation
// check if replacable
false = BackendVariable.isStateVar(v1);
Expand Down Expand Up @@ -3207,7 +3207,7 @@ algorithm
equation
false = BackendVariable.isVarDiscrete(var) "do not change discrete vars to states, because they have no derivative" ;
false = BackendVariable.isStateVar(var);
var1 = BackendVariable.setVarKind(var,BackendDAE.STATE());
var1 = BackendVariable.setVarKind(var,BackendDAE.STATE(1));
vars = BackendVariable.addVar(var1, inVars);
then (vars,iExp);
case(_,_,_)
Expand Down Expand Up @@ -3240,7 +3240,7 @@ algorithm
equation
false = BackendVariable.isVarDiscrete(var) "do not change discrete vars to states, because they have no derivative" ;
false = BackendVariable.isStateVar(var);
var = BackendVariable.setVarKind(var,BackendDAE.STATE());
var = BackendVariable.setVarKind(var,BackendDAE.STATE(1));
vars = BackendVariable.addVar(var, inVars);
vars = updateStatesVars(vars,newStates,true);
then vars;
Expand Down
6 changes: 3 additions & 3 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -6168,7 +6168,7 @@ algorithm
(r2,res) = generateJacobianVars2(var, restVar, inMatrixName);
then (r2,res);

case(var as BackendDAE.VAR(varName=cref,varKind=BackendDAE.STATE()), currVar::restVar, _) equation
case(var as BackendDAE.VAR(varName=cref,varKind=BackendDAE.STATE(_)), currVar::restVar, _) equation
cref = ComponentReference.crefPrefixDer(cref);
derivedCref = differentiateVarWithRespectToX(cref, currVar, inMatrixName);
r1 = BackendDAE.VAR(derivedCref, BackendDAE.STATE_DER(), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.NON_CONNECTOR());
Expand Down Expand Up @@ -6216,7 +6216,7 @@ algorithm
then
creatallDiffedVars(restVar,cref,inAllVars,inIndex, inMatrixName,iVars);

case(BackendDAE.VAR(varName=currVar,varKind=BackendDAE.STATE())::restVar,cref,_,_, _, _) equation
case(BackendDAE.VAR(varName=currVar,varKind=BackendDAE.STATE(_))::restVar,cref,_,_, _, _) equation
({v1}, _) = BackendVariable.getVar(currVar, inAllVars);
currVar = ComponentReference.crefPrefixDer(currVar);
derivedCref = differentiateVarWithRespectToX(currVar, cref, inMatrixName);
Expand All @@ -6231,7 +6231,7 @@ algorithm
then
creatallDiffedVars(restVar, cref, inAllVars, inIndex+1, inMatrixName,r1::iVars);

case(BackendDAE.VAR(varName=currVar,varKind=BackendDAE.STATE())::restVar,cref,_,_, _, _) equation
case(BackendDAE.VAR(varName=currVar,varKind=BackendDAE.STATE(_))::restVar,cref,_,_, _, _) equation
currVar = ComponentReference.crefPrefixDer(currVar);
derivedCref = differentiateVarWithRespectToX(currVar, cref, inMatrixName);
r1 = BackendDAE.VAR(derivedCref, BackendDAE.VARIABLE(), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), DAE.NON_CONNECTOR());
Expand Down
22 changes: 8 additions & 14 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -1178,7 +1178,7 @@ algorithm
DAE.ElementSource source;

case (BackendDAE.VAR(varName = cr,
varKind = BackendDAE.STATE(),
varKind = BackendDAE.STATE(_),
varDirection = dir,
varParallelism = prl,
varType = tp,
Expand Down Expand Up @@ -2937,31 +2937,25 @@ protected function findState
"function: findState
author: PA
Returns the first state from a list of component references."
input BackendDAE.Variables inVariables;
input BackendDAE.Variables vars;
input list<DAE.ComponentRef> inExpComponentRefLst;
output DAE.ComponentRef outComponentRef;
algorithm
outComponentRef:=
matchcontinue (inVariables,inExpComponentRefLst)
matchcontinue (vars,inExpComponentRefLst)
local
BackendDAE.Var v;
BackendDAE.Variables vars;
DAE.ComponentRef cr;
list<DAE.ComponentRef> crs;

case (vars,(cr :: crs))
case (_,(cr :: crs))
equation
((v :: _),_) = BackendVariable.getVar(cr, vars);
BackendDAE.STATE() = BackendVariable.varKind(v);
BackendDAE.STATE(_) = BackendVariable.varKind(v);
then
cr;

case (vars,(cr :: crs))
equation
cr = findState(vars, crs);
case (_,(cr :: crs))
then
cr;

findState(vars, crs);
end matchcontinue;
end findState;

Expand Down Expand Up @@ -3523,7 +3517,7 @@ algorithm

case ((DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)})}),(vars,i)))
equation
((BackendDAE.VAR(cr,BackendDAE.STATE(),a,prl,b,c,d,lstSubs,source,dae_var_attr,comment,ct) :: _),_) = BackendVariable.getVar(cr, vars) "der(der(s)) s is state => der_der_s" ;
((BackendDAE.VAR(cr,BackendDAE.STATE(_),a,prl,b,c,d,lstSubs,source,dae_var_attr,comment,ct) :: _),_) = BackendVariable.getVar(cr, vars) "der(der(s)) s is state => der_der_s" ;
dummyder = ComponentReference.crefPrefixDer(cr);
dummyder = ComponentReference.crefPrefixDer(dummyder);
vars_1 = BackendVariable.addVar(BackendDAE.VAR(dummyder, BackendDAE.DUMMY_DER(), a, prl, b, NONE(), NONE(), lstSubs, source, NONE(), comment, ct), vars);
Expand Down
20 changes: 10 additions & 10 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -907,7 +907,7 @@ algorithm
local
BackendDAE.Var v;
Boolean b;
case ((v as BackendDAE.VAR(varKind=BackendDAE.STATE()),_))
case ((v as BackendDAE.VAR(varKind=BackendDAE.STATE(_)),_))
then ((v,false,false));
case ((v,b)) then ((v,b,b));
end match;
Expand Down Expand Up @@ -1131,7 +1131,7 @@ algorithm
then
((var,(nx,ny+1,ny_string, ny_int,ny_bool)));

case ((var as BackendDAE.VAR(varKind = BackendDAE.STATE()),(nx,ny,ny_string, ny_int, ny_bool)))
case ((var as BackendDAE.VAR(varKind = BackendDAE.STATE(_)),(nx,ny,ny_string, ny_int, ny_bool)))
then
((var,(nx+1,ny,ny_string, ny_int,ny_bool)));

Expand Down Expand Up @@ -1641,7 +1641,7 @@ algorithm
((e,false,(vars,e::expl)));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)})),(vars,expl)))
equation
((BackendDAE.VAR(varKind = BackendDAE.STATE()) :: _),_) = BackendVariable.getVar(cr, vars);
((BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: _),_) = BackendVariable.getVar(cr, vars);
then
((e,false,(vars,e::expl)));
// is this case right?
Expand Down Expand Up @@ -3798,7 +3798,7 @@ algorithm
/*If variable x is a state, der(x) is a variable in incidence matrix,
x is inserted as negative value, since it is needed by debugging and
index reduction using dummy derivatives */
case (BackendDAE.VAR(varKind = BackendDAE.STATE()) :: rest,i::irest,vars,b)
case (BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: rest,i::irest,vars,b)
equation
i1 = Util.if_(b,-i,i);
failure(_ = List.getMemberOnTrue(i1, vars, intEq));
Expand Down Expand Up @@ -3909,7 +3909,7 @@ algorithm
failure(_ = List.getMemberOnTrue(i, vars, intEq));
res = incidenceRowExp1(rest,irest,i::vars,b);
then res;
case (BackendDAE.VAR(varKind = BackendDAE.STATE()) :: rest,i::irest,vars,b)
case (BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: rest,i::irest,vars,b)
equation
failure( true = b);
failure(_ = List.getMemberOnTrue(i, vars, intEq));
Expand Down Expand Up @@ -5341,7 +5341,7 @@ algorithm
// if not negatet rowmark then
false = intEq(rowmark[r],-mark);
// solved?
BackendDAE.VAR(varName=cr1,varKind=BackendDAE.STATE()) = BackendVariable.getVarAt(vars, r);
BackendDAE.VAR(varName=cr1,varKind=BackendDAE.STATE(_)) = BackendVariable.getVarAt(vars, r);
true = ComponentReference.crefEqualNoStringCompare(cr, cr1);
false = Expression.expHasDerCref(e2,cr);
then
Expand All @@ -5351,7 +5351,7 @@ algorithm
// if not negatet rowmark then
false = intEq(rowmark[r],-mark);
// solved?
BackendDAE.VAR(varName=cr1,varKind=BackendDAE.STATE()) = BackendVariable.getVarAt(vars, r);
BackendDAE.VAR(varName=cr1,varKind=BackendDAE.STATE(_)) = BackendVariable.getVarAt(vars, r);
true = ComponentReference.crefEqualNoStringCompare(cr, cr1);
false = Expression.expHasDerCref(e1,cr);
then
Expand Down Expand Up @@ -5443,7 +5443,7 @@ algorithm
// if not negatet rowmark then linear or nonlinear
false = intEq(rowmark[r],-mark);
// de/dvar
BackendDAE.VAR(varName=cr,varKind=BackendDAE.STATE()) = BackendVariable.getVarAt(vars, r);
BackendDAE.VAR(varName=cr,varKind=BackendDAE.STATE(_)) = BackendVariable.getVarAt(vars, r);
cr1 = ComponentReference.crefPrefixDer(cr);
de = Expression.crefExp(cr);
((de,_)) = Expression.replaceExp(Expression.expSub(e1,e2), DAE.CALL(Absyn.IDENT("der"),{de},DAE.callAttrBuiltinReal), Expression.crefExp(cr1));
Expand Down Expand Up @@ -5868,13 +5868,13 @@ algorithm
/*If variable x is a state, der(x) is a variable in incidence matrix,
x is inserted as negative value, since it is needed by debugging and
index reduction using dummy derivatives */
case (BackendDAE.VAR(varKind = BackendDAE.STATE()) :: rest,i::irest,_,false,_,_,_)
case (BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: rest,i::irest,_,false,_,_,_)
equation
false = intEq(intAbs(rowmark[i]),mark);
_ = arrayUpdate(rowmark,i,Util.if_(unsolvable,-mark,mark));
res = adjacencyRowExpEnhanced1(rest,irest,i::vars,notinder,mark,rowmark,unsolvable);
then res;
case (BackendDAE.VAR(varKind = BackendDAE.STATE()) :: rest,i::irest,_,true,_,_,_)
case (BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: rest,i::irest,_,true,_,_,_)
equation
i1 = -i;
failure(_ = List.getMemberOnTrue(i1, vars, intEq));
Expand Down
6 changes: 4 additions & 2 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -2358,9 +2358,11 @@ public function kindString
algorithm
kindStr:=
match (inVarKind)
local Absyn.Path path;
local
Absyn.Path path;
Integer i;
case BackendDAE.VARIABLE() then "VARIABLE";
case BackendDAE.STATE() then "STATE";
case BackendDAE.STATE(i) then "STATE(" +& intString(i) +& ")";
case BackendDAE.STATE_DER() then "STATE_DER";
case BackendDAE.DUMMY_DER() then "DUMMY_DER";
case BackendDAE.DUMMY_STATE() then "DUMMY_STATE";
Expand Down
26 changes: 10 additions & 16 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -175,7 +175,7 @@ algorithm
fixed is by default false. For all variables declared as constant it is an error to have "fixed = false".
case (v) // states are by default fixed.
equation
BackendDAE.STATE() = varKind(v);
BackendDAE.STATE(_) = varKind(v);
fixes = Flags.isSet(Flags.INIT_DLOW_DUMP);
then
not fixed;
Expand Down Expand Up @@ -889,7 +889,7 @@ public function isStateVar
algorithm
outBoolean:=
matchcontinue (inVar)
case (BackendDAE.VAR(varKind = BackendDAE.STATE())) then true;
case (BackendDAE.VAR(varKind = BackendDAE.STATE(_))) then true;
case (_) then false;
end matchcontinue;
end isStateVar;
Expand All @@ -906,7 +906,7 @@ algorithm
BackendDAE.Variables vars;
case(cr,vars)
equation
((BackendDAE.VAR(varKind = BackendDAE.STATE()) :: _),_) = getVar(cr, vars);
((BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: _),_) = getVar(cr, vars);
then
true;
case(_,_) then false;
Expand Down Expand Up @@ -1077,7 +1077,7 @@ public function isStateorStateDerVar
algorithm
outBoolean:=
match (inVar)
case (BackendDAE.VAR(varKind = BackendDAE.STATE())) then true;
case (BackendDAE.VAR(varKind = BackendDAE.STATE(_))) then true;
case (BackendDAE.VAR(varKind = BackendDAE.STATE_DER())) then true;
else
then false;
Expand Down Expand Up @@ -1135,19 +1135,13 @@ algorithm
Boolean res;
BackendDAE.Var v;
list<BackendDAE.Var> vs;
case ((BackendDAE.VAR(varKind=BackendDAE.VARIABLE(),varType = DAE.T_INTEGER(source = _)) :: _)) then false;
case ((BackendDAE.VAR(varKind=BackendDAE.VARIABLE(),varType = DAE.T_BOOL(source = _)) :: _)) then false;
case ((BackendDAE.VAR(varKind=BackendDAE.VARIABLE(),varType = DAE.T_ENUMERATION(source = _)) :: _)) then false;
case ((BackendDAE.VAR(varKind=BackendDAE.VARIABLE()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.STATE()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.VARIABLE(),varType = DAE.T_REAL(source = _)) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.VARIABLE(),varType = DAE.T_ARRAY(ty=DAE.T_REAL(source = _))) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.STATE(_)) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.STATE_DER()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.DUMMY_DER()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.DUMMY_STATE()) :: _)) then true;
case ((v :: vs))
equation
res = hasContinousVar(vs);
then
res;
case ((v :: vs)) then hasContinousVar(vs);
case ({}) then false;
end match;
end hasContinousVar;
Expand Down Expand Up @@ -1610,7 +1604,7 @@ public function createDummyVar "function createDummyVar
output DAE.ComponentRef outCr;
algorithm
outCr := ComponentReference.makeCrefIdent("$dummy",DAE.T_REAL_DEFAULT,{});
outVar := BackendDAE.VAR(outCr, BackendDAE.STATE(),DAE.BIDIR(),DAE.NON_PARALLEL(),DAE.T_REAL_DEFAULT,NONE(),NONE(),{},
outVar := BackendDAE.VAR(outCr, BackendDAE.STATE(1),DAE.BIDIR(),DAE.NON_PARALLEL(),DAE.T_REAL_DEFAULT,NONE(),NONE(),{},
DAE.emptyElementSource,
SOME(DAE.VAR_ATTR_REAL(NONE(),NONE(),NONE(),(NONE(),NONE()),NONE(),SOME(DAE.BCONST(true)),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE())),
NONE(),DAE.NON_CONNECTOR());
Expand Down Expand Up @@ -2547,7 +2541,7 @@ algorithm
_:=
match (inVarKind)
case (BackendDAE.VARIABLE()) then ();
case (BackendDAE.STATE()) then ();
case (BackendDAE.STATE(_)) then ();
case (BackendDAE.DUMMY_STATE()) then ();
case (BackendDAE.DUMMY_DER()) then ();
case (BackendDAE.DISCRETE()) then ();
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/DAEQuery.mo
Expand Up @@ -658,7 +658,7 @@ algorithm

case (DAE.CREF(componentRef = cr),vars)
equation
((BackendDAE.VAR(varKind = BackendDAE.STATE()) :: _),p) =
((BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: _),p) =
BackendVariable.getVar(cr, vars) "If variable x is a state, der(x) is a variable in incidence matrix,
x is inserted as negative value, since it is needed by debugging and index
reduction using dummy derivatives" ;
Expand Down Expand Up @@ -818,7 +818,7 @@ algorithm

case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}),vars)
equation
((BackendDAE.VAR(varKind = BackendDAE.STATE()) :: _),p) = BackendVariable.getVar(cr, vars);
((BackendDAE.VAR(varKind = BackendDAE.STATE(_)) :: _),p) = BackendVariable.getVar(cr, vars);
pStr = List.map(p, intString);
then
pStr;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/Derive.mo
Expand Up @@ -370,7 +370,7 @@ algorithm
// Continuous-time variables (and for shared eq-systems, also unknown variables: keep them as-is)
case ((e as DAE.CREF(componentRef = cr,ty = tp)),(timevars,_))
equation
// ({BackendDAE.VAR(varKind = BackendDAE.STATE())},_) = BackendVariable.getVar(cr, timevars);
// ({BackendDAE.VAR(varKind = BackendDAE.STATE(_))},_) = BackendVariable.getVar(cr, timevars);
then DAE.CALL(Absyn.IDENT("der"),{e},DAE.CALL_ATTR(tp,false,true,DAE.NO_INLINE(),DAE.NO_TAIL()));

// der(sign(x)) -> 0
Expand Down

0 comments on commit 5003b97

Please sign in to comment.