Skip to content

Commit

Permalink
- start to ged rid of the wrapper equations
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12194 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jun 23, 2012
1 parent 4649fe6 commit 5834280
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 260 deletions.
33 changes: 26 additions & 7 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -117,10 +117,17 @@ uniontype Equation "- Equation"
.DAE.ElementSource source "origin of equation";
end EQUATION;

record ARRAY_EQUATION
record ARRAY_EQUATIONWRAPPER
Integer index "index ; index in arrayequations 0..n-1" ;
list< .DAE.Exp> crefOrDerCref "crefOrDerCref ; CREF or der(CREF)" ;
.DAE.ElementSource source "origin of equation";
end ARRAY_EQUATIONWRAPPER;

record ARRAY_EQUATION
list<Integer> dimSize "dimSize ; dimension sizes" ;
.DAE.Exp left "left ; lhs" ;
.DAE.Exp right "right ; rhs" ;
.DAE.ElementSource source "the element source";
end ARRAY_EQUATION;

record SOLVED_EQUATION
Expand All @@ -135,28 +142,40 @@ uniontype Equation "- Equation"
end RESIDUAL_EQUATION;

record ALGORITHM
Integer size "size of equation" ;
.DAE.Algorithm alg;
.DAE.ElementSource source "origin of algorithm";
end ALGORITHM;

record ALGORITHMWRAPPER
Integer index "Index in algorithms, 0..n-1" ;
list< .DAE.Exp> in_ "Inputs CREF or der(CREF)" ;
list< .DAE.Exp> out "Outputs CREF or der(CREF)" ;
.DAE.ElementSource source "origin of algorithm";
end ALGORITHM;
end ALGORITHMWRAPPER;

record WHEN_EQUATION
WhenEquation whenEquation "whenEquation" ;
.DAE.ElementSource source "origin of equation";
end WHEN_EQUATION;

record COMPLEX_EQUATION "complex equations: recordX = function call(x, y, ..);"
record COMPLEX_EQUATIONWRAPPER "complex equations: recordX = function call(x, y, ..);"
Integer index "Index in algorithm clauses";
list< .DAE.Exp> crefOrDerCref "crefOrDerCref ; CREF or der(CREF)" ;
.DAE.ElementSource source "origin of equation";
end COMPLEX_EQUATION;
end COMPLEX_EQUATIONWRAPPER;

record COMPLEX_EQUATION "complex equations: recordX = function call(x, y, ..);"
Integer size "size of equation" ;
.DAE.Exp left "left ; lhs" ;
.DAE.Exp right "right ; rhs" ;
.DAE.ElementSource source "the element source";
end COMPLEX_EQUATION;

record IF_EQUATION " an if-equation"
Integer indx "" ;
Integer eindx "" ;
list< .DAE.Exp> expl "" ;
list< .DAE.Exp> conditions "Condition";
list<list<Equation>> eqnstrue "Equations of true branch";
list<Equation> eqnsfalse "Equations of false branch";
.DAE.ElementSource source "origin of equation";
end IF_EQUATION;

Expand Down
63 changes: 38 additions & 25 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -41,8 +41,8 @@ encapsulated package BackendDAECreate
public import Absyn;
public import BackendDAE;
public import DAE;
public import Algorithm;

protected import Algorithm;
protected import BackendDAEUtil;
protected import BackendEquation;
protected import BackendVariable;
Expand Down Expand Up @@ -1670,7 +1670,7 @@ algorithm
expl1 = BackendDAEUtil.statesAndVarsExp(e1, vars);
expl2 = BackendDAEUtil.statesAndVarsExp(e2, vars);
expl = listAppend(expl1, expl2);
complexEqs = List.fill(BackendDAE.COMPLEX_EQUATION(aindx,expl,source), i);
complexEqs = List.fill(BackendDAE.COMPLEX_EQUATIONWRAPPER(aindx,expl,source), i);
lst = listAppend(complexEqs,lst);
then
(lst,indx);
Expand Down Expand Up @@ -1867,7 +1867,7 @@ algorithm
numnodes_1 = numnodes - 1;
res = lowerMultidimeqn2(expl, numnodes_1, indx, source);
then
(BackendDAE.ARRAY_EQUATION(indx,expl,source) :: res);
(BackendDAE.ARRAY_EQUATIONWRAPPER(indx,expl,source) :: res);
end matchcontinue;
end lowerMultidimeqn2;

Expand Down Expand Up @@ -1976,14 +1976,14 @@ algorithm
list<DAE.Exp> inputs,outputs;
case (inputs as (_::_),{},0,aindx)
then
({},{BackendDAE.ALGORITHM(aindx,inputs,{},DAE.emptyElementSource)});
({},{BackendDAE.ALGORITHMWRAPPER(aindx,inputs,{},DAE.emptyElementSource)});
case (_,_,0,_) then ({},{});
case (inputs,outputs,numnodes,aindx)
equation
numnodes_1 = numnodes - 1;
(res,res1) = lowerAlgorithm2(inputs, outputs, numnodes_1, aindx);
then
(BackendDAE.ALGORITHM(aindx,inputs,outputs,DAE.emptyElementSource) :: res,res1);
(BackendDAE.ALGORITHMWRAPPER(aindx,inputs,outputs,DAE.emptyElementSource) :: res,res1);
end matchcontinue;
end lowerAlgorithm2;

Expand All @@ -1997,25 +1997,38 @@ public function lowerAlgorithmInputsOutputs
input BackendDAE.Variables inVariables;
input DAE.Algorithm inAlgorithm;
output tuple<list<DAE.Exp>,list<DAE.Exp>> outTplExpExpLst;
protected
list<Algorithm.Statement> ss;
algorithm
outTplExpExpLst := match (inVariables,inAlgorithm)
DAE.ALGORITHM_STMTS(statementLst = ss) := inAlgorithm;
outTplExpExpLst := lowerAlgorithmInputsOutputs1(inVariables, ss);
end lowerAlgorithmInputsOutputs;

public function lowerAlgorithmInputsOutputs1
"function: lowerAlgorithmInputsOutputs1
helper for lowerAlgorithmInputsOutputs."
input BackendDAE.Variables inVariables;
input list<Algorithm.Statement> statementLst;
output tuple<list<DAE.Exp>,list<DAE.Exp>> outTplExpExpLst;
algorithm
outTplExpExpLst := match (inVariables,statementLst)
local
list<DAE.Exp> inputs1,outputs1,inputs2,outputs2,inputs,outputs;
BackendDAE.Variables vars;
Algorithm.Statement s;
list<Algorithm.Statement> ss;
case (_,DAE.ALGORITHM_STMTS(statementLst = {})) then (({},{}));
case (vars,DAE.ALGORITHM_STMTS(statementLst = (s :: ss)))
case (_,{}) then (({},{}));
case (vars,s :: ss)
equation
(inputs1,outputs1) = lowerStatementInputsOutputs(vars, s);
((inputs2,outputs2)) = lowerAlgorithmInputsOutputs(vars, DAE.ALGORITHM_STMTS(ss));
((inputs2,outputs2)) = lowerAlgorithmInputsOutputs1(vars, ss);
inputs = List.unionOnTrue(inputs1, inputs2, Expression.expEqual);
outputs = List.unionOnTrue(outputs1, outputs2, Expression.expEqual);
inputs = List.fold1(outputs,List.removeOnTrue,Expression.expEqual,inputs);
then
((inputs,outputs));
end match;
end lowerAlgorithmInputsOutputs;
end lowerAlgorithmInputsOutputs1;

protected function lowerStatementInputsOutputs
"function: lowerStatementInputsOutputs
Expand Down Expand Up @@ -2072,15 +2085,15 @@ algorithm
then (inputs,outputs);
case(vars,DAE.STMT_IF(exp = e, statementLst = stmts, else_ = elsebranch))
equation
((inputs1,outputs1)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(stmts));
((inputs1,outputs1)) = lowerAlgorithmInputsOutputs1(vars,stmts);
(inputs2,outputs2) = lowerElseAlgorithmInputsOutputs(vars,elsebranch);
inputs3 = BackendDAEUtil.statesAndVarsExp(e,vars);
inputs = List.unionOnTrueList({inputs1, inputs2,inputs3}, Expression.expEqual);
outputs = List.unionOnTrue(outputs1, outputs2, Expression.expEqual);
then (inputs,outputs);
case(vars, DAE.STMT_FOR(type_= tp, iter = iteratorName, range = e, statementLst = stmts))
equation
((inputs1,outputs1)) = lowerAlgorithmInputsOutputs(vars, DAE.ALGORITHM_STMTS(stmts));
((inputs1,outputs1)) = lowerAlgorithmInputsOutputs1(vars, stmts);
inputs2 = BackendDAEUtil.statesAndVarsExp(e, vars);
// Split the output variables into variables that depend on the loop
// variable and variables that don't.
Expand All @@ -2097,21 +2110,21 @@ algorithm
then (inputs, outputs);
case(vars, DAE.STMT_WHILE(exp = e, statementLst = stmts))
equation
((inputs1,outputs)) = lowerAlgorithmInputsOutputs(vars, DAE.ALGORITHM_STMTS(stmts));
((inputs1,outputs)) = lowerAlgorithmInputsOutputs1(vars, stmts);
inputs2 = BackendDAEUtil.statesAndVarsExp(e, vars);
inputs = List.unionOnTrueList({inputs1, inputs2}, Expression.expEqual);
then (inputs, outputs);
case (vars,DAE.STMT_WHEN(exp = e,statementLst = statements,elseWhen = NONE()))
equation
((inputs1,outputs)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(statements));
((inputs1,outputs)) = lowerAlgorithmInputsOutputs1(vars,statements);
inputs2 = BackendDAEUtil.statesAndVarsExp(e, vars);
inputs = List.unionOnTrueList({inputs1, inputs2}, Expression.expEqual);
then
(inputs,outputs);
case (vars,DAE.STMT_WHEN(exp = e,statementLst = statements,elseWhen = SOME(stmt)))
equation
(inputs1, outputs1) = lowerStatementInputsOutputs(vars,stmt);
((inputs2,outputs2)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(statements));
((inputs2,outputs2)) = lowerAlgorithmInputsOutputs1(vars,statements);
inputs3 = BackendDAEUtil.statesAndVarsExp(e, vars);
inputs = List.unionOnTrueList({inputs1, inputs2, inputs3}, Expression.expEqual);
outputs = List.unionOnTrueList({outputs1, outputs2}, Expression.expEqual);
Expand All @@ -2138,19 +2151,19 @@ algorithm
// MetaModelica extension. KS
case(vars, DAE.STMT_FAILURE(body = statements))
equation
((inputs,outputs)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(statements));
((inputs,outputs)) = lowerAlgorithmInputsOutputs1(vars,statements);
then (inputs,outputs);
case(vars, DAE.STMT_TRY(tryBody = statements))
equation
((inputs,outputs)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(statements));
((inputs,outputs)) = lowerAlgorithmInputsOutputs1(vars,statements);
then (inputs,outputs);
case(vars, DAE.STMT_CATCH(catchBody = statements))
equation
((inputs,outputs)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(statements));
((inputs,outputs)) = lowerAlgorithmInputsOutputs1(vars,statements);
then (inputs,outputs);
case(vars, DAE.STMT_CATCH(catchBody = statements))
equation
((inputs,outputs)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(statements));
((inputs,outputs)) = lowerAlgorithmInputsOutputs1(vars,statements);
then (inputs,outputs);
case(vars, DAE.STMT_THROW(source=_)) then ({}, {});
case(_, _)
Expand Down Expand Up @@ -2180,15 +2193,15 @@ algorithm
case(vars,DAE.ELSEIF(e,stmts,elseBranch))
equation
(inputs1, outputs1) = lowerElseAlgorithmInputsOutputs(vars,elseBranch);
((inputs2, outputs2)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(stmts));
((inputs2, outputs2)) = lowerAlgorithmInputsOutputs1(vars,stmts);
inputs3 = BackendDAEUtil.statesAndVarsExp(e,vars);
inputs = List.unionOnTrueList({inputs1, inputs2, inputs3}, Expression.expEqual);
outputs = List.unionOnTrue(outputs1, outputs2, Expression.expEqual);
then (inputs,outputs);

case(vars,DAE.ELSE(stmts))
equation
((inputs, outputs)) = lowerAlgorithmInputsOutputs(vars,DAE.ALGORITHM_STMTS(stmts));
((inputs, outputs)) = lowerAlgorithmInputsOutputs1(vars,stmts);
then (inputs,outputs);
end match;
end lowerElseAlgorithmInputsOutputs;
Expand Down Expand Up @@ -2718,7 +2731,7 @@ algorithm
(accAlg,accResDiff,accArr) = extractAlgebraicAndDifferentialEqn(rest,accAlg,accResDiff,accArr);
then
(accAlg,accResDiff,accArr);
case (((eqn as BackendDAE.COMPLEX_EQUATION(crefOrDerCref = expl)) :: rest),accAlg,accResDiff,accArr) /* complex equation */
case (((eqn as BackendDAE.COMPLEX_EQUATIONWRAPPER(crefOrDerCref = expl)) :: rest),accAlg,accResDiff,accArr) /* complex equation */
equation
// fails if not all call results are true
isalg = List.mapAllValueBool(expl, isAlgebraic, true);
Expand All @@ -2727,7 +2740,7 @@ algorithm
(accAlg,accResDiff,accArr) = extractAlgebraicAndDifferentialEqn(rest,accAlg,accResDiff,accArr);
then
(accAlg,accResDiff,accArr);
case (((eqn as BackendDAE.ARRAY_EQUATION(index = indx,crefOrDerCref = expl)) :: rest),accAlg,accResDiff,accArr) /* array equation */
case (((eqn as BackendDAE.ARRAY_EQUATIONWRAPPER(index = indx,crefOrDerCref = expl)) :: rest),accAlg,accResDiff,accArr) /* array equation */
equation
// fails if not all call results are true
isalg = List.mapAllValueBool(expl, isAlgebraic, true);
Expand Down Expand Up @@ -3214,7 +3227,7 @@ algorithm
(res1,eq_reslst,mdeqs_res1,wc_reslst,algs,ces_res) = findZeroCrossings2(v, knvars,xs,mdeqs,eq_count, {}, 0,{},countZC,res,0,ces);
then
(res1,BackendDAE.EQUATION(eres1,eres2,source_)::eq_reslst,mdeqs_res1,wc_reslst,algs,ces_res);
case (v,knvars,((e as BackendDAE.COMPLEX_EQUATION(index= ind)) :: xs),mdeqs,eq_count,{},_,{},countZC,zcs,_,ces)
case (v,knvars,((e as BackendDAE.COMPLEX_EQUATIONWRAPPER(index= ind)) :: xs),mdeqs,eq_count,{},_,{},countZC,zcs,_,ces)
equation
// Find the correct multidim equation from the index
BackendDAE.COMPLEXEQUATION(size=index_,left=e1,right=e2,source=source) = listNth(ces,ind);
Expand All @@ -3226,7 +3239,7 @@ algorithm
(res1,eq_reslst,mdeqs_res1,wc_reslst,algs,ces_res) = findZeroCrossings2(v, knvars,xs,mdeqs,eq_count, {}, 0,{},countZC,res,0,ces);
then
(res1,e::eq_reslst,mdeqs_res1,wc_reslst,algs,ces_res);
case (v,knvars,((e as BackendDAE.ARRAY_EQUATION(index = ind)) :: xs),mdeqs,eq_count,{},_,{},countZC,zcs,_,ces)
case (v,knvars,((e as BackendDAE.ARRAY_EQUATIONWRAPPER(index = ind)) :: xs),mdeqs,eq_count,{},_,{},countZC,zcs,_,ces)
equation
// Find the correct multidim equation from the index
BackendDAE.MULTIDIM_EQUATION(dimSize=dimsize,left=e1,right=e2,source=source) = listNth(mdeqs,ind);
Expand Down

0 comments on commit 5834280

Please sign in to comment.