Hacker News new | ask | show | jobs
by choeger 2185 days ago
One thing about Modelica is quite important and might confuse newcomers: the language specification mostly only covers the model, not the equations. That is, when you evaluate your model under, e.g., Dymola and Openmodelica you will (most of the time) end up with equal (or rather similar) systems of equations. The meaning of these equations, i.e., the actual simulation can easily differ, though (except for the most trivial models).

The reason for this is that the equations are extremely powerful (you can express pretty much anything computable inside an equation) and their numerical solution is often driven by heuristics (e.g., finding initial values). This makes it extremely difficult to come up with a common standard for the solution of Midelica's hybrid DAEs. Hence you will find little written information about what exactly happens during a simulation that applies to more than one implementation.

1 comments

Another thing that we discovered when testing out using Modelica for some stuff is that it is very easy to design yourself a model that can't be solved. The whole premise of the "object orientation" is that you can just compose models without knowing how to solve the entire system. Once the solver fails, though, all that abstraction comes crashing down and you're left with trying to look at a 500-variable Jacobian and figure out what's making the solver choke.

One particular issue with the circuit model example in the article, for example, is that when you add switching elements you often end up with disconnected sub-diagrams and then the solver chokes trying to determine what the potential of the floating circuit is, even though it has no effect on anything you care about. So then you have to start adding stray capacitances and stray resistances to make the solver happy, etc.

Yes this is an art form in itself. I've been planning to do a series on debugging Modelica and the different failure modes that come up, and what tools can be used. It is a known limitation in the Modelica community, but it is also really hard to address in an automatic fashion. See [1] and [2] for some ideas.

[1] https://reference.wolfram.com/system-modeler/UserGuide/Simul...

[2] https://liu.diva-portal.org/smash/get/diva2:801004/FULLTEXT0...

This is a fundamental issue with all large scale DAE (differential-algebraic-equation) [1] systems, regardless of modeling environment. DAEs are different from ODEs in that it's really difficult to design a solver that will reliably solve every single DAE out there -- numerically so many things can go wrong, so it is up to the modeler to write models in such a way as to promote algorithm convergence.

For instance, even finding a set of consistent initial condition requires the solution of a typically nonconvex nonlinear system of equations, which in turn requires a good initial guess (where domain knowledge is needed -- arbitrary guesses may work but often don't). This is a "solved" problem in a sense that algorithms exist that do this, but anyone who's done numerical work know how hard it is to reliably find the solution of an arbitrary nonlinear system of equations -- there's a significant element of luck involved, even with heuristics.

Formulating good models is an art. Good modelers know to avoid issues with numerical scaling (by changing units of measure, doing log or linear scaling, or simply avoiding computation of certain intermediate quantities) and avoidance of singularities (e.g. instead of x/y = z, a better and equivalent formulation is x = y * z because if y is ever 0 during iteration, one avoids div-by-0 errors). It gets even tougher with hybrid models that have discontinuities (switching behavior), because the integrator needs to do a reinitialization every time a switch is encountered. Most solvers do this well (the integrated system up to that point is usually a good initial guess for the NLE system), but not 100% of the time.

When I was doing this stuff full-time, I've often thought of writing a linter that detected poor formulations and suggested improved ones -- sort of like a spell-checker for mathematical models. I never pursued it.

These days I guess this can be done using a language server that can parse equations and generate ASTs. I'm no longer interested in these problems, but I do wonder if anyone has tried to do something like this.

[1] DAEs take the following (implicit) form:

f(ẋ, x, z, θ) = 0, x(0) = x₀

g(x, z, θ) = 0

where x = differential vars, z = algebraic vars, θ = constants. The form is very general and can be used to model any number of continuous dynamic systems. Hybrid systems can be modeled by introducing discrete variables into the mix.

Very true. But I think it is important to understand the alternative.

The "object-oriented" part of Modelica is intended to make creating large scale models manageable. You make it sound like the object-oriented features are some kind of facade behind which all kinds of peril lies. But the peril is always there. And my experience of building such models both using a modeling language vs. creating them by hand is that you can't escape the limitations of contemporary numerical/symbolic methods or singular systems. You are, of course, free to hand code models or manually transform them into state space equations if you think there is some benefit to that in the same way. Perhaps it will make it easier to diagnose singular systems. But speaking entirely for me, I find being able to create acausal models using a modeling language gives me a productivity boost that I'd be unwilling to give up.

That's absolutely true. A severe limitation of Modelica's type system is that you cannot constrain your modeling domain to something that is always solvable, or at least analyzable. You cannot, for instance, limit yourself to linear equations. In my opinion it would be the library author's responsibility to constrain the usage of their models in a way that prevents typical modeling errors (as in your example, the switch).
But, of course, what is solvable changes and/or is implementation dependent. Modelica doesn't say much on the topic of impulses. In theory, a vendor could support variable index systems along with the ability to resolve impulses. It is a conscious decision not to codify contemporary constraints on solvers/methods into the language. Of course, Sebastien Furic did quite a bit of work along the lines you are describing but he was unable to persuade the group to accept such constraints.
"Error: Model is singular" is pretty frustrating to debug