3. Loading models

Compiled models, FMUs, JMUs, are loaded in the JModelica.org Python interface with the FMUModel(ME/CS) class from the pyfmi module, and the JMUModel class from the pyjmi module respectively, while optimization problems for the CasADi-based optimization are transferred directly into the OptimizationProblem class from the pyjmi module . This will be demonstrated in Section 3.3, “Loading an FMU”, Section 3.4, “Loading a JMU” and Section 3.5, “Transferring an OptimizationProblem”.

The model classes contain many methods with which models can be manipulated after instantiation. Amongst the most important methods are initialize, simulate and optimize. (optimize is only relevant for JMU model instances) which are used when simulating and optimizing. These are explained in Chapter 5, Simulation of FMUs and Chapter 6, Optimization. For more information on how to use the OptimizationProblem for optimization purposes, see Chapter 6, Optimization. The more basic methods for variable and parameter manipulation are explained in Section 4, “Changing model parameters”.

3.1. The FMU

The FMU (Functional Mock-up Unit) is a compressed file which follows the FMI (Functional Mock-up Interface) standard. An FMU is created when compiling a Modelica model with pymodelica.compile_fmu.

There are two types of FMUs, Model Exchange and Co-Simulation. In a Co-Simulation FMU, the integrator for solving the system is contained in the model while in an Model Exchange FMU, an external integrator is needed to solve the system. JModelica.org supports export and import of FMU-ME and FMU-CS version 1.0 and 2.0. The solvers supported for FMU-CS are CVode and Explicit Euler.

3.2. The JMU

The JMU 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, an XML file with model variable 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. A JMU is created when compiling a Modelica or Optimica model with pymodelica.compile_jmu, see Section 2, “Compilation”.

3.3. Loading an FMU

The FMU file can be loaded in JModelica.org with the method load_fmu in the pyfmi module. The following short example demonstrates how to do this in a Python shell or script.

# Import load_fmu from pyfmi
from pyfmi import load_fmu
myModel = load_fmu('myFMU.fmu')

load_fmu returns a class instance of the appropriate FMU type which then can be used to set parameters and used for simulations.

3.4. Loading a JMU

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

# Import JMUModel from pyjmi
from pyjmi import JMUModel
myModel = JMUModel('myPackage_myModel.jmu')

3.5. Transferring an OptimizationProblem

An optimization problem can be transferred directly from the compiler in JModelica.org into the class OptimizationProblem in the pyjmi module. The transfer is similar to the combined steps of compiling and then loading and FMU or JMU. The following short example demonstrates how to do this in a Python shell or script.

# Import transfer_optimization_problem
from pyjmi import transfer_optimization_problem

# Specify Modelica model and model file
model_name = 'myPackage.myModel'
mo_file = 'myModelFile.mo'

# Compile the model, return argument is an OptimizationProblem
myModel = transfer_optimization_problem(model_name, mo_file)