SymbolicContinuousCallback(conditions, affect) is an event that fires
when one of the condition equations lhs ~ rhs becomes satisfied, that is
when lhs - rhs crosses zero; the time is located by root finding.
affect runs on upward crossings and affect_neg on downward ones (by
default the same affect; affect_neg = NULL makes downward crossings stop
the integrator without changing anything). A callback without an affect
only stops the integrator at the crossing, for discontinuities.
Usage
SymbolicContinuousCallback(
conditions,
affect = NULL,
affect_neg = affect,
initialize = NULL,
finalize = NULL,
rootfind = "left",
reinitializealg = NULL,
discrete_parameters = list(),
zero_crossing_id = NULL,
initialize_save_discretes = TRUE,
iv = NULL
)
SymbolicDiscreteCallback(
condition,
affect = NULL,
initialize = NULL,
finalize = NULL,
reinitializealg = NULL,
discrete_parameters = list(),
initialize_save_discretes = TRUE,
iv = NULL
)
lhs %=>% rhsArguments
- conditions, condition
Event conditions, see Details.
- affect, affect_neg, initialize, finalize
Affects: equations with
Pre(), anAssignmentAffect(), anImperativeAffect(), orNULL.initializeruns when the integration starts andfinalizewhen it ends.- rootfind
"left"(default; the affect runs just before the crossing),"right"(just after) or"none"(at the end of the step).- reinitializealg
Reinitialization after the affect. Symbolic affects are consistent by construction; kept for compatibility.
- discrete_parameters
Parameters the affect may change.
- zero_crossing_id
An identifier of the event.
- initialize_save_discretes
Kept for compatibility.
- iv
The independent variable; unused, kept for compatibility.
- lhs, rhs
Conditions and affects, for
%=>%.
Details
SymbolicDiscreteCallback(condition, affect) is checked after each step.
The condition is a number dt (periodic event every dt), a numeric
vector or a list of numbers (events at these preset times; use list(2)
for a single time), a Clock() (periodic with a phase; it also fires at
the initial time when the clock ticks there), or a symbolic boolean such
as Eq(t, t1) & (x < 50) (built with symcompR's Eq, <, &), which
fires when it holds after a step; pass tstops to solve() so that the
integrator stops at the times the condition tests.
Systems take these through continuous_events and discrete_events,
which also accept a formula x ~ 0 (a condition only event), a pair
list(conditions, affects) or conditions %=>% affects, and lists of
these. A list of single formulas is a list of condition only events, as a
vector of equations in ModelingToolkit; write list(x ~ 0) %=>% affects
or list(list(x ~ 0), affects) for an event with an affect.
Examples
t <- t_nounits; D <- D_nounits
x <- Variable("x", t, default = 1); v <- Variable("v", t, default = 0)
ball <- mtkcompile(System(list(D(x) ~ v, D(v) ~ -9.8), t, name = "ball",
continuous_events = list(x ~ 0) %=>% list(v ~ -Pre(v))))
sol <- solve(ODEProblem(ball, list(), c(0, 2)))
min(sol[x]) >= 0
#> [1] TRUE