Skip to content

Commit

Permalink
- added template functions for DAE.SUM and SimCode.SES_FOR_LOOP
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25853 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Volker Waurich committed Apr 30, 2015
1 parent 799136a commit b85bc79
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Compiler/SimCode/SimCode.mo
Expand Up @@ -436,8 +436,8 @@ uniontype SimEqSystem
DAE.Exp iter;
DAE.Exp startIt;
DAE.Exp endIt;
DAE.ComponentRef cref;
DAE.Exp exp;
DAE.ComponentRef cref;//lhs
DAE.Exp exp;//rhs
DAE.ElementSource source;
end SES_FOR_LOOP;

Expand Down
72 changes: 72 additions & 0 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -3700,6 +3700,8 @@ template equation_(SimEqSystem eq, Context context, Text &varDecls, Text &eqs, S
then "NOT IMPLEMENTED EQUATION SES_RESIDUAL"
case e as SES_MIXED(__)
then equationMixed(e, context, &varD, &eqs, modelNamePrefix)
case e as SES_FOR_LOOP(__)
then equationForLoop(e, context, &varD, &tempeqns)
else
"NOT IMPLEMENTED EQUATION equation_"
let &varD += addRootsTempArray()
Expand Down Expand Up @@ -3801,6 +3803,32 @@ case SES_SIMPLE_ASSIGN(__) then
>>
end equationSimpleAssign;

template equationForLoop(SimEqSystem eq, Context context,
Text &varDecls, Text &auxFunction)
"Generates an equation that is a for-loop."
::=
match eq
case SES_FOR_LOOP(__) then
let &preExp = buffer ""
let expPart = daeExp(exp, context, &preExp, &varDecls, &auxFunction)
let crefPart = daeExp(crefExp(cref), context, &preExp, &varDecls, &auxFunction)
//let bodyStr = daeExpIteratedCref(body)
let start = printExpStr(startIt)
let stop = printExpStr(endIt)
let iterVar = daeExp(iter, context, &preExp, &varDecls, &auxFunction)
<<
<%modelicaLine(eqInfo(eq))%>
modelica_integer $P<%printExpStr(iter)%> = 0; // the iterator

// the for-equation
for($P<%printExpStr(iter)%> = <%start%>; $P<%printExpStr(iter)%> < <%stop%>; $P<%printExpStr(iter)%>++)
{
<%crefPart%> += <%expPart%>;
}
<%endModelicaLine()%>
>>
end equationForLoop;


template equationArrayCallAssign(SimEqSystem eq, Context context,
Text &varDecls, Text &auxFunction)
Expand Down Expand Up @@ -7733,6 +7761,7 @@ end elseExpr;
case e as BOX(__) then daeExpBox(e, context, &preExp, &varDecls, &auxFunction)
case e as UNBOX(__) then daeExpUnbox(e, context, &preExp, &varDecls, &auxFunction)
case e as SHARED_LITERAL(__) then daeExpSharedLiteral(e)
case e as SUM(__) then daeExpSum(e, context, &preExp, &varDecls, &auxFunction)
case e as CLKCONST(__) then '#error "<%ExpressionDump.printExpStr(e)%>"'
else error(sourceInfo(), 'Unknown expression: <%ExpressionDump.printExpStr(exp)%>')
end daeExp;
Expand Down Expand Up @@ -8499,6 +8528,49 @@ case IFEXP(__) then
resVar)
end daeExpIf;

template daeExpSum(Exp exp, Context context, Text &preExp,
Text &varDecls, Text &auxFunction)
"Generates code for an if expression."
::=
match exp
case SUM(__) then
let start = printExpStr(startIt)
let &anotherPre = buffer ""
let stop = printExpStr(endIt)
let bodyStr = daeExpIteratedCref(body)
let summationVar = <<sum>>
let iterVar = printExpStr(iterator)
let &preExp +=<<

modelica_integer $P<%iterVar%> = 0; // the iterator
modelica_real <%summationVar%> = 0.0; //the sum
for($P<%iterVar%> = <%start%>; $P<%iterVar%> < <%stop%>; $P<%iterVar%>++)
{
<%summationVar%> += <%bodyStr%>($P<%iterVar%>);
}

>>
summationVar
end daeExpSum;

template daeExpIteratedCref(Exp exp)
::=
match exp
case (CREF(__)) then
let subs = (crefSubs(componentRef) |> sub => subscriptToCStr(sub) ; separator=",")
<<<%iteratedCrefStr(componentRef)%>_index>>
end daeExpIteratedCref;

template iteratedCrefStr(ComponentRef cref)
::=
match cref
case (CREF_IDENT(__)) then
<<$P<%ident%>>>
case (CREF_QUAL(__)) then
<<$P<%ident%><%iteratedCrefStr(componentRef)%>>>
end iteratedCrefStr;


template resultVarAssignment(DAE.Type ty, Text lhs, Text rhs) "Tuple need to be considered"
::=
match ty
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/CodegenUtil.tpl
Expand Up @@ -334,6 +334,7 @@ template equationIndex(SimEqSystem eq)
case SES_NONLINEAR(__)
case SES_MIXED(__)
case SES_WHEN(__)
case SES_FOR_LOOP(__)
then index
end equationIndex;

Expand Down
11 changes: 8 additions & 3 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -501,11 +501,11 @@ package SimCode

record SES_FOR_LOOP
Integer index;
DAE.EXP iterator;
DAE.Exp iter;
DAE.Exp startIt;
DAE.Exp endIt;
DAE.ComponentRef cref;
DAE.Exp exp;
DAE.ComponentRef cref;//lhs
DAE.Exp exp;//rhs
DAE.ElementSource source;
end SES_FOR_LOOP;

Expand Down Expand Up @@ -2855,6 +2855,11 @@ end ComponentReference;

package Expression

function crefExp
input DAE.ComponentRef cr;
output DAE.Exp cref;
end crefExp;

function subscriptConstants
"returns true if all subscripts are known (i.e no cref) constant values (no slice or wholedim "
input list<DAE.Subscript> inSubs;
Expand Down

0 comments on commit b85bc79

Please sign in to comment.