Skip to content

Commit

Permalink
Compiler/BackEnd/Derive.mo
Browse files Browse the repository at this point in the history
- add der(sign(x)) -> 0 to Derive.differentiateExpTime

Compiler/FrontEnd/Ceval.mo
- change the way cardinality is evaluated (strip all subs from connect crefs)

Compiler/FrontEnd/Env.mo
- make the CSetsType a list
- add a function to merge CSetsType from 2 envs.

Inst.mo
- strip subs from crefs/prefix added to csets for cardinality sake.
- add connections from for equations

SCode.mo
- added makeEquation to make an SCode.Equation out of SCode.EEquation.

- quite some tests might fail, let's see what Hudson tells us.





git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14056 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Nov 24, 2012
1 parent 726d959 commit 97dbaff
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 79 deletions.
87 changes: 49 additions & 38 deletions Compiler/BackEnd/Derive.mo
Expand Up @@ -373,21 +373,28 @@ algorithm
// ({BackendDAE.VAR(varKind = BackendDAE.STATE())},_) = BackendVariable.getVar(cr, timevars);
then DAE.CALL(Absyn.IDENT("der"),{e},DAE.callAttrBuiltinReal);

// der(sign(x)) -> 0
case (DAE.CALL(path = Absyn.IDENT("sign"),expLst = {e}),_)
then
DAE.RCONST(0.0);

// der(sin(x)) = der(x)cos(x)
case (DAE.CALL(path = Absyn.IDENT("sin"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) "der(sin(x)) = der(x)cos(x)" ;
e_1 = differentiateExpTime(e, inVariables);
then
DAE.BINARY(e_1,DAE.MUL(DAE.T_REAL_DEFAULT),
DAE.CALL(Absyn.IDENT("cos"),{e},DAE.callAttrBuiltinReal));

// der(cos(x)) = -der(x)sin(x)
case (DAE.CALL(path = Absyn.IDENT("cos"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) "der(cos(x)) = -der(x)sin(x)" ;
e_1 = differentiateExpTime(e, inVariables);
then
DAE.UNARY(DAE.UMINUS(DAE.T_REAL_DEFAULT),DAE.BINARY(e_1,DAE.MUL(DAE.T_REAL_DEFAULT),
DAE.CALL(Absyn.IDENT("sin"),{e},DAE.callAttrBuiltinReal)));

// der(arccos(x)) = -der(x)/sqrt(1-x^2)
// der(arccos(x)) = -der(x)/sqrt(1-x^2)
case (DAE.CALL(path = Absyn.IDENT("acos"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables);
Expand All @@ -396,59 +403,63 @@ algorithm
DAE.CALL(Absyn.IDENT("sqrt"),{DAE.BINARY(DAE.RCONST(1.0),DAE.SUB(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},
DAE.callAttrBuiltinReal)));

// der(arcsin(x)) = der(x)/sqrt(1-x^2)
case (DAE.CALL(path = Absyn.IDENT("asin"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) ;
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),
DAE.CALL(Absyn.IDENT("sqrt"),{DAE.BINARY(DAE.RCONST(1.0),DAE.SUB(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},
DAE.callAttrBuiltinReal));

// der(arctan(x)) = der(x)/1+x^2
case (DAE.CALL(path = Absyn.IDENT("atan"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) ;
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),DAE.BINARY(DAE.RCONST(1.0),DAE.ADD(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e)));

// der(arctan2(y,0)) = der(sign(y)*pi/2) = 0
case (DAE.CALL(path = Absyn.IDENT("atan2"),expLst = {e,e1}),_)
equation
true = Expression.isZero(e1);
(exp,_) = Expression.makeZeroExpression({});
then
exp;

// der(arctan2(y,x)) = der(y/x)/1+(y/x)^2
case (DAE.CALL(path = Absyn.IDENT("atan2"),expLst = {e,e1}),_)
equation
false = Expression.isZero(e1);
exp = Expression.makeDiv(e,e1);
e_1 = differentiateExpTime(exp, inVariables);
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),DAE.BINARY(DAE.RCONST(1.0),DAE.ADD(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e)));
// der(arcsin(x)) = der(x)/sqrt(1-x^2)
case (DAE.CALL(path = Absyn.IDENT("asin"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) ;
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),
DAE.CALL(Absyn.IDENT("sqrt"),{DAE.BINARY(DAE.RCONST(1.0),DAE.SUB(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e))},
DAE.callAttrBuiltinReal));

// der(arctan(x)) = der(x)/1+x^2
case (DAE.CALL(path = Absyn.IDENT("atan"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) ;
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),DAE.BINARY(DAE.RCONST(1.0),DAE.ADD(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e)));

// der(arctan2(y,0)) = der(sign(y)*pi/2) = 0
case (DAE.CALL(path = Absyn.IDENT("atan2"),expLst = {e,e1}),_)
equation
true = Expression.isZero(e1);
(exp,_) = Expression.makeZeroExpression({});
then
exp;

// der(arctan2(y,x)) = der(y/x)/1+(y/x)^2
case (DAE.CALL(path = Absyn.IDENT("atan2"),expLst = {e,e1}),_)
equation
false = Expression.isZero(e1);
exp = Expression.makeDiv(e,e1);
e_1 = differentiateExpTime(exp, inVariables);
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),DAE.BINARY(DAE.RCONST(1.0),DAE.ADD(DAE.T_REAL_DEFAULT),DAE.BINARY(e,DAE.MUL(DAE.T_REAL_DEFAULT),e)));

// der(exp(x)) = der(x)exp(x)
case (DAE.CALL(path = fname as Absyn.IDENT("exp"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) "der(exp(x)) = der(x)exp(x)" ;
e_1 = differentiateExpTime(e, inVariables);
then
DAE.BINARY(e_1,DAE.MUL(DAE.T_REAL_DEFAULT),
DAE.CALL(fname,{e},DAE.callAttrBuiltinReal));

// der(log(x)) = der(x)/x
case (DAE.CALL(path = Absyn.IDENT("log"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) "der(log(x)) = der(x)/x";
e_1 = differentiateExpTime(e, inVariables);
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),e);

// der(log10(x)) = der(x)/(ln(10)*x)
case (DAE.CALL(path = Absyn.IDENT("log10"),expLst = {e}),_)
equation
e_1 = differentiateExpTime(e, inVariables) "der(log10(x)) = der(x)/(ln(10)*x)";
e_1 = differentiateExpTime(e, inVariables);
r = realLn(10.0);
then
DAE.BINARY(e_1,DAE.DIV(DAE.T_REAL_DEFAULT),DAE.BINARY(DAE.RCONST(r),DAE.MUL(DAE.T_REAL_DEFAULT),e));


case (DAE.CALL(path = fname as Absyn.IDENT("max"),expLst = {e1,e2},attr=DAE.CALL_ATTR(ty=tp)),_)
equation
e1_1 = differentiateExpTime(e1, inVariables);
Expand Down
61 changes: 48 additions & 13 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -972,7 +972,7 @@ algorithm
equation
id = Absyn.pathString(path);
handler = cevalBuiltinHandler(id);
(cache,v,st) = handler(cache,env, args, impl, st, msg);
(cache,v,st) = handler(cache, env, args, impl, st, msg);
then
(cache,v,st);
case (cache,env,(e as DAE.CALL(path = funcpath,expLst = expl,attr = DAE.CALL_ATTR(builtin = true))),impl,(st as NONE()),msg)
Expand Down Expand Up @@ -1743,12 +1743,54 @@ algorithm
Absyn.Ident currentPrefixIdent;
list<DAE.Exp> expl;
list<Values.Value> vals;
Env.CSetsType clst;

case (DAE.CREF(componentRef = cr), env)
equation
(env as (Env.FRAME(connectionSet = (crs,prefix))::_)) = Env.stripForLoopScope(env);
env = Env.stripForLoopScope(env);
Env.FRAME(connectionSet = clst)::_ = env;
res = cevalCardinality2(cr, clst, env, 0);
then
Values.INTEGER(res);

case (DAE.ARRAY(array = expl), _)
equation
vals = List.map1(expl, cevalCardinality, inEnv);
dim = listLength(vals);
then
Values.ARRAY(vals, {dim});

end match;
end cevalCardinality;

protected function cevalCardinality2
input DAE.ComponentRef inCref;
input Env.CSetsType inCSets;
input Env.Env inEnv;
input Integer inStartValue;
output Integer outValue;
algorithm
outValue := match(inCref, inCSets, inEnv, inStartValue)
local
Env.Env env;
list<DAE.ComponentRef> cr_lst,cr_lst2,cr_totlst,crs;
Integer res, dim;
DAE.ComponentRef cr;
DAE.ComponentRef prefix,currentPrefix;
Absyn.Ident currentPrefixIdent;
list<DAE.Exp> expl;
list<Values.Value> vals;
Env.CSetsType rest;

case (cr, {}, env, _) then inStartValue;

case (cr, (crs,prefix)::rest, env, _)
equation
// strip the subs from the cref!
cr = ComponentReference.crefStripSubs(cr);

cr_lst = List.select1(crs, ComponentReference.crefContainedIn, cr);
currentPrefixIdent= ComponentReference.crefLastIdent(prefix);
currentPrefixIdent = ComponentReference.crefLastIdent(prefix);
currentPrefix = ComponentReference.makeCrefIdent(currentPrefixIdent,DAE.T_UNKNOWN_DEFAULT,{});
// Select connect references that has cr as suffix and correct Prefix.
cr_lst = List.select1r(cr_lst, ComponentReference.crefPrefixOf, currentPrefix);
Expand All @@ -1759,26 +1801,19 @@ algorithm

cr_totlst = List.unionOnTrue(listAppend(cr_lst,cr_lst2),{},ComponentReference.crefEqual);
res = listLength(cr_totlst);

/*print("inFrame :");print(Env.printEnvPathStr(env));print("\n");
print("cardinality(");print(ComponentReference.printComponentRefStr(cr));print(")=");print(intString(res));
print("\nicrefs =");print(stringDelimitList(List.map(crs,ComponentReference.printComponentRefStr),","));
print("\ncrefs =");print(stringDelimitList(List.map(cr_totlst,ComponentReference.printComponentRefStr),","));
print("\n");
print("prefix =");print(ComponentReference.printComponentRefStr(prefix));print("\n");*/
// print("env:");print(Env.printEnvStr(env));
res = cevalCardinality2(cr, rest, inEnv, res + inStartValue);
then
Values.INTEGER(res);

case (DAE.ARRAY(array = expl), _)
equation
vals = List.map1(expl, cevalCardinality, inEnv);
dim = listLength(vals);
then
Values.ARRAY(vals, {dim});
res;

end match;
end cevalCardinality;
end cevalCardinality2;

protected function cevalBuiltinCat "function: cevalBuiltinCat
author: PA
Expand Down

0 comments on commit 97dbaff

Please sign in to comment.