'assert()'

assert()

Information

This information is part of the Modelica Standard Library maintained by the Modelica Association.

Trigger error and print error message if assertion condition is not fulfilled

Syntax

assert(condition, message, level = AssertionLevel.error)

Description

The Boolean expression condition shall be true for successful model evaluations. Otherwise, an error occurs using the string expression message as error message.

If the condition of an assert statement is true, message is not evaluated and the procedure call is ignored. If the condition evaluates to false different actions are taken depending on the level input:

  • level = AssertionLevel.error:
    The current evaluation is aborted. The simulation may continue with another evaluation [e.g., with a shorter step-size, or by changing the values of iteration variables]. If the simulation is aborted, message indicates the cause of the error. Failed assertions takes precedence over successful termination, such that if the model first triggers the end of successful analysis by reaching the stop-time or explicitly with terminate(), but the evaluation with terminal()=true triggers an assert, the analysis failed.
  • level = AssertionLevel.warning:
    The current evaluation is not aborted, message indicates the cause of the warning [It is recommended to report the warning only once when the condition becomes false, and it is reported that the condition is no longer violated when the condition returns to true. The assert(..) statement shall have no influence on the behavior of the model. For example, by evaluating the condition and reporting the message only after accepted integrator steps. The condition needs to be implicitly treated with noEvent(..) since otherwise events might be triggered that can lead to slightly changed simulation results].

The AssertionLevel.error case can be used to avoid evaluating a model outside its limits of validity; for instance, a function to compute the saturated liquid temperature cannot be called with a pressure lower than the triple point value. The AssertionLevel.warning case can be used when the boundary of validity is not hard: for instance, a fluid property model based on a polynomial interpolation curve might give accurate results between temperatures of 250 K and 400 K, but still give reasonable results in the range 200 K and 500 K. When the temperature gets out of the smaller interval, but still stays in the largest one, the user should be warned, but the simulation should continue without any further action. The corresponding code would be

  assert(T > 250 and T < 400, "Medium model outside full accuracy range",
         AssertionLevel.warning);
  assert(T > 200 and T < 500, "Medium model outside feasible region");

Examples

  parameter Real upperLimit=2;
  parameter Real lowerLimit=-2;
equation
  assert(upperLimit > lowerLimit, "upperLimit must be greater than lowerLimit.");