3. Loading models

Compiled models, JMUs, are loaded in the JModelica.org Python interface with the JMUModel class from the jmodelica.jmi module. This will be demonstrated in Section 3.2. The JMUModel class contains many methods with which the model can be manipulated once it has been instantiated. Amongst the most important methods are the initialize, simulate and optimize methods. These are explained in Chapter 7 and Chapter 8. The more basic JMUModel methods for variable and parameter manipulation are explained in Section 4.

3.1. The JMU file

The JMU file is a compressed file which contains all files needed to load and work with the compiled model in JModelica.org. The JMU contains the shared object file, the XML files with model variable and parameter data and some other files created during compilation of the model. The JMU file format is a JModelica.org specific format but is designed to follow the FMU file format from the FMI standard as much as possible. The JMU file is created when compiling with jmodelica.jmi.compile_jmu, see Section 2.

3.2. Loading a JMU

A JMU file is loaded in JModelica.org with the class JMUModel in the jmodelica.jmi module. The following simple example demonstrates how to do this in a Python shell or script.

# import JMUModel from jmodelica.jmi
from jmodelica.jmi import JMUModel
myModel = JMUModel('myPackage_myModel.jmu')

The only parameter in the JMUModel constructor is the name of the JMU file, including any file path. When compiling and loading it is therefore practical to use the return argument from compile_jmu, which is the path to the JMU created. The following example demonstrates this.

# import compile_jmu and JMUModel
from jmodelica.jmi import compile_jmu
from jmodelica.jmi import JMUModel

# compile and load model
jmu_name = compile_jmu('myPackage.myModel','myPackage.mo')
myModel = JMUModel(jmu_name)

3.3. Loading an FMU

An FMU (Functional Mock-up Unit) is a compressed file which follows the FMI (Functional Mock-up Interface) standard. The FMU file can be loaded in JModelica.org with the class FMUModel in the jmodelica.fmi module. The following short example demonstrates how to do this in a Python shell or script.

# import FMUModel from jmodelica.fmi
from jmodelica.fmi import FMUModel
myModel = FMUModel('myFMU.fmu')

The FMUModel instance can then be used to set parameters and used for simulations. Read more about the FMI and FMU in Chapter 5. How to simulate an FMU is described in Chapter 7.