Program customized run-time errors and warnings
Warn = true|false);
equations section may contain the
which lets you specify customized run-time errors and warnings:
Warn = true|false);
|The expression to be evaluated at run time. It can be a function of time, inputs, parameters, and variables.|
|Optional text string (with single quotes) that tells the block user why the run-time error or warning is triggered.|
|Optional attribute that specifies whether simulation errors
out when the predicate condition is violated (|
You can use the
assert construct in:
The top-level equations.
if-elseif-else branches of
a conditional expression.
The expression clause and the right-hand side of the
declaration clause of a
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
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
b > 0 is evaluated only when the
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
message string (in
Insufficient fluid volume for proper operation)
An active link to the block that triggered the assertion.
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
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
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
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 outputs a warning message. The format of the warning message is the same as of the error message described in the previous example.
The warning message appears once, at the first time step when the predicate condition is violated. In this example, the warning message appears at the first time step when the pressure drops below vapor level. As long as the pressure stays below that level, the message is not repeated at subsequent time steps. However, as the simulation continues, if the pressure raises above the vapor saturation level and then again drops below that level, the assertion gets reactivated and the warning message appears once again.