4. Variable and parameter manipulation

Model variables and parameters can be manipulated with methods in the model classes once the model has been loaded. Some short examples in Section 4.2 will demonstrate this. Model variable meta data and parameter values are saved in XML files which are generated during compilation, these are briefly explained in Section 4.1. The XML file containing the parameters can be used to save different sets of parameters for one model, see Section 4.3.

4.1. Model variable XML files

The model variable meta data and parameter values are saved in XML files which are generated during the compilation. They follow the name convention:

  • modelDescription.xml

  • <model_class_name>_values.xml

The variable meta data is saved in modelDescription.xml and the parameter values in <model_class_name>_values.xml. Note that the parameter values file is JModelica.org specific and is not a part of the FMI standard and it is not part of the FMUX either. The name of the parameter is used to map a parameter value in the XML values file to a parameter specification in the XML variables file.

4.2. Setting and getting variables

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

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

# Compile and load the model
from pymodelica import compile_jmu
from pyjmi import JMUModel
jmu_name = compile_jmu('RLC_Circuit','RLC_Circuit.mo')
rlc_circuit = JMUModel(jmu_name)

# Get the value of the variable 'resistor.R'
resistor_r = rlc_circuit.get('resistor.R')
resistor_r
>> 1.0

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

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

# Create a list of variables and values
vars = ['resistor.R', 'resistor.v', 'capacitor.C', 'capacitor.v']
values = rlc_circuit.get(vars)
values
>> [2.0, 0.0, 1.0, 0.0]

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

4.3. Loading and saving parameters

This section is only relevant for the JMUModel.

4.3.1. Loading XML values file

It is possible to (re)load the parameter values from an XML file as is done automatically when the pyjmi.JMUModel object was first created. If, for example, there were many local changes to parameters it could be desirable to reset everything as it was from the beginning. The following example shows how reloading the parameter values from the XML file resets the parameters in the model. The model is taken from the pyjmi.examples package and is assumed to be compiled and loaded.

# Look at parameters 'resistor.R' and 'sine.offset'
rlc_circuit.get('resistor.R)
>> 1.0
rlc_circuit.get('sine.offset')
>> 0.0

# Change them
rlc_circuit.set('resistor.R', 2.0)
rlc_circuit.set('sine.offset', 0.5)

# Look at them again
rlc_circuit.get('resistor.R)
>> 2.0
rlc_circuit.get('sine.offset')
>> 0.5

# Reset them by loading the original XML values file
rlc_circuit.load_parameters_from_XML()

# 'resistor.R' and 'sine.offset' have now been reset
rlc_circuit.get('resistor.R)
>> 1.0
rlc_circuit.get('sine.offset')
>> 0.0

The default behaviour is to load the same file as was created during compilation. If another file should be used this must be passed as an argument to the method.

# Load other XML file
rlc_circuit.load_parameters_from_XML('new_values.xml')

4.3.2. Writing to XML values file

Setting a parameter value with JMUModel.set only changes the value in the vector loaded when pyjmi.JMUModel was created, which means that it will not be saved when the model is discarded. To save all local changes made to the model parameters, the values have to be written to the XML values file.

# Set a parameter
rlc_circuit.set('inductor.L', 1.5)

# Save parameters to the XML values file
rlc_circuit.write_parameters_to_XML()

# Load the XML values file once again and see that the changed parameter was saved in 
# the XML file
rlc_circuit.load_parameters_from_XML()
rlc_circuit.get('inductor.L')
>> 1.5

If write_parameters_to_XML() is called without arguments the values will be written to the XML values file in the JMU which was created when the model was compiled (following the name conventions mentioned above). It is also possible to save the changes in a new XML file. This is quite convenient since different parameter value settings can then easily be saved and reloaded in the model.

# Save to specific XML file
rlc_circuit.write_parameters_to_XML('test_values.xml')