Skip to content

Commit

Permalink
- added SimCode.SES_FOR_LOOP and DAE.SUM expression to express loop-e…
Browse files Browse the repository at this point in the history
…quations

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25801 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Volker Waurich committed Apr 29, 2015
1 parent 9ad1051 commit 6c0221c
Show file tree
Hide file tree
Showing 14 changed files with 532 additions and 322 deletions.
52 changes: 32 additions & 20 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -1416,76 +1416,88 @@ public function equationString "Helper function to e.g. dump."
algorithm
outString := matchcontinue (inEquation)
local
String s1,s2,s3,s4,res;
String s1,s2,s3,s4,s5,res;
DAE.Exp e1,e2,e,cond;
list<DAE.Exp> expl;
DAE.ComponentRef cr;
BackendDAE.WhenEquation weqn;
BackendDAE.EquationAttributes attr;
DAE.Algorithm alg;
DAE.ElementSource source;
list<list<BackendDAE.Equation>> eqnstrue;
list<BackendDAE.Equation> eqnsfalse,eqns;
case (BackendDAE.EQUATION(exp = e1,scalar = e2))
case (BackendDAE.EQUATION(exp = e1,scalar = e2, source=source, attr=attr))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ExpressionDump.printExpStr(e1);
s2 = ExpressionDump.printExpStr(e2);
res = stringAppendList({s1," = ",s2});
s3 = printEqAtts(attr);
res = stringAppendList({s1," = ",s2," ",s5,s3});
then
res;
case (BackendDAE.COMPLEX_EQUATION(left = e1,right = e2))
case (BackendDAE.COMPLEX_EQUATION(left = e1,right = e2, source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ExpressionDump.printExpStr(e1);
s2 = ExpressionDump.printExpStr(e2);
res = stringAppendList({s1," = ",s2});
res = stringAppendList({s1," = ",s2," ", s5});
then
res;
case (BackendDAE.ARRAY_EQUATION(left = e1,right = e2))
case (BackendDAE.ARRAY_EQUATION(left = e1,right = e2, source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ExpressionDump.printExpStr(e1);
s2 = ExpressionDump.printExpStr(e2);
res = stringAppendList({s1," = ",s2});
res = stringAppendList({s1," = ",s2," ", s5});
then
res;
case (BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e2))
case (BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e2, source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ComponentReference.printComponentRefStr(cr);
s2 = ExpressionDump.printExpStr(e2);
res = stringAppendList({s1," := ",s2});
res = stringAppendList({s1," := ",s2," ", s5});
then
res;
case (BackendDAE.WHEN_EQUATION(whenEquation = BackendDAE.WHEN_EQ(condition=cond,left = cr,right = e2, elsewhenPart = SOME(weqn))))
case (BackendDAE.WHEN_EQUATION(whenEquation = BackendDAE.WHEN_EQ(condition=cond,left = cr,right = e2, elsewhenPart = SOME(weqn)), source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ComponentReference.printComponentRefStr(cr);
s2 = ExpressionDump.printExpStr(e2);
s3 = whenEquationString(weqn);
s4 = ExpressionDump.printExpStr(cond);
res = stringAppendList({"when ",s4," then\n ",s1," := ",s2,"\n",s3,"end when"});
res = stringAppendList({"when ",s4," then\n ",s1," := ",s2,"\n",s3,"end when"," ", s5});
then
res;
case (BackendDAE.WHEN_EQUATION(whenEquation = BackendDAE.WHEN_EQ(condition=cond,left = cr,right = e2)))
case (BackendDAE.WHEN_EQUATION(whenEquation = BackendDAE.WHEN_EQ(condition=cond,left = cr,right = e2), source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ComponentReference.printComponentRefStr(cr);
s2 = ExpressionDump.printExpStr(e2);
s4 = ExpressionDump.printExpStr(cond);
res = stringAppendList({"when ",s4," then\n ",s1," := ",s2,"\nend when"});
res = stringAppendList({"when ",s4," then\n ",s1," := ",s2,"\nend when"," ", s5});
then
res;
case (BackendDAE.RESIDUAL_EQUATION(exp = e))
case (BackendDAE.RESIDUAL_EQUATION(exp = e, source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ExpressionDump.printExpStr(e);
res = stringAppendList({s1,"= 0"});
res = stringAppendList({s1,"= 0"," ", s5});
then
res;
case (BackendDAE.ALGORITHM(alg = alg,source = source))
equation
s5 = DAEDump.getSourceInformationStr(source);
res = DAEDump.dumpAlgorithmsStr({DAE.ALGORITHM(alg,source)});
res = stringAppendList({res," "});
then
res;
case (BackendDAE.IF_EQUATION(conditions=e1::expl, eqnstrue=eqns::eqnstrue, eqnsfalse=eqnsfalse))
case (BackendDAE.IF_EQUATION(conditions=e1::expl, eqnstrue=eqns::eqnstrue, eqnsfalse=eqnsfalse, source=source))
equation
s5 = DAEDump.getSourceInformationStr(source);
s1 = ExpressionDump.printExpStr(e1);
s2 = stringDelimitList(List.map(eqns,equationString),"\n ");
s3 = stringAppendList({"if ",s1," then\n ",s2});
s3 = stringAppendList({"if ",s1," then\n ",s2," ", s5});
res = ifequationString(expl,eqnstrue,eqnsfalse,s3);
then
res;
Expand Down Expand Up @@ -2319,7 +2331,7 @@ algorithm
outStr := DAEDump.dumpDirectionStr(dir) + ComponentReference.printComponentRefStr(cr) + ":"
+ kindString(kind) + "(" + connectorTypeString(ct) + attributesString(dae_var_attr)
+ ") " + optExpressionString(bindExp,"") + DAEDump.dumpCommentAnnotationStr(comment)
+ stringDelimitList(paths_lst, ", ") + " type: " + dumpTypeStr(var_type);
+ stringDelimitList(paths_lst, ", ") + " type: " + dumpTypeStr(var_type)+ "[" + stringDelimitList(List.map(arrayDim, ExpressionDump.dimensionString), ", ") + "]";
end varString;

public function dumpKind
Expand Down Expand Up @@ -3637,7 +3649,7 @@ algorithm
s := printLoopInfoStr(li);
end printEqAtts;

protected function printLoopInfoStr"outputs a string representation of a loopInfo"
public function printLoopInfoStr"outputs a string representation of a loopInfo"
input BackendDAE.LoopInfo loopInfoIn;
output String s;
algorithm
Expand All @@ -3660,7 +3672,7 @@ algorithm
end match;
end printLoopInfoStr;

protected function printIterCrefStr"outputs a string representation of a IterCref"
public function printIterCrefStr"outputs a string representation of a IterCref"
input BackendDAE.IterCref itCref;
output String s;
algorithm
Expand Down
80 changes: 75 additions & 5 deletions Compiler/BackEnd/BackendVarTransform.mo
Expand Up @@ -1214,17 +1214,21 @@ public function replaceCref"replaces a cref.
author: Waurich TUD 2014-06"
input DAE.ComponentRef crefIn;
input VariableReplacements replIn;
output DAE.ComponentRef crefOut;
output DAE.Exp expOut;
output Boolean changedOut;
algorithm
(crefOut,changedOut) := matchcontinue(crefIn,replIn)
(expOut,changedOut) := matchcontinue(crefIn,replIn)
local
DAE.Exp exp;
case(_,_)
equation
true = hasReplacement(replIn,crefIn);
DAE.CREF(componentRef=crefOut) = getReplacement(replIn,crefIn);
then (crefOut,true);
expOut = getReplacement(replIn,crefIn);
then (expOut,true);
else
then (crefIn,false);
equation
expOut = DAE.CREF(crefIn,ComponentReference.crefType(crefIn));
then (expOut,false);
end matchcontinue;
end replaceCref;

Expand Down Expand Up @@ -1703,6 +1707,7 @@ algorithm
true = b1 or b2;
source = DAEUtil.addSymbolicTransformationSubstitution(b1,source,e1,e1_1);
source = DAEUtil.addSymbolicTransformationSubstitution(b2,source,e2,e2_1);
eqAttr = replaceEquationAttributes(eqAttr,repl);
(DAE.EQUALITY_EXPS(e1_2,e2_2),source) = ExpressionSimplify.simplifyAddSymbolicOperation(DAE.EQUALITY_EXPS(e1_1,e2_1),source);
then
(BackendDAE.EQUATION(e1_2,e2_2,source,eqAttr)::inAcc,true);
Expand Down Expand Up @@ -2623,6 +2628,71 @@ algorithm
end match;
end replaceBindingExp;

protected function replaceEquationAttributes
input BackendDAE.EquationAttributes eqAttrIn;
input VariableReplacements repl;
output BackendDAE.EquationAttributes eqAttrOut;
algorithm
eqAttrOut := matchcontinue(eqAttrIn,repl)
local
Boolean differentiated;
BackendDAE.EquationKind kind;
Integer subPartitionIndex;
BackendDAE.LoopInfo loopInfo;
DAE.Exp startIt, endIt;
list<BackendDAE.IterCref> crefs;
case(BackendDAE.EQUATION_ATTRIBUTES(differentiated=differentiated, kind=kind,subPartitionIndex=subPartitionIndex, loopInfo=BackendDAE.LOOP(startIt=startIt,endIt=endIt,crefs=crefs)),_)
equation
crefs = List.map1(crefs,replaceIterationCrefs,repl);
then BackendDAE.EQUATION_ATTRIBUTES(differentiated, kind, subPartitionIndex, BackendDAE.LOOP(startIt,endIt,crefs));
else
then eqAttrIn;
end matchcontinue;
end replaceEquationAttributes;

protected function replaceIterationCrefs
input BackendDAE.IterCref iterCrefIn;
input VariableReplacements repl;
output BackendDAE.IterCref iterCrefOut;
algorithm
iterCrefOut := matchcontinue(iterCrefIn,repl)
local
DAE.ComponentRef cref;
DAE.Exp iterator, crefExp;
DAE.Operator op;
case(BackendDAE.ITER_CREF(cref = cref, iterator=iterator),_)
equation
(crefExp,_) = replaceCref(cref,repl);
{cref} = Expression.extractCrefsFromExp(crefExp);
then BackendDAE.ITER_CREF(cref, iterator);
case(BackendDAE.ACCUM_ITER_CREF(cref = cref, op=op),_)
equation
(crefExp,_) = replaceCref(cref,repl);
if Expression.isNegativeUnary(crefExp) then
op = negateOperator(op);
end if;
{cref} = Expression.extractCrefsFromExp(crefExp);
then BackendDAE.ACCUM_ITER_CREF(cref, op);
else
then iterCrefIn;
end matchcontinue;
end replaceIterationCrefs;

protected function negateOperator
"makes an add out of a sub and a sub out of an add."
input DAE.Operator inOp;
output DAE.Operator outOp;
algorithm
outOp:= match(inOp)
local
DAE.Type ty;
case(DAE.UMINUS(ty=ty)) then DAE.ADD(ty);
case(DAE.SUB(ty=ty)) then DAE.ADD(ty);
case(DAE.ADD(ty=ty)) then DAE.SUB(ty);
else inOp;
end match;
end negateOperator;

/*********************************************************/
/* dump replacements */
/*********************************************************/
Expand Down
12 changes: 7 additions & 5 deletions Compiler/BackEnd/HpcOmScheduler.mo
Expand Up @@ -4158,7 +4158,7 @@ algorithm
DAE.ElementSource source;
DAE.Exp exp,lhs;
SimCode.SimEqSystem simEqSys;
list<DAE.Exp> expLst;
list<DAE.Exp> expLst, crefExps;
list<DAE.ComponentRef> crefs;
list<DAE.ElementSource> sources;
list<DAE.Statement> stmts;
Expand Down Expand Up @@ -4236,17 +4236,19 @@ algorithm
then (simEqSys,changed);
case(SimCode.SES_WHEN(index=idx,conditions=crefs,initialCall=ic,left=cref,right=exp,elseWhen=NONE(),source=source),_)
equation
(crefs,bLst) = List.map1_2(crefs,BackendVarTransform.replaceCref,replIn);
(cref,changed) = BackendVarTransform.replaceCref(cref,replIn);
(crefExps,bLst) = List.map1_2(crefs,BackendVarTransform.replaceCref,replIn);
crefs = List.map(crefExps,Expression.expCref);
(DAE.CREF(componentRef=cref),changed) = BackendVarTransform.replaceCref(cref,replIn);
changed = List.fold(bLst,boolOr,changed);
(exp,changed1) = BackendVarTransform.replaceExp(exp,replIn,NONE());
changed = boolOr(changed,changed1);
simEqSys = SimCode.SES_WHEN(idx,crefs,ic,cref,exp,NONE(),source);
then (simEqSys,changed);
case(SimCode.SES_WHEN(index=idx,conditions=crefs,initialCall=ic,left=cref,right=exp,elseWhen=SOME(simEqSys),source=source),_)
equation
(crefs,bLst) = List.map1_2(crefs,BackendVarTransform.replaceCref,replIn);
(cref,changed) = BackendVarTransform.replaceCref(cref,replIn);
(crefExps,bLst) = List.map1_2(crefs,BackendVarTransform.replaceCref,replIn);
crefs = List.map(crefExps,Expression.expCref);
(DAE.CREF(componentRef=cref),changed) = BackendVarTransform.replaceCref(cref,replIn);
changed = List.fold(bLst,boolOr,changed);
(exp,changed1) = BackendVarTransform.replaceExp(exp,replIn,NONE());
changed = boolOr(changed,changed1);
Expand Down

0 comments on commit 6c0221c

Please sign in to comment.