Skip to contents

An affect says what happens when an event fires. Symbolic affects are equations relating the values after the event to the values before it, written with Pre(): list(v ~ -Pre(v)) reverses v, and list(x ~ Pre(x) + 1) adds one to x. Variables without Pre are the values after the event, so the affect equations are solved together with the algebraic and observed equations of the system: unknowns that the equations do not fix keep their values, and the others change to keep the system consistent. Parameters change only when they are discrete parameters: those given in discrete_parameters of the callback, and, as an R convenience, variables declared with Discrete() or discretes() (the @discretes of ModelingToolkit) that are the left-hand side of an affect equation. ModelingToolkit requires discrete parameters to be time dependent, Parameter("c", t); modelcompR also accepts time independent ones.

Usage

AssignmentAffect(assignments)

ImperativeAffect(
  f,
  modified = list(),
  observed = list(),
  ctx = NULL,
  skip_checks = FALSE
)

Arguments

assignments

A list of formulas lhs ~ rhs or pairs list(lhs, rhs).

f

An R function function(modified, observed, ctx, integ).

modified, observed

Named lists of variables (modified) or expressions (observed).

ctx

Any R object passed to f.

skip_checks

Skip the checks of the modified variables.

Value

A SymbolicAffect or an ImperativeAffect.

Details

AssignmentAffect(list(x ~ expr)) assigns values computed entirely from the values before the event: each right-hand side is wrapped in Pre(), and parameters on the left-hand side become discrete parameters.

ImperativeAffect(f, modified, observed, ctx) runs an R function. modified and observed are named lists of variables or expressions (unnamed variables are named after themselves). When the event fires, f(modified, observed, ctx, integ) receives their current values as named lists and returns a named list with new values for some of the modified entries, which are written back to the unknowns and parameters. integ is the integrator, so terminate(integ) stops the simulation.

Examples

t <- t_nounits
x <- Variable("x", t); k <- Parameter("k")
AssignmentAffect(list(x ~ 2 * x, k ~ k + 1))
#> Affect equations:
#>   x(t) ~ 2*Pre(x(t))
#>   k ~ Pre(k) + 1
#> Discrete parameters: k
ImperativeAffect(function(m, o, ctx, integ) list(x = -m$x), modified = list(x))
#> ImperativeAffect(observed: [], modified: [x(t) => x])