Main Content

Run Fault Simulations and View Results

This example shows how to run fault simulations. When you model faults, you often need to run more than one simulation. You can adjust the parameters in your fault behaviors programmatically and automate the simulations with MATLAB® code. You can inspect the results in the Simulation Data Inspector.

Simulate the Model Interactively

Open the sldemo_fuelsys_fault_analyzer model and set up the faults with the helper function, myFuelSysCase.

myFuelSysCase(4)

After executing the function, the model contains faults on each input port to the To Controller subsystem. The model has one activated fault, throttle_fault, that triggers when the time is 25.

Simulate the model. The Scope block captures signal values during the most recent simulation.

model = "sldemo_fuelsys_fault_analyzer";
sim(model)

To see the status of the faults throughout simulation, you can inspect the results with the Simulation Data Inspector. In the Fault Analyzer tab, in the Review Results section, click Data Inspector. You can also open the Simulation Data Inspector with this command:

Simulink.sdi.view

The Simulation Data Inspector logs the status of faults as either true or false. In this example, confirm that the fault was not injected until 25 seconds by expanding the Fault list for the first run and selecting the fault, throttle_fault, to plot the status versus time.

Use Scripts to Adjust Fault Properties and Simulate

Each fault corresponds to a Fault object that has properties that you can adjust programmatically. In this example, increment the trigger time of the fault, throttle_fault, and simulate after each adjustment. This fault is on the first input port to To Controller.

To Controller is the only block with faults. Use the Simulink.fault.findFaultedElements function to retrieve the paths of the faults on this block as a string array.

elements = Simulink.fault.findFaultedElements(model);

Each array element lists the model path to the input port in numerical order. For example, the first element of elements is the path to the first input port.

elements(1)
ans = 

    "sldemo_fuelsys_fault_analyzer/To Controller/Inport/1"

Get the Fault object by using the Simulink.fault.findFaults function.

sweptFault = Simulink.fault.findFaults(model,...
ModelElement=elements(1),Name="throttle_fault",TriggerType="Timed");

Enable faults on the first input port to To Controller, and disable faults on the other ports by using the Simulink.fault.enable function.

for n = 1:length(elements)
    if n == 1
        Simulink.fault.enable(elements(n),true)
    else
        Simulink.fault.enable(elements(n),false)
    end
end

Set throttle_fault as the active fault by using the activate function.

activate(sweptFault)

Turn fault simulation on by using the Simulink.fault.injection function.

Simulink.fault.injection(model,1);

throttle_fault uses a timed trigger. When the simulation reaches the specified trigger time, the fault is injected. To adjust the trigger time, modify the StartTime property of the Fault object. In this example, start at a trigger time of 10, and increment the trigger time by 2.

numberOfSims = 6;
for n = 1:numberOfSims
    sweptFault.StartTime = 8+2*n;
    sim(model);
end

Use Scripts to Change Fault Behaviors and Simulate

You can also modify the behavior of the faults in your Simulink® model by accessing the Fault Subsystem block in the fault model. In this example, adjust the behavior associated with the fault, ego_fault.

Get the Fault object for ego_fault.

sweptFault = Simulink.fault.findFaults(model,...
ModelElement=elements(3));

Get the paths to the fault model and Fault Subsystem block associated with ego_fault by using the Simulink.fault.getFaultModels and getBehavior functions.

faultModel = Simulink.fault.getFaultModels(model);
faultSubsystem = getBehavior(sweptFault);

Open the Fault Subsystem block ego_fault to view the behavior.

open_system(faultModel)
open_system(faultSubsystem)

The behavior overrides the signal with a Constant block with a value equal to 12. In this example, adjust the value of this block. Enable faults on the third input port to To Controller and disable faults on the other input ports.

for n = 1:length(elements)
    if n == 3
        Simulink.fault.enable(elements(n),true)
    else
        Simulink.fault.enable(elements(n),false)
    end
end

Set ego_fault as the active fault.

activate(sweptFault)

Increment the Constant value parameter of the Constant block from 7 to 12 incrementing by 1 for each simulation.

sweptBlock = "Constant";
numberOfSims = 6;
for n = 1:numberOfSims
    constVal = 6 + numberOfSims;
    set_param(faultSubsystem + "/" + sweptBlock,Value=string(n))
    sim(model);
end

Additional Simulation Options

To capture the numerical results of simulation without saving changes to the model, simulate the model by defining a Simulink.SimulationInput object for the model and saving the simulation results to a Simulink.SimulationOutput object. After creating these objects, you can also run simulations by using parsim.

Automate Adding, Deleting, and Updating Faults

You can also create, delete, and update faults with programmatic options. For a list of programmatic options, see Fault Modeling and Fault Simulation. For an additional example, see Add and Configure Faults Programmatically.

See Also

Related Topics