Events in SimBiology Models
Overview
In SimBiology, an event is a discrete transition in value of a quantity or expression in a model. This discrete transition occurs when a customized condition becomes true. The condition can be a specific time and/or a time-independent condition. Such conditions are defined in anEvent object
.
Event Triggers
An event object has aTrigger
property that specifies a condition that must be true to trigger the event to execute.
Typical event triggers are:
A specific simulation time — Specify that the event must change the amounts or values of species or parameters. For example, at time = 5 s, increase the amount of an inhibitor species above the threshold to inhibit a given reaction.
In response to state or changes in the system — Change amounts/values of certain species/parameters in response to events that are not tied to any specific time. For example, when species
A
reaches an amount of30
molecules, double the value of reaction rate constantk
. Or when temperature reaches42
°C
, inhibit a particular reaction by setting its reaction rate to zero.
Note
Currently, events cannot be triggered at time = 0. However, you can get the event to happen just after time = 0 by usingtime > timeSmall
as the event trigger wheretimeSmall
can be a tiny fraction of a second such as 1.0 picosecond.
Event Functions
An event has anEventFcns
property that specifies what occurs when the event is triggered. Event functions can range from simple to complex. For example, an event function might:
Change the values of compartments, species, or parameters.
Double the value of a reaction rate constant.
Specifying Event Triggers
TheTrigger
property of an event specifies a condition that must become true for an event to execute. Typically, the condition uses a combination of relational and logical operators to build a trigger expression.
A trigger can contain the keywordtime
and relational operators to trigger an event that occurs at a specific time during the simulation. For example,time >= x
. For more information see theTrigger
property.
Use MATLAB®syntax to write expressions for event triggers. Note that the expression must be a single MATLAB statement that returns a logical. No semicolon or comma is needed at the end of an expression. MATLAB uses specific operator precedence to evaluate trigger expressions. Precedence levels determine the order in which MATLAB evaluates an expression. Within each precedence level, operators have equal precedence and are evaluated from left to right. To find more information on how relational and logical operators are evaluated seeRelational OperationsandLogical (Boolean) Operations.
Some examples of triggers are:
Trigger | Explanation |
---|---|
(time >= 5) && (speciesA < 1000) |
Execute the event when the following condition becomes true: 时间是大于或等于 Tip Using a |
(time >= 5) || (speciesA < 1000) |
Execute the event when the following condition becomes true: 时间是大于或等于 |
(s1 >= 10.0) || (time >= 250) && (s2 < 5.0E17) |
Execute the event when the following condition becomes true: Species, Because of operator precedence, the expression is treated as if it were Thus, it is always a good idea to use parenthesis to explicitly specify the intended precedence of the statements. |
((s1 >= 10.0) || (time >= 250)) && (s2 < 5.0E17) |
Execute the event when the following condition becomes true: Species |
((s1 >= 5000.0) && (time >= 250)) || (s2 < 5.0E17) |
Execute the event when the following condition becomes true: Species |
Tip
IfUnitConversion
is on and your model has any event, follow the recommendation below.
Non-dimensionalize any parameters used in the eventTrigger
if they are not already dimensionless. For example, suppose you have a triggerx > 1
, wherexis the species concentration in mole/liter. Non-dimensionalizexby scaling (dividing) it with a constant such asx/x0 > 1
, wherex0is a parameter defined as 1.0 mole/liter. Note thatx不需要有相同的单位作为常量tx0, but must be dimensionally consistent with it. For example, the unit ofxcan be picomole/liter instead of mole/liter.
Specifying Event Functions
TheEventFcns
property of an event specifies what occurs when the event is triggered. You can use an event function to change the value of a compartment, species, or parameter, or you can specify complex tasks by calling a custom function or script.
Use MATLAB syntax to define expressions for event functions. The expression must be a single MATLAB assignment statement that includes=
, or a cell array of such statements. No semicolon or comma is needed at the end of the expression.
Following are rules for writing expressions for event functions:
EventFcn | Explanation |
---|---|
speciesA = speciesB |
When the event is executed, set the amount ofspeciesA equal to that ofspeciesB . |
k = k/2 |
When the event is executed, halve the value of the rate constantk . |
{'speciesA = speciesB','k = k/2'} |
When the event is executed, set the amount ofspeciesA equal to that ofspeciesB , and halve the value of the rate constantk . |
kC = my_func(A,B,kC) |
When the event is executed, call the custom functionmy_func() . This function takes three arguments: The first two arguments are the current amounts of two species (A andB ) during simulation and the third argument is the current value of a parameter,kC . The function returns the modified value ofkC as its output. |
Simulation Solvers for Models Containing Events
To simulate models containing events, use a deterministic (ODE or SUNDIALS) solver or the stochasticssa
solver. Other stochastic solvers do not support events. For more information, seeChoosing a Simulation Solver.
How Events Are Evaluated
Consider the example of a simple event where you specify that at4s
, you want to assign a value of10
to speciesA
.
Attime = 4 s
the trigger becomes true and the event executes. In the previous figure assuming that0
is false and1
is true, when the trigger becomes true, the amount of speciesA
is set to10
. In theory, with a perfect solver, the event would be executed exactly attime = 4.00 s
. In practice there is a very minute delay (for example you might notice that the event is executed attime = 4.00001 s
). Thus, you must specify that the trigger can become true at or after4s
, which istime >= 4 s
.
Trigger | EventFcn |
---|---|
time >= 4 |
A = 10 |
The point at which the trigger becomes true is called arising edge. SimBiology®events execute theEventFcn
onlyat rising edges.
The trigger is evaluated at every time step to check whether the condition specified in the trigger transitions from false to true. The solver detects and tracksfalling edges, which is when the trigger becomes false, so if another rising edge is encountered, the event is reexecuted. If a trigger is already true before a simulation starts, then the event does not execute at the start of the simulation. The event is not executed until the solver encounters a rising edge. Very rarely, the solver might miss a rising edge. An example of this is when a rising edge follows very quickly after a falling edge, and the step size results in the solver skipping the transition point.
If the trigger becomes true exactly at the stop time of the simulation, the event might or might not execute. If you want the event to execute, increase the stop time.
Note
Since the rising edge is instantaneous and changes the system state, there are two values for the state at the same time. The simulation data thus contains the state before and after the event, but both points are at the same time value. This leads to multiple values of the system state at a single instant in time.
Evaluation of Simultaneous Events
When two or more trigger conditions simultaneously become true, the solver executes the events sequentially in the order in which they are listed in the model. You can reorder events using thereorder
method. For example, consider this case.
Event Number | Trigger |
EventFcn |
---|---|---|
1 | SpeciesA >= 4 |
SpeciesB = 10 |
2 | SpeciesC >= 15 |
SpeciesB = 25 |
解算器试图找到的前沿events within a certain level of tolerance. If this results in both events occurring simultaneously, then the value ofSpeciesB
after the time step in which these two events occur, will be25
. If you reorder the events to reverse the event order, then the value ofSpeciesB
after the time step in which these two events occur, will be10
.
Consider an example in which you include event functions that change model components in a dependent fashion. For example, the event function in Event 2, stipulates thatSpeciesB
takes the value ofSpeciesC
.
Event Number | Trigger |
EventFcn |
---|---|---|
1 | SpeciesA >= 4 |
SpeciesC = 10 |
2 | time >= 15 |
SpeciesB = SpeciesC |
Event 1 and Event 2 might or might not occur simultaneously.
If Event 1 and Event 2 do not occur simultaneously, when Event 2 is triggered,
SpeciesB
is assigned the value thatSpeciesC
has at the time of the event trigger.If Event 1 and Event 2 occur simultaneously, the solver executes Event 1 first, then executes Event 2. In this example, if
SpeciesC = 15
when the events are triggered, after the events are executed,SpeciesC = 10
andSpeciesB = 10
.
Evaluation of Multiple Event Functions
Consider an event function in which you specify that the value of a model component (SpeciesB
) depends on the value of model component (SpeciesA
), butSpeciesA
also is changed by the event function.
Trigger |
EventFcn |
---|---|
time >= 4 |
{'SpeciesA = 10, SpeciesB = SpeciesA'} |
The solver stores the value ofSpeciesA
at the rising edge and before any event functions are executed and uses this stored value to assignSpeciesB
its value. So in this example ifSpeciesA = 15
at the time the event is triggered, after the event is executed,SpeciesA = 10
andSpeciesB = 15
.
When One Event Triggers Another Event
In the next example, Event 1 includes an expression in the event function that causes Event 2 to be triggered (assuming thatSpeciesA
has amount less than5
when Event 1 is executed).
Event Number | Trigger |
EventFcn |
---|---|---|
1 | time >= 5 |
{'SpeciesA = 10, SpeciesB = 5'} |
2 | SpeciesA >= 5 |
SpeciesC = SpeciesB |
When Event 1 is triggered, the solver evaluates and executes Event 1 with the result thatSpeciesA = 10
andSpeciesB = 5
. Now, the trigger for Event 2 becomes true and the solver executes the event function for Event 2. Thus,SpeciesC = 5
at the end of this event execution.
You can thus have event cascades of arbitrary length, for example, Event 1 triggers Event 2, which in turn triggers Event 3, and so on.
Cyclical Events
在一些situations, a series of events can trigger a cascade that becomes cyclical. Once you trigger a cyclical set of events, the only way to stop the simulation is by pressingCtrl+C. You lose any data acquired in the current simulation. Here is an example of cyclical events. This example assumes thatSpecies B <= 4
at the start of the cycle.
Event Number | Trigger |
EventFcn |
---|---|---|
1 | SpeciesA > 10 |
{'SpeciesB = 5', 'SpeciesC = 1'} |
2 | SpeciesB > 4 |
{'SpeciesC = 10', 'SpeciesA = 1'} |
3 | SpeciesC > 9 |
{'SpeciesA = 15', 'SpeciesB = 1'} |
Using Events to Address Discontinuities in Rule and Reaction Rate Expressions
The solvers provided with SimBiology gives inaccurate results when the following expressions are not continuous and differentiable:
Repeated assignment rule
Algebraic rule
Rate rule
Reaction rate
Either ensure that the previous expressions are continuous and differentiable or use events to reset the solver at the discontinuity, as described inDeterministic Simulation of a Model Containing a Discontinuity.