Skip to content

Commit

Permalink
- implement elsewhen part of whenequations
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5272 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Apr 7, 2010
1 parent abf20b1 commit 1a08cf8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
62 changes: 53 additions & 9 deletions Compiler/DAELow.mo
Expand Up @@ -773,11 +773,11 @@ algorithm
(outWheneq, outVars) := matchcontinue(wheneq,vars)
local DAE.ComponentRef cr; DAE.Exp e1; Integer indx; WhenEquation elsewheneq;
case(WHEN_EQ(indx,cr,e1,SOME(elsewheneq)),vars) equation
print("A1\n");
// print("A1\n");
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
print("A2\n");
// print("A2\n");
(elsewheneq,vars) = expandDerOperatorWhenEqn(elsewheneq,vars);
print("A3\n");
// print("A3\n");
then (WHEN_EQ(indx,cr,e1,SOME(elsewheneq)),vars);

case(WHEN_EQ(indx,cr,e1,NONE),vars) equation
Expand Down Expand Up @@ -3076,6 +3076,38 @@ algorithm
end matchcontinue;
end dumpEqns2;

protected function whenEquationStr
"function: whenEquationStr
Helper function to equationStr"
input WhenEquation inWhenEqn;
output String outString;
algorithm
outString := matchcontinue (inWhenEqn)
local
String s1,s2,res,indx_str,is,var_str,intsStr,outsStr;
DAE.Exp e1,e2,e;
Value indx,i;
list<DAE.Exp> expl,inps,outs;
DAE.ComponentRef cr;
WhenEquation weqn;
case (WHEN_EQ(index = i,left = cr,right = e2, elsewhenPart = SOME(weqn)))
equation
s1 = whenEquationStr(weqn);
s2 = Exp.printExpStr(e2);
is = intString(i);
res = Util.stringAppendList({" ; ",s2," elsewhen clause no: ",is /*, "\n" */, s1});
then
res;
case (WHEN_EQ(index = i,left = cr,right = e2, elsewhenPart = NONE()))
equation
s2 = Exp.printExpStr(e2);
is = intString(i);
res = Util.stringAppendList({" ; ",s2," elsewhen clause no: ",is /*, "\n" */});
then
res;
end matchcontinue;
end whenEquationStr;

public function equationStr
"function: equationStr
Helper function to e.g. dump."
Expand All @@ -3084,12 +3116,12 @@ public function equationStr
algorithm
outString := matchcontinue (inEquation)
local
String s1,s2,res,indx_str,is,var_str,intsStr,outsStr;
String s1,s2,s3,res,indx_str,is,var_str,intsStr,outsStr;
DAE.Exp e1,e2,e;
Value indx,i;
list<DAE.Exp> expl,inps,outs;
DAE.ComponentRef cr;

WhenEquation weqn;
case (EQUATION(exp = e1,scalar = e2))
equation
s1 = Exp.printExpStr(e1);
Expand Down Expand Up @@ -3118,6 +3150,16 @@ algorithm
res = Util.stringAppendList({s1," := ",s2});
then
res;

case (WHEN_EQUATION(whenEquation = WHEN_EQ(index = i,left = cr,right = e2, elsewhenPart = SOME(weqn))))
equation
s1 = Exp.printComponentRefStr(cr);
s2 = Exp.printExpStr(e2);
is = intString(i);
s3 = whenEquationStr(weqn);
res = Util.stringAppendList({s1," := ",s2," when clause no: ",is /*, "\n" */, s3});
then
res;
case (WHEN_EQUATION(whenEquation = WHEN_EQ(index = i,left = cr,right = e2)))
equation
s1 = Exp.printComponentRefStr(cr);
Expand Down Expand Up @@ -4482,13 +4524,14 @@ algorithm
reinit_count = listLength(reinit);
hasReinit = (reinit_count > 0);
extra = Util.if_(hasReinit, 1, 0);
tot_count = equation_count + extra;
whenClauseList1 = makeWhenClauses(equation_count, cond, {});
whenClauseList2 = makeWhenClauses(extra, cond, reinit);
tot_count = equation_count + extra;
(res1,i_1,whenClauseList3) = mergeClauses(trueEqnLst,elseEqnLst,whenClauseList2,
whenClauseList3 = listAppend(whenClauseList2, whenClauseList1);
(res1,i_1,whenClauseList4) = mergeClauses(trueEqnLst,elseEqnLst,whenClauseList3,
elseClauseList,nextWhenIndex + tot_count);
then
(res1,vars,i_1,whenClauseList3);
(res1,vars,i_1,whenClauseList4);

case (DAE.WHEN_EQUATION(condition = cond),_,_)
equation
Expand Down Expand Up @@ -4879,7 +4922,8 @@ algorithm
(vars, knvars, extVars, eqns, reqns, ieqns, (e_1 :: aeqns), algs, whenclauses_1, extObjCls);

/* When equations */
case (DAE.DAE((e as DAE.WHEN_EQUATION(condition = c,equations = eqns,elsewhen_ = NONE)) :: xs,funcs),states,vars,knvars,extVars,whenclauses)
// case (DAE.DAE((e as DAE.WHEN_EQUATION(condition = c,equations = eqns,elsewhen_ = NONE)) :: xs,funcs),states,vars,knvars,extVars,whenclauses)
case (DAE.DAE((e as DAE.WHEN_EQUATION(condition = c,equations = eqns)) :: xs,funcs),states,vars,knvars,extVars,whenclauses)
equation
(vars1,knvars,extVars,eqns1,reqns,ieqns,aeqns,algs,whenclauses_1,extObjCls)
= lower2(DAE.DAE(xs,funcs), states, vars, knvars, extVars, whenclauses);
Expand Down
37 changes: 34 additions & 3 deletions Compiler/SimCodegen.mo
Expand Up @@ -8260,13 +8260,13 @@ algorithm
DAELow.VariableArray vararr;
DAELow.EquationArray eqns;
Integer[:] ass1,ass2;

DAELow.WhenEquation weqn;
/* assignments1 assignments2 equation no. cg var_id cg var_id */
case (dae,DAELow.DAELOW(orderedVars = DAELow.VARIABLES(varArr = vararr),orderedEqs = eqns),ass1,ass2,e,index,cg_id)
equation
e_1 = e - 1;
DAELow.WHEN_EQUATION(whenEquation = DAELow.WHEN_EQ(wc_ind,cr,expr,_)) = DAELow.equationNth(eqns, e_1); //TODO: elsewhen
(index == wc_ind) = true;
DAELow.WHEN_EQUATION(whenEquation = weqn) = DAELow.equationNth(eqns, e_1); //TODO: elsewhen
DAELow.WHEN_EQ(wc_ind,cr,expr,_) = buildWhenEquation1(weqn,index);
v = ass2[e_1 + 1];
v_1 = v - 1;
((va as DAELow.VAR(cr,kind,_,_,_,_,_,_,origname,_,dae_var_attr,comment,flowPrefix,streamPrefix))) = DAELow.vararrayNth(vararr, v_1);
Expand All @@ -8277,10 +8277,41 @@ algorithm
cfn = Codegen.cPrependStatements(cfn, {save_stmt});
then
(cfn,cg_id_1);

case (_,_,_,_,_,_,cg_id) then (Codegen.cEmptyFunction,cg_id);
end matchcontinue;
end buildWhenEquation;

protected function buildWhenEquation1
"function: buildWhenEquation1
Helper function to buildWhenEquation."
input DAELow.WhenEquation inWEqn;
input Integer inInteger;
output DAELow.WhenEquation outWEqn;
algorithm
outWEqn := matchcontinue (inWEqn,inInteger)
local
Integer wc_ind,index;
DAELow.WhenEquation weqn,we,we1;

case(weqn as DAELow.WHEN_EQ(index = wc_ind,elsewhenPart = NONE()),index)
equation
(index == wc_ind) = true;
then
weqn;
case(weqn as DAELow.WHEN_EQ(index = wc_ind,elsewhenPart = SOME(we)),index)
equation
(index == wc_ind) = true;
then
weqn;
case(weqn as DAELow.WHEN_EQ(index = wc_ind,elsewhenPart = SOME(we)),index)
equation
we1 = buildWhenEquation1(we,index);
then
we1;
end matchcontinue;
end buildWhenEquation1;

protected function generateComputeResidualState
"function: generateComputeResidualState
This function generates the code for the calculation of the
Expand Down

0 comments on commit 1a08cf8

Please sign in to comment.