Discrete events are used by various blocks in order to trig an action. The snapshot block uses a discrete event in order to trigger the snapshot, the transfer block can specify a discrete transfer by referencing a discrete event.
The discrete event can also be used to notify the ODE solver that something drastic has happened in the model and that the solver needs to reduce the current time_step. A typical case is for models that contain conditions. In a model with a condition such as (time>t_leakage), where t_leakage is a parameter indicating when a container starts to leak, it is important that the solver includes this time point when the simulation is run.
The block has a right hand side expression and a left hand side expression. The event is triggered when these are equal. Whenever a time step is taken by the solver, the solver will see if the difference of the two expressions has changed sign since the last time step. If so, the solver will try to backtrace to the time point when they are equal. After, the solver will restart with a time step given in simulation settings as the initial time step.
From the Projects window
From the Matrix window
From the Blocks window
Place the mouse cursor over the table and right-click on it. Select New | Discrete event from the pop-up menu, or
In the title bar, select Discrete event from the Type drop-down list and click on the button.
Note | Most block properties can be edited directly in the blocks window. |
The block editor is shown by right-clicking the block in a window and selecting Edit. In the Blocks window, you can also show the block editor by selecting the block and clicking the button, or by double-clicking the right-most “validation” column of the block.
The block editor has three tabs:
This page lets you edit basic properties such as name, sub-system and base unit. See editing properties for more information.
This page lets you edit the visual properties of the block in the Matrix window. See Editing appearance for more information.
Note | When entering an expression, you can also enter names of objects that are not yet created and let Ecolego use automatic object creation to define them. |
This page lets you specify the condition for the event. It is important that the terms are put on the correct sides. The left hand side should approach the right hand side.
For the example given in the introduction, the left hand side would be “time” and the right hand side would be “t_leakage” because time will approach t_leakage from below.
Another example would be when a discrete event is used to assert that the quantity of a compartment does not decrease below a certain limit. In a transfer, the following expression is written: (Comp1>limit) · TC, where limit is the lower boundary for the compartment. In this case, Comp1 will approach limit from above, so the terms should be shifted in the discrete event: the left hand term is limit and the right hand side is Comp1.
Dimension - You can specify the dimension of the block by using the Type drop down list. Depending on the dimension the index drop down lists are activated. See editing values for more information.
Data
Indices - For blocks that are non-scalar, the first columns will specify indices from the index lists. This allows you to specify different expressions for different indices.
First expression - The left hand side.
Direction - The expected direction of the event. If the left hand side is expected to exceed the right hand side when the event occurs, use >>. In the opposite case, use <<. If the event should be triggered for both cases, use BOTH.
Second expression - The right hand side.
Information - By using the Table control button you can display more columns that will help you document your model.
For recurring events, you do not need to create several discrete event blocks. By using a trick, it is possible to make an event trigger several times. A typical case is when an event should be triggered every year, month or day.
During the simulation an event is triggered when the sign of the difference of the left hand side expression and the right hand side expression changes between two time points. In other words; for every time step taken, sign(leftexpression-rightexpression)
will be checked. As explained in the introduction, when the sign changes the solver will backtrace to a time point when the two sides are equal.
In the following example we will create an event that is trigged every month of the year. The simulation time unit is years. We need to construct expressions so that the left hand side is equal to the right hand side 12 times every year.
One way to achieve this is to compare the two expressions time/12
with round(time/12)
. The table below shows the value of the two over two years:
Time | time/12 | round(time/12) | time/12-round(time/12) |
---|---|---|---|
0,0 | 0,0 | 0,0 | 0,0 |
1,0 | 0,1 | 0,0 | 0,1 |
2,0 | 0,2 | 0,0 | 0,2 |
6,0 | 0,5 | 1,0 | -0,5 |
12,0 | 1,0 | 1,0 | 0,0 |
13,0 | 1,1 | 1,0 | 0,1 |
18,0 | 1,5 | 2,0 | -0,5 |
24,0 | 2,0 | 2,0 | 0,0 |
40,0 | 3,3 | 3,0 | 0,3 |
45,0 | 3,8 | 4,0 | -0,3 |