Main Content

Programming Run-Time Errors and Warnings

Use theassertconstruct to implement run-time error and warning messages for a custom block. In the component file, you specify the condition to be evaluated, as well as the error message to be output if this condition is violated. When the custom block based on this component file is used in a model, it will output this message if the condition is violated during simulation. The optionalActionattribute of theassertconstruct specifies whether simulation stops when the predicate condition is violated, continues with a warning, or ignores the violation.

The following component file implements a variable resistor, where input physical signal R supplies the resistance value. Theassertconstruct checks that this input signal is greater than or equal to zero:

component MyVariableResistor % Variable Resistor % Models a linear variable resistor. The relationship between voltage V % and current I is V=I*R where R is the numerical value presented at the % physical signal port R. If this signal becomes negative, simulation % errors out. % inputs R = { 0.0, 'Ohm' }; end nodes p = foundation.electrical.electrical; % +:left n = foundation.electrical.electrical; % -:right end variables i = { 0, 'A' }; v = { 0, 'V' }; end branches i : p.i -> n.i; end equations assert( R >= 0, 'Negative resistance is not modeled' ); v == p.v - n.v; v == i*R; end end

If a model contains this Variable Resistor block, and signal R becomes negative during simulation, then simulation stops and the Simulation Diagnostics window opens with a message similar to the following:

At time 3.200000, an assertion is triggered. Negative resistance is not modeled. The assertion comes from: Block path: dc_motor1/Variable Resistor Assert location: between line: 29, column: 14 and line: 29, column: 18 in file: C:/Work/libraries/+MySimscapeLibrary/+ElectricalElements/MyVariableResistor.ssc

The error message contains the following information:

  • Simulation time when the assertion got triggered

  • Themessagestring (in this example,Negative resistance is not modeled)

  • An active link to the block that triggered the assertion. Click the块路径link to highlight the block in the model diagram.

  • 积极维护的链接位置混合涂料nent source file. Click theAssert locationlink to open the Simscape™ source file of the component, with the cursor at the start of violated predicate condition. For Simscape protected files, theAssert locationinformation is omitted from the error message.

See theassertreference page for syntax specifics and more examples.

Related Topics