Modelica
modelica.org
modelica.org
> Modelica is a high-level declarative language for describing mathematical behavior. It is typically applied to engineering systems...
We use Modelica quite a bit in HVAC industry. In my case (controls engineer), I can request FMUs of various components from systems engineers for optimization work. (Functional Mockup Unit (FMU)[1]: stand-alone binary representing a dynamical system that can be driven by another application). My background is in Reinforcement learning/Model predictive control/python. Having a physics-driven model written in a domain-specific language which I can embed into my python workflow [2] is convenient.
I will say, Modelica requires a different perspective from "regular" imperative programming (python/matlab). It is a declarative language: you define equations, variables, constraints for a system, regardless of order. The compiler decides how to run the simulation; which variables to solve first etc.
While OpenModelica[3] has come a long way towards making an open source implementation of the language standard, proprietary applications (Dymola) still have an edge in the industry.
[1]: https://fmi-standard.org/
[1] https://juliahub.com/products/juliasim
Still not comparable with Modelica that has proper specification, including graphical representation of models.
Modelica is acausal. You define the variables and how they are related (equations). The compiler handles variable dependencies and resolution internally.
There are pros & cons of each. Both are used for simulating cyber-physical systems.
So what is really the meaning of "acausal" in this context?
This makes modeling much easier because causality doesn't compose. Adding a new component to a model can totally change what the optimal causalization is, so using an acausal framework the compiler will figure that out, but in a causal framework the user has to re-derive the causality of their model.
ETA: Apparently MathWorks has Simscape in this category.
The language spec is open source but there many commercial compilers, Dymola is the most popular.
I code in this language extensively and its acausal nature is extremely powerful. It makes your models highly composable, you can basically assemble a mechanical system like a bunch of lego blocks and the equations fall out automatically. You can also easily invert your models.
The closest analogy in the programming world is Haskell.
I feel like acausal modeling environments are also much like symbolic computer algebra systems (because they are basically applied CAS...)
Think of it like a solver for many coupled differential equations. The coupling happens through "linear" equality constraints. Such as "the output pressure variable of component A needs to be equal the input pressure variable of component B".
Something that Modelica doesn't do very well is stochastic systems. There you would need to go into SDE and that brings a lot of technicalities.
[1] Petzold, Linda R. Description of DASSL: a differential/algebraic system solver. No. SAND-82-8637; Sandia National Labs, 1982.
[2] Kunkel, Peter. Differential-algebraic equations: analysis and numerical solution. European Mathematical Society, 2006.
30 seconds of clicking around and I've failed to find sth compelling.
https://mbe.modelica.university/behavior/equations/electrica...
I don't know much beyond this.
There are several other tools in this space, not the least prevalent of which is Mathworks' Simscape product line [1]. Wolfram has a solution that is also very similar to Modelica [2]. Finally, I believe that you will find ModelingToolkit.jl [3] worth a look (along with Julia in general) if your interest is piqued here by Modelica. I believe MTK is a big slice of the future of acausal modeling tools.
[1] https://www.mathworks.com/products/simscape.html
[2] https://www.wolfram.com/system-modeler/
[3] https://docs.sciml.ai/ModelingToolkit/stable/examples/spring...
https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&d...
Would you recommend the second to introduce a colleague to OpenModelica? He is into gPROMS but will lose access to the software at retirement.
Sidenote: invest in software with freedom to operate and good knowledge reusability.
> Let us consider an extremely simple differential equation:
x = (1-X)
Looking at this equation, we see there is only one variable, x
This equation can be represented in Modelica as follows:
model FirstOrder
Real x;
equation
der(x) = 1-x;
end FirstOrder;
This code starts with the keyword model which is used to indicate the start of the model definition.The model keyword is followed by the model name, FirstOrder. This, in turn, is followed by a declaration of all the variables we are interested in.
[et cetera]
Took me under 5 seconds to find.
There’s not a single example on the landing page. There’s not a single example on any of the main links from the landing page.
But the landing page does have a merch store so you can buy mugs and hoodies and look at huge company logos of “organizational members”, whatever that is.
You made something. Show me what you made. I’m probably not going to spend more than 30 seconds trying to figure it out unless I already know I need it.
Why make it so difficult?
https://mbe.modelica.university/behavior/equations/first_ord...
or PCI-SIG:
Or maybe the design language is too modern...did you mistake it for a startup project rather than a 25+ year old industry standard?
It's interesting to contemplate the idea that being too modern might have a detrimental effect on perception and expectation. How would you have reacted to the site design circa 2018? http://web.archive.org/web/20180214180117/https://www.modeli...
It is for people simulating complex multi-disciplinary systems, who need a way to describe, bundle and exchange the behavior of a system.
Literally the first sentence.
The Modelica library is quite mature and complete and the numerical solvers included with OpenModelica robust and performing.
It looks me a while to learn it but now it is paying out.
In addition the fact that Modelica is a standard implemented by several suppliers with an open source application is also great to avoid vendor lock in so that is a technology on which is safe to invest as an engineer and as a company.
[1] https://openmodelica.org/doc/OpenModelicaUsersGuide/v1.11.0/...
Can any expert in this field give me some pointers. My current belief is that understanding the theory of bond graphs will give me intuitive understanding of just about every system I work with in my field.
I find that this field is surprisingly niche, as most of my peers have never paid special attention to it, but when I found about it, it seemed to me like a magic bullet for all my problems.
[1] https://dirac.ruc.dk/~heine/paynter/analysis_and_design_of_e...
I'm sure Bond Graph fans will disagree. I am just sharing my personal, subjective opinion here.
[1]: https://www.linkedin.com/feed/update/urn:li:ugcPost:72725163... [2]: https://mbe.modelica.university/components/connectors/simple...
Until I discovered Hamiltonian physics :)
All about Modelica: An equation-based language for modeling physical systems - https://news.ycombinator.com/item?id=23690788 - June 2020 (44 comments)
Modelica - https://news.ycombinator.com/item?id=16013179 - Dec 2017 (12 comments)
This is very nice, especially if it is general enough.
However, what is even more interesting is the general methodology to actually run the time-based simulation of such a system, especially if this allows to describe arbitrarily complex diffeqs (other than brute force monte-carlo style integration/sampling).
https://docs.sciml.ai/ModelingToolkit/dev/
There is also a project by Hilding Elmqvist, who worked for Dassault on Dymola (the leading commercial implementation of Modelica). His project is Modia.jl:
https://github.com/ModiaSim/Modia.jl
I can personally feel the Julia community settling on MTK, but Modia was ahead in the early stages of dynamic system simulation in Julia, and I believe MTK has drawn a lot of inspiration from each Modia and Modelica. Modia is a bit more ergonomic while also being the first to integrating things like 3D viewers and a complete multibody package by years, with Julia Computing only now catching up [1]. MTK has a better support for back-end solvers and holds a lot of promise to leapfrog Modia, especially since the release cadence for Modia seems to have slowed.
Elmqvist, H. (1978). A Structured Model Language for Large Continuous Systems. [Doctoral Thesis (monograph), Department of Automatic Control]. Department of Automatic Control, Lund Institute of Technology (LTH).
portal: https://portal.research.lu.se/en/publications/a-structured-m...
direct pdf link: https://lucris.lub.lu.se/ws/portalfiles/portal/4602422/85704...
I haven't used the free Version though.
model lorenz_attractor:
state:
x real = 1
y real = 1
z real = 1
equations:
x' = sigma * (y - x) / s
y' = (x * (rho - z) - y) / s
z' = (x * y - beta * z) / s
update:
integrate_odes()
parameters:
sigma real = 10
beta real = 8/3
rho real = 28
For events, there are constructs like "onReceive(in_port_name)" and "emit_spike()" (nomenclature there being clearly somewhat influenced from our neuroscience application domain).It's still a work in progress, but we already have some cool applications, like a spiking neural network that learns and then replays sequences (https://nestml.readthedocs.io/en/latest/tutorials/sequence_l...).
I was frankly surprised that something like this did not already exist when I started development on NESTML. Modelica is similar, but does not seem to have support for discrete events. I realise all of this is a shameless plug ;) but in actuality we are of course very happy to receive comments and feedback! All development is out in the open on GitHub and it is GPL licensed. If someone knows of similar DSLs, I would be very happy to read your comments. Cheers!
https://specification.modelica.org/maint/3.6/synchronous-lan...
In the docs you say > Currently, there is support for GSL, forward Euler, and exact integration
Does 'GSL' refer to GNU Scientific Library, which you use as a backend?
In short, Modelica is better for simulation well-specified physical systems. System Dynamics is good for simulating the approximate behaviour of larger, often poorly understood systems.
https://doc.modelica.org/Modelica%204.0.0/Resources/helpDymo...
Are NEMA motors modeled? Could one use this to simulate/model a CNC machine?
EDIT:
Apparently not, given that "NEMA" doesn't show up searching:
https://ie.utcluj.ro/files/acta/2003/Number%202/Paper08_Mora...
Operationalizing this into a Modelica model is pretty straightforward. Did not look for a driver model, but a driver model would be a lot more important in modern times. I am guessing that not many people would simulate this fidelity of stepping dynamics as a "first cut", and anyone who does is likely to also have a detailed driver model. Demand for this model in a standard library is likely quite small.
NEMA isn't a type of motor, it's a connection standard used for motors. You might be able to model stepper motors with some of the components in the modelica standard library for magnetic modeling.
Turns out "stepper" would probably have been better:
https://mbe.modelica.university/components/components/rot_co...
There is not a single third party way to generate a Model Exchange FMU for Linux, using anything but the very badly designed C reference FMUs or some commercial project. Even Matlab has poor support for it.
It is, perhaps, only a few toolboxes away (Simulink Test, Simulink Coverage, Simulink Coder).
Any idea if that's the case?
In Acausal systems like modelica, it does not matter whether you write V=IR or any alternate formulation. You simply provide the description and it is up to the compiler to obtain the relevant set of equations that can be fed into the ODE solver. The advantage being that you spend less time fitting your problem into the causal framework required by Simulink or alternates where, when you change your model, you end up doing significant rewrites.
TL;DR: Causal descriptions take more time to arrange as compared to Acausal.
In which case, isn't comparing it to Simulink instead of Sympy a bit ... odd?
It's like coming up with a Functional language, and choosing to compare it to Java instead of Haskell.
In any case, interesting language. It would be interesting to see a proper comparison against sympy / symbolic.
(I spotted this, but it's not really a comparison https://pmc.ncbi.nlm.nih.gov/articles/PMC7924524/ )
No, as it is not used as Sympy. Modelica is used to model physical systems in a composable way with graphical representation of connections between components. Many people use Simulink for that, so this is why it is compared with it. Sympy is a nice project, but is applied in a different context, definitely not to model systems with thousands of equations in the industrial setting (that would be painful).
https://github.com/pymoca/pymoca
It targets CasADi (and therefore C) and to a lesser extent, SymPy.
Modelica allows you to create models in a similar way to spice by describing components and how they are connected. But instead of a netlist of nodes, Modelica has a concept of connectors. But otherwise it is fairly similar. However, Modelica's scope is well beyond that of Spice or Verilog-A. Those languages claim that by analogy they can do other domains but you are mainly limited to simple "equivalent" circuit models for thermal systems and their analogies to mechanical systems are (in my opinion), hopelessly flawed. Not to mention that there are high quality libraries in Modelica for modeling multibody systems and two phase fluid systems...things I would never attempt in Spice or Verilog-A.
As for Simulink...that is an entirely different beast. Simulink's focus is on modeling of dynamics using a representation of the _math_, not the physics. What this means in practice is that when you build a model you translate the text book equations into a _causalized_ mathematical representation of your system. The problem with this is that changing even very basic assumptions will require you to re-causalize the system of equations and this is quite tedious, time-consuming and error prone. The way I always describe it is that Simulink is like performing long division (you do all the tedious, time consuming and error prone work yourself) whereas Modelica is like a calculator (it does all that stuff for you and just helps you get to the answer quickly). But the key point about Modelica is that it leverages a compiler that does a ton of work for you (not just causlization but state selection, index reduction, etc). Now MathWorks has Simscape that supports this "acausal" approach that Modelica uses, but in my opinion Modelica is not only technically better and more powerful, but also more open (see OpenModelica, for example).
Do you know if there's a decent C autocoding solution like that of Simulink?
But it sounds like you might be talking about autocoding of an embedded controller. In that case, I'm not aware of any tools with that target. Part of this is because Modelica can be used to model the controls, the plant or both. But for autocoding you'd need a clear partitioning and some way of connecting the controls to a scheduler. But I don't know of a Modelica tool that supports this. My hope is that the next generation of tools will address this (that's part of my day job ;-)).
And yes, I'm talking about autocoding of an embedded controller. Which also means the C code has to be ready for that (hard real time, carefully controlled calls to libraries, no heap allocation, etc.)
Right now Simulink embedded coder can be (and is) used to generate production code for all sorts of aerospace vehicles (rockets/spacecraft/drones/etc). The moment any Modelica tool can be used like this I'll take a very serious look.
This renews my interest in Modelica...
As for C code, we're working on it through JuliaSim. JuliaSim's modeling language is very similar to Modelica in some aspects (Mike Tiller who is in this thread and the author of many of the main Modelica teaching tools is also one of the creators of this language), though there were some breaks which were required in order to make better downstream integrations with the Julia stack and in order to modernize development workflows (integrations with package management, some new langauge features, etc.). Part of what we're trying to do is solve exactly where Modelica got stuck: embedded code generation and other "non-GUI workflows" (CI/CD for example) and making those first-class integrated with the declarative modeling language. There's still a good amount of work to do but we've already started trickling out some results in this direction and running workshops on the tools at Modelica conferences.
Here’s the GitHub repo: https://github.com/marco-compiler/marco
And here’s a link to the latest published results: https://ecp.ep.liu.se/index.php/modelica/article/view/909
Would love to hear your thoughts or ideas!