modelica / ModelicaSpecification

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

Need to specify late or no inlining for portable applicability of inverse and derivative annotations #2942

Open henrikt-ma opened 3 years ago

henrikt-ma commented 3 years ago

Background: https://github.com/modelica/ModelicaSpecification/pull/2901#discussion_r629641182

When using the inverse annotation one typically wants the inverse function to remain applicable until at least the LateInline = true stage, but the specification doesn't recommend tools to delay automatic inlining until this stage. Hence, a user that wants the inverse annotation to be applicable when needed also in some tool currently not at hand, are thus forced to take control over the situation by either specifying Inline = false or LateInline = true.

It would make more sense if the specification would tell tools to not throw away useful inverse annotations by inlining more eagerly than with LateInline = true. Exactly what is a "useful" annotation can be left as a quality of implementation, allowing tools to still, for instance, do early inlining of function calls with only constant arguments.

A similar situation exists for the derivative annotation, but here InlineAfterIndexReduction = true is also a useful alternative to LateInline = true. Since differentiation is not only useful for index reduction, it might make sense to require an explicit InlineAfterIndexReduction = true annotation in case one insists on inlining more eagerly than when inverse is present. That is, tools may only automatically inline more eagerly than at the LateInline = true stage in case it can be concluded that the derivative annotation isn't useful in subsequent stages. It should be noted that "being useful" is in itself a quality of implementation, as tools may differ in how much use they make of symbolic derivatives during the stages following index reduction.

eshmoylova commented 3 years ago

The same question should be addressed about functions that have the derivative annotation.

henrikt-ma commented 3 years ago

The same question should be addressed about functions that have the derivative annotation.

Right. I have updated the issue title and the top comment accordingly.

thorade commented 2 years ago

Related SO question: https://stackoverflow.com/questions/55019892/modelica-annotation-inverse-of-function-is-ignored

henrikt-ma commented 2 years ago

Related SO question: https://stackoverflow.com/questions/55019892/modelica-annotation-inverse-of-function-is-ignored

Great! I added a cross reference also in the other direction.

thorade commented 2 years ago

Here is a small test model, there is an unexpected nonlinear system.

model Inverse_MSL_MoistAir
  replaceable package Medium = Modelica.Media.Air.MoistAir;

  Modelica.SIunits.AbsolutePressure p;

//     Modelica.SIunits.SpecificEnthalpy h1;
//     Modelica.SIunits.Temperature T1;

      Modelica.SIunits.SpecificEnthalpy h2;
      Modelica.SIunits.Temperature T2;

equation 
  p = 101325;

  // T1 is known 
  // h1 is computed from nonlinear system
  // Jacobian for nonlinear system is numerical
//    T1 = 273.15 + time;
//    T1 = Medium.temperature_phX(p, h1);

  // h2 is known
  // T2 is computed from nonlinear system (but why??)
  // Jacobian for nonlinear system is analytical 
    h2 = 200e3 + time;
    h2 = Medium.specificEnthalpy_pTX(p, T2);
   annotation(uses(Modelica(version="3.2.3")));
end Inverse_MSL_MoistAir;
henrikt-ma commented 2 years ago

Here is a small test model, there is an unexpected nonlinear system.

I'm not a domain expert, but I note that System Modeler doesn't produce any nonlinear systems for this model. Is the point of the model that some tools do it, while others don't, and that this is related to portable applicability of inverse and derivative annotations? In that case, I think elaborating this a bit would be very helpful.

thorade commented 2 years ago

Sorry for being unclear. Dymola 2022 shows a nonlinear system, and this is unexpected, because the function that is being called has an annotation inverse. The SystemModeler behavior would then be the desired behavior.

HansOlsson commented 2 years ago

It seems there is agreement here, and there even seems to be a commit referencing this for the specification. What is the status for that @henrikt-ma - especially as it isn't linked.

The only issue is that some models might rely on different behavior, but that is either a tool or model issue.

henrikt-ma commented 2 years ago

If the commit you refer to is https://github.com/henrikt-ma/ModelicaSpecification/commit/56d3461a5924994fa2ed398de871e277b39e2ed2, then this is already present on master.

But so far the conversation above shows agreement about there being similar problems with both inverse and derivative. (There are similarities to #2986, but I don't think that any agreement on that issue would also apply here.)