Opened 15 years ago
Last modified 14 years ago
#1233 closed defect (fixed)
Derivative operator
Reported by: | Martin Sjölund | Owned by: | Martin Sjölund |
---|---|---|---|
Priority: | normal | Milestone: | |
Component: | Version: | ||
Keywords: | Cc: | Martin Sjölund, AlexeyLebedev |
Description
Modelica Spec 3.2: Section 3.7.2
der(expr): For Real parameters and constants the result is a zero scalar or array of the same size as the variable.
{{class A
Real a = der(1);
end A;}}
{{class A
paramater Real pa = 1;
Real a = der(pa);
end A;}}
Does not work in trunk. Should have a working implementation for arrays and scalars, plus good testcases.
Change History (7)
comment:1 by , 15 years ago
comment:2 by , 15 years ago
- Well. A constant boolean may not be differentiated. It should be caught by other rules though.
- "If the expression expr is a scalar it needs to be a subtype of Real."
This means the expression should be typecast to a Real if it is an Integer. It's not an error to call der(1) - it's the same as der(1.0).
comment:3 by , 15 years ago
- Yes, there is another error for such cases:
{{{(DERIVATIVE_NON_REAL,TRANSLATION(),ERROR(),
"Illegal derivative. der(%s) where %s is of type %s, which is not a subtype of Real"),}}}
- So is it also legal to call der on constant expressions which are intetionally declared Integer? I mean something like
{{{parameter Integer p = 1;
Real r = der(p);}}}
- I have implemented my fix in elaboration phase (in Static.elabBuiltinDer function where DER_APPLIED_TO_CONST error was called before). But may be I should have implemented it in constant evaluation or simplification phase instead?
comment:4 by , 15 years ago
Constant and parameter integers are fine. Discrete variability should not be possible to derive.
comment:5 by , 15 years ago
I have uploaded fix and some test cases (mofiles/DerConstant1-3.mo) in revision 5635. There is a strange problem with DerConstant2, though: expected result is started to read too early, in file's header, so equation mismatch error is produced. What is most strange, the same problem happens even if DerConstant2.mo is replaced by a copy of DerConstant1.mo (and DerConstant1.mo does NOT produce an error!), so the problem seems to be not in the file but somewhere else, and I have no idea where.
Martin, could you please have a look at it?
comment:6 by , 15 years ago
You simply forgot to add " endResult" to the files. I didn't fix rtest to check if it's defined in the file - it's an old bug :)
comment:7 by , 15 years ago
Saw that Peter Aronsson has added a rule der(constant) = 0.0 to simplification. It is not quite correct - if the expression is an array, the result must also be an array. I have modified the rule.
I have made a fix which can process, e.g., the following example:
{{{class A
end A; }}}
I have not uploaded it yet because some things are not clear to me:
(DER_APPLIED_TO_CONST,TRANSLATION(),ERROR(),
"der operator applied to constant expression der(%s)")
This error is generated by the function Static.setUniqueErrorMessageForDer, which is called by Static.elabBuiltinDer
Are there really cases where applying der to a constant expression is an error? (The trunk version of Static.elabBuiltinDer looks to me as if it is always an error.)
When should a derivative of Integer parametric or constant expression be considered legal (and equal to 0.0 or an array of zeros)? Always? Never? For expressions which only contain Integer literals but no component references?