Using model construction commands in SPS 💻

Did you know you can build and wire SPS circuits entirely through MATLAB scripts?

Using model construction commands is extremely useful for automating large networks, running parameter sweeps, or generating models programmatically without touching the Simulink® GUI.

Here is a quick guide on how to add SPS blocks and connect their electrical ports using MATLAB code.

The Scenario

Suppose you want to add a PI Section Line and a Voltage Measurement block to a model named Mymodel, and then connect the terminals of the measurement block across the line.


Step 1: Add and Position the Blocks

First, you use the add_block command to bring the components from the sps_lib into your model, and set_param to position them on the canvas.

% Add the blocks to 'Mymodel'
add_block('sps_lib/Power Grid Elements/Pi Section Line', 'Mymodel/Block1');
add_block('sps_lib/Sensors and Measurements/Voltage Measurement', 'Mymodel/Block2');

% Position them on the screen [left, top, right, bottom]
set_param('Mymodel/Block1', 'position', [340, 84, 428, 106]);
set_param('Mymodel/Block2', 'position', [520, 183, 545, 207]);

Step 2: Get the Port Handles

Unlike standard Simulink signals, SPS blocks use special physical modeling connection ports. To wire them, you need to find the handles (IDs) of these specific terminals.

% Retrieve the Port Handles for both blocks
Block1PortHandles = get_param('Mymodel/Block1', 'PortHandles');
Block2PortHandles = get_param('Mymodel/Block2', 'PortHandles');

Step 3: Connect the Blocks (add_line)

The port handle structure contains specific fields for physical connections:

  • LConn: Represents the Left connectors of the block.
  • RConn: Represents the Right connectors of the block.

To wire the circuit, use the add_line command and specify the indices of the connectors you want to link.

% Connect the + terminal (LConn 1 of Block 2) to the left side of the PI Line (LConn 1 of Block 1)
add_line('Mymodel', Block1PortHandles.LConn(1), Block2PortHandles.LConn(1));

% Connect the - terminal (LConn 2 of Block 2) to the right side of the PI Line (RConn 1 of Block 1)
add_line('Mymodel', Block1PortHandles.RConn(1), Block2PortHandles.LConn(2));

:light_bulb: Pro Tip

If you are unsure of the exact library path of an SPS block (e.g., 'sps_lib/Power Grid Elements/Pi Section Line'), simply drag the block into an empty model manually, click on it, and type gcb (Get Current Block) in the MATLAB command window to see its full path!