Skip to content

mathworks/run-deployed-simulations-using-python

Repository files navigation

This repo has three examples that show how to simulate a deployed Simulink® model from Python using MATLAB® Runtime. The examples have been tested using R2024b (or later) version of MathWorks® products.

Design and implementation to ease model parameter and signal access

All the examples use the model1.slx included in this repo.

the model

The example model model1 and the MATLAB function simulate illustrate implementation choices that make data marshaling between Python and MATLAB/Simulink relatively straight forward and can be used with any Simulink model. These are:

  • Parameterizing the Simulink model using workspace variables makes it easy to simulate with new parameter values passed in from Python.
  • Labeling the logged signals in the model with valid identifiers, makes it easy to pack the results into a MATLAB struct and return to Python.
  • Extracting the time and data values as numeric vectors from simulation output and returning these to Python makes data marshaling relatively easy.

Build the python package simulate_ModelName

The first step is to package the Simulink model along with the MATLAB function simulate into a Python package named simulate_ModelName so that it can be simulated in deployment mode. The MATLAB function is used to create and install this python package:

>> build_python_package_to_run_deployed_model('ModelName','model1')

The above command first builds an installer in the sub-folder ./simulate_model1_installer/<computer> and asks you if it should go ahead and install it. If you say yes, the python package will be installed in the sub-folder ./simulate_model1/<computer>. Note that <computer> is the name of your system/OS, such as: pcwin64, glnxa64, etc.

The following products are required to build the python package:

  • MATLAB®
  • MATLAB Compiler™
  • MATLAB Compiler SDK™
  • Simulink®
  • Simulink Compiler™

Using simulate_ModelName package with MATLAB Runtime

Once the simulate_ModelName python package installer is built, you can use and share it freely along with the MATLAB Runtime without any licensing requirements. Follow these links for instructions to download, install and configure MATLAB Runtime on your system.

The examples below assume that MATLAB Runtime has been properly installed on your system and the simulate_model1 python package is installed in the current directory in the ./simulate_model1/<computer> sub-folder. All examples use the helper function simulate_model.load_and_init_pkg to locate, import and initialize the simulate_model1 python package.

This example shows how to import and use the simulate_model1 package to run simulations with different parameter and external input signal values, retrieve the simulation results and plot them after each simulation.

$ python example0_basic.py --mdl model1

results from basic example

This example shows how to use the simulate_model1 package to simulate model1 with an output callback function outputFcn defined in Python. outputFcn is called periodically during the simulation with latest results. outputFcn can also return a boolean value to request simulation stop.

The OutputFcnDecimation argument to simulate can be used to control how often the outputFcn is called; increasing this decimation value improves performance because it reduces the callback overhead.

It is important to note here that outputFcn is executed in a separate Python process that is started by the MATLAB Runtime simulating model1. So outputFcn cannot access variables that are initialized in the python process that is running this example.

$ python3 example1_with_output_callback.py --mdl model1
    Locating 'simulate_model1' package for model 'model1' ...
    Importing simulate_model1 package from ./simulate_model1/glnxa64/local/lib/python3.11/dist-packages
    Initializing simulate_model1 ...
    Calling simulate ...
    @T =  0.22709525245242995 ; #x1 =  10 ; #x2 =  10
    @T =  1.58678955037287 ; #x1 =  10 ; #x2 =  10
    @T =  2.754467180297537 ; #x1 =  10 ; #x2 =  10
    :
    :
    @T =  99.0 ; #x1 =  10 ; #x2 =  10
    @T =  100.57172049121597 ; #x1 =  10 ; #x2 =  10
    @T =  100.57172049121597 ; Requesting simulation stop
    Press enter to exit ...
    Terminating ...
$

This example shows how to use the simulate_model1 package to simulate model1 with an input callback function inputFcn defined in Python. inputFcn is called during the simulation when new input signal values are needed. inputFcn is required to return the input signal value at the simTime specified, however it can also return input signal values at future time points. Returning input values for time points beyond the current simulation time reduces the number of calls to the input function during simulation and improves performance.

It is important to note here that inputFcn is executed in a separate Python process that is started by the MATLAB Runtime simulating model1. So inputFcn cannot access variables that are initialized in the python process that is running this example.

$ python example2_with_input_callback.py --mdl model1
    Locating 'simulate_model1' package for model 'model1' ...
    Importing simulate_model1 package from ./simulate_model1/glnxa64/local/lib/python3.11/dist-packages
    Initializing simulate_model1 ...
    Calling simulate ...
    @T =  0.0 : Returning  22  input values
    @T =  22.0 : Received  228  x1 points,  228  x2 points
    @T =  22.0 : Returning  17  input values
    @T =  39.0 : Received  175  x1 points,  175  x2 points
    @T =  39.0 : Returning  18  input values
    :
    :
    @T =  498.0 : Received  215  x1 points,  215  x2 points
    @T =  498.0 : Returning  13  input values
    @T =  511.0 : Received  125  x1 points,  125  x2 points
    @T =  511.0 : Requesting simulation stop
    Press enter to exit ...
    Terminating ...
$

results from example with input callback

About

Run deployed simulations using Simulink Compiler and Python

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published