Main Content

Create Custom Block by Modifying a Library Block

This example shows how to modify a Foundation library block to create a custom block and deploy it in a model by using the Simscape Component block. You can use this approach to quickly customize existing Simscape™ blocks.

In this example, you modify the Pipe (TL) block to add elevation change to the model in the Optimal Pipeline Geometry for Heated Oil Transportation example. However, you can apply this process to any Simscape block that has available source code.

Create Component File

  1. Open the OptimalPipelineGeometryForHeatedOilTransportationExample model.

    openExample('simscape/OptimalPipelineGeometryForHeatedOilTransportationExample')
  2. Open the Pipe (TL) dialog box. In the Description tab, click the Source code link to open the source code for the block.

  3. Open a new Simscape file with the sscnewfile function.

    sscnewfile('mypipe')
  4. Delete the contents of the file, mypipe.ssc, except for the first line of code. After deletion, the file contains only the definition of the component name:

    component mypipe

  5. Excluding the first line, copy the contents of the Pipe (TL) source file, pipe.ssc, into your component file. Close the pipe.ssc file.

Edit Component File

Modify the code from the Pipe (TL) block to model elevation change.

  1. Add the parameters that represent the elevation change and gravitational acceleration. Add these lines in the parameters section:

    elevation_gain = {0, 'm' }; % Elevation gain from port A to port B
    g = {9.81, 'm/s^2'}; % Gravitational acceleration
  2. Update the energy conservation equations to account for elevation change. The new equation for energy conservation is

    Vd(ρu)dt=ϕA+ϕB+QHm˙avggΔz.

    The component file defines the energy conservation equations in two places. This is because the block uses different equations when modeling energy conservation with and without dynamic compressibility. Update the right side of these expressions to account for elevation change. Find this equation in the equations section:

    % Energy conservation
    der(u_I) * rho_nominal * volume == Phi_A + Phi_B + Q_H;

    Replace this line with:

    % Energy conservation
    der(u_I) * rho_nominal * volume == Phi_A + Phi_B + Q_H - mdot_avg * g * elevation_gain;

    Find this equation in the equations section:

    % Energy conservation
    der(p_I)*dUdp + der(T_I)*dUdT == Phi_A + Phi_B + Q_H;

    Replace this line with:

    % Energy conservation
    der(p_I)*dUdp + der(T_I)*dUdT == Phi_A + Phi_B + Q_H - mdot_avg * g * elevation_gain;

  3. Update the momentum balance equations to account for elevation change. The new equation for momentum balance is

    pApB=Δpf+ρIgΔz.

    When the block calculates the pressure differential over each half of the pipe, assume that half of the pressure change due to elevation occurs in each side of the pipe.

    The component file defines the momentum balance equations in three places. This is because the block uses different equations when modeling momentum balance with and without dynamic compressibility and fluid inertia. Update the right side of these expressions to account for elevation change.

    Find this equation in the equations section:

    % Momentum balance
    A.p - B.p == pressure_loss;

    Replace this line with:

    % Momentum balance
    A.p - B.p == pressure_loss + rho_I * g * elevation_gain;

    Find these equations in the equations section:

    % Momentum balance
    A.p - p_I == pressure_loss_A;
    B.p - p_I == pressure_loss_B;

    Replace these lines with:

    % Momentum balance
    A.p - p_I == pressure_loss_A + rho_I * g * elevation_gain/2;
    B.p - p_I == pressure_loss_B - rho_I * g * elevation_gain/2;

    Find these equations in the equations section:

    % Momentum balance
    A.p - p_I == der(mdot_A)*length/2/area + pressure_loss_A;
    B.p - p_I == der(mdot_B)*length/2/area + pressure_loss_B;

    Replace these lines with:

    % Momentum balance
    A.p - p_I == der(mdot_A)*length/2/area + pressure_loss_A + rho_I * g * elevation_gain/2;
    B.p - p_I == der(mdot_B)*length/2/area + pressure_loss_B - rho_I * g * elevation_gain/2;

  4. Save the component file. The component file name must match the component name. Because you used the sscnewfile function, the file is automatically named mypipe.ssc and saved in your working directory.

Add Custom Block to Model

  1. In the Simulink® model, delete the Pipe (TL) block. Open the Simscape > Utilities library and add the Simscape Component block in its place.

  2. Open the Simscape Component block dialog box and specify the source file as mypipe. For more information on using the Simscape Component block, see Deploy a Component File in Block Diagram.

  3. Connect the Simscape Component block ports to the model.

  4. When you copy the source code for a block, it automatically populates the default parameter values. To match the parameters from the original Pipe (TL) block, set these parameters:

    • Set Pipe length to lPipeline.

    • Set Cross-sectional area to pi*D1^2/4.

    • Set Hydraulic diameter to D1.

    • Clear the Enable dynamic compressibility checkbox.

    • Set Initial liquid temperature to TUpstream.

  5. Set the new Elevation gain from port A to port B parameter to a nonzero value and run the model. Confirm that the model behaves as expected.

To reuse this block in several models, use the sscbuild function to add the block to a reusable custom block library, which is a more efficient way to manage custom blocks. For more information, see Building Custom Block Libraries.

See Also

Topics