Simulate Functional Mock-up Units (FMUs) in Python

0.3.29 · active · verified Thu Apr 16

FMPy is a free Python library to simulate Functional Mock-up Units (FMUs), supporting FMI 1.0, 2.0, and 3.0 for both Co-Simulation and Model Exchange. It runs on Windows, Linux, and macOS, offering a command-line interface, a graphical user interface, and a web app. Additionally, FMPy facilitates creating Jupyter Notebooks and compiling C code FMUs, making it a versatile tool for system simulation. The library is actively maintained by Dassault Systèmes, with the current version being 0.3.29.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an FMU, retrieve its metadata, simulate it for a specified duration, and then plot selected simulation results using FMPy. It highlights the core `simulate_fmu` and `plot_result` functions. Users need to provide an actual FMU file (e.g., 'Rectifier.fmu') for the code to run successfully.

import os
from fmpy import simulate_fmu, dump
from fmpy.util import plot_result

# NOTE: FMPy requires an FMU file to operate. 
# For this example, you would typically download one, e.g., 'Rectifier.fmu'
# from the FMPy GitHub repository or create your own.
# For demonstration, we'll assume 'Rectifier.fmu' is in the current directory.

fmu_path = 'Rectifier.fmu' # Replace with actual path to your FMU file

if not os.path.exists(fmu_path):
    print(f"Error: FMU file '{fmu_path}' not found. Please provide a valid FMU file.")
    # Example of how you might download one for a real quickstart:
    # from fmpy.util import download_file
    # download_file('https://github.com/CATIA-Systems/FMPy/raw/main/FMPy/fmus/Rectifier.fmu', fmu_path)
    exit()

# Get information about the FMU
print(f"\nFMU Information for {fmu_path}:")
dump(fmu_path)

# Simulate the FMU
print(f"\nSimulating {fmu_path}...")
result = simulate_fmu(fmu_path, stop_time=1.0)

# Plot the results
print("\nPlotting results...")
plot_result(result, variables=['Rectifier1.Capacitor1.v', 'Rectifier1.Diode1.i'])
print("Plot generated (a new window should appear).")

view raw JSON →