Modelica Compiler with Sympy Model Output

357 views
Skip to first unread message

James Goppert

unread,
Apr 26, 2016, 6:02:55 PM4/26/16
to sympy
I'm working on a Modelica compiler written in pure python that outputs a sympy model. Please let me know if anyone is interested in contributing.

Please see the jupyter notebook below for a quick overview of the current capabilities:


The useful thing about the modelica language is that it can all be mapped into one differential algebraic equation, that can be represented in certain cases well in sympy.

This allows one model in Modelica to be used to generate:
1. Sympy models for mathematical analysis/ simulation.
2. C/C++ code for embedded controllers/ estimators (to be done).
3. Whatever else you can dream up.

The compiler is very hack-able. I'm using jinja 2 for rendering the AST.
I'm  using antlr4 python runtime to robustly construct the AST.

I really need some good python programmers to help me get to the point that the compiler can handle more language constructs and parse the entire Modelica standard library.
This will enable use of many pre-written modelica models that handle multi-body mechanics, control blocks, and many other. https://github.com/modelica/Modelica

Feel free to contact me if you are interested:

Jason Moore

unread,
Apr 26, 2016, 6:22:27 PM4/26/16
to sy...@googlegroups.com
This is cool stuff. I'd always imagined outputting modelica models from sympy instead of the other way around. Aren't there already a lot of software packages that take modelica models as input for various numerical analyses purposes? For multi-body dynamics models, constructing the model and generating the correct differential algebraic equations is the hard part. It'd be nice to pass these off to modelica based tools. Doesn't JModelica offer all the simulation side of things? I thought it generated code from modelica models and is Python based.

What is the use case for converting a pre-generated modelica model into sympy symbolics? Why not generate code, your #2, directly from modelica model code?

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/f7c7d0e7-7d87-4ab4-8135-25d94e3664f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Goppert

unread,
Apr 26, 2016, 9:56:39 PM4/26/16
to sympy
Hi Jason,

Thanks for you interest:

I think Modelica is a nicer format for the core mathematical model of dynamic systems since it is made for mathematical modeling of dynamic systems. With the object oriented nature/ inheritance etc. you can describe very complicated systems with Modelica, which would look pretty ugly in sympy. Also, you are guaranteed from the Modelica language that this all maps back into a clean differential algebraic equation, unlike creating a model in python/ c++ etc. My motivation is mathematical modelling multi-rotors/ planes etc. I am very familiar with the sympy mechanics toolbox as well, but I think the Modelica approach also has advantages over that due to easy component reuse etc/ classes etc as mentioned above.

I have tried to use JModelica, but I am on Ubuntu linux and despite my experience have failed to get everything running each time. Also, OpenModelica exists, but their compiler for OpenModelica is actually their core research and I find the compiler very hard to hack, in addition to the fact that their license isn't very friendly. I'm releasing this as BSD and you can get it running with just "pip install pymola". Which is 10x easier than any other Modelica compiler since it is pure python.

The use case for generating the sympy model from the Modelica model is that you can use sympy to convert it into state space format, solve the ode etc, and then linearize the model etc. analytically. I'm a developer of the PX4 autopilot, and I hate doing a bunch of Jacobians for estimators by hand. I added an example of going from a Modelica model to a bode plot via sympy  on the spring notebook.

For code generation I do plan to just use my AST and jinja2. Sympy is just for the symbolic math.

-James

James Goppert

unread,
Apr 26, 2016, 10:00:18 PM4/26/16
to sympy
You could compare my work here to the wolfram system modeler. If you think of wolfram mathematica = sympy, then pymola = system modeller: http://www.wolfram.com/system-modeler/

They have the same kind of modelica -> wolframe equations export, so I think the use case is valid and most people will find it useful.

Jason Moore

unread,
Apr 26, 2016, 11:24:52 PM4/26/16
to sy...@googlegroups.com
> The use case for generating the sympy model from the Modelica model is that you can use sympy to convert it into state space format, solve the ode etc, and then linearize the model etc. analytically. I'm a developer of the PX4 autopilot, and I hate doing a bunch of Jacobians for estimators by hand. I added an example of going from a Modelica model to a bode plot via sympy  on the spring notebook.

Ok, this makes sense. If the modelica libs don't provide ways to take derivatives I can see how this is useful. Thanks for the explanation.

James Goppert

unread,
Apr 27, 2016, 11:32:47 AM4/27/16
to sympy
Modelica is just a language. Some Modelica compilers do provide analytical jacobians of models, such as OpenModelica, but at this point, I'm using sympy to do that for me.

Denis Akhiyarov

unread,
Apr 27, 2016, 11:53:41 PM4/27/16
to sympy
I have mentioned pymola here before and was impressed with your work at that point:


My reading at that time showed that FMI is very closely related to Modelica.

But I learned about some limitations - poor support for PDEs in both Modelica and FMI.

I think the point of Modelica is building simple components on top of each other or using existing sets of components. 

Then connecting and simulating these blocks in UI such as System Modeler. FMI helps connected arbitrary external simulators.

For UI you may try integration with blockly.


On Wednesday, April 27, 2016 at 10:32:47 AM UTC-5, James Goppert wrote:
Modelica is just a language. Some Modelica compilers do provide analytical jacobians of models, such as OpenModelica, but at this point, I'm using sympy to do that for me.

On Tuesday, April 26, 2016 at 11:24:52 PM UTC-4, Jason Moore wrote:
> The use case for generating the sympy model from the Modelica model is that you can use sympy to convert it into state space format, solve the ode etc, and then linearize the model etc. analytically. I'm a developer of the PX4 autopilot, and I hate doing a bunch of Jacobians for estimators by hand. I added an example of going from a Modelica model to a bode plot via sympy  on the spring nefore brotebook.

Reply all
Reply to author
Forward
0 new messages