Mhamad Mahdi Alloush
Mhamad Mahdi Alloush

Reputation: 11

OpenModelica generated C-code: I need to make parametrised runs on different machines

I have a simple model on OpenModelica. I generated a C-code (using export FMU functionality) of the model and I want to adjust the code so as to make a parametrised study of a parameter and print the output. In other words, I want that when I run the executable, to pass an argument to the executable which contains the value of a certain parameter, and print the value of some output of the model in a file.

Is that possible?

Upvotes: 1

Views: 766

Answers (1)

AnHeuermann
AnHeuermann

Reputation: 848

Yes that is possible. You mentioned FMU export, so I'll stick to this and it is probably the easiest way.

The binaries inside a FMU are compiled libraries containing all internal runtimes and stuff to simulate your exported model. These libraries are usually handled by an FMU importing tool, e.g. OMSimulator (part of OpenModelica) or FMPy.

I wouldn't recomment changing the generated C code directly. It is difficult and you don't need to do it.

In your FMU import tool you can usually change values of varaibles and parameters via the fmi2SetXXX functions before runing a simulation. Most tools wrap that interface call into some function to change values. Have a look at the documentation of your tool.


So let's look at an example. I'm using OpenModelica to export helloWorld and OMSimulator from Python, but check your tool and use what you prefere.

model helloWorld
  parameter Real a = 1;
  Real x(start=1,fixed=true);
equation
 der(x) = a*x;
end helloWorld

Export as 2.0 Model Exchange FMU in OMEdit with File->Export->FMU or right-click on your model and Export->FMU. You can find your export settings in Tools->Options->FMI.

Now I have helloWorld.fmu and can simulate it with OMSimulator. I want to change the value of parameter helloWorld.a to 2.0 and use setReal to do so.

>>> from OMSimulator import OMSimulator
>>> oms = OMSimulator()
>>> oms.newModel("helloWorld")
>>> oms.addSystem("helloWorld.root", oms.system_sc)
>>> oms.addSubModel("helloWorld.root.fmu", "helloWorld.fmu")
>>> oms.instantiate("helloWorld")
>>> oms.setReal("helloWorld.root.fmu.a", 2.0)
>>> oms.initialize("helloWorld")
info:    maximum step size for 'helloWorld.root': 0.001000
info:    Result file: helloWorld_res.mat (bufferSize=10)
>>> oms.simulate("helloWorld")
>>> oms.terminate("helloWorld")
info:    Final Statistics for 'helloWorld.root':
         NumSteps = 1001 NumRhsEvals  = 1002 NumLinSolvSetups = 51
         NumNonlinSolvIters = 1001 NumNonlinSolvConvFails = 0 NumErrTestFails = 0
>>> oms.delete("helloWorld")

Now add a loop to this to iterate over different values of your parameter and do whatever post-processing you want.

Upvotes: 1

Related Questions