Modelica Series VI – Deployment and Integration
- Written by: Erik Åberg
Welcome to yet another chapter of our Modelica Series. In this tutorial, we'll take a closer look at how models developed in Modelica can be shared between tools and integrated into engineering workflows. You'll learn about thermal modeling and two companion technologies to Modelica, FMI and SSP.
If you've missed the previous chapters of this series, it might be a good idea to read them through before starting with this one. You can find all previous chapters here.
Energy utilization in buildings
During the fall and winter of 2021, energy prices rose rapidly in Europe due to a combination of factors, including shortages of gas and coal, limited transmission capacity, and cold weather. Once the initial shock over my electricity bill for December had subsided, I started doing some simple analysis of the temperature gradients in my house using Modelica, attempting to optimize the heat distributed by a poorly placed air-to-air heat pump. While this may seem overambitious and somewhat nerdy (!), it is not too complicated to model thermal structures in Modelica thanks to the Thermal.HeatTransfer package of the Modelica Standard Library. This package contains most of the base components, sensors, and sources required to model thermal networks. Using the HeatTransfer package, I managed to build a model of the rooms, wall structures, and heat sources of my house, giving me a way to experiment with different scenarios. Eventually this led to a better utilization of the heat pump, lower electricity bills, and a way to roughly predict the size of future electricity bills from weather forecasts.
In today's example, we'll implement a much-simplified version of my house model, consisting of a single thermal mass, heat transfer to ambient, and a heat source.
Building a building
Start by creating a new model in your Modelica tool. Go to the Diagram view and drag and drop two connectors from the Modelica.Thermal.HeatTransfer.Interfaces package, HeatPort_a and HeatPort_b, and a RealOutput connector from Modelica.Blocks.Interfaces. Call them outside, heater, and T. Next, drag and drop a ThermalConductor and a HeatCapacitor from Modelica.Thermal.HeatTransfer.Components, and a TemperatureSensor from Modelica.Thermal.HeatTransfer.Sensors. Name them wall, heatCapacitor, and temperatureSensor. The wall represents all heat transfer to the ambient through the walls, roof, floor, etc., and the heatCapacitor represents the total thermal mass of the room, including the walls, roof, and air.
Introduce parameters for the wall thermal conductance, G_wall = 200 W/K, and the room heat capacity, C_room = 1000 J/K. Use suitable units from the Modelica.Units.SI package. Anyone familiar with thermal modeling will notice that this heat capacity is far too small for a real room. Don't worry, we'll fix that later.
After connecting everything together, you should have something like this:
Lastly, finalize the model by giving it a fancy icon depicting a house. We can now use the model for various experiments by connecting it to different boundary conditions. Figure 2 shows such an experiment, where the heater supplies a constant heat flow rate of 2.5 kW and the outside temperature behaves like a sine wave varying between -5 °C and +5 °C over one day (86400 seconds).
The resulting indoor temperature is plotted together with the outdoor temperature in Figure 3.
While the plotted indoor temperature is a bit cool for my taste, the model seems to show reasonable behavior despite its simplicity. A good starting point! We can make it even more realistic by introducing more advanced wall structures, windows, and convection along surfaces. But for now, we’ll stick to this model. Let's share it with a colleague!
Sharing Modelica models
The open nature of the Modelica language makes it easy to share models among fellow Modelica users. In theory, models written in Modelica can be opened and simulated in any Modelica tool. While this works as intended in many cases, the reality is sometimes a bit more challenging for various reasons. Models may contain encrypted or licensed code, different tools have different interpretations of the language specification, solvers work differently, etc. To circumvent these issues, we need to make sure that the colleague we’re sharing the Modelica model with uses a Modelica configuration in which the model has been tested. If you do that and adopt well-thought-out processes for developing and testing code, you’ll find that sharing models with other Modelica users is a piece of cake.
But what if we want to share models with someone who doesn’t use Modelica?
Using the Functional Mock-Up Interface (FMI)
In our house example, we've declared two parameters, G_wall and C_room, that represent the thermal conductance of the walls and the heat capacity of the room. The parameter values depend on which house we are mimicking with our model. It is possible to calculate these values based on the geometry and material data of the house. However, for the benefit of this example, we’ll perform some parameter studies in Python instead. To transition our model from Modelica to Python, we’ll use something called the Functional Mock-Up Interface (FMI).
FMI is an open standard that allows users to exchange dynamic simulation models between different tools. An extensive and growing number of tools currently support the standard. You can find the full list of tools on https://fmi-standard.org.
FMI comes in several versions and two different flavors, model exchange and co-simulation:
-
Model exchange means that a model is exported without a numerical solver. When imported and simulated in another tool, the model is solved using the solver of the importing tool.
-
Co-simulation means that the model is exported together with a numerical solver. When imported and simulated in another tool, the model is solved by its own solver.
There are advantages and disadvantages to both methods. Co-simulation is beneficial since the model is solved by a solver suited to the problem. On the other hand, co-simulation introduces sampled behavior that can be problematic. Hence, which flavor to use often depends on your preferences and requirements.
Exporting the model
Let's try to export the experiment model shown previously in Figure 2. Exporting models using FMI is often easy, but the workflow differs somewhat between tools. Figure 4 highlights the process used in the open-source tool OpenModelica. First, we select what FMI flavor to use under Tools > Options > FMI. In this tutorial, we'll use co-simulation. Then, we right-click the model and select Export > FMU to export the model.

The model is now compiled into a Functional Mock-Up Unit (FMU). It’s a ZIP file that contains the binaries of the model, a model description XML file, and optionally source code and other resources.

Importing the model
Now that we have an FMU, we can use it from other tools. In this example, we'll import the FMU in Python. If you don't have Python installed, you can try importing it back into your Modelica tool and do some analysis there instead. There are several Python packages that support importing FMUs. The code below highlights import and simulation of our FMU using OMSimulator, a package that comes with OpenModelica.
Results are written in a result file (.mat) and can be accessed conveniently using another Python package, DyMat. Alternatively, we can create our own result trajectory by stepping through the simulation and picking values using the stepUntil() and getValue() methods. If we introduce such a get_trajectory() function, we can sweep over different values of G_wall using the setValue() method to explore how wall materials and insulation affect the indoor temperature.
The results are shown in Figure 6. As expected, the indoor temperature increases as the insulation improves, i.e. for lower values of G_wall.
By using parameter sweeps, we can build an understanding of how different factors affect the systems we are working with. This is especially useful in cases where we are interested in playing around with a single or a limited number of parameters, like insulation. However, if we are looking at doing systematic parameter tuning in more complex systems, other methods might be a better fit.
Adding an external interface
If we have access to measurement data from an actual house, we can use that data to find better values for the parameters G_wall and C_room. This is done by using measurements of the outdoor temperature and the heat flow rate supplied by the heater as boundary conditions for the model. We then tune the values of G_wall and C_room until the simulated indoor temperature matches the measured indoor temperature. In the following example, we'll expand our previous Modelica-to-FMI workflow with such a calibration step in Python.
The first challenge we need to overcome to achieve that is to find a convenient way to set the boundaries of our model. In our previous example, the boundaries were set directly in our Modelica experiment. While this may make sense when we work with simple experiments in the Modelica environment, it will introduce additional overhead in the FMI workflow if we need to recompile the FMU for every new boundary configuration. We can avoid this by adding an external input-output interface to our house.
Duplicate the house experiment from Figure 2 and remove the sine and the constant sources. Name the model SimpleRoom. Drag and drop two RealInput connectors and a RealOutput from the Modelica.Blocks.Interfaces package. Name the connectors T_outside, Q_heater, and T_inside and connect them to the house model as in Figure 7. Then, export the FMU.
In this example we don't have access to actual measurements. Instead, we'll cheat a bit and create our own measurements from a house model with a different parameterization. Duplicate the experiment from Figure 2 once more. Name the new model SimpleMeasuredHouse and change the parameter values to G_wall = 166 W/K and C_room = 145000 J/K. Change the outdoor temperature to a constant value of 8 °C and the heater heat flow rate to a step 0-2000 W after 120 seconds. Add three RealOutputs called T_outside, Q_heater, and T_inside to the model and connect them to the corresponding variables by using connections or RealExpression sources. When finished, you should have something resembling Figure 8. Lastly, export the FMU.
Connecting several FMUs using SSP
Now we have two FMUs, one mimicking our house and one with the measurement data. To connect them together in Python, we'll use another tool-independent standard called System Structure and Parameterization (SSP). The purpose of this standard is to define and package complete simulation models consisting of one or more FMUs, connections, and parameter settings for exchange between different simulation tools. This is very useful as it allows us to assemble and simulate bundles of FMUs from various tools. We can create and export SSP models in Python using OMSimulator. In the example code below, we create an SSP model consisting of our two FMUs. First, we add the FMUs. Then we instantiate the components and add connections. Finally, we export the SSP model.
The generated SSP is a ZIP file containing the FMUs of the system and descriptions of the system structure, parameter values, etc. We can now open this model in other tools supporting the SSP standard. However, in this example we'll import the SSP back into Python so that we can use it for our calibration workflow.
Model calibration
Before setting up the calibration workflow it's interesting to understand how well the current values of G_wall and C_room fit the measurement data. The code below shows how to import and simulate the SSP model with OMSimulator.
As before, we have several options for viewing the results, i.e. using DyMat or some get_trajectory() function. The results are shown in Figure 9. The dynamics of our simulated house seem to be way too fast compared to the measured indoor temperature, indicating that the value of C_room is too small. In addition, the steady-state temperature is lower in our simulation than in the measurement, indicating that the thermal conductance value is too high. Let's see if we can improve these values.
The calibration step can be performed in Python using the optimization methods of the SciPy package. In this example, we’re using a least squares estimation. To make this work, we introduce a simulate_simpleRoom() function that simulates the SSP model for given values of G_wall and C_room and returns a resulting temperature trajectory. This function is called from a residual function that returns the error between the simulated and measured temperatures.
The results are shown in Figure 10.
After calibration, the indoor temperature curves become identical. The estimated values are G_wall = 166 W/K and C_room = 145000 J/K. Spot on! The simplified house model is now ready to use for analysis similar to the one I did back in 2021.
In this tutorial, we've seen how easy it is to deploy models using the FMI and SSP standards. In addition, we've learned a bit about thermal modeling using the Thermal.HeatTransfer package of the Modelica Standard Library. It is clear that by using open technologies like Modelica, FMI, and SSP, you can build extremely powerful simulation workflows that help you analyze and solve real-world issues, such as the size of your electricity bill.
🚀 Ready to Go Further with Modelica?
Whether you want to build your skills or get hands-on help with a real project, we can help.
📚 Build your skills.
Our Modelica Introduction Course is a two-day, hands-on program covering system modeling and simulation from the ground up. Perfect for engineers, innovators, and anyone ready to build smarter systems.
💬 Have a project in mind?
Book a free call and we'll talk through your specific modeling, simulation, or digital twin challenge.