Open henrikt-ma opened 3 years ago
The same question should be addressed about functions that have the derivative
annotation.
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.
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.
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;
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.
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.
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.
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.)
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 theLateInline = true
stage, but the specification doesn't recommend tools to delay automatic inlining until this stage. Hence, a user that wants theinverse
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 specifyingInline = false
orLateInline = true
.It would make more sense if the specification would tell tools to not throw away useful
inverse
annotations by inlining more eagerly than withLateInline = 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 hereInlineAfterIndexReduction = true
is also a useful alternative toLateInline = true
. Since differentiation is not only useful for index reduction, it might make sense to require an explicitInlineAfterIndexReduction = true
annotation in case one insists on inlining more eagerly than wheninverse
is present. That is, tools may only automatically inline more eagerly than at theLateInline = true
stage in case it can be concluded that thederivative
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.