I know it's possible to do parameter studies by using python dymola interface. However this assume that there's already an existing model. What I'm specificly interested into is to write modelica equations in python. But I didn't find any documents about this. Does anyone has some feedbac? Thanks!!
2 Answers
As matth has pointed out, the cleanest way would probably be to use a template engine. But to get started, simple python string formatting will also do it.
However, the problem is more or less complex, depending if you want to update parts of existing models or just create new models from scratch.
Creating new models
Here is an example how a simple Modelica model can be created from your parameters, variables and equations if they are already available in Python:
name = 'myModel'
parameters = ['Real x=3', 'Boolean a=false']
variables = ['Real y', 'Boolean b']
equations = ['der(y) = x*time', 'b=not a']
template = """
model {name}
{parameters}
{variables}
equation
{equations}
end {name};"""
model = template.format(name=name,
parameters='parameter '+';\n parameter '.join(parameters)+';' if parameters else '',
variables=';\n '.join(variables)+';' if variables else '',
equations=';\n '.join(equations)+';' if equations else '')
If needed, constants could be handled the same way as parameters.
Updating existing models
If parts of an existing model should be updated it gets more complex, since you have to parse the Modelica code and make sure that you correctly identify the parameters, variable, equations, etc.
If you only want to do that with certain models which were designed to have e.g. updated equations it gets simpler. You could mark the start and the end of the equation sections to easily identify them and insert the new equations. So you would e.g. have a model like this:
model myModel
equation
// #equationSectionStart
...
// #equationSectionEnd
end myModel;
and then use regular expressions to replace the content between the tags.
You also have to read and write the model somehow, so there are some hints below.
Reading the model in Python
You can either read the content of the .mo file or use the python interface and get the code of a loaded Modelica class from Dymola with
getClassText(fullName, includeAnnotations=True, formatted=False)
where fullName is the full path to the Modelica class in Dymolas package browser.
Writing models
Dymola does not offer anything to update parts of a model, but there is a command which creates new classes or overwrites the entire class text if the class already exists:
setClassText(parentName, fullText)
parentName
is the path to the parent class, which is usually a package (or an empty string if the class shall be on top level) and the new class text will be fullText
.
As for reading, you can also operate on file basis and just create a .mo file and load that in Dymola.
Note for non-python developers
The functions setClassText
and getClassText
are also available for the java interface and inside Dymola in the DymolaCommands
library.
-
I didn't know about DymolaCommands built-in functions. Thanks a lot this is really helpful. Although after reading the dymola documentation, I still can't see clearly how to use getClassText in Python. Besides, what I'm trying to do in python is build a model with parameters, variables and equation section. The information about variables, classes and such stuffs are already collected in a data base that is read into python. Would you give me some more hints if you could? Thanks!! – Yulu Dong Oct 20 '17 at 11:54
-
`getClassText` takes the class path of an already loaded class as first argument (_fullName_). So if you want to get the code of e.g. Modelica.Blocks.Continuous.Integrator, just pass it as string to the function. – marco Nov 02 '17 at 12:13
-
Regarding your question about how to create a model from data base information: I have updated the answer with a minimal example for the creation of a new model. – marco Nov 02 '17 at 12:59
The documentation for the Dymola Python interface is on your local computer, for Windows users it is here (just copy the URL to your webbrowser):
file:///C:/Program Files (x86)/Dymola 2018/Modelica/Library/python_interface/doc/index.html
Scanning it quickly I could not see any function to e.g. create a model from a string. I thought I had seen such a function in the OpenModelica scripting documentation, but I cannot find it anymore.
But you can of course always write that string to a .mo file and then simulate that file. Maybe using a templating engine like Mako or Jinja2 is an interesting option for you here? You would have to write a template of a model that just replaces few variables. Or use the f-strings introduced with Python 3.6 to do the same.
Building on top of that, you could also write your own object-oriented Python classes for a model with parameters, attributes, methods, constructor and so on. And an abstraction layer so that the model can be simulated in Dymola, OpenModelica, SimulationX or as FMU.
Or maybe one of the Modelica-related packages on PyPI can do what you need?

- 2,568
- 2
- 21
- 41
-
There are very detailed ways of doing it and they seem promising! Thanks a lot! – Yulu Dong Oct 09 '17 at 12:04
-
Hey Matth, I've got some progresses on the .mo file which I import later to Modelica enviroment. But I'm not sure understand the role of templating engine mentionned in your anwser. Would you mind to explain a little bit more how to use the emplating engine? Thanks a lot!! – Yulu Dong Oct 20 '17 at 12:12
-
A templating engine is a software that takes a file (the template), scans it and replaces some variables therein with a value taken e.g. from a database or json file or similar. You can start by writign your template from an existing and working mo file, then make some parts of it variable. That technique is used very frequently for generating HTML, but it works for any text file. – matth Oct 20 '17 at 19:18