Skip to main content

Co-simulate with Simulink

Co-simulation lets you run a gas network and a control, electrical, or thermal model in Simulink as part of the same job. NexusFlow solves the gas network. Simulink solves the part that interacts with it. They exchange values at the configured interval in simulation time. The Communication Interface in the workbench specifies which signals to exchange. The NexusFlow Bridge desktop app starts Simulink on its computer and passes the signals between the two.

Availability. The Communication Interface is currently available only in the Gas Network Simulation Workbench.

Systems you can co-simulate​

Decide what Simulink will calculate, what it needs from the gas network, and how its results will affect that network. The following are three ways to set up the exchange.

ApplicationWhat NexusFlow sends to SimulinkReturned value and its use
Valve controlPressure measured in the gas networkThe controller calculates a target opening. An actuator sends the command to the valve
Compressor station electrical simulationCentrifugal compressor shaft power as the motor's mechanical loadA motor and drive model calculates electrical demand and returns a speed setpoint subject to power supply constraints. An actuator sends it to the compressor in speed control mode
Cooling loop thermal simulationThe actual heat duty of a heat exchanger in a non-isothermal gas networkThe cooling loop calculates its supply water temperature. The heat exchanger uses external temperature and overall heat transfer coefficient mode, and an actuator updates its external fluid temperature

The electrical and thermal networks are solved inside their respective Simulink models. The two tools exchange boundary values as ordinary Simulink signals. Each channel carries one finite real number per exchange. Use a separate channel for each value and agree on units, sign conventions, and initial values. For example, the heat exchanger reports heat absorbed by the gas as positive. Convert the sign as needed for the cooling loop. Communication Interface reference

If the electrical or thermal model uses Simscape, use sensors or controlled sources to read values and apply inputs, with PS-Simulink Converter and Simulink-PS Converter blocks to connect to ordinary signal ports. Bridge requires the model to run in Normal mode with a fixed-step solver. The communication interval must be a whole multiple of the Simulink base step. MathWorks: Connect Simscape to Simulink signals

Integrator example​

For a first connection, use a constant, an integrator, and an oscilloscope to verify the round trip. The workbench sends u = 2. Simulink integrates it from zero, and the workbench oscilloscope receives the result y. The simulation runs for 3 seconds with an exchange every 0.5 seconds. The final reading should be 6.

Before you start​

  • Install and pair NexusFlow Bridge. Confirm it is online under Personal Center → Bridge Management. Use the same account in the workbench.
  • Make sure MATLAB and Simulink are licensed on the computer running Bridge. For first-time setup, install a C compiler supported by your MATLAB release and select it by running mex -setup c in MATLAB. MathWorks compiler setup

1. Set the signals to send and receive in the workbench​

Download the integrator example ZIP and import it into a blank tab in the Gas Network Simulation Workbench with New/Open → Local File. The example includes the channels and simulation settings. Select the Communication Interface on the canvas and check the two channels.

Channel tableChannel nameBound portUnitInitial value
Input ChannelsuOutput signal port of the Constant InputBlank0
Output ChannelsyInput signal port of the OscilloscopeBlank0

Input and output are named from Simulink's perspective. The constant's output is Simulink's input u, and the integral is Simulink's output y. The Bound Port setting connects the three workbench components, so you can leave them unwired on the canvas. Keep the oscilloscope input available for y.

The input channel u binds the constant output and the output channel y binds the oscilloscope input
The input channel u binds the constant output and the output channel y binds the oscilloscope input
Build the example from a blank model
  1. Add a Constant Input from Controllers, name it Send u, and set Constant Value to 2.
  2. Add an Oscilloscope and a Communication Interface from Signals and Communication. Name the oscilloscope Receive y.
  3. Select the Communication Interface and add one row to each channel table using the values above. Hover over an option in the port selector to see the full sheet, component, and port names.

Right-click the Communication Interface on the canvas and choose Export Compute Interface. Save the exported JSON file on the computer running Bridge. It lists the signals to exchange, and Bridge uses it to generate the corresponding Simulink ports. You can leave Target Bridge and Compute Configuration unselected for now.

Export Compute Interface in the Communication Interface context menu
Export Compute Interface in the Communication Interface context menu
  1. In Simulink, create a blank model and save it as bridge_integrator.slx. Open Modeling → Model Settings → Solver. Set Type to Fixed-step and Solver to ode4 (Runge-Kutta). Expand Solver details, set Fixed-step size (fundamental sample time) to 0.1 seconds, and save the model. MathWorks fixed-step solvers

    Simulink solver settings with Fixed-step, ode4, and the 0.1-second fundamental sample time marked
    Simulink solver settings with Fixed-step, ode4, and the 0.1-second fundamental sample time marked
  2. In the Bridge desktop app, open Compute Configurations → New Compute Configuration. Name it Simulink integrator example and set Launch Mode to Simulink Model. Select the local MATLAB executable, bridge_integrator.slx, and the communication interface manifest you just exported. Set the base step to 0.1 seconds. If your project has dependencies, provide the project directory and any additional dependency directories.

  3. In Bridge, use Check Environment and Compile Interface Self-test to check licensing and compilation on first use. Select Generate Simulink Interface Subsystem and save the copy as bridge_integrator_linked.slx. Bridge adds NexusFlow Interface to the copy and sets Model File to the copy's path.

  4. In Simulink, open the copy. Add an Integrator from the Continuous library and set its initial condition to 0. Wire the NexusFlow Interface output u_out1 to the integrator input. Wire the integrator output to the NexusFlow Interface input y_in1. Save the copy.

    Simulink wiring from u_out1 to the integrator input and from the integrator output back to y_in1
    Simulink wiring from u_out1 to the integrator input and from the integrator output back to y_in1
  5. In Bridge, confirm that Model File points to the wired copy and Communication Interface Manifest points to the exported JSON. Click Save and Sync to Platform.

3. Select the configuration and run from the workbench​

  1. Select the Communication Interface. Set Target Bridge to your paired device and Compute Configuration to Simulink integrator example.
  2. Click a blank area of the canvas and check the global parameters below. If you imported the example, use its simulation settings.
ParameterExample value
Simulation typeTransient simulation
Calculation start and end times3 seconds apart
Minimum and maximum time steps0.1 and 0.5 seconds, respectively
Development and Debugging → Kernel Execution ModePlatform Image, so the platform kernel runs the main model
Development and Debugging → Communication Interval0.5 seconds
Development and Debugging → Communication Wait Timeout100 seconds to allow for first-time loading

Simulink advances 0.1 seconds per step and exchanges signals with the main kernel every five steps. When changing these settings, use a communication interval that is a whole multiple of the Simulink base step and a simulation duration that is a whole multiple of the communication interval. Global simulation parameters

Save the model, open Logs, and select Validate Simulation Model. Once validation passes, click Start Calculation. Bridge starts MATLAB, and the platform kernel and Simulink begin exchanging signals at the start of the simulation.

4. Check the values on the oscilloscope​

Open the run from Tasks on the left and confirm that it succeeded. Select the oscilloscope that receives y and open Signal → Signal Trend Chart. Measure time from the start of the calculation and check the values below. Floating-point values may be rounded slightly.

Elapsed simulation time (seconds)00.511.522.53
Oscilloscope value y0123456

Change the constant from 2 to 3, save, and run again. At three seconds the oscilloscope should read 9. With your own model, send a gas network measurement to Simulink and use an oscilloscope to check the magnitude and timing of the returned signal. Then connect outputs that affect the gas network to actuators.

Troubleshooting​

SymptomWhat to check
The compute configuration is missing in the workbenchSave the Simulink configuration in Bridge. Confirm Bridge is online and paired with your account, then reopen the configuration selector
Configuration revision or interface digest mismatchIf you changed execution settings in Bridge, reselect the configuration in the workbench. If you added or removed channels or changed their names, units, or initial values, export the manifest again, regenerate the interface, and save its wiring. You can reuse the manifest when switching to another valid port of the same direction
No port is available, or validation reports multiple driversUse an enabled control output as the source and an enabled control input as the target. Check that the target signal has a single source
Startup or advancement times outCheck the platform task log and Tasks and Logs in Bridge for licensing, dependencies, step size, and simulation duration. If a disconnect ends the task, reconnect Bridge before starting another run
MATLAB reports an existing interfaceEach model uses one session interface. Generate a fresh copy from the original model and wire the new copy