3. Loading models

Compiled models, FMUs, are loaded in the JModelica.org Python interface with the FMUModel(ME/CS) class from the pyfmi module, 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.2, “Loading an FMU”, ??? and Section 3.3, “Transferring an OptimizationProblem”.

The model classes contain many methods with which models can be manipulated after instantiation. Amongst the most important methods are initialize and simulate, which are used when simulating. 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 export are CVode and Explicit Euler.

3.2. Loading an FMU

An 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.3. 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 an FMU. 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)