Skip to content

Commit

Permalink
- Derive.mo
Browse files Browse the repository at this point in the history
  - add derivativeTime of abs 
  - use type to generate zero
  - check if call have only parameters -> no derivative of call needet
- Expression.mo
  - use type to generate zero
  - vectors are scalar arrays -> for function makeZeroExpression
- SimCode.mo
  - use Expression.expSub, Expression.negate, ... 
  - error message from translateModel commented out because of testsuite 


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7287 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 4, 2010
1 parent 6962507 commit 6503f60
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
46 changes: 34 additions & 12 deletions Compiler/Derive.mo
Expand Up @@ -334,8 +334,11 @@ algorithm
local
DAE.ExpType tp;
DAE.ComponentRef cr;
list<list<DAE.ComponentRef>> crefslstls;
list<DAE.ComponentRef> crefs;
list<Boolean> blst;
String cr_str,cr_str_1,e_str,str,s1;
DAE.Exp e,e_1,e1_1,e2_1,e1,e2,e3_1,e3,d_e1,exp,e0;
DAE.Exp e,e_1,e1_1,e2_1,e1,e2,e3_1,e3,d_e1,exp,e0,zero;
BackendDAE.Variables timevars;
DAE.Operator op,rel;
list<DAE.Exp> expl_1,expl,sub;
Expand All @@ -351,6 +354,7 @@ algorithm

case (DAE.ICONST(integer = _),_) then DAE.RCONST(0.0);
case (DAE.RCONST(real = _),_) then DAE.RCONST(0.0);
case (e as DAE.BCONST(bool = _),_) then e;

case (DAE.CREF(componentRef = DAE.CREF_IDENT(ident = "time",subscriptLst = {}),ty = tp),_)
then DAE.RCONST(1.0);
Expand Down Expand Up @@ -491,6 +495,14 @@ algorithm
then
DAE.CALL(fname,expl_1,false,true,tp,inl);

// abs(x)
case (DAE.CALL(path=fname, expLst={exp},tuple_ = b,builtin = c,ty=tp,inlineType=inl),(timevars,functions))
equation
Builtin.isAbs(fname);
e1_1 = differentiateExpTime(exp, (timevars,functions));
then
DAE.IFEXP(DAE.RELATION(e1_1,DAE.GREATER(DAE.ET_REAL()),DAE.RCONST(0.0)), e1_1, DAE.UNARY(DAE.UMINUS(DAE.ET_REAL()),e1_1));

case (e0 as DAE.BINARY(exp1 = e1,operator = DAE.POW(tp),exp2 = (e2 as DAE.RCONST(_))),(timevars,functions)) /* ax^(a-1) */
equation
d_e1 = differentiateExpTime(e1, (timevars,functions)) "e^x => xder(e)e^x-1" ;
Expand All @@ -502,14 +514,10 @@ algorithm
then
exp;

case ((e as DAE.CREF(componentRef = cr,ty = tp as DAE.ET_ARRAY(arrayDimensions=_))),(timevars,functions)) /* list_member(cr,timevars) => false */
equation
// generate zeros
expl_1 = Util.listFill(DAE.RCONST(0.0), Expression.sizeOf(tp));
then DAE.ARRAY(tp,true,expl_1);

case ((e as DAE.CREF(componentRef = cr,ty = tp)),(timevars,functions))
then DAE.RCONST(0.0);
equation
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then zero;

case (DAE.BINARY(exp1 = e1,operator = DAE.ADD(ty = tp),exp2 = e2),(timevars,functions))
equation
Expand Down Expand Up @@ -581,6 +589,17 @@ algorithm
then
DAE.CALL(a,expl_1,b,c,tp,inl);

case (e as DAE.CALL(path = a,expLst = expl,tuple_ = b,builtin = c,ty=tp),(timevars,functions))
equation
// if only parameters no derivative needed
crefslstls = Util.listMap(expl,Expression.extractCrefsFromExp);
crefs = Util.listFlatten(crefslstls);
blst = Util.listMap1(crefs,BackendVariable.existsVar,timevars);
false = Util.boolOrList(blst);
(e1,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then
e1;

case (e as DAE.CALL(path = a,expLst = expl,tuple_ = b,builtin = c),(timevars,functions))
equation
// get Derivative function
Expand Down Expand Up @@ -1198,7 +1217,7 @@ algorithm
local
Real rval;
DAE.ComponentRef cr,crx,tv;
DAE.Exp e,e1_1,e2_1,e1,e2,const_one,d_e1,d_e2,exp,e_1,exp_1,e3_1,e3,cond;
DAE.Exp e,e1_1,e2_1,e1,e2,const_one,d_e1,d_e2,exp,e_1,exp_1,e3_1,e3,cond,zero;
DAE.ExpType tp, ctp;
Absyn.Path a,fname;
Boolean b,c;
Expand All @@ -1220,11 +1239,12 @@ algorithm
then
DAE.RCONST(rval);

case ((e as DAE.CREF(componentRef = cr)),crx,_)
case ((e as DAE.CREF(componentRef = cr,ty=tp)),crx,_)
equation
false = ComponentReference.crefEqual(cr, crx) "D(c)/dx => 0" ;
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then
DAE.RCONST(0.0);
zero;

case (DAE.BINARY(exp1 = e1,operator = DAE.ADD(ty = tp),exp2 = e2),tv,differentiateIfExp)
equation
Expand Down Expand Up @@ -1578,8 +1598,10 @@ algorithm
the derivative is zero. For efficiency reasons this rule
is last. Otherwise expressions is allways traversed twice
when differentiating." ;
tp = Expression.typeof(e);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then
DAE.RCONST(0.0);
zero;

// Differentiate if-expressions if last argument true
case (DAE.IFEXP(cond,e1,e2),tv,differentiateIfExp as true)
Expand Down
13 changes: 8 additions & 5 deletions Compiler/Expression.mo
Expand Up @@ -2020,7 +2020,7 @@ public function getTermsContainingX
algorithm
(outExp1,outExp2) := matchcontinue (inExp1,inExp2)
local
DAE.Exp xt1,nonxt1,xt2,nonxt2,xt,nonxt,e1,e2,cr,e;
DAE.Exp xt1,nonxt1,xt2,nonxt2,xt,nonxt,e1,e2,cr,e,zero;
Type ty;
Boolean res;
case (DAE.BINARY(exp1 = e1,operator = DAE.ADD(ty = ty),exp2 = e2),(cr as DAE.CREF(componentRef = _)))
Expand Down Expand Up @@ -2083,11 +2083,12 @@ algorithm
nonxt = DAE.UNARY(DAE.UMINUS_ARR(ty),nonxt1);
then
(xt,nonxt);
case (e,(cr as DAE.CREF(componentRef = _)))
case (e,(cr as DAE.CREF(ty = ty)))
equation
res = expContains(e, cr);
xt = Util.if_(res, e, DAE.RCONST(0.0));
nonxt = Util.if_(res, DAE.RCONST(0.0), e);
(zero,_) = makeZeroExpression(arrayDimension(ty));
xt = Util.if_(res, e, zero);
nonxt = Util.if_(res, zero, e);
then
(xt,nonxt);
case (e,cr)
Expand Down Expand Up @@ -2852,14 +2853,16 @@ algorithm
DAE.Exp e;
list<DAE.Exp> eLst;
DAE.Type ty;
Boolean scalar;
case {} then (DAE.RCONST(0.0), DAE.T_REAL_DEFAULT);
case d::dims
equation
i = dimensionSize(d);
(e, ty) = makeZeroExpression(dims);
eLst = Util.listFill(e,i);
scalar = Util.isListEmpty(dims);
then
(DAE.ARRAY(DAE.ET_ARRAY(DAE.ET_REAL(),d::dims),false,eLst),
(DAE.ARRAY(DAE.ET_ARRAY(DAE.ET_REAL(),d::dims),scalar,eLst),
(DAE.T_ARRAY(d,ty),NONE()));
end matchcontinue;
end makeZeroExpression;
Expand Down
50 changes: 30 additions & 20 deletions Compiler/SimCode.mo
Expand Up @@ -934,7 +934,7 @@ algorithm
(outCache,outValue,outInteractiveSymbolTable,outBackendDAE,outStringLst,outFileDir,resultValues):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inFileNamePrefix,addDummy, inSimSettingsOpt)
local
String filenameprefix,file_dir;
String filenameprefix,file_dir,resstr;
list<SCode.Class> p_1;
DAE.DAElist dae;
list<Env.Frame> env;
Expand Down Expand Up @@ -993,9 +993,19 @@ algorithm
("timeBackend", Values.REAL(timeBackend)),
("timeFrontend", Values.REAL(timeFrontend))
};
// resstr = Absyn.pathString(className);
// resstr = stringAppendList({"SimCode: The model ",resstr," has been translated"});
resstr = "SimCode: The model has been translated";
then
(cache,Values.STRING("SimCode: The model has been translated"),st,indexed_dlow_1,libs,file_dir, resultValues);
end matchcontinue;
(cache,Values.STRING(resstr),st,indexed_dlow_1,libs,file_dir, resultValues);
/* case (_,_,className,_,_,_, _)
equation
resstr = Absyn.pathString(className);
resstr = stringAppendList({"SimCode: The model ",resstr," could not been translated"});
Error.addMessage(Error.INTERNAL_ERROR, {resstr});
then
fail();
*/ end matchcontinue;
end translateModel;

public function translateFunctions
Expand Down Expand Up @@ -4103,24 +4113,21 @@ algorithm

case (BackendDAE.EQUATION(exp=e1, scalar=e2), v, arrayEqs,inEntrylst)
equation
tp = Expression.typeof(e1);
new_exp = DAE.BINARY(e1,DAE.SUB(tp),e2);
new_exp = Expression.expSub(e1,e2);
rhs_exp = BackendDAEUtil.getEqnsysRhsExp(new_exp, v);
rhs_exp_1 = DAE.UNARY(DAE.UMINUS(tp),rhs_exp);
rhs_exp_1 = Expression.negate(rhs_exp);
rhs_exp_2 = ExpressionSimplify.simplify(rhs_exp_1);
then (rhs_exp_2,inEntrylst);

case (BackendDAE.ARRAY_EQUATION(index=index), v, arrayEqs,inEntrylst)
equation
BackendDAE.MULTIDIM_EQUATION(dimSize=ds,left=e1, right=e2) = arrayEqs[index+1];
tp = Expression.typeof(e1);
new_exp = DAE.BINARY(e1,DAE.SUB_ARR(tp),e2);
new_exp = Expression.expSub(e1,e2);
ad = Util.listMap(ds,Util.makeOption);
(subs,entrylst1) = BackendDAEUtil.getArrayEquationSub(index,ad,inEntrylst);
new_exp1 = Expression.applyExpSubscripts(new_exp,subs);
rhs_exp = BackendDAEUtil.getEqnsysRhsExp(new_exp1, v);
tp1 = Expression.typeof(rhs_exp);
rhs_exp_1 = DAE.UNARY(DAE.UMINUS(tp1),rhs_exp);
rhs_exp_1 = Expression.negate(rhs_exp);
rhs_exp_2 = ExpressionSimplify.simplify(rhs_exp_1);
then (rhs_exp_2,entrylst1);

Expand Down Expand Up @@ -4392,27 +4399,30 @@ algorithm
equation
true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
ty = Expression.typeof(e2);
e2 = Expression.negate(e2);
then
SES_ARRAY_CALL_ASSIGN(eltcr, DAE.UNARY(DAE.UMINUS_ARR(ty),e2));
SES_ARRAY_CALL_ASSIGN(eltcr, e2);

case (cr,eltcr,e1,(e2 as DAE.UNARY(exp=DAE.CREF(componentRef = cr2))))
equation
true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
ty = Expression.typeof(e1);
e1 = Expression.negate(e1);
then
SES_ARRAY_CALL_ASSIGN(eltcr, DAE.UNARY(DAE.UMINUS_ARR(ty),e1));
SES_ARRAY_CALL_ASSIGN(eltcr, e1);

case (cr,eltcr,e1,DAE.UNARY(DAE.UMINUS_ARR(ty),e2))
equation
cr2 = getVectorizedCrefFromExp(e2);
e1 = Expression.negate(e1);
then
SES_ARRAY_CALL_ASSIGN(cr2, DAE.UNARY(DAE.UMINUS_ARR(ty),e1));
SES_ARRAY_CALL_ASSIGN(cr2, e1);

case (cr,eltcr,DAE.UNARY(DAE.UMINUS_ARR(ty),e1),e2) /* e2 is array of crefs, {v{1},v{2},...v{n}} */
equation
cr2 = getVectorizedCrefFromExp(e1);
e2 = Expression.negate(e2);
then
SES_ARRAY_CALL_ASSIGN(cr2, DAE.UNARY(DAE.UMINUS_ARR(ty),e2));
SES_ARRAY_CALL_ASSIGN(cr2, e2);

case (cr,eltcr,e1,e2) /* e2 is array of crefs, {v{1},v{2},...v{n}} */
equation
Expand Down Expand Up @@ -6263,7 +6273,7 @@ protected function solveTrivialArrayEquation
algorithm
(outE1,outE2) := matchcontinue(v,e1,e2)
local
DAE.Exp e12,e22,vTerm,res,rhs,f;
DAE.Exp e,e12,e22,vTerm,res,rhs,f;
list<DAE.Exp> terms,exps,exps_1,expl_1;
DAE.ExpType tp;
Boolean b;
Expand All @@ -6275,7 +6285,7 @@ algorithm
(f::exps_1) = Util.listMap(exps, Expression.expStripLastSubs); //Strip last subscripts
Util.listMap1AllValue(exps_1, Expression.expEqual, f, true);
c = ComponentReference.crefStripLastSubs(c);
(e12,e22) = solveTrivialArrayEquation(v,Expression.makeCrefExp(c,tp),DAE.UNARY(DAE.UMINUS_ARR(tp),e2));
(e12,e22) = solveTrivialArrayEquation(v,Expression.makeCrefExp(c,tp),Expression.negate(e2));
then
(e12,e22);

Expand All @@ -6284,7 +6294,7 @@ algorithm
(f::exps_1) = Util.listMap(exps, Expression.expStripLastSubs); //Strip last subscripts
Util.listMap1AllValue(exps_1, Expression.expEqual, f, true);
c = ComponentReference.crefStripLastSubs(c);
(e12,e22) = solveTrivialArrayEquation(v,DAE.UNARY(DAE.UMINUS_ARR(tp),e2),Expression.makeCrefExp(c,tp));
(e12,e22) = solveTrivialArrayEquation(v,Expression.negate(e2),Expression.makeCrefExp(c,tp));
then
(e12,e22);

Expand All @@ -6293,8 +6303,8 @@ algorithm
// Solve simple linear equations.
case(v,e1,e2)
equation
tp = Expression.typeof(e1);
res = ExpressionSimplify.simplify(DAE.BINARY(e1,DAE.SUB_ARR(tp),e2));
e = Expression.expSub(e1,e2);
res = ExpressionSimplify.simplify(e);
(f,rhs) = Expression.getTermsContainingX(res,Expression.crefExp(v));
(vTerm as DAE.CREF(_,_)) = ExpressionSimplify.simplify(f);
rhs = ExpressionSimplify.simplify(rhs);
Expand Down

0 comments on commit 6503f60

Please sign in to comment.