Skip to content

Commit 25f8749

Browse files
author
Alexey Lebedev
committed
If expr is a constant or parametric expression, der(expr) is replaced during elaboration:
by 0.0 if expr is Real or Integer; by an array of corresponding size filled by 0.0 if expr is an array of Real or Integer. See http://openmodelica.ida.liu.se:8080/cb/issue/1233 git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5635 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 44088dd commit 25f8749

File tree

3 files changed

+80
-59
lines changed

3 files changed

+80
-59
lines changed

Compiler/DAEUtil.mo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,6 +4758,29 @@ algorithm
47584758
end matchcontinue;
47594759
end isIfEquation;
47604760

4761+
public function makeZeroExpression
4762+
" creates a Real or array<Real> zero expression with given dimensions, also returns its type"
4763+
input list<Option<Integer>> inDims;
4764+
output DAE.Exp outExp;
4765+
output DAE.Type outType;
4766+
algorithm
4767+
(outExp,outType) := matchcontinue(inDims)
4768+
local
4769+
Integer d;
4770+
list<Option<Integer>> dims;
4771+
DAE.Exp e;
4772+
list<DAE.Exp> eLst;
4773+
DAE.Type ty;
4774+
case {} then (DAE.RCONST(0.0), DAE.T_REAL_DEFAULT);
4775+
case SOME(d)::dims
4776+
equation
4777+
(e, ty) = makeZeroExpression(dims);
4778+
eLst = Util.listFill(e,d);
4779+
then
4780+
(DAE.ARRAY(DAE.ET_ARRAY(DAE.ET_REAL(),SOME(d)::dims),false,eLst), (DAE.T_ARRAY(DAE.DIM(SOME(d)),ty),NONE));
4781+
end matchcontinue;
4782+
end makeZeroExpression;
4783+
47614784
public function splitElements_dispatch
47624785
"@author: adrpo
47634786
This function will split DAE elements into:

Compiler/Static.mo

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5478,22 +5478,20 @@ algorithm
54785478
Env.Cache cache;
54795479
DAE.DAElist dae;
54805480

5481-
/* use elab_call_args to also try vectorized calls */
5481+
// Replace der of constant Real, Integer or array of Real/Integer by zero(s)
54825482
case (cache,env,{exp},_,impl)
54835483
local
5484-
DAE.Type ety,restype,ty;
5485-
DAE.Exp ee1;
5486-
String es1,es2,es3;
5487-
list<String> ls;
5484+
DAE.Type ety,ty;
5485+
list<Option<Integer>> dims;
54885486
equation
5489-
(_,ee1,DAE.PROP(ety,c),_,dae) = elabExp(cache,env, exp, impl, NONE,false);
5490-
false = Types.isRealOrSubTypeReal(ety);
5491-
ls = Util.listMap({exp}, Dump.printExpStr);
5492-
es1 = Util.stringDelimitList(ls, ", ");
5493-
es3 = Types.unparseType(ety);
5494-
Error.addMessage(Error.DERIVATIVE_NON_REAL, {es1,es1,es3});
5487+
(_,_,DAE.PROP(ety,c),_,_) = elabExp(cache, env, exp, impl, NONE,false);
5488+
failure(equality(c=DAE.C_VAR));
5489+
dims = Types.getRealOrIntegerDimensions(ety);
5490+
(e,ty) = DAEUtil.makeZeroExpression(dims);
54955491
then
5496-
fail();
5492+
(cache,e,DAE.PROP(ty,DAE.C_CONST),DAE.DAE({}, DAE.AVLTREENODE(NONE, 0, NONE, NONE)));
5493+
5494+
/* use elab_call_args to also try vectorized calls */
54975495
case (cache,env,{exp},_,impl)
54985496
local
54995497
DAE.Type ety,restype,ty;
@@ -5503,60 +5501,32 @@ algorithm
55035501
(_,ee1,DAE.PROP(ety,c),_,_) = elabExp(cache, env, exp, impl, NONE,true);
55045502
ety = Types.arrayElementType(ety);
55055503
true = Types.isRealOrSubTypeReal(ety);
5506-
(cache,e,(prop as DAE.PROP(ty,DAE.C_VAR())),dae) = elabCallArgs(cache,env, Absyn.IDENT("der"), {exp}, {}, impl, NONE);
5504+
(cache,e,(prop as DAE.PROP(ty,_)),dae) = elabCallArgs(cache,env, Absyn.IDENT("der"), {exp}, {}, impl, NONE);
55075505
then
55085506
(cache,e,prop,dae);
55095507

5510-
case(cache,env,expl,_,impl)
5508+
case (cache,env,{exp},_,impl)
5509+
local
5510+
DAE.Type ety;
5511+
String es3;
55115512
equation
5512-
setUniqueErrorMessageForDer(cache,env,expl,impl);
5513-
then
5514-
fail();
5513+
(_,_,DAE.PROP(ety,_),_,_) = elabExp(cache,env, exp, impl, NONE,false);
5514+
false = Types.isRealOrSubTypeReal(ety);
5515+
s = Dump.printExpStr(exp);
5516+
es3 = Types.unparseType(ety);
5517+
Error.addMessage(Error.DERIVATIVE_NON_REAL, {s,s,es3});
5518+
then
5519+
fail();
5520+
case (cache,env,expl,_,_)
5521+
equation
5522+
lst = Util.listMap(expl, Dump.printExpStr);
5523+
s = Util.stringDelimitList(lst, ", ");
5524+
s = Util.stringAppendList({"der(",s,")"});
5525+
Error.addMessage(Error.WRONG_TYPE_OR_NO_OF_ARGS, {s});
5526+
then fail();
55155527
end matchcontinue;
55165528
end elabBuiltinDer;
55175529

5518-
protected function setUniqueErrorMessageForDer "
5519-
Author: BZ, 2009-02
5520-
Function to set correct error message for der.
5521-
(if we have an error with constant arguments to der() then do not give Error.WRONG_TYPE_OR_NO_OF_ARGS
5522-
"
5523-
input Env.Cache cache;
5524-
input Env.Env env;
5525-
input list<Absyn.Exp> expl;
5526-
input Boolean impl;
5527-
algorithm _ := matchcontinue(cache,env,expl,impl)
5528-
local
5529-
Absyn.Exp exp;
5530-
list<Ident> lst;
5531-
Ident s;
5532-
case (cache,env,{(exp as Absyn.CREF(componentRef = _))},impl)
5533-
equation
5534-
failure((cache,_,DAE.PROP(_,DAE.C_VAR),_,_) = elabExp(cache,env, exp, impl, NONE,true));
5535-
lst = Util.listMap({exp}, Dump.printExpStr);
5536-
s = Util.stringDelimitList(lst, ", ");
5537-
s = Util.stringAppendList({"der(",s,")'.\n"});
5538-
Error.addMessage(Error.DER_APPLIED_TO_CONST, {s});
5539-
then ();
5540-
case (cache,env,{exp},impl)
5541-
equation
5542-
(_,_,DAE.PROP(_,DAE.C_CONST),_,_) = elabExp(cache,env, exp, impl, NONE,true);
5543-
lst = Util.listMap({exp}, Dump.printExpStr);
5544-
s = Util.stringDelimitList(lst, ", ");
5545-
s = Util.stringAppendList({"der(",s,")'.\n"});
5546-
Error.addMessage(Error.DER_APPLIED_TO_CONST, {s});
5547-
then ();
5548-
/*
5549-
case (cache,env,expl,_)
5550-
equation
5551-
lst = Util.listMap(expl, Dump.printExpStr);
5552-
s = Util.stringDelimitList(lst, ", ");
5553-
s = Util.stringAppendList({"der(",s,")'.\n"});
5554-
Error.addMessage(Error.WRONG_TYPE_OR_NO_OF_ARGS, {s});
5555-
then ();
5556-
*/
5557-
end matchcontinue;
5558-
end setUniqueErrorMessageForDer;
5559-
55605530
protected function elabBuiltinSample "function: elabBuiltinSample
55615531
author: PA
55625532

Compiler/Types.mo

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5589,4 +5589,32 @@ algorithm
55895589
end matchcontinue;
55905590
end resTypeToListTypes;
55915591

5592+
public function getRealOrIntegerDimensions
5593+
"If the type is a Real, Integer or an array of Real or Integer, the function returns
5594+
list of dimensions; otherwise, it fails."
5595+
input Type inType;
5596+
output list<Option<Integer>> outDims;
5597+
algorithm
5598+
outType := matchcontinue (inType)
5599+
local
5600+
Type ty;
5601+
Option<Integer> d;
5602+
list<Option<Integer>> dims;
5603+
5604+
case ((DAE.T_REAL(varLstReal=_),_))
5605+
then
5606+
{};
5607+
case ((DAE.T_INTEGER(varLstInt=_),_))
5608+
then
5609+
{};
5610+
case ((DAE.T_COMPLEX(_,_,SOME(ty),_),_))
5611+
then getRealOrIntegerDimensions(ty);
5612+
case ((DAE.T_ARRAY(arrayDim=DAE.DIM(d),arrayType=ty),_))
5613+
equation
5614+
dims = getRealOrIntegerDimensions(ty);
5615+
then
5616+
d::dims;
5617+
end matchcontinue;
5618+
end getRealOrIntegerDimensions;
5619+
55925620
end Types;

0 commit comments

Comments
 (0)