4. Changing model parameters

Model parameters can be altered with methods in the model classes once the model has been loaded. Some short examples in Section 4.1, “Setting and getting parameters” will demonstrate this.

4.1. Setting and getting parameters

The model parameters can be accessed with via the model class interfaces. It is possible to set and get one specific parameter at a time or a whole list of parameters.

The following code example demonstrates how to get and set a specific parameter using an example FMU model from the pyjmi.examples package.

# Compile and load the model
from pymodelica import compile_fmu
from pyfmi import load_fmu
my_fmu = compile_fmu('RLC_Circuit','RLC_Circuit.mo')
rlc_circuit = load_fmu(my_fmu)

# Get the value of the parameter 'resistor.R' and save the result in a variable 'resistor_r'
resistor_r = rlc_circuit.get('resistor.R')

# Give 'resistor.R' a new value
resistor_r = 2.0
rlc_circuit.set('resistor.R', resistor_r)

The following example demonstrates how to get and set a list of parameters using the same example model as above. The model is assumed to already be compiled and loaded.

# Create a list of parameters, get and save the corresponding values in a variable 'values'
vars = ['resistor.R', 'resistor.v', 'capacitor.C', 'capacitor.v']
values = rlc_circuit.get(vars)

# Change some of the values
values[0] = 3.0
values[3] = 1.0
rlc_circuit.set(vars, values)