Python pybamm and MATLAB/Simulink integration

12 views (last 30 days)
yunus
yunus on 4 Apr 2025
Answered: Himanshu on 10 Apr 2025
I have a battery model written in python (py 3.11) with pybamm 24.11.2 and casadi solver 3.6.7.
I want to use this model in simulink in real time. I want to dynamically run the current dependent pybamm model.
Websearch and other attempts did not work. How can I integrate simulink and pybamm?
  1 Comment
syed khaleduzzaman
syed khaleduzzaman on 5 Apr 2025

Hi, I am planning on same thing to implement. Let me know if you get any direction!

Sign in to comment.

Answers (1)

Himanshu
Himanshu on 10 Apr 2025
Hello,
I see that you are attempting to integrate PyBaMM (a Python-based tool) with MATLAB/Simulink in a real-time environment. MATLAB provides functionality to directly call Python using external language interfaces. The main challenge is enabling communication between Python and Simulink, as Simulink typically requires compiled C/C++ code or MATLAB functions. PyBaMM’s reliance on Python/CasADi complicates direct integration.
One viable approach is to use MATLAB’s Python Engine to call the PyBaMM model from Simulink via a MATLAB Function block. Ensure that MATLAB recognizes your Python environment by using:
pyenv("Version", "your_python_executable_path")
Then, create a MATLAB wrapper function that interacts with PyBaMM.
Another method is to use co-simulation via TCP/IP or UDP. The idea is to run the PyBaMM model as a separate process that communicates with Simulink over a network. In Simulink, you can use TCP/IP Send and Receive blocks to exchange data. On the Python side, set up a server to listen for Simulink's commands, run the model, and send back results. This decouples the two environments but requires handling synchronization and latency.
Please refer to the attached documentation for more information:
I hope this helps.

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!