Skip to content

Navigation Menu

Sign in
Sign up

What is a "numerically fragile" model, and how to avoid them in the MSL #4577

casella started this conversation in Ideas
Discussion options

In #4559, @AHaumer posed an interesting question: what is a numerically fragile model? Which carries along another question: should we always strive for tools to bend over backwards to get them running?

This discussion is meant to collect and discuss ideas. Ideally, the final outcome should be some quantitative criterion that we could apply to MSL models to avoid models that may be prone to fail or produce different results on different tools and different versions of the same tool.

You must be logged in to vote

Replies: 11 comments 8 replies

Comment options

I'm not sure whether we can always avoid numerically fragile models. Sometimes this depends on parameters - as we have seen in #4559 where changing parameters of the diodes reduces chattering.
We have usages of components that are stable and others that are really challenging, see discussion in #4485. It deals with IdealizedOpAmpLimited - which is the only OpAmp model in MSL with saturation and finite amplification (this is essential for some applications). This component has been "enhanced" (and detoriated!) after the first implementation - we have to redesign it.
But the question is: Can we avoid numerically fragile models by different implementation of the components or is it also a question of the system (i.e. the example to be simulated)?
See also #4477 which triggerd my implementation of chaotic systems which shows that for certain parameter values we get periodic results and for other parameter values we get chaotic behaviour, i.e. for smallest variations of initial conditions (or a different solver algorithm!) we get drastically different trajectories.
I would appreciate to discuss with other Modelicans - how can we reach them?

You must be logged in to vote
1 reply
Comment options

I think we have to be clearer than "numerically fragile model", as it is a problem with time-simulations of the model - not necessarily a problem with the "model" itself:

  • A chaotic system is defined as having large variation in (finite) trajectories based on small variations in numerics/parameters/etc.
  • What we have in these other cases is that similar small variations in "inputs" lead to:
    • No solution for some system of equations
    • Chattering

For chaotic systems there are solutions for handling it (including comparing phase-plots).

For "no solution" and "chattering" it seems more complicated as we need to restrict equations in some way (positivity etc) and/or switch to different solution methods.

Comment options

casella
Apr 24, 2025
Maintainer Author

Note that the problem we had in #4559 was not that a small variation of inputs/parameters/initial conditions caused a large deviation of the solution. The problem was that the formulation of the model around the diode switching was ill-conditioned due to the resistance and the conductance being too close to zero. If you set Ron = 0, Goff = 0, the model is theoretically singular. If those parameters are set to non-zero but too small values, the model is practically nearly singular due to very large condition numbers of some implicit equations.

The symptom in this case was chattering, but it was not chattering caused by a system with a discontinuous RHS ODE system, as in models that have sliding modes; as I understand, it was an ill-conditioned situation caused by too small Ron and Goff. Making them larger (but still negligible from a practical point of view) solved the problem. I believe we should check all condition numbers during the numerical solution process, and reject models that lead to condition numbers larger than, say, 1e9 or something like that.

Chaotic systems are a different story. Their problem is that their trajectories are inherently unstable. I would not classify them as "fragile" or "ill-conditioned".

You must be logged in to vote
1 reply
Comment options

I would say that chaotic systems are almost the text-book definition of "ill-conditioned"; as small changes in inputs lead to large changes in outputs (with the caveat that the outputs are bounded).

Even though that's slightly different from ill-condition equations (such as small Ron and Goff leading to large variations in results at a specific point in time).

And #4559 is similar to normal chattering as sliding modes; it's just that instead of continuing that chattering forever it only continues for a small time (with a small but non-zero time time between the events). Just look at the event log (from Dymola):

Expression diodeBridge2mPulse.diode_p.idealDiode[2].s < 0 became false ( (diodeBridge2mPulse.diode_p.idealDiode[2].s)-(0) = 1.6736e-14 )
Variable diodeBridge2mPulse.diode_p.idealDiode[2].off = 0 at time 0.664016
Iterating to find consistent restart conditions.
during event at Time : 0.6640160164133539
Expression diodeBridge2mPulse.diode_p.idealDiode[2].s < 0 became true ( (diodeBridge2mPulse.diode_p.idealDiode[2].s)-(0) = -1e-10 )
Variable diodeBridge2mPulse.diode_p.idealDiode[2].off = 1 at time 0.664017
Iterating to find consistent restart conditions.
during event at Time : 0.6640166491462307
Expression diodeBridge2mPulse.diode_p.idealDiode[2].s < 0 became false ( (diodeBridge2mPulse.diode_p.idealDiode[2].s)-(0) = 1.00665e-10 )
Variable diodeBridge2mPulse.diode_p.idealDiode[2].off = 0 at time 0.664017
Iterating to find consistent restart conditions.
during event at Time : 0.6640167642730948
Expression diodeBridge2mPulse.diode_p.idealDiode[2].s < 0 became true ( (diodeBridge2mPulse.diode_p.idealDiode[2].s)-(0) = -1e-10 )
Variable diodeBridge2mPulse.diode_p.idealDiode[2].off = 1 at time 0.664017
Iterating to find consistent restart conditions.
during event at Time : 0.6640173573820016
Expression diodeBridge2mPulse.diode_p.idealDiode[2].s < 0 became false ( (diodeBridge2mPulse.diode_p.idealDiode[2].s)-(0) = 1.05869e-10 )
Variable diodeBridge2mPulse.diode_p.idealDiode[2].off = 0 at time 0.664017
Iterating to find consistent restart conditions.
during event at Time : 0.6640174792957966
Expression diodeBridge2mPulse.diode_p.idealDiode[2].s < 0 became true ( (diodeBridge2mPulse.diode_p.idealDiode[2].s)-(0) = -1e-10 )
Variable diodeBridge2mPulse.diode_p.idealDiode[2].off = 1 at time 0.664018
Iterating to find consistent restart conditions.
during event at Time : 0.6640182163703822

The exact length of that chattering-sequence varies; and it is likely numerically fragile in some way.
You can also have such chattering-cases for friction.

However, the important part is that there's more to the model than the chattering (even during that time-period) so it recovers on its own.

Comment options

It's a pity that experiments and components both are named "model" although they are very different:
A component can't be simulated. You have to add the ambient of an application to get an experiment and simulate,
Are we talking about components or experiments?
If we're talking about experiments, I agree with @casella : We should dig into condition numbers (sigh, I'm not very experienced in numerical mathematics). Could Tolerance have an influence on condition numbers?
To classify the different problems:
#4559 shows that changing parametrization (Ron and Goff) of the experiment could improve numerical condition.
#4485 shows that a component (OpAmp) that works fine in one application causes problems in another experiment. It would be a pity to reject an experiment that is no big deal in reality like the multi-vibrator or the signalGenerator.
#4477 is about a chaotic model. As I wrote in #4637 I think for most such experiments we might find a parameterization that allows compariability and leave it to the user to change parameters to obtain chaotic behaviour.

You must be logged in to vote
1 reply
Comment options

casella Apr 28, 2025
Maintainer Author

It's a pity that experiments and components both are named "model" although they are very different: A component can't be simulated. You have to add the ambient of an application to get an experiment and simulate, Are we talking about components or experiments?

In principle, both. In some cases, a certain component model could lead to nearly-singular equations no matter how it is connected to others; in other cases, this could be a system-level property.

Could Tolerance have an influence on condition numbers?

In principle, no. However, it sometimes happens that sloppy tolerance lead to accept numerically challenged steps, from which the solver cannot recover afterwards, e.g. due to chattering. I remember one such case with a pressurizere model for nuclear power plants I studied for EDF during the EuroSysLib project.

Comment options

Looking more closely at SPMP_Braking #4559 seems to suggest why it is ill-conditioned and how it relates to the chattering.

SMPM_Braking

Notice how the current doesn't seem to increase linearly after the event, but it looks more like a quadratic function.

  • That will make it harder to compute, and since it is close to a zero-crossing it will increase the risk of chattering.
  • It also indicates why it is temporary, since quadratic functions are only problematic close to zero.

That also suggests that a quick-fix would be to introduce a new iteration variable in the IdealDiode:

 Real i2;
equation
 i- Goff*Vknee=noEvent(abs(i2))*i2;

That crazy idea seems to work as far I could tell, indicating that it is an ill-conditioned system of equations (it's well-known that quadratic systems are ill-conditioned near zero) leading to chattering.

Obviously changing the ideal diode in that way in general is problematic; but it may serve as a starting point for a good solution - and also dismiss bad ideas.

I don't know why the solution has a current that increases like that.

You must be logged in to vote
1 reply
Comment options

casella Apr 28, 2025
Maintainer Author

Notice how the current doesn't seem to increase linearly after the event, but it looks more like a quadratic function.

Which probably means that at the event, there is some nearly-zero derivative, i.e., a nearly singular Jacobian somewhere in the model.

Comment options

@HansOlsson do I understand correctly: i2 is just an additional variable (unit sqrt(A)) making the iteration easier?
I tried it and I see:
With the original diode I see Sizes of linear systems of equations: {2}.
With the "new" diode I see Sizes of nonlinear systems of equations: {1, 1, 1, 1, 1, 1} and the simulation time gets higher.
Astonishing: You zoomed in at a point in time when mechanical speed is nearly zero. Therefore induced voltage is nearly zero and nearly nothing should happen. Ok maybe challenging in the rectifier with current and voltage near zero.
With the "new" diode the chattering still happens:
SMPM_Braking

You must be logged in to vote
0 replies
Comment options

BTW: If you want to have in iteration variable i2 shouldn't we write:
Real i2 = noEvent(abs(i - Goff*Vknee))*(i - Goff*Vknee);
Sizes of nonlinear systems of equations: { }

You must be logged in to vote
0 replies
Comment options

... and @casella is right: Higher (but technically speaking neglectible) Ron=1e-4 and Goff=1e-4 solves the problem completely,
even without additional iteration variable:
SMPM_Braking
What's astonishing: Sizes of nonlinear systems of equations: { } - why that? Due to change of Ron and Goff?

You must be logged in to vote
2 replies
Comment options

Sizes of nonlinear systems of equations: { } - why that? Due to change of Ron and Goff?

No, but because the original model had no non-linear system of equations either. They were just introduced by my "i2" - so I need to rethink that (there's still a non-linear behavior).

Comment options

@HansOlsson you're right, I mixed it up.

Comment options

Here's a post by Christopher Rackauckas that proves that for chaotic models (resp. models with parameterization that leads to chaotic behaviour) comparability totally depends on accuracy. He calls the point in time where chaos starts "Lyapunov time". And this Lyapunov time depends on the algorithm, tolerance and accuracy (computer system and OS). So for me the safe solution to achieve comparability is to compare with parameterization that leads to periodic results and leave it to the user to vary parameters and investigate the path to chaos.
https://www.juliabloggers.com/how-chaotic-is-chaos-how-some-ai-for-science-sciml-papers-are-overstating-accuracy-claims/

You must be logged in to vote
1 reply
Comment options

The Lyapunov time only depends on the system (assuming it is ergodic; if some starting positions are stable and some unstable it is messier) - and does not depend on algorithm, tolerance and accuracy.

However, as noted in https://en.wikipedia.org/wiki/Lyapunov_time chaos doesn't start exactly after that time, it is instead a rough time-scale where algorithm, starting position, and tolerance will scale up the onset of chaos a bit.

(And in practice just simulating the system and seeing where chaos starts is a lot easier than computing the Lyapunov time.)

But that is continuous analysis (*) and for many of the numerically fragile models we have seen that problems occur for state events (as in this case), and to avoid problems we need to:

  • Ensure that the number of events are bounded in each interval (i.e., no chattering)
  • Ensure that the event iterations/continuous part is solvable for all numerically reachable discrete states.

The s-parametrization was introduced to avoid some of those issues, but it may be that we need something better for these cases.
There are completely different ways to handle events that allow unbounded number of events, like Moreau-type integrators.

*: The discrete analysis for Lyapunov would in Modelica correspond to regularly sampled or clocked systems, not hybrid systems.

Comment options

Maybe the only way to check whether a model (with a certain parameterization!) is numerically fragile or not is to simulate with different algorithms, different tools and carry out a sensivtivity analysis: How large are the deviations of trajectories for small changes of parameters and / or initial conditions?
Bear in mind all our models are deterministic, and talking about chaos means "deterministic chaos", i.e. the trajectories lie in a certain region but which of the possible trajectory is hit is impossible to predict.

You must be logged in to vote
1 reply
Comment options

Yes, and carrying out this experiment should be the responsibility of the modeler adding an example with "deterministic chaos". It shouldn't take too long to figure out a suitable TestCase.StopTime for the relatively small number of models in question.

Comment options

casella
Jun 3, 2025
Maintainer Author

I don't think chaotic models are "numerically fragile". That's the way they behave. In real life. I mean, their behaviour is truly unpredictable, that's what Lorentz's butterfly was all about.

To me, "numerically fragile" means that the real system has well-defined behaviour, but the model is prone to crash or become singular or produce bogus results because some parts of them are ill-posed.

You must be logged in to vote
0 replies
Comment options

Then I see 2 types of "numerically fragile" models;

  1. The one @casella did describe "The real system has well-defined behaviour, but the model is prone to crash or become singular or produce bogus results because some parts of them are ill-posed." BTW what is the definition of "well-defined"? FInite results? Predictible results? Repeated experiments show the same results?
  2. "Chaotic models" show only for a certain parameter range deterministic chaos, for other parameters they show periodic results. Yes to produce chaotic results some prerequisites have to be implemented in the model, for some models by intention, for some models by chance. Deterministic chaos means that though the model algorithm is determinstic the results are unpredictable, since small changes in parameters, initial values, algorithm and tolerance lead to finite but unpredictable different results.
    Isn't the second case also "numerically fragile"? Interesting to read: Christopher Rackauckas' post mentioned above.
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /