Main Content

Perform Cosimulation of Multiversion Components

This topic shows how you can create Simulink® components that import models that you created in previous versions of Simulink and cosimulate them with your current Simulink session using the Multiversion Co-simulation block. This workflow requires the Multi-Version Co-Simulation for Simulink support package.

When you have models created in different versions of Simulink:

Use the Multiversion Co-simulation block for simulation when you cannot update your models from previous versions to the current Simulink version or use with a protected model. Using the Multiversion Co-simulation block is best when you are running isolated code from a previous Simulink version. You must have all the associated versions of Simulink already installed on your computer. Simulink models created in the previous six releases are tested for backward compatibility with the Multiversion Co-simulation block.

Note

The recommendation is to only use multiversion cosimulation workflow on large, loosely coupled submodels so that the overhead of data exchange is not dominant.

Set up MATLAB Session

To perform multiversion cosimulation of components in the current parent Simulink session, you need to first set up running MATLAB sessions that correspond to the Simulink version of the referenced submodels. For instance, if there are two components from R2022b and one component from R2022a, you need to set up two running MATLAB sessions for R2022b and one running session for R2022a. Use the Release Manager to set up your MATLAB sessions.

  1. Launch the Release Manager using this command:

    multivercosim.internal.startUI
    Release Manager UI with list of active MATLAB sessions

  2. Click the Add button to mention the installation paths to all required MATLAB versions. You can change the name of the session using the Name field.

    You can also perform this function using this command.

    Simulink.CoSimServiceUtils.registerMatlab('matlab1', 'E:\R2022b_Update8\matlab');
    Here, the first argument is the session name and the second argument is the installation path.

    Use the matlabroot function in a MATLAB session to find the installation path for that particular version.

  3. After you add the MATLAB paths, select the version you want to launch and click Launch.

    You can also perform this function using this command.

    Simulink.CoSimServiceUtils.launchMatlab('matlab1');
    After launching a session, wait till all the sessions are shown running. Get the list of all the running MATLAB session using this command.
    list = Simulink.CoSimServiceUtils.listRunningMatlabs;
    

    Launch a corresponding MATLAB session for each referenced submodel. The number of running MATLAB sessions should correspond to the number of components for each version. Each MATLAB version continues running in the background while you simulate your model.

    • Stop a MATLAB session using the Stop button or this command.

      Simulink.CoSimServiceUtils.stopMatlab('matlab1');

Set up Parent Simulink model

  1. Open the parent Simulink model in which you want to create components that reference submodels created in previous versions.

  2. Add the Multiversion Co-simulation block from the block library.

    Multiversion Co-simulation block without a model attached to it.

  3. Specify the name of the submodel in Target name parameter in the block dialog box. All the component files should be in the path of the parent MATLAB session.

  4. Set the Simulation Mode and Cosim sample time parameters for each submodel component.

  5. Specify the release of the referenced submodel by selecting an option from the Select the Release menu in the Block Parameters dialog box.

    You can also launch the Release Manager from the Block Parameters dialog box to create, check, or update MATLAB sessions. To launch the Release Manager, select .

  6. You can optionally specify cosimulation setup and cleanup scripts for each of the referenced submodels in the Block Parameters dialog box.

Log Signals in Components

You can log signals inside the referenced submodels using either one of these methods:

  • In the submodel, select the signal you want to log, then go to the Simulation tab, and from the Prepare menu select Log Signals. You can then use the Data Inspector in the parent model to view the logged signals.

  • Save the signal data to a MAT-file using the To File block in the submodel. Make sure that the Multiversion Co-Simulation block references the submodel only once by setting the Total number of instances allowed per top model configuration parameter under Model Referencing to One. The MAT-file is saved in a folder named after the Multiversion Co-simulation block that references the submodel that contains the To File block. You can find this folder in a folder named after the parent model in the working directory of the parent model.

Debug Submodel from Parent Model

You can debug and view signals inside the submodel from the parent model by using the Simulink debugger from the parent model. Add a Scope block to the signal that you need to observe inside the submodel. Enable debugging for a referenced submodel by selecting the Enable Debugging check box in the Block Parameters dialog box.

To debug and observe signals inside the referenced submodels:

  1. Click Step Forward in the parent Simulink model to compile the parent model, compile and open the submodels from their respective MATLAB sessions, and pause the simulation at time zero.

  2. The opened submodels are locked initially. Double-click on the Scope blocks that are connected to the signals you want to observe. Since the submodels are currently locked, the Scope blocks do not open at this step.

  3. Click Step Forward in the parent submodel to continue stepping forward by one time step of the parent model.

  4. The Scope blocks of the submodels open when the simulation time of the parent model reaches the time step of the submodel.

  5. Continue to step forward in the parent model. The Scope blocks of the submodel displays the signal values at the time steps when the submodel is executed.

See Also

|