Hacker News new | past | comments | ask | show | jobs | submit login
All about Modelica: An equation-based language for modeling physical systems (marcobonvini.com)
193 points by macro-b 13 days ago | hide | past | favorite | 44 comments





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.


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...


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.


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.


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

There's a project to create a language similar to Modelica as a DSL in Julia, Modia[1]. I'm not currently a user of either language, but saw a presentation at JuliaCon about it[2] and thought it looked interesting.

[1] https://github.com/ModiaSim/Modia.jl [2] https://youtu.be/hVg1eL1Qkws


FYI, it is by the same people who initiated the Modelica effort (Hilding Elmqvist and Martin Otter). I think part of the goal was to shed having to maintain a whole language (and the need to specify all the semantics details of such) and simply leverage a language that had macros.

I haven't really heard many updates on this in some time. I don't think it is necessarily a bad idea. But any effort along these lines takes a lot of dedicated people quite a while to make progress (as was the case with Modelica).


> by the same people who initiated the Modelica effort

Thanks for pointing this out, I hadn't realized! I knew they were somehow involved in the Modelica community but not that they initiated it.


That looks really promising.

But development seems to have completely stopped earlier this year. Anybody knows what's going on there?


The two main contributors are just busy in their "real" jobs. https://github.com/ModiaSim/Modia.jl/graphs/contributors

This is a great overview of Modelica. For those looking to get started - check out OpenModelica (https://openmodelica.org/) and if you are curious how to approach deploying and using Modelica in web services the article's author as a rather awesome open source project called LambdaSim see(https://github.com/mbonvini/LambdaSim)

a quick intro for programmers - Modelica is basically the equivalent of Haskell for engineers - the compiler does most of the work for you and produces highly performant code. You can compose a very complex system from individually modeled elements. The Modelica compiler will check constraints and basically come up with a system of equations. If it compiles, it is probably correct. The model wont compile if a particular variable is being modified in two places. Models have full introspection where you can observe any variable you choose.

That's an interesting way of framing it. I tend to agree. It isn't so much the presence of the type system so much as the fact that the compiler is given sufficient information (much of it symbolic) to aggressively optimize the generated code. So it seems to share that with Haskell, yes.

Been working in Modelica for a couple of years. Modelica compilers generate C code since they're heavily used in the embedded and real-time space. Kinda amazing to see systems with hundreds of thousands of equations simulate faster than real time.

what Modelica software do you use?

I have been used modelica and modelica-like languages for the past few months, and I have completely been blown away by the experience.

I think the solvers are fantastic and produce better results, in terms of speed and scale, than any other method I had used previously, I was solving the DEs using either wolfram or Julia's Differential equations.

OpenModelica is a wonderful piece of software, I just wish it had a better UI, UX and error messages. Wolfram's system modeler offers an excellent experience but it is very expensive.

Recently I have been using Mathworks take on model-based languages, Simscape[1], which is great for my use case, as it integrates the whole MATLAB and simulink ecosystem. It is not modelica compatible, but it is possible to import FMUs created with modelica.

As a final note, I truly wished that Modelica was taught more in engineering and physics degrees.

[1] https://www.mathworks.com/products/simscape.html


The secret to Modelica's results here isn't the numerics but the symbolics: they always preprocess the equations to something much simpler to simulate. With Julia we have the https://github.com/SciML/ModelingToolkit.jl to bring the same kind of symbolic-numerics approaches to practice.

Chris, I agree that the symbolic processing is quite important. I've often made the argument that a purely numerical approach just can't compete because of the optimizations and insights that come out of the symbolic analysis will always put it on a better footing (curious to know if you agree). But I'd say another factor in the success of Modelica is the ability to make it approachable by non-programmers. All the graphical aspects, acausal semantics, etc. make it easy to understand for non-programmer engineers as well. This has nothing to do with the results, per se, but is I think a factor in its success.

okay, its neat and all, but I missed seeing any output from the models, or examples how you use the models you built to analyze something? How do you interact with it, what sort visualizations you get etc etc??

You can plot variable outputs in 2D and 3D plots, use values to drive simulation based animations (2D and 3D) and via Functional Mockup interface standard (fmi-standard.org) you can interconnect external simulations and interactive visualizations (2D, 3D, VR etc). For examples of these in OpenModelica see the user guide here: https://www.openmodelica.org/doc/OpenModelicaUsersGuide/Open...

There is also a notebook UI that lets you do a lot of Ipython/Jupyter style dev with feedback plots and visualizations, in addition to code tools and visual authoring.


In my experience Modelica is a good fit for sustainability engineering, as it works well across engineering disciplines. Simulation of hybrid gas/electric engines was one of Modelica's first major applications, power plants was another.

As others have noted Modelica is both more abstract and more performant than a lot of other ways of describing multiphysics systems; it got its start in out-of-the-way places, which may explain it's relative obscurity, but there's an opportunity for it to become pervasive in engineering, both in the academy and industry.


I've been using Modelica for about 15 years now as an engineer. Mainly 3 different interpreter front ends: Dymola, OpenModelica, and SystemModeler.

At first I was mainly interested in it's ability to symbolically manipulate equations for me. With complex physical systems and interconnections it can become a tremendous chore rearranging systems to provide certain inputs and certain outputs. Modelica and the above compilers allowed me to specify the equations, and in fact automatically generate and interconnect larger and larger systems as one model. Specifying what were inputs and what were outputs became a simple few lines of code. For example, forward and inverse kinematics are the same equations in Modelica, you just specify different input/outputs, and the compiler determines what's the best way to do this. This can have significant challenges sometimes, when things don't easily work, but on the whole the process has been more often than not straightforward.

Interconnection of the models to other simulations has also become more and more a delight. Initially we had to depend on specific interfaces being built, either on the modelica interpreter side, or on the external application side (for example, Mathworks or Dymola), but fortunately the growth in such use prompted the modelica community to partner with industry to foster an open specification (like modelica is) for model exchange, called FMI. Integrating my models with external software is now a relatively straightforward task, still with challenges, but not expensive ones.

I'd highly recommend modelica and the various compilers as a means of creating very effective models; easily readable and therefore maintainable, re-usable, and now more and more deployable.

I, myself, have used them in the truck industry, CNC machine controls, flight systems, and in underwater vehicles and power systems. I routinely interact with people who use modelica in a much wider domain than this, and more and more in on-board systems, such as controllers. For example, I believe northern Germany uses modelica for its power controls for a significant portion of their country's energy supply.


If curious see also (from 2017): https://news.ycombinator.com/item?id=16013179

Is there a graphical interface, like Simulink, for Modelica?

There are commercial ones like ones like Altair SolidThinking, Wolfram System Modeller, ESI's SimulationX and there are open source ones such as OMedit. Which meets your needs will be partly budget / workflow / toolbox support based. If your new to Modelica - then OpenModelica and OMEdit (OMEdit comes as part of the OpenModelica download) are great starting points. To get an idea of the available commercial and open source options see:

https://www.modelica.org/tools

and https://openmodelica.org/openmodelicaworld/tools


Many. Dymola is probably the go-to solution, but also openmodelica, simulationX and wolfram, I think.

Here is a list: https://www.modelica.org/tools


OpenModelica is free. There are many commercial IDEs like Dymola from Dassault, Wolfram System Modeler etc.

Stellar example of naming gone right in software.

Are there Modelica programmers? Or is it a tool engineers use? Like could you become a Modelica developer and do that as a living?

I work as a full time Modelica programmer :-) And I do have colleagues.

Not fully up to date, but a starting point: https://modelica.org/jobs


Very cool - what sort of education do you have / need?

It's a domain specific language mainly for Modeling dynamic systems. Typically engineers focusing on simulation use it along with a commercial IDE and paid libraries with a lot of prebuilt components.

does anyone know any industry usage of Modelica?

Check the vendors user stories: https://www.wolfram.com/system-modeler/customer-stories/ Check the Modelica conferences (https://modelica.org/events/modelica2019/proceedings/html/Mo...) and you'll see a lot of automotive companies (Toyota, Volvo, Bosch, etc)

energy companies (powerplants, building systems etc.), aerospace, automotive, motorsports, robotics

Maybe also in physiology (BTW I would be happy if someone could point out similar or better resources)

https://www.physiolibrary.org/doc/Physiolibrary.html#Physiol...


Some more industry case sturdies here: https://www.modelon.com/industries/

Is Modelica used for economic modeling as well?

While most Modelica's applications are primarily engineering related, it is used for stock and flow models and there are some folks digging into use it for economic modeling (as there are others using it for environmental and biological systems modelling). If you are looking to get started using Modelica for economics modeling these might be of interest:

https://openmodelica.org/images/M_images/Moses206/moses2016-...

https://openmodelica.org/images/M_images/Moses206/moses2016-...

https://ep.liu.se/ecp/157/037/ecp19157037.pdf


I don't know enough about economic modeling, but if the models can somehow be expressed as driven by a "potential" and a "flow" variable (like electric voltage and electric currents), then it would definitely make sense to try that out in Modelica.

historical stock and flow economic modelling: https://en.wikipedia.org/wiki/MONIAC

(and then there's Cybersyn. It's difficult to evaluate how effective or ineffective it may have been, as the copper drop in value relative to gold by 5x in the early 1970's must've been a serious exogenous shock to the Chilean economy)




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: