Main Content

Validate Subsystem Reference Use and Build Model Using Component Codes

This example shows how to check the valid use of subsystem reference instances in a model and reuse generated subsystem component codes to build the model using unit testing.

Subsystem components adapt to the environment in which they are used. As a result, you might get undesired results when you use subsystem components in an untested environment. To ensure pertinent results, you can mark the test environments of a subsystem reference that produce the intended results as unit tests.

Use the Unit Test features available in the Simulink® toolstrip to:

  • Specify valid testing environments.

  • Capture signatures to diagnose and resolve invalid use of components.

  • Generate code for components to reuse in the top model.

Explore the Model

1. Open the model slexModelUsingReusableSS.

open_system('slexModelUsingReusableSS');

In this example, slexModelUsingReusableSS uses three instances of the Subsystem Reference block slexReusableSS, named sr1, sr2, and sr3. The inputs to the subsystem reference instances sr1, sr2, and sr3 are constants of data type double, int32, and int8, respectively.

2. Open the subsystem file slexReusableSS.

open_system('slexReusableSS');

The subsystem slexReusableSS limits the input between (-10,10) and then amplifies the result by a factor of 50. slexReusableSS has two test harnesses, ssref1_double and ssref1_int32. To accommodate data between (-500,500), ssref1_double and ssref1_int32 use double and int32 data types, respectively. As these two test harnesses give desired results for the Subsystem Reference block, this example uses them as unit tests.

Validate Component Use

This section explains how to validate the use of subsystem reference instances sr1, sr2, and sr3 in the model slexModelUsingReusableSS using unit test features.

1. In the subsystem file slexReusableSS, select the test harnesses ssref1_double and ssref1_int32 from the drop-down menu of the Select unit tests button on the Simulink® toolstrip.

set_param('slexReusableSS','UnitTestNames',{'ssref1_double', 'ssref1_int32'});

If you create new test harnesses for the subsystem file, set the subsystem block in the test harness as Atomic with Reusable function packaging.

2. To generate signatures, select Generate Signature option from the Generate Signature menu on the Simulink® toolstrip. This action generates signatures for the subsystem block slexReusableSS in the selected test harnesses ssref1_double and ssref1_int32.

Simulink.SubsystemReference.generateSignatures('slexReusableSS');

Alternatively, in complex subsystems, you can generate signatures for the selected unit tests in parallel using multiple instances of MATLAB®. This approach enables you to speed up signature generation. For more information, see Parallel Computing Toolbox Product Description (Parallel Computing Toolbox). To generate signatures in parallel, run the MATLAB® script generateSignatureInParallel.m. The script performs the following steps:

  • Load subsystem file and unit tests.

  • Set up parallel computing.

  • Generate signatures in parallel using multiple MATLAB® sessions.

  • Save signatures in subsystem file.

3. Save the subsystem file slexReusableSS.

save_system("slexReusableSS");

4. Run the model slexModelUsingReusableSS to verify use of the subsystem reference instances sr1, sr2, and sr3. Simulink® compares the signatures of these subsystem reference instances in the model with the captured unit test signatures of the Subsystem Reference block slexReusableSS in the test harnesses ssref1_double and ssref1_int32. Use of a subsystem reference instance is valid when its signature matches at least one unit test signature.

5. Diagnostic Viewer displays an error message to highlight the invalid use of the subsystem reference instance sr3 in the model as sr3 does not have matching signature with any of the unit tests.

To disable the error message, set the diagnostic parameter Behavior when a matching unit test for subsystem reference is missing to none or warning in the Configuration Parameters dialog box.

Alternatively, use the Signature diff dialog to identify the reason for the signature mismatch and resolve these differences. The Signature diff dialog box provides parsed differences between the attributes of the subsystem reference sr3 in the model and the attributes of the subsystem reference slexReusableSS in the test harnesses ssref1_double and ssref1_int32.

6. Resolve the diagnostic messages and run the model slexModelUsingReusableSS again.

Generate Code

This section explains how to generate code for the Subsystem Reference block slexReusableSS in the test harnesses ssref1_double and ssref1_int32.

1. In the subsystem file slexReusableSS, open test harness ssref1_double and go to Model Settings > Code Generation > Interface and set Shared code placement to Shared location. This action sets a common location to save generated codes. For more information, see Shared code placement (Simulink Coder). Apply the same setting to the other test harness ssref1_int32.

2. To generate code, select the Generate Code option from the Generate Signature menu on the Simulink® toolstrip. This action generates code for the subsystem block slexReusableSS in the selected test harnesses ssref1_double and ssref1_int32.

Reuse Code

This section explains how you can reuse the generated code of the Subsystem Reference block to build the model.

1. Set the code generation folder structure using Simulink.fileGenControl (Simulink Coder).

Simulink.fileGenControl('set','CodeGenFolderStructure', Simulink.filegen.CodeGenFolderStructure.TargetEnvironmentSubfolder);

2. Press Ctrl + B to build the model slexModelUsingReusableSS.

In the build, the model slexModelUsingReusableSS uses the code generated for Subsystem Reference block slexReusableSS. To check the reused codes, open the code generation report for the model.

See Also

Simulate a Subsystem Block Diagram with a Test Harness