
All about Modelica: An equation-based language for modeling physical systems - macro-b
https://marcobonvini.com/modelica/2020/06/29/all-about-modelica.html
======
choeger
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.

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

~~~
choeger
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).

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

------
pflanze
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](https://github.com/ModiaSim/Modia.jl)
[2] [https://youtu.be/hVg1eL1Qkws](https://youtu.be/hVg1eL1Qkws)

~~~
michaeltiller
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).

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

------
ptrott2017
This is a great overview of Modelica. For those looking to get started - check
out OpenModelica ([https://openmodelica.org/](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](https://github.com/mbonvini/LambdaSim))

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

~~~
budadre75
what Modelica software do you use?

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

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

------
NotPhysicsPhd
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](https://www.mathworks.com/products/simscape.html)

~~~
ChrisRackauckas
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](https://github.com/SciML/ModelingToolkit.jl)
to bring the same kind of symbolic-numerics approaches to practice.

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

------
zokier
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??

~~~
ptrott2017
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...](https://www.openmodelica.org/doc/OpenModelicaUsersGuide/OpenModelicaUsersGuide-
latest.pdf)

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.

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

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

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

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

~~~
ptrott2017
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](https://www.modelica.org/tools)

and
[https://openmodelica.org/openmodelicaworld/tools](https://openmodelica.org/openmodelicaworld/tools)

------
jwilber
Stellar example of naming gone right in software.

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

~~~
john_a
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](https://modelica.org/jobs)

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

------
budadre75
does anyone know any industry usage of Modelica?

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

~~~
JPLeRouzic
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...](https://www.physiolibrary.org/doc/Physiolibrary.html#Physiolibrary)

------
dang
Is Modelica used for economic modeling as well?

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

~~~
082349872349872
historical stock and flow economic modelling:
[https://en.wikipedia.org/wiki/MONIAC](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)

