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 AlexeyLebedev, 15 years ago

I have made a fix which can process, e.g., the following example:
{{{class A

parameter Real pa = 1;

Real a = der(pa);

Real b = der(1.0);

parameter Real[3] pc = {1,2,3};

Real[3] c = der(pc); 

Real[3] d = der({1.0,2.0,3.0}); 

end A; }}}

I have not uploaded it yet because some things are not clear to me:

  1. The file Error.mo contains an error
              (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.)
  1. How should expressions like der(1) be processed? Strictly speaking, 1 is an Integer, not a Real (unlike 1.0), so - formally - it can not have a derivative. At the moment, my fix considers this expression to be an error (just like trunk does).
    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?

comment:2 by Martin Sjölund, 15 years ago

  1. Well. A constant boolean may not be differentiated. It should be caught by other rules though.
  1. "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 AlexeyLebedev, 15 years ago

  1. 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"),}}}

  1. 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);}}}

  1. 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 Martin Sjölund, 15 years ago

Constant and parameter integers are fine. Discrete variability should not be possible to derive.

comment:5 by AlexeyLebedev, 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 Martin Sjölund, 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 AlexeyLebedev, 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.

Note: See TracTickets for help on using tickets.