🚀 Getting Started with CoFMPy¶
CoFMPy is a framework designed to make co-simulation easy and efficient. This guide walks you through setting up and running a your first simulation with CoFMPy!
🐾 Installation¶
Before you begin, ensure CoFMPy is installed:
pip install cofmpy
💻 Running Your First Simulation¶
With CoFMPy, you can simulate complex systems composed of multiple FMUs connected together. You can even integrate external files or network interfaces. For now, we will keep it simple and consider the system consisting of an alternating voltage source and a resistor, as shown in the figure below:

- The source outputs a voltage signal, denoted as \(V\) (in Volts).
- The resistor takes \(V\) as input and produces an output current \(I\) (in Amperes). The resistance parameter \(R\) is set to \(0.5\) \Omega$.
We've already prepared FMUs and configuration file associated to this scenario. Let's start by downloading them.
import os
import requests
import zipfile
url = "https://share-is.pf.irt-saintexupery.com/s/39zaG9HkQWnePbi/download"
# Local path to ressources folder
ressources_path = "example1.zip" #
# Download the file
response = requests.get(url, stream=True)
with open(ressources_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Ressources downloaded as {ressources_path}.")
# Unzip the file
with zipfile.ZipFile("example1.zip", "r") as zip_ref:
zip_ref.extractall(".")
# remove the zip file
os.remove(ressources_path)
print("Ressources unzipped in example1 folder!")
Step 1: Instantiate the Coordinator¶
The heart of CoFMPy is the Coordinator
, which is responsible for managing the simulation process. We begin by instantiating it and loading the configuration file just downloaded above:
# Import the main CoFMPy component
from cofmpy import Coordinator
# Create an instance of the Coordinator
coordinator = Coordinator()
# Load the simulation setup from a JSON configuration file
coordinator.start("example1/config.json")
To make sure your simulation is correctly set up, you can visualize the connection graph of your system:
# Display a graphical representation of the simulated system
graph = coordinator.graph_engine.plot_graph()
graph.show()
Step 2: Execute a Simulation Step or run it until completion¶
Here, you can either execute the simulation step by step or run it until completion.
# Define the time step for the simulation
step_size = 0.05
# Run a single step of the simulation
coordinator.do_step(step_size)
# Print the current time in the simulation
print(f"Current simulation time: {coordinator.master.current_time}")
# Alternatively, run the co-simulation until a specified end time
end_time = 0.5
coordinator.run_simulation(step_size, end_time)
print("Simulation completed!")
Step 3: Access Results¶
Once the simulation is finished, you can retrieve and inspect the results.
# Retrieve stored simulation results
results = coordinator.get_results()
# Display a preview of the results
print(results[("source", "V")])
print(results[("resistor", "I")])
⏭ What’s Next?¶
- Try modifying the configuration file to explore different scenarios.
- Experiment with different step sizes to see how they affect results.
- Explore CoFMPy's examples gallery.
- Dive deeper into advanced features by checking the official documentation.