modelica / ModelicaSpecification

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

Are variable-structure models allowed in Modelica? #2411

Open casella opened 5 years ago

casella commented 5 years ago

The common wisdom in the Modelica community is that the main limitation of Modelica is that it cannot allow variable-structure models. What exactly that means, though, remains unclear.

It is a fact that Modelica models cannot have a time-varying number of equations and variables, because the language itself does not allow you to achieve this goal: conditional components are instantiated based on parameters, arrays are sized based on parameters, if-then-else equations depending on time-varying conditions must have the same number of equations in the same branch and cannot contain connect equations.

However, there is a further limitation that can be loosely stated as "Modelica models cannot have a variable number of states after index reduction". For example, if I try this model in Dymola and OpenModelica

model Test
  Real x;
equation 
  if time < 0.5 then 
    der(x) = 10;
  else
    x = -5;
  end if;
end Test;

I get a runtime error at time = 0.5. The obvious reason is that after that event, the number of states decreases from 1 to 0.

However, I couldn't find any statement in the Specification that makes this limit explicit. To the contrary, Appendix C of the Specification, which describes the mathematical formulation of Modelica models after processing, says:

The equations define a DAE (Differential Algebraic Equations) which may have discontinuities, a variable structure and/or which are controlled by a discrete-event system

The tools I know best seem to base the code generation on these assumptions:

Models that do not fit these assumptions cannot be simulated, even though they look syntactically and semantically ok, because they are doomed to fail at run-time due to numerical issues, caused by the violation of such assumptions.

Should we write down these assumptions explicitly in the Specification?

Or is the above-mentioned Test model legal Modelica?

beutlich commented 5 years ago

However, I couldn't find any statement in the Specification that makes this limit explicit.

Fo that reason it is tool-specific how to solve it. SimulationX (internal solvers only) can successfully simulate the model - with warnings.

grafik

HansOlsson commented 5 years ago

I agree that we should add a restriction along those lines at the moment.

We have also experimented with varying number of states in 3D Experience Platform (and Dymola), since it natural when constructing mechanical systems. However, primarily for a restricted sub-set as the real challenge is to combine that with index reduction and the specification indicates that index reduction shall be used.

It is a fact that Modelica models cannot have a time-varying number of equations and variables,

Note that since Modelica 3.3 this is only true "outside of state-machines". However, state-machines must be clocked, which means that it doesn't influence the discussion about number of states.

henrikt-ma commented 5 years ago

For the record, SystemModeler gives a warning during translation (which would be an error in pedantic mode), before failing during simulation.

I am generally in favor of making restrictions that will help us achieve the goal of portability of simulation results (including portability of translatability and simulatability). However, the prominent role of index reduction to index so and so, and in particular the references to the dummy derivatives method, is too much for my taste.

If anything, I'd prefer a formulation in terms of the dimension of the solution manifold, or state space if that puts less people off.

However, I have the feeling that a constant dimension of the solution manifold wouldn't capture what we want to say anyway, as illustrated by this model:

model Test2
  Real x;
  Real y;
equation 
  if time < 0.5 then 
    der(x) = 10;
    y = 1;
  else
    x = -5;
    der(y) = 1;
  end if;
end Test2;
AtiyahElsheikh commented 3 years ago

I was referred to this issue from an interesting question in stack-overflow which is a special case from this thread.

Models with runtime-varying differential indices (though the number of variables remain constant) is a special but a sophisticated case. A special parameter value can convert a sophisticated equation to a trivial one which may influence the differential index of a given DAE. The same argument applies to special start values. Moreover, for a model say with N conditions, there are $2^N$ possible combinations of values each would correspond to a different equation systems. These systems may have different differential indices. Thus, obviously a tool that allows such models can not pre-compute all the 2^N computational graphs in advance. Thus, only runtime-decisions during simulations seem to be imaginable, s.a., internal conversions within computational graphs, retaining equations from a previous index-reduction step, or conducting further index reduction etc. could make such special case realizable.

Should the specification even constrain models that are not allowed to have a varying differential index?

HansOlsson commented 3 years ago

The given examples presents a too simple picture of this involving just one if-equation.

However, in practice the variable structure may involve multiple equations.

One kind of example is an electrical circuit with multiple ideal thyristors (or similarly with friction) where some specific configurations have such problems; and it is not even clear if that configuration is reachable.