assert
Program customized run-time errors and warnings
Parent Section: equations
Syntax
assert(predicate_condition
,message
,Action);
Description
The equations
section may contain the assert
construct, which lets you specify customized run-time errors and warnings:
assert(predicate_condition,message,Action);
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. |
Action | Optional attribute that specifies whether triggering the assert results in a warning or an error during simulation. The default action is error. |
The Action
attribute lets you specify the assert action based on an
enumerated parameter value. A built-in enumeration
simscape.enum.assert.action
allows three possible actions when
the assertion is triggered: error
, warn
, and
none
. You can provide an enumerated value directly to the
Action
attribute:
assert(u > 0,Action = simscape.enum.assert.action.warn)
or create an enumerated parameter and let the block user control the assert action:
parameters assert_action = simscape.enum.assert.action.warn end equations assert(u > 0,Action = assert_action) end
You can use the assert
construct in:
The top-level equations, including initial 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.
When you include assert
constructs in initial
equations, their predicate conditions are checked only once, after solving for initial
conditions (before the start of simulation, see Initial Conditions Computation). Use these assertions to safeguard against the model initializing with nonphysical
values.