Skip to content

Commit

Permalink
- DAELow.mo
Browse files Browse the repository at this point in the history
  - extend multidimeqn after inlining if it is possible
- Exp.mo, SimCode.mo 
  - fix some bugs relatet to arrays


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5739 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jun 28, 2010
1 parent 787f3b4 commit cfe815d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 16 deletions.
35 changes: 33 additions & 2 deletions Compiler/DAELow.mo
Expand Up @@ -4880,7 +4880,39 @@ algorithm
list<Equation> eqns,eqns2,res;
MultiDimEquation a;
list<MultiDimEquation> algs;
list<DAE.Exp> a1,a2,a1_1,an;
list<tuple<DAE.Exp,DAE.Exp>> ealst;
DAE.ElementSource source;
case (vars,{},aindx) then ({},aindx);
case (vars,((a as MULTIDIM_EQUATION(left=DAE.ARRAY(array=a1,scalar=true),right=DAE.ARRAY(array=a2,scalar=true),source=source)) :: algs),aindx)
equation
ealst = Util.listThreadTuple(a1,a2);
eqns = Util.listMap1(ealst,generateEQUATION,source);
aindx = aindx + 1;
(eqns2,aindx) = lowerMultidimeqns2(vars, algs, aindx);
res = listAppend(eqns, eqns2);
then
(res,aindx);
case (vars,((a as MULTIDIM_EQUATION(left=DAE.UNARY(exp=DAE.ARRAY(array=a1,scalar=true)),right=DAE.ARRAY(array=a2,scalar=true),source=source)) :: algs),aindx)
equation
an = Util.listMap(a1,Exp.negate);
ealst = Util.listThreadTuple(an,a2);
eqns = Util.listMap1(ealst,generateEQUATION,source);
aindx = aindx + 1;
(eqns2,aindx) = lowerMultidimeqns2(vars, algs, aindx);
res = listAppend(eqns, eqns2);
then
(res,aindx);
case (vars,((a as MULTIDIM_EQUATION(left=DAE.ARRAY(array=a1,scalar=true),right=DAE.UNARY(exp=DAE.ARRAY(array=a2,scalar=true)),source=source)) :: algs),aindx)
equation
an = Util.listMap(a2,Exp.negate);
ealst = Util.listThreadTuple(a1,an);
eqns = Util.listMap1(ealst,generateEQUATION,source);
aindx = aindx + 1;
(eqns2,aindx) = lowerMultidimeqns2(vars, algs, aindx);
res = listAppend(eqns, eqns2);
then
(res,aindx);
case (vars,(a :: algs),aindx)
equation
eqns = lowerMultidimeqn(vars, a, aindx);
Expand Down Expand Up @@ -5261,8 +5293,7 @@ algorithm
/* Special Case for Records */
case ((e as DAE.CREF(componentRef = cr,ty = tp)),vars)
equation
true = Exp.isRecord(cr);
DAE.ET_COMPLEX(varLst=varLst) = Exp.typeof(e);
DAE.ET_COMPLEX(varLst=varLst) = Exp.crefLastType(cr);
expl = Util.listMap1(varLst,generateCrefsExpFromType,e);
lst = Util.listMap1(expl, statesAndVarsExp, vars);
res = Util.listListUnionOnTrue(lst, Exp.expEqual);
Expand Down
82 changes: 79 additions & 3 deletions Compiler/Exp.mo
Expand Up @@ -4078,6 +4078,7 @@ algorithm
local Type t;
/* to avoid unnessecary --e */
case(DAE.UNARY(DAE.UMINUS(t),e)) then e;
case(DAE.UNARY(DAE.UMINUS_ARR(t),e)) then e;

/* -0 = 0 */
case(e) equation
Expand All @@ -4086,8 +4087,14 @@ algorithm

case(e) equation
t = typeof(e);
true = isArrayType(t);
outExp = DAE.UNARY(DAE.UMINUS_ARR(t),e);
then outExp;
case(e) equation
t = typeof(e);
false = isArrayType(t);
outExp = DAE.UNARY(DAE.UMINUS(t),e);
then outExp;
then outExp;
end matchcontinue;
end negate;

Expand Down Expand Up @@ -4122,29 +4129,98 @@ algorithm
then
res;

case (DAE.BINARY(exp1 = e1,operator = DAE.ADD_ARR(ty = _),exp2 = e2))
equation
f1 = allTerms(e1);
f2 = allTerms(e2);
res = listAppend(f1, f2);
then
res;
case (DAE.BINARY(exp1 = e1,operator = DAE.SUB_ARR(ty = _),exp2 = e2))
equation
f1 = allTerms(e1);
f2 = allTerms(e2);
f2_1 = Util.listMap(f2, negate);
res = listAppend(f1, f2_1);
then
res;

/* terms( a*(b+c)) => {a*b, c*b} */
case (e as DAE.BINARY(e1,DAE.MUL(tp),e2)) equation
(f1 as _::_::_) = allTerms(e2);
f1 = Util.listMap1(f1,makeProduct,e1);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;

case (e as DAE.BINARY(e1,DAE.MUL_ARR(tp),e2)) equation
(f1 as _::_::_) = allTerms(e2);
f1 = Util.listMap1(f1,makeProduct,e1);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.MUL_SCALAR_ARRAY(tp),e2)) equation
(f1 as _::_::_) = allTerms(e2);
f1 = Util.listMap1(f1,makeProduct,e1);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.MUL_ARRAY_SCALAR(tp),e2)) equation
(f1 as _::_::_) = allTerms(e2);
f1 = Util.listMap1(f1,makeProduct,e1);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
/* terms( (b+c)*a) => {b*a, c*a} */
case (e as DAE.BINARY(e1,DAE.MUL(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeProduct,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;

case (e as DAE.BINARY(e1,DAE.MUL_ARR(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeProduct,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.MUL_SCALAR_ARRAY(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeProduct,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.MUL_ARRAY_SCALAR(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeProduct,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
/* terms( (b+c)/a) => {b/a, c/a} */
case (e as DAE.BINARY(e1,DAE.DIV(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeFraction,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.DIV_ARR(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeFraction,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.DIV_ARRAY_SCALAR(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeFraction,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case (e as DAE.BINARY(e1,DAE.DIV_SCALAR_ARRAY(tp),e2)) equation
(f1 as _::_::_) = allTerms(e1);
f1 = Util.listMap1(f1,makeFraction,e2);
f1 = Util.listFlatten(Util.listMap(f1,allTerms));
then f1;
case ((e as DAE.BINARY(operator = DAE.MUL(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.MUL_ARR(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.MUL_SCALAR_ARRAY(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.MUL_ARRAY_SCALAR(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.DIV(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.DIV_ARR(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.DIV_ARRAY_SCALAR(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.DIV_SCALAR_ARRAY(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.POW(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.POW_ARR(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.POW_ARR2(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.POW_ARRAY_SCALAR(ty = _)))) then {e};
case ((e as DAE.BINARY(operator = DAE.POW_SCALAR_ARRAY(ty = _)))) then {e};
case ((e as DAE.CREF(componentRef = cr))) then {e};
case ((e as DAE.ICONST(integer = _))) then {e};
case ((e as DAE.RCONST(real = _))) then {e};
Expand Down
31 changes: 20 additions & 11 deletions Compiler/SimCode.mo
Expand Up @@ -2296,9 +2296,9 @@ algorithm
/* An array equation */
case ((DAELow.ARRAY_EQUATION(index=aindx) :: rest), aeqns)
equation
DAELow.MULTIDIM_EQUATION(left=e1, right=e2) = aeqns[aindx];
DAELow.MULTIDIM_EQUATION(left=e1, right=e2) = aeqns[aindx+1];
tp = Exp.typeof(e1);
res_exp = DAE.BINARY(e1,DAE.SUB(tp),e2);
res_exp = DAE.BINARY(e1,DAE.SUB_ARR(tp),e2);
res_exp = Exp.simplify(res_exp);
res_exp = replaceDerOpInExp(res_exp);
// (cfunc_1,cg_id_1,rest2,indx_1) = generateOdeSystem2NonlinearResidualsArrayEqn(aindx,rest,aeqns,indx,repl,cg_id);
Expand Down Expand Up @@ -3435,11 +3435,11 @@ algorithm
then rhs_exp_2;
case (DAELow.ARRAY_EQUATION(index=index), v, arrayEqs)
equation
DAELow.MULTIDIM_EQUATION(left=e1, right=e2) = arrayEqs[index];
DAELow.MULTIDIM_EQUATION(left=e1, right=e2) = arrayEqs[index+1];
tp = Exp.typeof(e1);
new_exp = DAE.BINARY(e1,DAE.SUB(tp),e2);
new_exp = DAE.BINARY(e1,DAE.SUB_ARR(tp),e2);
rhs_exp = DAELow.getEqnsysRhsExp(new_exp, v);
rhs_exp_1 = DAE.UNARY(DAE.UMINUS(tp),rhs_exp);
rhs_exp_1 = DAE.UNARY(DAE.UMINUS_ARR(tp),rhs_exp);
rhs_exp_2 = Exp.simplify(rhs_exp_1);
then rhs_exp_2;
case (dlowEq,_,_)
Expand Down Expand Up @@ -5399,24 +5399,33 @@ algorithm
case (DAELow.DAELOW(orderedVars = vars,orderedEqs = eqnarr))
equation
eqn_lst = DAELow.equationList(eqnarr);
singleArrayEquation2(eqn_lst);
singleArrayEquation2(eqn_lst,NONE);
then
();
end matchcontinue;
end singleArrayEquation;

protected function singleArrayEquation2
input list<DAELow.Equation> inDAELowEquationLst;
input Option<Integer> Index;
algorithm
_:=
matchcontinue (inDAELowEquationLst)
local list<DAELow.Equation> res;
case ({}) then ();
case ((DAELow.ARRAY_EQUATION(index = _) :: res))
matchcontinue (inDAELowEquationLst,Index)
local
list<DAELow.Equation> res;
Integer i,i1;
case ({},_) then ();
case ((DAELow.ARRAY_EQUATION(index = i) :: res),NONE)
equation
singleArrayEquation2(res);
singleArrayEquation2(res,SOME(i));
then
();
case ((DAELow.ARRAY_EQUATION(index = i) :: res),SOME(i1))
equation
true = intEq(i,i1);
singleArrayEquation2(res,SOME(i1));
then
();
end matchcontinue;
end singleArrayEquation2;

Expand Down

0 comments on commit cfe815d

Please sign in to comment.