Skip to content

Commit

Permalink
- do pre replacements also in event info
Browse files Browse the repository at this point in the history
- check also alias vars for discrete pre variables for the system

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14521 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 22, 2012
1 parent 033352e commit 66de325
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 50 deletions.
147 changes: 99 additions & 48 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7973,18 +7973,28 @@ algorithm
case((DAE.CALL(path=Absyn.IDENT(name = "pre"),expLst={e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))}),(knvars,aliasvars,_)))
then
((e,(knvars,aliasvars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "pre"),expLst={e as DAE.UNARY(DAE.UMINUS(_),DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time")))}),(knvars,aliasvars,_)))
then
((e,(knvars,aliasvars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "pre"),expLst={DAE.CREF(componentRef=cr,ty=tp)},attr=attr),(knvars,aliasvars,_)))
equation
(var::{},_) = BackendVariable.getVar(cr, aliasvars);
(cr,negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr,tp);
e = Debug.bcallret1(negate,Expression.negate,e,e);
e = simplifyTimeIndepFuncCallsGetAliasExp(DAE.CALL(Absyn.IDENT("pre"),{e},attr),knvars);
(e,_) = ExpressionSimplify.simplify(DAE.CALL(Absyn.IDENT("pre"),{e},attr));
((e,_)) = Expression.traverseExp(e,traverserExpsimplifyTimeIndepFuncCalls,(knvars,aliasvars,false));
then
((e,(knvars,aliasvars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "change"),expLst={e as DAE.CREF(componentRef=cr,ty=tp)}),(knvars,aliasvars,_)))
equation
(_::{},_) = BackendVariable.getVar(cr, knvars);
(_::_,_) = BackendVariable.getVar(cr, knvars);
zero = Expression.arrayFill(Expression.arrayDimension(tp),DAE.BCONST(false));
then
((zero,(knvars,aliasvars,true)));
case((e as DAE.CALL(path=Absyn.IDENT(name = "change"),expLst={DAE.CREF(componentRef=cr,ty=tp)}),(knvars,aliasvars,_)))
equation
(_::_,_) = BackendVariable.getVar(cr, aliasvars);
zero = Expression.arrayFill(Expression.arrayDimension(tp),DAE.BCONST(false));
then
((zero,(knvars,aliasvars,true)));
Expand All @@ -7997,7 +8007,8 @@ algorithm
(cr,negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr,tp);
e = Debug.bcallret1(negate,Expression.negate,e,e);
e = simplifyTimeIndepFuncCallsGetAliasExp(DAE.CALL(Absyn.IDENT("change"),{e},attr),knvars);
(e,_) = ExpressionSimplify.simplify(DAE.CALL(Absyn.IDENT("change"),{e},attr));
((e,_)) = Expression.traverseExp(e,traverserExpsimplifyTimeIndepFuncCalls,(knvars,aliasvars,false));
then
((e,(knvars,aliasvars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "edge"),expLst={e as DAE.CREF(componentRef=cr,ty=tp)}),(knvars,aliasvars,_)))
Expand All @@ -8014,56 +8025,15 @@ algorithm
(var::{},_) = BackendVariable.getVar(cr, aliasvars);
(cr,negate) = BackendVariable.getAlias(var);
e = DAE.CREF(cr,tp);
e = Debug.bcallret1(negate,Expression.negate,e,e);
e = simplifyTimeIndepFuncCallsGetAliasExp(DAE.CALL(Absyn.IDENT("edge"),{e},attr),knvars);
e = Debug.bcallret1(negate,Expression.negate,e,e);
(e,_) = ExpressionSimplify.simplify(DAE.CALL(Absyn.IDENT("edge"),{e},attr));
((e,_)) = Expression.traverseExp(e,traverserExpsimplifyTimeIndepFuncCalls,(knvars,aliasvars,false));
then
((e,(knvars,aliasvars,true)));
case _ then tpl;
end matchcontinue;
end traverserExpsimplifyTimeIndepFuncCalls;

protected function simplifyTimeIndepFuncCallsGetAliasExp
input DAE.Exp inE;
input BackendDAE.Variables knvars;
output DAE.Exp outE;
algorithm
outE := matchcontinue(inE,knvars)
local
DAE.ComponentRef cr;
DAE.Exp e;
DAE.Type tp;
case (DAE.CALL(path=Absyn.IDENT(name = "pre"),expLst={e as DAE.CREF(componentRef=cr)}),_)
equation
(_::{},_) = BackendVariable.getVar(cr, knvars);
then
e;
case (DAE.CALL(path=Absyn.IDENT(name = "pre"),expLst={e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))}),_)
then
e;
case(DAE.CALL(path=Absyn.IDENT(name = "change"),expLst={DAE.CREF(componentRef=cr,ty=tp)}),_)
equation
(_::{},_) = BackendVariable.getVar(cr, knvars);
e = Expression.arrayFill(Expression.arrayDimension(tp),DAE.BCONST(false));
then
e;
case (DAE.CALL(path=Absyn.IDENT(name = "change"),expLst={e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))}),_)
then
DAE.BCONST(false);
case(DAE.CALL(path=Absyn.IDENT(name = "edge"),expLst={DAE.CREF(componentRef=cr,ty=tp)}),_)
equation
(_::{},_) = BackendVariable.getVar(cr, knvars);
e = Expression.arrayFill(Expression.arrayDimension(tp),DAE.BCONST(false));
then
e;
case (DAE.CALL(path=Absyn.IDENT(name = "edge"),expLst={e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))}),_)
then
DAE.BCONST(false);
else
then
inE;
end matchcontinue;
end simplifyTimeIndepFuncCallsGetAliasExp;

protected function simplifyTimeIndepFuncCallsShared "function simplifyTimeIndepFuncCallsShared
simplifies time independent built in function calls like
pre(param) -> param
Expand Down Expand Up @@ -8093,13 +8063,94 @@ algorithm
_ = BackendDAEUtil.traverseBackendDAEExpsVarsWithUpdate(knvars,traversersimplifyTimeIndepFuncCalls,(knvars,aliasVars,false));
_ = BackendDAEUtil.traverseBackendDAEExpsEqnsWithUpdate(inieqns,traversersimplifyTimeIndepFuncCalls,(knvars,aliasVars,false));
_ = BackendDAEUtil.traverseBackendDAEExpsEqnsWithUpdate(remeqns,traversersimplifyTimeIndepFuncCalls,(knvars,aliasVars,false));
(eventInfo,_) = traverseEventInfoExps(eventInfo,traversersimplifyTimeIndepFuncCalls,(knvars,aliasVars,false));
then
BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,clsAttrs,cache,env,funcTree,eventInfo,eoc,btp,symjacs));
end match;
end simplifyTimeIndepFuncCallsShared;

protected function traverseEventInfoExps
replaceable type Type_a = polymorphic<Any>;
input BackendDAE.EventInfo iEventInfo;
input FuncExpType func;
input Type_a inTypeA;
output BackendDAE.EventInfo oEventInfo;
output Type_a outTypeA;
partial function FuncExpType
input tuple<DAE.Exp, Type_a> inTpl;
output tuple<DAE.Exp, Type_a> outTpl;
end FuncExpType;
protected
list<BackendDAE.WhenClause> whenClauseLst;
list<BackendDAE.ZeroCrossing> zeroCrossingLst,sampleLst,relationsLst;
Integer relationsNumber,numberMathEvents;
algorithm
BackendDAE.EVENT_INFO(whenClauseLst,zeroCrossingLst,sampleLst,relationsLst,relationsNumber,numberMathEvents) := iEventInfo;
(whenClauseLst,outTypeA) := traverseWhenClauseExps(whenClauseLst,func,inTypeA,{});
(zeroCrossingLst,outTypeA) := traverseZeroCrossingExps(zeroCrossingLst,func,outTypeA,{});
(sampleLst,outTypeA) := traverseZeroCrossingExps(sampleLst,func,outTypeA,{});
(relationsLst,outTypeA) := traverseZeroCrossingExps(relationsLst,func,outTypeA,{});
oEventInfo := BackendDAE.EVENT_INFO(whenClauseLst,zeroCrossingLst,sampleLst,relationsLst,relationsNumber,numberMathEvents);
end traverseEventInfoExps;

protected function traverseWhenClauseExps
replaceable type Type_a = polymorphic<Any>;
input list<BackendDAE.WhenClause> iWhenClauses;
input FuncExpType func;
input Type_a inTypeA;
input list<BackendDAE.WhenClause> iAcc;
output list<BackendDAE.WhenClause> oWhenClauses;
output Type_a outTypeA;
partial function FuncExpType
input tuple<DAE.Exp, Type_a> inTpl;
output tuple<DAE.Exp, Type_a> outTpl;
end FuncExpType;
algorithm
(oWhenClauses,outTypeA) := match(iWhenClauses,func,inTypeA,iAcc)
local
list<BackendDAE.WhenClause> whenClause;
DAE.Exp condition;
list<BackendDAE.WhenOperator> reinitStmtLst;
Option<Integer> elseClause;
Type_a arg;
case({},_,_,_) then (listReverse(iAcc),inTypeA);
case(BackendDAE.WHEN_CLAUSE(condition,reinitStmtLst,elseClause)::whenClause,_,_,_)
equation
((condition,arg)) = Expression.traverseExp(condition,func,inTypeA);
(whenClause,arg) = traverseWhenClauseExps(whenClause,func,arg,BackendDAE.WHEN_CLAUSE(condition,reinitStmtLst,elseClause)::iAcc);
then
(whenClause,arg);
end match;
end traverseWhenClauseExps;


protected function traverseZeroCrossingExps
replaceable type Type_a = polymorphic<Any>;
input list<BackendDAE.ZeroCrossing> iZeroCrossing;
input FuncExpType func;
input Type_a inTypeA;
input list<BackendDAE.ZeroCrossing> iAcc;
output list<BackendDAE.ZeroCrossing> oZeroCrossing;
output Type_a outTypeA;
partial function FuncExpType
input tuple<DAE.Exp, Type_a> inTpl;
output tuple<DAE.Exp, Type_a> outTpl;
end FuncExpType;
algorithm
(oZeroCrossing,outTypeA) := match(iZeroCrossing,func,inTypeA,iAcc)
local
list<BackendDAE.ZeroCrossing> zeroCrossing;
DAE.Exp relation_;
list<Integer> occurEquLst,occurWhenLst;
Type_a arg;
case({},_,_,_) then (listReverse(iAcc),inTypeA);
case(BackendDAE.ZERO_CROSSING(relation_,occurEquLst,occurWhenLst)::zeroCrossing,_,_,_)
equation
((relation_,arg)) = Expression.traverseExp(relation_,func,inTypeA);
(zeroCrossing,arg) = traverseZeroCrossingExps(zeroCrossing,func,arg,BackendDAE.ZERO_CROSSING(relation_,occurEquLst,occurWhenLst)::iAcc);
then
(zeroCrossing,arg);
end match;
end traverseZeroCrossingExps;

/*
* tearing
Expand Down
47 changes: 46 additions & 1 deletion Compiler/BackEnd/Initialization.mo
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ algorithm
BackendDAE.DAE(systs, BackendDAE.SHARED(knownVars=knownVars)) := inDAE;
outVars := selectInitializationVariables(systs);
outVars := BackendVariable.traverseBackendDAEVars(knownVars, selectInitializationVariables2, outVars);
/* what is with alias variables, I guess this one should also be checked */
end selectInitializationVariablesDAE;

protected function selectInitializationVariables "function selectInitializationVariables
Expand Down Expand Up @@ -386,7 +387,8 @@ algorithm
fixvars = BackendVariable.emptyVars();
eqns = BackendEquation.emptyEqns();
reeqns = BackendEquation.emptyEqns();


((vars, fixvars)) = BackendVariable.traverseBackendDAEVars(avars, collectInitialAliasVars, (vars, fixvars));
((vars, fixvars)) = BackendVariable.traverseBackendDAEVars(knvars, collectInitialVars, (vars, fixvars));
((eqns, reeqns)) = BackendEquation.traverseBackendDAEEqns(inieqns, collectInitialEqns, (eqns, reeqns));

Expand Down Expand Up @@ -935,6 +937,49 @@ algorithm
end matchcontinue;
end collectInitialVars;

protected function collectInitialAliasVars "function collectInitialAliasVars
author: lochel
This function collects all the vars for the initial system."
input tuple<BackendDAE.Var, tuple<BackendDAE.Variables, BackendDAE.Variables>> inTpl;
output tuple<BackendDAE.Var, tuple<BackendDAE.Variables, BackendDAE.Variables>> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
BackendDAE.Var var, preVar, derVar;
BackendDAE.Variables vars, fixvars;
DAE.ComponentRef cr, preCR, derCR;
Boolean isFixed;
DAE.Type ty;
DAE.InstDims arryDim;
Option<DAE.Exp> startValue;
DAE.Exp startExp, bindExp;
String errorMessage;

// discrete
case((var as BackendDAE.VAR(varName=cr, varKind=BackendDAE.DISCRETE(), varType=ty, arryDim=arryDim), (vars, fixvars))) equation
isFixed = BackendVariable.varFixed(var);
startValue = BackendVariable.varStartValueOption(var);

preCR = ComponentReference.crefPrefixPre(cr); // cr => $PRE.cr
preVar = BackendDAE.VAR(preCR, BackendDAE.DISCRETE(), DAE.BIDIR(), DAE.NON_PARALLEL(), ty, NONE(), NONE(), arryDim, DAE.emptyElementSource, NONE(), NONE(), DAE.NON_CONNECTOR());
preVar = BackendVariable.setVarFixed(preVar, isFixed);
preVar = BackendVariable.setVarStartValueOption(preVar, startValue);

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

case ((var, _)) equation
errorMessage = "./Compiler/BackEnd/Initialization.mo: function collectInitialAliasVars failed for: " +& BackendDump.varString(var);
Error.addMessage(Error.INTERNAL_ERROR, {errorMessage});
then fail();

else equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/Initialization.mo: function collectInitialAliasVars failed"});
then fail();
end matchcontinue;
end collectInitialAliasVars;

protected function collectInitialBindings "function collectInitialBindings
author: lochel
This function collects all the vars for the initial system."
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ algorithm

// replace pre(alias) in time-equations
dlow = BackendDAEOptimize.simplifyTimeIndepFuncCalls(dlow);

// generate system for inline solver
(inlineDAE, _) = InlineSolver.generateDAE(dlow);

Expand Down

0 comments on commit 66de325

Please sign in to comment.