Skip to content

Commit

Permalink
Generating when handlers
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11789 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
fbergero committed Apr 20, 2012
1 parent 33a4aa3 commit bfef39e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDump.mo
Expand Up @@ -620,7 +620,7 @@ algorithm
case (BackendDAE.DAE(eqs,shared))
equation
List.map_0(eqs,dumpEqSystem);
print("\n");
print("**************************************************\n");
dumpShared(shared);
then
();
Expand Down
85 changes: 83 additions & 2 deletions Compiler/BackEnd/BackendQSS.mo
Expand Up @@ -63,6 +63,7 @@ uniontype QSSinfo "- equation indices in static blocks and DEVS structure"
list<DAE.ComponentRef> stateVars;
list<DAE.ComponentRef> discreteVars;
list<DAE.ComponentRef> algVars;
BackendDAE.EquationArray eqs;
end QSSINFO;
end QSSinfo;

Expand Down Expand Up @@ -91,7 +92,9 @@ algorithm
list<SimCode.SimEqSystem> eqs;
list<list<Integer>> s;
list<DAE.ComponentRef> states,disc,algs;
case (dlow, ass1, ass2, m, mt, comps,SimCode.SIMCODE(odeEquations={eqs}))
BackendDAE.EquationArray eqsdae;
BackendDAE.Shared shared;
case (dlow as BackendDAE.DAE({BackendDAE.EQSYSTEM(_,eqsdae,_,_,_)},shared), ass1, ass2, m, mt, comps,SimCode.SIMCODE(odeEquations={eqs}))
equation
print("\n ----------------------------\n");
print("BackEndQSS analysis initialized");
Expand All @@ -105,7 +108,7 @@ algorithm
states = List.map(stateVarsList,getCref);
s = computeStateRef(List.map(states,ComponentReference.crefPrefixDer),eqs,{});
then
(QSSINFO(s,states,disc,algs),simCode);
(QSSINFO(s,states,disc,algs,eqsdae),simCode);
else
equation
print("- Main function BackendQSS.generateStructureCodeQSS failed\n");
Expand Down Expand Up @@ -275,6 +278,19 @@ refs := match qssInfo
end match;
end getStates;

public function getEqs
input QSSinfo qssInfo;
output BackendDAE.EquationArray eqs;
algorithm
refs := match qssInfo
local
BackendDAE.EquationArray s;
case (QSSINFO(eqs=s))
then s;
end match;
end getEqs;


public function getAlgs
input QSSinfo qssInfo;
output list<DAE.ComponentRef> refs;
Expand Down Expand Up @@ -357,6 +373,9 @@ algorithm
equation
ident=System.stringReplace(ComponentReference.crefStr(cr),".","_");
then ((DAE.CREF(DAE.CREF_IDENT(ident,t1,subs),t),(states,disc,algs)));
case ((e as DAE.IFEXP(expCond=expCond as DAE.RELATION(_,_,_,_,_), expThen=expThen,expElse=expElse),(states,disc,algs)))
then ((DAE.BINARY(DAE.BINARY(DAE.CALL(Absyn.IDENT("boolToReal"),{expCond},DAE.callAttrBuiltinReal),DAE.MUL(DAE.T_REAL_DEFAULT),expThen),DAE.ADD(DAE.T_REAL_DEFAULT),
DAE.BINARY(DAE.BINARY(DAE.RCONST(1.0),DAE.SUB(DAE.T_REAL_DEFAULT),DAE.CALL(Absyn.IDENT("boolToReal"),{expCond},DAE.callAttrBuiltinReal)),DAE.MUL(DAE.T_REAL_DEFAULT),expElse)),(states,disc,algs)));
case ((e as DAE.IFEXP(expCond=expCond, expThen=expThen,expElse=expElse),(states,disc,algs)))
then ((DAE.BINARY(DAE.BINARY(expCond,DAE.MUL(DAE.T_REAL_DEFAULT),expThen),DAE.ADD(DAE.T_REAL_DEFAULT),
DAE.BINARY(DAE.BINARY(DAE.RCONST(1.0),DAE.SUB(DAE.T_REAL_DEFAULT),expCond),DAE.MUL(DAE.T_REAL_DEFAULT),expElse)),(states,disc,algs)));
Expand All @@ -368,9 +387,11 @@ algorithm
then ((DAE.BINARY(e,DAE.DIV(DAE.T_REAL_DEFAULT),e2),(states,disc,algs)));
case ((e,(states,disc,algs)))
equation
/*
print("****** NOT REPLACING *********\n");
print(ExpressionDump.dumpExpStr(e,0));
print("*********\n");
*/
then ((e,(states,disc,algs)));

end matchcontinue;
Expand Down Expand Up @@ -478,7 +499,67 @@ algorithm
end matchcontinue;
end replaceCref;

public function negate
input DAE.Exp exp;
output DAE.Exp exp_out;
algorithm
exp_out:=
matchcontinue (exp)
local
DAE.Exp e1,e2;
DAE.Operator op;
Integer i;
DAE.Type t;
Option<tuple<DAE.Exp,Integer,Integer>> o;
case (DAE.RELATION(e1,DAE.LESS(t),e2,i,o)) then DAE.RELATION(e1,DAE.GREATER(t),e2,i,o);
case (DAE.RELATION(e1,DAE.LESSEQ(t),e2,i,o)) then DAE.RELATION(e1,DAE.GREATER(t),e2,i,o);
case (DAE.RELATION(e1,DAE.GREATER(t),e2,i,o)) then DAE.RELATION(e1,DAE.LESS(t),e2,i,o);
case (DAE.RELATION(e1,DAE.GREATEREQ(t),e2,i,o)) then DAE.RELATION(e1,DAE.LESS(t),e2,i,o);
case (e1) then e1;
end matchcontinue;
end negate;

public function generateHandler
input BackendDAE.EquationArray eqs;
input list<Integer> handlers;
input list<DAE.ComponentRef> states;
input list<DAE.ComponentRef> disc;
input list<DAE.ComponentRef> algs;
input DAE.Exp condition;
input Boolean v;
output String out;
algorithm
out:=
matchcontinue (eqs,handlers,states,disc,algs,condition,v)
local
Integer h;
Boolean b;
BackendDAE.Equation eq;
DAE.Exp exp,e1;
DAE.Exp scalar "scalar" ;
String s;
case (_,{h},_,_,_,_,true)
equation
BackendDAE.EQUATION(exp=exp,scalar=scalar) = BackendDAEUtil.equationNth(eqs,h-1);
s = stringAppend("",ExpressionDump.printExpStr(replaceVars(exp,states,disc,algs)));
s = stringAppend(s," := ");
((e1,_))=Expression.replaceExp(scalar,condition,DAE.RCONST(1.0));
s= stringAppend(s,ExpressionDump.printExpStr(replaceVars(e1,states,disc,algs)));
s= stringAppend(s,";");
then s;
case (_,{h},_,_,_,_,false)
equation
BackendDAE.EQUATION(exp=exp,scalar=scalar) = BackendDAEUtil.equationNth(eqs,h-1);
s= stringAppend("",ExpressionDump.printExpStr(replaceVars(exp,states,disc,algs)));
s= stringAppend(s," := ");
((e1,_))=Expression.replaceExp(scalar,condition,DAE.RCONST(0.0));
s= stringAppend(s,ExpressionDump.printExpStr(replaceVars(e1,states,disc,algs)));
s= stringAppend(s,";");
((e1,_))=Expression.replaceExp(scalar,condition,DAE.RCONST(0.0));
then s;
end matchcontinue;
end generateHandler;



////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
43 changes: 20 additions & 23 deletions Compiler/susan_codegen/SimCode/CodegenQSS.tpl
Expand Up @@ -75,7 +75,7 @@ case SIMCODE(__) then
<% odeEquations |> eq => generateOdeEqs(eq,BackendQSS.getStateIndexList(qssInfo),BackendQSS.getStates(qssInfo),BackendQSS.getDisc(qssInfo),BackendQSS.getAlgs(qssInfo));separator="\n" %>
algorithm
<% generateDiscont(zeroCrossings,BackendQSS.getStates(qssInfo),BackendQSS.getDisc(qssInfo),BackendQSS.getAlgs(qssInfo),
whenClauses,allEquations) %>
whenClauses,BackendQSS.getEqs(qssInfo)) %>
end <% getName(modelInfo) %>;

>>
Expand All @@ -101,7 +101,7 @@ case MODELINFO(varInfo=varInfo as VARINFO(__), name=name as IDENT(__)) then
constant Integer N = <%varInfo.numStateVars%>;
Real x[N](start=xinit());
discrete Real d[<% intAdd(varInfo.numIntAlgVars,varInfo.numBoolAlgVars) %>](start=dinit());
Real a[<% varInfo.numAlgVars %>](start=ainit());
Real a[<% varInfo.numAlgVars %>];
<% generateExtraVars(modelInfo) %>
<% generateInitFunction(modelInfo,states,disc,algs) %>
>>
Expand Down Expand Up @@ -134,7 +134,7 @@ template InitAlgVariable(SimVar simVar, list<DAE.ComponentRef> vars)
match simVar
case SIMVAR(__) then
<<
a[<% intAdd(List.position(name,vars),1) %>]:= <% OptionInitial(initialValue) %> /* <% crefStr(name) %> */;
a[<% intAdd(List.position(name,vars),1)%>]:= <% OptionInitial(initialValue) %> /* <% crefStr(name) %> */;
>>
end InitAlgVariable;

Expand Down Expand Up @@ -178,6 +178,12 @@ template generateInitFunction(ModelInfo modelInfo, list<DAE.ComponentRef> states
match modelInfo
case MODELINFO(vars=SIMVARS(__)) then
<<
function boolToReal
input Boolean b;
output Real r;
algorithm
r:=if b then 1.0 else 0.0;
end boolToReal;
function xinit
output Real x[N];
algorithm
Expand All @@ -189,13 +195,6 @@ case MODELINFO(vars=SIMVARS(__)) then
<%vars.intAlgVars |> var hasindex i0 => InitDiscVariable(var,disc);separator="\n"%>
<%vars.boolAlgVars |> var hasindex i0 => InitDiscVariable(var,disc);separator="\n"%>
end dinit;
function ainit
output Real a[<% listLength(vars.algVars) %>];
algorithm
<%vars.algVars |> var hasindex i0 => InitAlgVariable(var,algs);separator="\n"%>
end ainit;


>>
end generateInitFunction;

Expand All @@ -217,6 +216,7 @@ template generateOdeEqs(list<SimEqSystem> odeEquations,list<list<Integer>> index
>>
end generateOdeEqs;

/*
template generateAlgorithm(SimEqSystem algEquation, list<DAE.ComponentRef> states, list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs)
"Generates one algorithm equation of the model"
::=
Expand All @@ -230,10 +230,10 @@ case SES_ALGORITHM(statements={DAE.STMT_WHEN(exp=exp)}) then
else
"NOT IMPLEMENTED EQUATION"
end generateAlgorithm;
*/

template generateOdeEq(SimEqSystem odeEquation,list<Integer> indexEq, list<DAE.ComponentRef> states, list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs)
"Generates one ODE equation of the model
der(x[ indexEq ]) = "
"Generates one ODE equation of the model"
::=
match odeEquation
case SES_SIMPLE_ASSIGN(__) then
Expand All @@ -243,36 +243,34 @@ case SES_SIMPLE_ASSIGN(__) then
end generateOdeEq;

template generateZC(list<BackendDAE.ZeroCrossing> zcs, list<DAE.ComponentRef> states,
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs, list<SimCode.SimWhenClause> whens, list<SimCode.SimEqSystem> eqs)
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs, list<SimCode.SimWhenClause> whens,
BackendDAE.EquationArray eqs)
"Generates one zc equation of the model"
::=
<<
<% (zcs |> zc => generateOneZC(zc,states,disc,algs,eqs);separator="\n") %>
>>
end generateZC;

template generateAssigment(SimCode.SimEqSystem eq,list<DAE.ComponentRef> states,
template generateAssigment(BackendDAE.EqSystem eq,list<DAE.ComponentRef> states,
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs)
"gnereates an assigment"
::=
match eq
case SimCode.SES_SIMPLE_ASSIGN(__) then
<<
<% BackendQSS.replaceCref(cref,states,disc,algs) %> = <% ExpressionDump.printExpStr(BackendQSS.replaceVars(exp,states,disc,algs)) %>;
>>
end generateAssigment;

template generateOneZC(BackendDAE.ZeroCrossing zc,list<DAE.ComponentRef> states,
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs, list<SimCode.SimEqSystem> eqs)
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs, BackendDAE.EquationArray eqs)
"generates one "
::=
match zc
case BackendDAE.ZERO_CROSSING(__) then
<<
when <% ExpressionDump.printExpStr(BackendQSS.replaceVars(relation_,states,disc,algs)) %> then
/* In Eq <% listNth(occurEquLst,0) %> out of <% listLength(occurEquLst) %> */
<% generateAssigment(listNth(eqs,listNth(occurEquLst,0)),states,disc,algs) %>

<% BackendQSS.generateHandler(eqs,occurEquLst,states,disc,algs,relation_,true) %>
elsewhen <% ExpressionDump.printExpStr(BackendQSS.replaceVars(BackendQSS.negate(relation_),states,disc,algs)) %> then
<% BackendQSS.generateHandler(eqs,occurEquLst,states,disc,algs,relation_,false) %>
end when;
>>
end generateOneZC;
Expand Down Expand Up @@ -311,10 +309,9 @@ template generateCond(list<tuple<DAE.Exp, Integer>> conds, list<DAE.ComponentRef
end generateCond;

template generateDiscont(list<BackendDAE.ZeroCrossing> zcs, list<DAE.ComponentRef> states,
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs, list<SimCode.SimWhenClause> whens, list<SimCode.SimEqSystem> eqs)
list<DAE.ComponentRef> disc, list<DAE.ComponentRef> algs, list<SimCode.SimWhenClause> whens,BackendDAE.EquationArray eqs)
::=
<<
/* Got <% listLength(eqs) %> equations */
<% generateZC(zcs,states,disc,algs,whens,eqs) %>
>>
end generateDiscont;
Expand Down
24 changes: 24 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -2259,6 +2259,7 @@ package BackendQSS
list<DAE.ComponentRef> stateVars;
list<DAE.ComponentRef> discreteAlgVars;
list<DAE.ComponentRef> algVars;
BackendDAE.EqSystems eqs;
end QSSINFO;
end QSSinfo;

Expand Down Expand Up @@ -2296,6 +2297,29 @@ package BackendQSS
input QSSinfo qssInfo;
output list<DAE.ComponentRef> refs;
end getAlgs;

function negate
input DAE.Exp exp;
output DAE.Exp exp_out;
end negate;

function getEqs
input QSSinfo qssInfo;
output BackendDAE.EquationArray eqs;
end getEqs;

function generateHandler
input BackendDAE.EquationArray eqs;
input list<Integer> handlers;
input list<DAE.ComponentRef> states;
input list<DAE.ComponentRef> disc;
input list<DAE.ComponentRef> algs;
input DAE.Exp condition;
input Boolean v;
output String out;
end generateHandler;


end BackendQSS;

package BackendVariable
Expand Down

0 comments on commit bfef39e

Please sign in to comment.