Guy on Simulink

Simulink & Model-Based Design

Deploying the Virus Spread Simulator Using Simulink Compiler

After covering the basics of Simulink Compiler in my previous post, it's now time to look at a more complex application.

For that, I decided to leverage the project I published on this blog a few months ago where I simulated the evolution of an exponential spread such as the propagation of a virus like COVID-19.

Using App Designer, I created an app where you can tune parameters and visualize the results of the simulation. My colleagues from the app deployment area set up a MATLAB Web App Server in the cloud so everybody reading this blog can try the app in a web browser.

Try the final Web App by clicking here or on the image below.

COVID-19 simulator Web App

Getting the Source Code

To view how I implemented the app, you can download the project on MATLAB Central or GitHub.

One way I find convenient to retrieve projects from GitHub is through the Simulink Start Page. You can simply click the Project from source control button and paste the address of my repo:

Let's look at what I had to do to make to the project compatible with Simulink Compiler, along with tips and tricks I learned along the way.

Rapid Accelerator

In my previous post, I mentioned that to simulate a model in a deployed application, the SimulationInput object must be configured for deployment using simulink.compiler.configureForDeployment. To understand what this function does, let's apply it to a default SimulationInput object:

Configure for deployement

As you can see, configuring an app for deployment means simulating the model in Rapid Accelerator mode and disabling the RapidAcceleratorUpToDateCheck parameter. For future releases, we are planning on adding more functionality to simulink.compiler.configureForDeployment, for example to flag settings of model components not compatible with Simulink Compiler

In my case, the most significant consequence is that I had to remove SimEvents blocks from the model because SimEvents does not support Rapid Accelerator. Instead of using SimEvents to schedule how long it will take for an infected agent to recover, I implemented a counter in a Simulink function to detect when an agent has been infected long enough to recover and change its status.

I recommend looking at this documentation page for other limitations associated with the Rapid Accelerator requirement.

Tunable Parameters

The second thing you want to be careful with is how you tune parameters. With the requirement that the model must run in Rapid Accelerator mode without rebuilding, only certain parameters can be tuned, and they need to be tuned in a specific way.

In my case, I need to tune 4 properties of the agents every time I simulate the model:

  • Initial position
  • Initial velocity
  • Radius
  • Recovery time

Luckily, those are tunable by default, so I did not have to make any change to make them tunable. What you need to be careful with is how you specify new values for those parameters. With Simulink Compiler, you need to specify new parameter values through the setVariable method of a Simulink.SimulationInput object.

In my app, this means that the code simulating the model looks like the following:

Specifying Tunable Parameters

Here is one additional tip I did not have to use in this project, but that you might find useful is you are a Simscape user.

Simscape parameters: By default, Simscape parameters are not tunable. To make a Simscape parameter tunable, you first need to go to your preferences and enable the option "Show run-time parameter settings":

Simscape preferences

Once this preference is enabled, a new drop-down will appear next to all Simscape parameters that can potentially be made run-time tunable:

Simscape Run-Time parameter

I recommend setting only the Simscape parameters you are actually planning to tune to be Run-Time tunable. Setting all Simscape parameter to be Run-Time tunable can lead to performance and robustness issues.

Here are a few more tricks I find convenient if you plan to deploy a compiled app as Web App.

Tip 1: Show a progress bar while the simulation runs

In R2020a, simulation results are available only once the simulation is complete. If the simulation takes more than a few seconds, I recommend displaying a progress bar during the simulation, so the end-user knows that the simulation is happening.

Note: In R2020b, it is possible to stream data in and out of the compiled model. I am planning to cover that in a future post.

Tip 2: Show potential simulation errors in an alert dialog

In case something goes wrong, it's always a good idea to wrap some of your code in a try-catch statement and use uialert to display a the error. It might help your end-user to understand what went wrong and fix it if possible.

Tip 3: Handling Simulink startup time

If you deploy the app on a Web App server, Simulink is not launched automatically. This means that the first time the model will be simulated, extra time will be necessary to load Simulink. To avoid the end-user wondering what's happening, a progress bar can also help.

Here is an example snippet of code combining those 3 tips:

Tips 1, 2 and 3

Now it's your turn

With an app created using App Designer, compiled with Simulink Compiler and hosted on a MATLAB Web App Server, everyone in your company can have access to the power of Simulink in a web browser, without a MATLAB installation.

If you have not done it yet, click here to try the deployed Web App in your web browser. To see the implementation, download the project from MATLAB Central or GitHub.

Let us know in the comments below if you are already leveraging Simulink Compiler and/or Web Apps, or if you are planning to soon.

  • print
  • send email


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.