modelica / ModelicaSpecification

Specification of the Modelica Language
https://specification.modelica.org
Creative Commons Attribution Share Alike 4.0 International
102 stars 41 forks source link

Figure annotations don't support variable derivatives #3004

Closed henrikt-ma closed 2 years ago

henrikt-ma commented 3 years ago

I consider it an omission of the specification of the figures-annotation to not provide any means to either plot or reference a variable's derivative. For example, I believe it is common for Modelica tools to also store der(x) in addition to x for a model like this:

model HelloWorld
  Real x(start = 1.0, fixed = true);
equation
  der(x) = -x;
end HelloWorld;

Plotting der(x) is currently prohibited by this rule about a Curve:

The mandatory x and y expressions are restricted to be component references referring to a scalar variable or time.

Further, referencing der(x) in variable replacements or text markup in captions is not possible due to the specification's use of the component-reference production rule in the grammar.

Solving this for der(x) seems straight-forward, but we should also agree on a solution for higher order derivatives. Here, there's a conflict between:

For the figures-annotations we could consider allowing both forms, but agreeing on one seems ideal to me.

Similar to the current use of component-reference, it would be natural to describe the derivatives using grammar, for example:

result-reference:
   component-reference
   | "der" "(" component-reference [ "," UNSIGNED-INTEGER ] ")"

We should probably also make a note regarding the somewhat unpredictable presence of variable derivatives in simulation results.

casella commented 3 years ago

I agree that being able to display der(<cref>) values and text mark-ups is a must have and I support this proposal.

I won't be worried by der(<expr>), since I understand all Modelica tools eventually reduce the system equations to 1-st order ODEs or DAEs, so even if I write der(x*y) = ... in my model, I would be happy if I can plot der(x) and der(y).

I've never seen a model with der(der(x)) in my life, though I guess it is technically allowed. Modelica models of mechanical systems normally include first order equations for generalized position and velocity coordinates, so that second derivatives are not really needed. This is also consistent with the fact that Modelica tools must be able to run generic multi-domain models, so they won't use second-order ODE solvers.

henrikt-ma commented 3 years ago

Note that this issue should not be confused with the need for more general curve expressions. That is, plotting der(x + y) is a completely different topic, and I'd even say it's another step beyond plotting variable derivatives and simple expressions such as x + y.

HansOlsson commented 3 years ago

Note that this issue should not be confused with the need for more general curve expressions. That is, plotting der(x + y) is a completely different topic, and I'd even say it's another step beyond plotting variable derivatives and simple expressions such as x + y.

I agree that plotting der(x+y) would be complicated, but if we support derivatives and simple expressions we could plot der(x)+der(y) - which is the same (assuming it exists).

henrikt-ma commented 3 years ago

I agree that plotting der(x+y) would be complicated, but if we support derivatives and simple expressions we could plot der(x)+der(y) - which is the same (assuming it exists).

Sure, one could do that, but exactly how complicated it would be to include support for der expressions will depend on how rich the rest of the plot curve expression language is. I think one would cover many needs by a combination of der on variables and some simple (non-differential) expression language. Anyway, the main point is that I don't want us to consider this case now.

henrikt-ma commented 3 years ago

I've never seen a model with der(der(x)) in my life, though I guess it is technically allowed. Modelica models of mechanical systems normally include first order equations for generalized position and velocity coordinates, so that second derivatives are not really needed. This is also consistent with the fact that Modelica tools must be able to run generic multi-domain models, so they won't use second-order ODE solvers.

Then I'm pleased to introduce Mr Marginally Stable, a friend of System Modeler:

model MarginallyStable
  Real x(start = 1.0, fixed = true);
equation
  der(der(x)) = -x;
end MarginallyStable;

When simulated, you'll find der(der(x)) in the result, and of course we don't need a second-order ODE solver to get the result (does such a thing even exist?).

To make it a little more interesting, you can also give initial conditions for der(x):

model MarginallyStable2
  Real x(start = 0.0, fixed = true);
initial equation
  der(x) = 1.0;
equation
  der(der(x)) = -x;
end MarginallyStable2;
casella commented 2 years ago

Then I'm pleased to introduce Mr Marginally Stable, a friend of System Modeler:

model MarginallyStable
  Real x(start = 1.0, fixed = true);
equation
  der(der(x)) = -x;
end MarginallyStable;

Interesting. OpenModelica generates invalid C code with it, while Dymola says more politely "The model includes derivatives of order > 1 for x but that is not supported."

But I guess that's legal Modelica. @HansOlsson, @kabdelhak, what do you think.

To make it a little more interesting, you can also give initial conditions for der(x):

But not a start attribute, if you needed a guess value for some nonlinear initial conditions. You could do that only by introducing an alias for der(x), but then the whole point of der(der(x)) would be lost.

henrikt-ma commented 2 years ago

Interesting. OpenModelica generates invalid C code with it, while Dymola says more politely "The model includes derivatives of order > 1 for x but that is not supported."

But I guess that's legal Modelica. @HansOlsson, @kabdelhak, what do you think.

If it should be disallowed for some reason, I hope that we can have the discussion about that later and not let this prevent us from fixing the problem with derivatives in figures in the context of current restrictions on how der may be used in equations.

henrikt-ma commented 2 years ago

To make it a little more interesting, you can also give initial conditions for der(x):

But not a start attribute, if you needed a guess value for some nonlinear initial conditions. You could do that only by introducing an alias for der(x), but then the whole point of der(der(x)) would be lost.

True, but this problem is not specific to higher order differential equations, as demonstrated by this example of a first order model:

model NonlinearInitialDerivativeEquation
  Real x;
equation
  der(x) = -x;
initial equation
  der(x)^2 = 2;
end NonlinearInitialDerivativeEquation;
HansOlsson commented 2 years ago

The difference is that if you have a variable for der(x) you can modify its start-value after translation (and separately there is no similar mechanism for modifying initial equations). For instance if you get the wrong solution to the initial equation (did you want the positive or negative one?) you can normally set guess-values for variables to steer it right, but you cannot set such a guess-value for der(x) (well, except for all the aliasing).

casella commented 2 years ago

To be clear, I'm fine with closing this ticket, the discussion on der(der(x)) is completely off-topic.

BTW, here is how you can solve the der(x) start value issue, if you want to pick the negative solution.

model NonlinearInitialDerivativeEquation
  Real x;
  Real der_x(start = -1.4);
equation
  der(x) = -x;
  der_x = der(x);
initial equation
  der(x)^2 = 2;
end NonlinearInitialDerivativeEquation;