0

I would like to run alternative FMU models using python fmpy module.

I can run simple simulation withs default inputs and strating values ploting the various output variables defined in ouputs. However I can not find the structure of nd array which should allow alternate inputs. I found only coupled_clutches which is some sort of timeline like signal.

There are variables iVarX defined in FMU as inputs as well as oVarY variables as outputs here is my simple fmpy call

inputv = np.array([('iVar0', 28.7), 
                   ('iVar1', 51.6),
                   ('iVar2', 51.6),
                   ('iVar3', 37.2),
                   ('iVar4', 2920)]

outputs = ['oVar0',          
    'oVar1',          
    'oVar2',          
    'oVar3']


result = fmpy.simulate_fmu(filename,input=inputv, output=outputs)

If i do not use the input=inputv the simulation runs ok with default FMU defined input values. But running the code above leads to

...
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\fmpy\simulation.py:758 in simulate_fmu
    result = simulateCS(model_description, fmu, start_time, stop_time, relative_tolerance, start_values, apply_default_start_values, input, output, output_interval, timeout, step_finished, set_input_derivatives, use_event_mode, early_return_allowed, validate, initialize, terminate)

  File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\fmpy\simulation.py:1180 in simulateCS
    input = Input(fmu=fmu, modelDescription=model_description, signals=input_signals, set_input_derivatives=set_input_derivatives)

  File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\fmpy\simulation.py:219 in __init__
    self.t = signals[signals.dtype.names[0]]

TypeError: 'NoneType' object is not subscriptable

I tried to use a list of values in the input variable order,dataframe and array described above as a input. None of that actually works.

Would you advise the correct structure of the inputv variables array please?

Karkar
  • 46
  • 4

1 Answers1

1

You might have confused inputs with tunable parameters.

  • Inputs can change their value over time. Hence a time series must be provided.
  • Tunable parameters can be set by the user, but their value is fixed for the entire simulation.

Setting inputs

Inputs are set with time series. An easy way to do that is shown in the coupled_clutches example of the fmpy package.

There the time series of one input is defined in a csv file:

"time","inputs"
0.000000000000000000,0.000000000000000000
0.010000000000000000,0.000000000000000000
0.020000000000000000,0.000000000000000000
0.030000000000000000,0.000000000000000000
0.040000000000000000,0.000000000000000000
...

Then, the numpy array is created with:

input = np.genfromtxt('CoupledClutches_in.csv', delimiter=',', names=True)

This input can be passed to simulate_fmu.

For more inputs, add more columns to the file.

Setting tunable parameters

Tunable parameters are set at simulation start via start_value by passing a dictionary, like:

fmpy.simulate_fmu(fmu, start_values={'a': 1, 'b': 2})

Example

Here is a quick Demo to summarize everything.

We start with this Modelica model and export it with Dymola as Demo.fmu:

model Demo
  input Real x;
  input Real y;
  parameter Real k = 1;
  final parameter Real y0 = 1;
  output Real z = k*x + y + y0;
end Demo;

Then we create a csv file for the time trajectory of the inputs x and y:

"Time", "x", "y"
0.0, 0.1, 0
0.2, 0.1, 0
0.2, 0.3, 0
0.5, 0.3, 0
0.7, 0.3, 1
1.0, 0.3, 1

Now we can simulate our fmu with the following code:

from fmpy import simulate_fmu, plot_result
import numpy as np

fmu = "Demo.fmu"
input = np.genfromtxt('demo.csv', delimiter=',', names=True)
start_values = {'k': 2}

result = simulate_fmu(fmu, input=input, start_values=start_values)
plot_result(result)

Note that the parameter y0 is not tunable (due to the prefix final) and changing it gives an error:

> simulate_fmu(fmu, input=input, start_values={'y0': 3})
...
Exception: The start values for the following variables could not be set: y0
marco
  • 5,944
  • 10
  • 22