Although the question is already answered I would like to add some points, as I think it could be useful for many.
There are some common reasons to use the noEvent()
statement:
Guarding expressions: This is used to prevent a function from being evaluated outside of their validity range. A typical example is der(x) = if x>=0 then sqrt(x) else 0;
which would work perfectly in most common programming languages. This doesn't work always in Modelica for the following reason: When searching for the time when the condition x>=0
becomes false, it is possible that both branches are evaluated with values of x
varying around 0. The same fact is mentioned in the screenshot posted by marvel This results in a crash if the square root of a negative x
is evaluated. Therefore der(x) = if noEvent(x>=0) then -sqrt(x) else 0;
Is used to suppress the iteration to search for the crossing time, leaving the handling of the discontinuity to the solver (often referred to as "expressions are taken literally instead of generating crossing functions"). In case of a variable step-size solver being used, this makes the solver reduce the step-size to meet it's relative error tolerance, which will likely result in degraded performance. Additionally this can be critical if the function described is not smooth enough resulting in non-precise or even instable simulations.
Continuous Expressions: When a function is continuous there is actually no event necessary. This comes down to the fact, that events are used to describe discontinuities. So if there is none, usually the event is simply superfluous and can therefore be suppressed. This is actually covered by the smooth()
operator in Modelica, but the specification says, that a tool is free to still generate events. To my experience, tools generate events if the change to the function is relatively big. Therefore it can make sense to have a noEvent()
within a smooth()
.
Avoid chattering: noEvent
can help here but actually chattering is a more general problem. Therefore I'd recommend to solve issues related to chattering by re-building the model.
If none of the above is true the use of noEvent
should be considered carefully.
noEvent()
..." seems a bit strange to me. I would like to get some more context... – MilesmilesiannoEvent
and possiblysmooth()
. – Milesmilesian