Program customized run-time errors and warnings
assert (predicate_condition, message,
Warn = true|false);
The equations section may contain the assert construct, which lets you specify customized run-time errors and warnings:
assert (predicate_condition, message, Warn = true|false);
|predicate_condition||The expression to be evaluated at run time. It can be a function of time, inputs, parameters, and variables.|
|message||Optional text string (with single quotes) that tells the block user why the run-time error or warning is triggered.|
|Warn = true|false||Optional attribute that specifies whether simulation errors out when the predicate condition is violated (Warn = false), or continues with a warning (Warn = true). The default is Warn = false.|
You can use the assert construct in:
The top-level equations.
The if-elseif-else branches of a conditional expression.
The expression clause and the right-hand side of the declaration clause of a let expression.
When you use an assert construct in a branch of a conditional expression, it is not counted towards the number of expressions in the branch, and is therefore exempt from the general rule that the total number of equation expressions, their dimensionality, and their order must be the same for every branch of the if-elseif-else statement. For example, the following is valid:
if x>1 y == 1; else assert(b > 0); y == 3; end
The scope of the assert construct is defined by the scope of its branch. In the preceding example, the predicate condition b > 0 is evaluated only when the else branch is in effect, that is, when x is less than or equal to 1.
Generate a run-time error if the fluid volume in a reservoir becomes negative:
assert( V >= 0, 'Insufficient fluid volume for proper operation' );
During simulation, if the internal variable V (corresponding to the volume of fluid in the reservoir) assumes a negative value, simulation stops and outputs an error message containing the following information:
Simulation time when the assertion got triggered
The message string (in this example, Insufficient fluid volume for proper operation)
An active link to the block that triggered the assertion. Click the Block path link to highlight the block in the model diagram.
An active link to the assert location in the component source file. Click the Assert location link to open the Simscape™ source file of the component, with the cursor at the start of violated predicate condition. For Simscape protected files, the Assert location information is omitted from the error message.
If you do not want simulation to stop, but still want to display a warning that a certain condition has been violated, set the Warn attribute to true. For example, if hydraulic pressure drops below fluid vapor saturation level at some point, this condition may result in cavitation and invalidate the modeling assumptions used in a block. You can add the following assert construct to the hydraulic component equations:
assert( p > p_cav, 'Pressure is below vapor level; cavitation possible', Warn = true );
In this case, if the predicate condition is violated, the simulation continues, but the warning message appears in the MATLAB® Command Window. The format of the warning message is the same as of the error message described in the previous example.