Skip to content

Commit

Permalink
- BackendDAETransform.mo
Browse files Browse the repository at this point in the history
  - speedup replaceDummyDer
- set starvalue of dummy_der to zero
- BackendDump.mo
  - add Startvalue to dump
- BackendVariable.mo
  - use named parameter for case
  - States are not fixed=true for default
  - add function varStartValueFail
- Expression.mo
  add case for DAE.REDUCTION to isConst


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7335 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 9, 2010
1 parent f326a74 commit 3e75fc6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
50 changes: 27 additions & 23 deletions Compiler/BackendDAETransform.mo
Expand Up @@ -1193,7 +1193,9 @@ algorithm
list<String> es;
String es_1;
list<tuple<Integer,Integer,Integer>> derivedAlgs,derivedAlgs1;
list<tuple<Integer,Integer,Integer>> derivedMultiEqn,derivedMultiEqn1;
list<tuple<Integer,Integer,Integer>> derivedMultiEqn,derivedMultiEqn1;
DAE.Exp stateexp,stateexpcall,dummyderexp;
DAE.ExpType tp;

case (dae,m,mt,nv,nf,i,inFunctions,derivedAlgs,derivedMultiEqn)
equation
Expand All @@ -1218,7 +1220,11 @@ algorithm
// print("Chosen dummy: ");print(ComponentReference.printComponentRefStr(dummy_der));print("\n");
reqns = BackendDAEUtil.eqnsForVarWithStates(mt, stateno);
changedeqns = Util.listUnionOnTrue(deqns, reqns, intEq);
(dae,m,mt) = replaceDummyDer(state, dummy_der, dae, m, mt, changedeqns)
stateexp = Expression.crefExp(state);
tp = Expression.typeof(stateexp);
stateexpcall = DAE.CALL(Absyn.IDENT("der"),{stateexp},false,true,tp,DAE.NO_INLINE());
dummyderexp = Expression.crefExp(dummy_der);
(dae,m,mt) = replaceDummyDer(stateexpcall, dummyderexp, dae, m, mt, changedeqns)
"We need to change variables in the differentiated equations and in the equations having the dummy derivative" ;
dae = makeAlgebraic(dae, state);
(m,mt) = BackendDAEUtil.updateIncidenceMatrix(dae, m, mt, changedeqns);
Expand Down Expand Up @@ -1433,8 +1439,8 @@ protected function replaceDummyDer
outputs: (BackendDAE,
IncidenceMatrix,
IncidenceMatrixT)"
input DAE.ComponentRef inComponentRef1;
input DAE.ComponentRef inComponentRef2;
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input BackendDAE.BackendDAE inBackendDAE3;
input BackendDAE.IncidenceMatrix inIncidenceMatrix4;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT5;
Expand All @@ -1444,43 +1450,39 @@ protected function replaceDummyDer
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT;
algorithm
(outBackendDAE,outIncidenceMatrix,outIncidenceMatrixT):=
matchcontinue (inComponentRef1,inComponentRef2,inBackendDAE3,inIncidenceMatrix4,inIncidenceMatrixT5,inIntegerLst6)
matchcontinue (inExp1,inExp2,inBackendDAE3,inIncidenceMatrix4,inIncidenceMatrixT5,inIntegerLst6)
local
DAE.ComponentRef state,dummy,dummyder;
BackendDAE.BackendDAE dae;
array<list<BackendDAE.Value>> m,mt;
BackendDAE.Value e_1,e;
BackendDAE.Equation eqn,eqn_1;
BackendDAE.Variables v_1,v,kv,ev;
BackendDAE.AliasVariables av;
BackendDAE.EquationArray eqns_1,eqns,seqns,ie,ie1;
BackendDAE.EquationArray eqns_1,eqns,seqns,seqns1,ie,ie1;
array<BackendDAE.MultiDimEquation> ae,ae1,ae2,ae3;
array<DAE.Algorithm> al,al1,al2,al3;
BackendDAE.EventInfo wc;
list<BackendDAE.Value> rest;
BackendDAE.ExternalObjectClasses eoc;
DAE.Exp stateexp,stateexpcall,dummyderexp;
DAE.ExpType tp;

DAE.Exp stateexpcall,dummyderexp;

case (state,dummy,dae,m,mt,{}) then (dae,m,mt);
case (stateexpcall,dummyderexp,BackendDAE.DAE(v,kv,ev,av,eqns,seqns,ie,ae,al,wc,eoc),m,mt,{})
equation
(ie1,(al1,ae1,_,_)) = BackendEquation.traverseBackendDAEEqnsWithUpdate(ie,traversereplaceDummyDer,(al, ae, replaceDummyDer2Exp,(stateexpcall,dummyderexp)));
(seqns1,(al2,ae2,_,_)) = BackendEquation.traverseBackendDAEEqnsWithUpdate(seqns,traversereplaceDummyDer,(al1, ae1, replaceDummyDer2Exp,(stateexpcall,dummyderexp)));
then (BackendDAE.DAE(v,kv,ev,av,eqns,seqns1,ie1,ae2,al2,wc,eoc),m,mt);

case (state,dummyder,BackendDAE.DAE(v,kv,ev,av,eqns,seqns,ie,ae,al,wc,eoc),m,mt,(e :: rest))
case (stateexpcall,dummyderexp,BackendDAE.DAE(v,kv,ev,av,eqns,seqns,ie,ae,al,wc,eoc),m,mt,(e :: rest))
equation
e_1 = e - 1;
eqn = BackendDAEUtil.equationNth(eqns, e_1);
stateexp = Expression.crefExp(state);
tp = Expression.typeof(stateexp);
stateexpcall = DAE.CALL(Absyn.IDENT("der"),{stateexp},false,true,tp,DAE.NO_INLINE());
dummyderexp = Expression.crefExp(dummyder);
(eqn_1,al1,ae1,_) = traverseBackendDAEExpsEqn(eqn, al, ae, replaceDummyDer2Exp,(stateexpcall,dummyderexp));
(ie1,(al2,ae2,_,_)) = BackendEquation.traverseBackendDAEEqnsWithUpdate(ie,traversereplaceDummyDer,(al, ae, replaceDummyDer2Exp,(stateexpcall,dummyderexp)));
(eqn_1,al3,ae3,v_1) = traverseBackendDAEExpsEqn(eqn_1,al2,ae2,replaceDummyDerOthersExp,v);
(eqn_1,al2,ae2,v_1) = traverseBackendDAEExpsEqn(eqn_1,al1,ae1,replaceDummyDerOthersExp,v);
eqns_1 = BackendEquation.equationSetnth(eqns, e_1, eqn_1)
"incidence_row(v\'\',eqn\') => row\' &
Util.list_replaceat(row\',e\',m) => m\' &
transpose_matrix(m\') => mt\' &" ;
(dae,m,mt) = replaceDummyDer(state, dummyder, BackendDAE.DAE(v_1,kv,ev,av,eqns_1,seqns,ie1,ae3,al3,wc,eoc), m, mt, rest);
(dae,m,mt) = replaceDummyDer(stateexpcall, dummyderexp, BackendDAE.DAE(v_1,kv,ev,av,eqns_1,seqns,ie,ae2,al2,wc,eoc), m, mt, rest);
then
(dae,m,mt);

Expand Down Expand Up @@ -1516,7 +1518,7 @@ algorithm
equation
(e1,algs1,ae1,ext_arg_1) = traverseBackendDAEExpsEqn(e,algs,ae,func,ext_arg);
then
((e,(algs,ae,func,ext_arg)));
((e1,(algs1,ae1,func,ext_arg_1)));
case inTpl then inTpl;
end matchcontinue;
end traversereplaceDummyDer;
Expand Down Expand Up @@ -1803,7 +1805,7 @@ algorithm
BackendDAE.Value idx;
DAE.ComponentRef name,dummyvar_cr,var;
DAE.ElementSource source "origin of equation";
Option<DAE.VariableAttributes> dae_var_attr;
Option<DAE.VariableAttributes> dae_var_attr,dae_var_attr1;
Option<SCode.Comment> comment;
DAE.Flow flowPrefix;
DAE.Stream streamPrefix;
Expand All @@ -1819,8 +1821,10 @@ algorithm
case (var,BackendDAE.DAE(vars, kv, ev, av, eqns, seqns, ie, ae, al, wc,eoc))
equation
((BackendDAE.VAR(name,kind,dir,tp,bind,value,dim,idx,source,dae_var_attr,comment,flowPrefix,streamPrefix) :: _),_) = BackendVariable.getVar(var, vars);
dummyvar_cr = ComponentReference.crefPrefixDer(var);
dummyvar = BackendDAE.VAR(dummyvar_cr,BackendDAE.DUMMY_DER(),dir,tp,NONE(),NONE(),dim,0,source,dae_var_attr,comment,flowPrefix,streamPrefix);
dummyvar_cr = ComponentReference.crefPrefixDer(name);
/* start value is not the same */
dae_var_attr1 = DAEUtil.setStartAttr(dae_var_attr,DAE.RCONST(0.0));
dummyvar = BackendDAE.VAR(dummyvar_cr,BackendDAE.DUMMY_DER(),dir,tp,NONE(),NONE(),dim,0,source,dae_var_attr1,comment,flowPrefix,streamPrefix);
/* Dummy variables are algebraic variables, hence fixed = false */
dummyvar = BackendVariable.setVarFixed(dummyvar,false);
vars_1 = BackendVariable.addVar(dummyvar, vars);
Expand Down
10 changes: 7 additions & 3 deletions Compiler/BackendDump.mo
Expand Up @@ -888,7 +888,7 @@ protected function dumpVars2
algorithm
_ := matchcontinue (inVarLst,inInteger)
local
String varnostr,dirstr,str,path_str,comment_str,s,indx_str;
String varnostr,dirstr,str,path_str,comment_str,s,indx_str,sstart;
list<String> paths_lst,path_strs;
BackendDAE.Value varno_1,indx,varno;
BackendDAE.Var v;
Expand All @@ -905,6 +905,7 @@ algorithm
list<BackendDAE.Var> xs;
BackendDAE.Type var_type;
DAE.InstDims arrayDim;
Boolean b;

case ({},_) then ();

Expand Down Expand Up @@ -935,7 +936,7 @@ algorithm
paths_lst = Util.listMap(paths, Absyn.pathString);
path_str = Util.stringDelimitList(paths_lst, ", ");
comment_str = DAEDump.dumpCommentOptionStr(comment);
print("= ");
print(" = ");
s = ExpressionDump.printExpStr(e);
print(s);
print(" ");
Expand Down Expand Up @@ -979,7 +980,10 @@ algorithm
print(str);
print(":");
dumpKind(kind);
print(" ");
b = DAEUtil.hasStartAttr(dae_var_attr);
sstart = DAEUtil.getStartAttrString(dae_var_attr);
sstart = Util.if_(b,sstart,"0.0");
print(stringAppendList({"(start= ",sstart,") "}));
print(path_str);
indx_str = intString(indx) "print \" former: \" & print old_name &" ;
str = dumpTypeStr(var_type);print( " type: "); print(str);
Expand Down
38 changes: 30 additions & 8 deletions Compiler/BackendVariable.mo
Expand Up @@ -301,7 +301,7 @@ end setVarFixed;
public function varFixed
"function: varFixed
author: PA
Extacts the fixed attribute of a variable.
Extracts the fixed attribute of a variable.
The default fixed value is used if not found. Default is true for parameters
(and constants) and false for variables."
input BackendDAE.Var inVar;
Expand All @@ -312,20 +312,24 @@ algorithm
local
Boolean fixed;
BackendDAE.Var v;
case (v as BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_REAL(_,_,_,_,_,SOME(DAE.BCONST(fixed)),_,_,_,_,_)))) then fixed;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_INT(_,_,_,SOME(DAE.BCONST(fixed)),_,_,_)))) then fixed;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_BOOL(_,_,SOME(DAE.BCONST(fixed)),_,_,_)))) then fixed;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_ENUMERATION(_,_,_,SOME(DAE.BCONST(fixed)),_,_,_)))) then fixed;
case (v) /* param is fixed */
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_REAL(fixed=SOME(DAE.BCONST(fixed)))))) then fixed;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_INT(fixed=SOME(DAE.BCONST(fixed)))))) then fixed;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_BOOL(fixed=SOME(DAE.BCONST(fixed)))))) then fixed;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_ENUMERATION(fixed=SOME(DAE.BCONST(fixed)))))) then fixed;
case (v) /* params are by default fixed */
equation
BackendDAE.PARAM() = varKind(v);
then
true;
case (v) /* states are by default fixed. */
/* See Modelica Spec 3.2 page 88:
For constants and parameters, the attribute fixed is by default true. For other variables
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);
then
true;
*/
case (_) then false; /* rest defaults to false*/
end matchcontinue;
end varFixed;
Expand All @@ -347,6 +351,24 @@ algorithm
end matchcontinue;
end varStartValue;

public function varStartValueFail
"function varStartValueFail
author: Frenkel TUD
Returns the DAE.StartValue of a variable if there is one.
Otherwise fail"
input BackendDAE.Var v;
output DAE.Exp sv;
algorithm
sv := matchcontinue(v)
local
Option<DAE.VariableAttributes> attr;
case (BackendDAE.VAR(values = attr))
equation
sv=DAEUtil.getStartAttrFail(attr);
then sv;
end matchcontinue;
end varStartValueFail;

public function varBindExp
"function varBindExp
author: Frenkel TUD 2010-12
Expand All @@ -373,7 +395,7 @@ algorithm
local
DAE.StateSelect stateselect;
BackendDAE.Var v;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_REAL(_,_,_,_,_,_,_,SOME(stateselect),_,_,_)))) then stateselect;
case (BackendDAE.VAR(values = SOME(DAE.VAR_ATTR_REAL(stateSelectOption=SOME(stateselect))))) then stateselect;
case (_) then DAE.DEFAULT();
end matchcontinue;
end varStateSelect;
Expand Down
11 changes: 9 additions & 2 deletions Compiler/Expression.mo
Expand Up @@ -4515,8 +4515,15 @@ algorithm
then
true;

case (DAE.END()) then true;

case (DAE.END()) then true;

case (DAE.REDUCTION(expr=e1,range=e2))
equation
true = isConst(e1);
true = isConst(e2);
then
true;

case (_) then false;

end matchcontinue;
Expand Down

0 comments on commit 3e75fc6

Please sign in to comment.