Skip to content

Commit

Permalink
- Util.mo
Browse files Browse the repository at this point in the history
  add function listIntRange3
- BackendDAECreate.mo
  start to implement: use the range of for_stms for findzerocrossings and detectimplicit discrete bug-1368

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7268 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 2, 2010
1 parent c609608 commit 5941b5d
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 41 deletions.
182 changes: 149 additions & 33 deletions Compiler/BackendDAECreate.mo
Expand Up @@ -131,7 +131,7 @@ algorithm
reqns = listAppend(algeqns1, reqns);
(vars,knvars,eqns,reqns,ieqns,aeqns1,algs_1,aliasVars) = BackendDAEOptimize.removeSimpleEquations(vars, knvars, eqns, reqns, ieqns, aeqns, algs, s);
vars_1 = detectImplicitDiscrete(vars, eqns);
vars_1 = detectImplicitDiscreteAlgs(vars_1, algs_1);
vars_1 = detectImplicitDiscreteAlgs(vars_1,knvars, algs_1);
eqns_1 = sortEqn(eqns);
(eqns_1,ieqns,aeqns1,algs,vars_1) = expandDerOperator(vars_1,eqns_1,ieqns,aeqns1,algs_1,functionTree);
(zero_crossings) = findZeroCrossings(vars_1,knvars,eqns_1,aeqns1,whenclauses_1,algs);
Expand Down Expand Up @@ -172,7 +172,7 @@ algorithm
// no simplify (vars,knvars,eqns,reqns,ieqns,aeqns1,algs_1,aliasVars) = BackendDAEOptimize.removeSimpleEquations(vars, knvars, eqns, reqns, ieqns, aeqns, algs, s);
aliasVars = BackendDAEUtil.emptyAliasVariables();
vars_1 = detectImplicitDiscrete(vars, eqns);
vars_1 = detectImplicitDiscreteAlgs(vars_1, algs);
vars_1 = detectImplicitDiscreteAlgs(vars_1,knvars, algs);
eqns_1 = sortEqn(eqns);
// no simplify (eqns_1,ieqns,aeqns1,algs,vars_1) = expandDerOperator(vars_1,eqns_1,ieqns,aeqns1,algs_1,functionTree);
(zero_crossings) = findZeroCrossings(vars_1,knvars,eqns_1,aeqns,whenclauses_1,algs);
Expand Down Expand Up @@ -2004,24 +2004,25 @@ protected function detectImplicitDiscreteAlgs
This function updates the variable kind to discrete
for variables set in when equations."
input BackendDAE.Variables inVariables;
input BackendDAE.Variables inKnVariables;
input list<DAE.Algorithm> inAlgsLst;
output BackendDAE.Variables outVariables;
algorithm
outVariables := matchcontinue (inVariables,inAlgsLst)
outVariables := matchcontinue (inVariables,inKnVariables,inAlgsLst)
local
BackendDAE.Variables v,v_1,v_2;
BackendDAE.Variables v,v_1,v_2,knv;
list<DAE.Statement> statementLst;
list<DAE.Algorithm> xs;
case (v,{}) then v;
case (v,(DAE.ALGORITHM_STMTS(statementLst = statementLst) :: xs))
case (v,_,{}) then v;
case (v,knv,(DAE.ALGORITHM_STMTS(statementLst = statementLst) :: xs))
equation
v_1 = detectImplicitDiscreteAlgsStatemens(v,statementLst,false);
v_2 = detectImplicitDiscreteAlgs(v_1, xs);
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst,false);
v_2 = detectImplicitDiscreteAlgs(v_1,knv, xs);
then
v_2;
case (v,(_ :: xs))
case (v,knv,(_ :: xs))
equation
v_1 = detectImplicitDiscreteAlgs(v, xs);
v_1 = detectImplicitDiscreteAlgs(v,knv, xs);
then
v_1;
end matchcontinue;
Expand All @@ -2032,70 +2033,185 @@ protected function detectImplicitDiscreteAlgsStatemens
This function updates the variable kind to discrete
for variables set in when equations."
input BackendDAE.Variables inVariables;
input BackendDAE.Variables inKnVariables;
input list<DAE.Statement> inStatementLst;
input Boolean insideWhen "true if its called from a when statement";
output BackendDAE.Variables outVariables;
algorithm
outVariables := matchcontinue (inVariables,inStatementLst,insideWhen)
outVariables := matchcontinue (inVariables,inKnVariables,inStatementLst,insideWhen)
local
BackendDAE.Variables v,v_1,v_2,v_3;
BackendDAE.Variables v,v_1,v_2,v_3,knv;
DAE.ComponentRef cr;
list<DAE.Statement> xs,statementLst;
BackendDAE.Var var;
list<BackendDAE.Var> vars;
DAE.Statement statement;
Boolean b;
case (v,{},_) then v;
case (v,(DAE.STMT_ASSIGN(exp1 =DAE.CREF(componentRef = cr)) :: xs),true)
DAE.ExpType tp;
DAE.Ident iteratorName;
DAE.Exp e,iteratorExp;
list<DAE.Exp> iteratorexps;
case (v,_,{},_) then v;
case (v,knv,(DAE.STMT_ASSIGN(exp1 =DAE.CREF(componentRef = cr)) :: xs),true)
equation
((var :: _),_) = BackendVariable.getVar(cr, v);
var = BackendVariable.setVarKind(var,BackendDAE.DISCRETE());
v_1 = BackendVariable.addVar(var, v);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1, xs,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,true);
then
v_2;
case (v,(DAE.STMT_ASSIGN_ARR(componentRef = cr) :: xs),true)
case (v,knv,(DAE.STMT_ASSIGN_ARR(componentRef = cr) :: xs),true)
equation
(vars,_) = BackendVariable.getVar(cr, v);
vars = Util.listMap1(vars,BackendVariable.setVarKind,BackendDAE.DISCRETE());
v_1 = BackendVariable.addVars(vars,v);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1, xs,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,true);
then
v_2;
case (v,(DAE.STMT_IF(statementLst = statementLst) :: xs),true)
case (v,knv,(DAE.STMT_IF(statementLst = statementLst) :: xs),true)
equation
v_1 = detectImplicitDiscreteAlgsStatemens(v,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1, xs,true);
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,true);
then
v_2;
case (v,(DAE.STMT_FOR(statementLst = statementLst) :: xs),true)
case (v,knv,(DAE.STMT_FOR(type_= tp, iter = iteratorName, range = e,statementLst = statementLst) :: xs),true)
equation
/* TODO: use the range for the componentreferences */
v_1 = detectImplicitDiscreteAlgsStatemens(v,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1, xs,true);
//cr = ComponentReference.makeCrefIdent(iteratorName, tp, {});
//iteratorExp = Expression.crefExp(cr);
//iteratorexps = extendRange(e,knv);
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,true);
then
v_2;
case (v,(DAE.STMT_WHEN(statementLst = statementLst,elseWhen = NONE()) :: xs),_)
case (v,knv,(DAE.STMT_WHEN(statementLst = statementLst,elseWhen = NONE()) :: xs),_)
equation
v_1 = detectImplicitDiscreteAlgsStatemens(v,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1, xs,false);
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv, xs,false);
then
v_2;
case (v,(DAE.STMT_WHEN(statementLst = statementLst,elseWhen = SOME(statement)) :: xs),_)
case (v,knv,(DAE.STMT_WHEN(statementLst = statementLst,elseWhen = SOME(statement)) :: xs),_)
equation
v_1 = detectImplicitDiscreteAlgsStatemens(v,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,{statement},true);
v_3 = detectImplicitDiscreteAlgsStatemens(v_2, xs,false);
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst,true);
v_2 = detectImplicitDiscreteAlgsStatemens(v_1,knv,{statement},true);
v_3 = detectImplicitDiscreteAlgsStatemens(v_2,knv, xs,false);
then
v_3;
case (v,(_ :: xs),b)
case (v,knv,(_ :: xs),b)
equation
v_1 = detectImplicitDiscreteAlgsStatemens(v, xs,b);
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv, xs,b);
then
v_1;
end matchcontinue;
end detectImplicitDiscreteAlgsStatemens;

protected function detectImplicitDiscreteAlgsStatemensFor
"function: detectImplicitDiscreteAlgsStatemensFor
"
input DAE.Exp inIteratorExp;
input list<DAE.Exp> inExplst;
input BackendDAE.Variables inVariables;
input BackendDAE.Variables inKnVariables;
input list<DAE.Statement> inStatementLst;
input Boolean insideWhen "true if its called from a when statement";
output BackendDAE.Variables outVariables;
algorithm
outVariables := matchcontinue (inIteratorExp,inExplst,inVariables,inKnVariables,inStatementLst,insideWhen)
local
BackendDAE.Variables v,v_1,v_2,knv;
list<DAE.Statement> statementLst,statementLst1;
BackendDAE.Var var;
Boolean b;
DAE.Exp e,ie;
list<DAE.Exp> rest;
case (_,{},v,_,_,_) then v;
case (ie,e::rest,v,knv,statementLst,b)
equation
(statementLst1,_) = DAEUtil.traverseDAEEquationsStmts(statementLst,replaceExp,((ie,e)));
v_1 = detectImplicitDiscreteAlgsStatemens(v,knv,statementLst1,true);
v_2 = detectImplicitDiscreteAlgsStatemensFor(ie,rest,v_1,knv,statementLst,b);
then
v_2;
case (_,_,_,_,_,_)
equation
print("BackendDAECreate.detectImplicitDiscreteAlgsStatemensFor failed \n");
then
fail();
end matchcontinue;
end detectImplicitDiscreteAlgsStatemensFor;

protected function replaceExp
"Help function to e.g. detectImplicitDiscreteAlgsStatemensFor"
input tuple<DAE.Exp,tuple<DAE.Exp,DAE.Exp>> tpl;
output tuple<DAE.Exp,tuple<DAE.Exp,DAE.Exp>> outTpl;
algorithm
outTpl := matchcontinue(tpl)
local
DAE.Exp e,e1,s,t;
case((e,(s,t))) equation
(e1,_) = Expression.replaceExp(e,s,t);
then ((e1,(s,t)));
case tpl then tpl;
end matchcontinue;
end replaceExp;

protected function extendRange
"function: extendRange
"
input DAE.Exp rangeExp;
input BackendDAE.Variables inKnVariables;
output list<DAE.Exp> outExpLst;
algorithm
outExpLst:=
matchcontinue (rangeExp,inKnVariables)
local
list<DAE.Exp> explst;
DAE.ExpType tp;
DAE.Exp startvalue,stopvalue,stepvalue;
Option<DAE.Exp> stepvalueopt;
BackendDAE.Variables knv;
Integer istart,istop,istep;
list<Integer> ilst;
case (DAE.RANGE(ty=tp,exp=startvalue,expOption=stepvalueopt,range=stopvalue),knv)
equation
stepvalue = Util.getOptionOrDefault(stepvalueopt,DAE.ICONST(1));
istart = expInt(startvalue,knv);
istep = expInt(stepvalue,knv);
istop = expInt(stopvalue,knv);
ilst = Util.listIntRange3(istart,istop,istep);
explst = Util.listMap(ilst,Expression.makeIntegerExp);
then
explst;
case (_,_)
equation
print("BackendDAECreate.extendRange failed \n");
then
fail();
end matchcontinue;
end extendRange;

protected function expInt "returns the int value of an expression"
input DAE.Exp exp;
input BackendDAE.Variables inKnVariables;
output Integer i;
algorithm
i := matchcontinue(exp,inKnVariables)
local
Integer i2;
DAE.ComponentRef cr;
BackendDAE.Variables knv;
DAE.Exp e;
case (DAE.ICONST(integer = i2),_) then i2;
case (DAE.ENUM_LITERAL(index = i2),_) then i2;
case (DAE.CREF(componentRef=cr),knv)
equation
((BackendDAE.VAR(bindExp=SOME(e)):: _),_) = BackendVariable.getVar(cr, knv);
i2 = expInt(e,knv);
then
i2;
end matchcontinue;
end expInt;

protected function sortEqn
"function: sortEqn
This function sorts the equation. It puts first the algebraic eqns
Expand All @@ -2114,7 +2230,7 @@ algorithm
res;
case (eqns)
equation
print("sort_eqn failed \n");
print("BackendDAECreate.sort_eqn failed \n");
then
fail();
end matchcontinue;
Expand Down
29 changes: 21 additions & 8 deletions Compiler/Util.mo
Expand Up @@ -482,6 +482,18 @@ algorithm
outTypeALst := {inTypeA1, inTypeA2};
end listMake2;

public function listIntRange3 "
Returns a list of integers from n to m. Only works if n < m.
Example listIntRange2(3,9,2) => {3,5,7,9}
"
input Integer n;
input Integer m;
input Integer s;
output list<Integer> res;
algorithm
res := listIntRangeHelp(n,m,s);
end listIntRange3;

public function listIntRange2 "
Returns a list of integers from n to m. Only works if n < m.
Example listIntRange2(3,5) => {3,4,5}
Expand All @@ -490,7 +502,7 @@ Example listIntRange2(3,5) => {3,4,5}
input Integer m;
output list<Integer> res;
algorithm
res := listIntRangeHelp(n,m);
res := listIntRangeHelp(n,m,1);
end listIntRange2;

public function listIntRange "function: listIntRange
Expand All @@ -499,7 +511,7 @@ public function listIntRange "function: listIntRange
input Integer n;
output list<Integer> res;
algorithm
res := listIntRangeHelp(1,n); /* listIntRange_tail(1, n, {}); */
res := listIntRangeHelp(1,n,1); /* listIntRange_tail(1, n, {}); */
end listIntRange;

protected function listIntRange_tail
Expand Down Expand Up @@ -532,22 +544,23 @@ end listIntRange_tail;
protected function listIntRangeHelp
input Integer inInteger1;
input Integer inInteger2;
input Integer inInteger3;
output list<Integer> outIntegerLst;
algorithm
outIntegerLst:=
matchcontinue (inInteger1,inInteger2)
matchcontinue (inInteger1,inInteger2,inInteger3)
local
Integer i_1,i,n;
Integer i_1,i,n,s;
list<Integer> res;
case (i,n)
case (i,n,s)
equation
(i < n) = true;
i_1 = i + 1;
i_1 = i + s;

res = listIntRangeHelp(i_1, n);
res = listIntRangeHelp(i_1, n,s);
then
(i :: res);
case (i,n) then {i};
case (i,n,s) then {i};
end matchcontinue;
end listIntRangeHelp;

Expand Down

0 comments on commit 5941b5d

Please sign in to comment.