4. Open-loop simulation in Simulink¶
This example demonstrates how to run the Python Anesthesia Simulator inside Simulink through MATLAB’s Python interface. The files associated with this example can be downloaded by clicking on the following link:
4.1. Script Overview¶
File: main_sim_openloop.m
The script performs the following steps:
Clears the MATLAB workspace
Configures the Python environment to use the simulator backend
Defines simulation and patient parameters
Runs a Simulink model (
sim_openloop.slx)Plots the simulation outputs (BIS and drug infusion rates)
Clears the Python interface to free memory
4.2. 1. Workspace Initialization¶
clear all
close all
clc
These commands reset the MATLAB session by removing all variables, closing figures, and clearing the console.
4.3. 2. Configure Python Environment¶
Before running the simulation, specify which Python interpreter MATLAB should use.
env = pyenv('Version', ...
'your_path\your_environment\Scripts\python.exe');
Replace the path above with the location of your virtual environment’s python.exe.
Example (Windows):
env = pyenv('Version', 'C:\Users\michele\python_anesthesia_env\Scripts\python.exe');
Example (macOS/Linux):
env = pyenv('Version', '/Users/your_username/python_anesthesia_env/bin/python');
⚠️ The environment must have the
python_anesthesia_simulatorpackage installed (pip install .).
4.4. 3. Define Simulation Parameters¶
simulation_time = 3600; % 1 hour
age = 18; % years
height = 170; % cm
weight = 60; % kg
sex = 0; % 0 = female, 1 = male
sampling_time = 1; % [s]
These parameters are passed to the Python simulator through the Simulink model.
4.5. 4. Run Simulink Model¶
y = sim('sim_openloop.slx');
The Simulink model sim_openloop.slx must contain:
A MATLAB Function Block that calls the function:
[bis, co, map, tol, nmb] = PythonStep(u_p,u_r,u_n,u_a,age,height,weight,sex,sampling_time);
The
PythonStepblock internally callscallPython.m, which manages communication with Python.
4.5.1. Required Files¶
File |
Role |
|---|---|
|
Creates and maintains a Python |
|
Simulink wrapper for |
|
Simulink model running the simulation |
✅ Simulation Mode must be set to “Normal”
(Python calls are not supported in Accelerator or Code Generation modes.)
4.6. 5. Plot Simulation Results¶
After running the model, the script visualizes:
BIS (depth of anesthesia)
Propofol infusion rate
Remifentanil infusion rate
subplot(3,1,1)
plot(y.bis, 'k', 'LineWidth', 1.2)
title('BIS')
subplot(3,1,2)
plot(y.u_prop, 'k', 'LineWidth', 1.2)
title('Propofol Infusion Rate')
subplot(3,1,3)
plot(y.u_remi, 'k', 'LineWidth', 1.2)
title('Remifentanil Infusion Rate')
Each subplot corresponds to one of the patient’s key signals, plotted over the simulation time.
4.7. 6. Clear Persistent Python Objects¶
At the end of the script:
clear callPython
This command clears the persistent variables stored in callPython.m, which hold the Python Patient instance.
It ensures that the next simulation starts with a fresh Python object.
4.8. Example Workflow¶
Open MATLAB and ensure your Python environment is active:
pyenv('Version','C:\path\to\env\Scripts\python.exe')
Open and configure
sim_openloop.slx(Normal mode).Run the simulation:
main_sim_openloopInspect the BIS and drug infusion plots.
Reset the Python interface when done:
clear callPython
4.9. Output Signals¶
Signal |
Description |
Units |
|---|---|---|
|
Bispectral Index (depth of hypnosis) |
– |
|
Propofol infusion rate |
mg/s |
|
Remifentanil infusion rate |
µg/s |
4.10. Notes¶
Works only in Normal Simulation Mode.
pyenvmust point to a valid Python environment containing the simulator.Each run initializes or reuses a persistent Python
Patientinstance.Use
clear callPythonbetween runs to fully reset the model.