1

I'm using python fmpy to run a fmu model. Executing, running and plotting results works fine. Defining inputs as well (following the tutorials).

But I'm struggling with changing the value of global parameter within the fmu model.

For example, if I run the same model in the GUI (https://fmpy.readthedocs.io/en/latest/tutorial/), I have the full list of all parameters available. There I can set the value for a parameter with the name "l1" to 412. If I log the FMI calls of the GUI, I can see, that the following command was used to set "l1" to the value of 412:

fmi2SetReal(fmu, vr=[671088641], nvr=1, value=[412])

How can I set "l1" to 412 with a python command?

from fmpy.util import plot_result
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from fmpy import *

fmu = 'CrankCase_ME2.fmu'
dump(fmu)

# read the model description
model_description = read_model_description(fmu)

# collect the value references
vrs = {}
for variable in model_description.modelVariables:
    vrs[variable.name] = variable.valueReference

L = 412
hPLug = 215
alphaDrvie = 60
start_vrs = [vrs['l1'], vrs['h_plug'], vrs['Alpha_drive']]
start_values = [L, hPLug, alphaDrvie]

dtype = [('time', np.double), ('l1', np.int), ('h_plug', np.int), ('Alpha_drive', np.int)]
signals = np.array([(0.0, L, hPLug, alphaDrvie)], dtype=dtype)

print_interval = 1e-5
result = simulate_fmu(fmu, start_time=0, stop_time=0.1, relative_tolerance=1e-7, output_interval=print_interval, input=signals)

t = result['time']
y_Force = result['expseu_.Out2'] # y force
plt.plot(t, y_Force)

My fmu is of type Model Exchange. I tried to define this as an input but this doesn’t work.

Any suggestions?

Cheers

Zor Ro
  • 76
  • 6

1 Answers1

1

So I just figured it out...

To define a global parameter within fmpy the "start_values" statement is used. There is a nice example in coupled_clutches.py. But for the sake of completeness here a minimal working example for the 'Rectifier.fmu' model. 'vrs' contains all saved variables in the fmu model and there you can find the frequency 'f'. The default value was set to 50Hz and if the frequency parameter should be set to 100Hz just use 'start_values={'f': 100}' like:

from fmpy import dump, simulate_fmu, read_model_description
from fmpy.util import plot_result

fmu = 'Rectifier.fmu'
dump(fmu)

model_description = read_model_description(fmu)

vrs = {}
for variable in model_description.modelVariables:
    vrs[variable.name] = variable.valueReference

print_interval = 1e-5
result = simulate_fmu(fmu, 
                      start_time=0, 
                      stop_time=0.1, 
                      relative_tolerance=1e-7, 
                      output_interval=print_interval, 
                      start_values={'f': 100})

plot_result(result)

If you want to explore the saved parameters visually you can open the fmu within the FMPy GUI

Zor Ro
  • 76
  • 6