Clock
Jusdl
is a clocked simulation environment. That is, model components are evolved in different time intervals, called the sampling interval. During the simulation, model components are triggered by these generated time pulses. The Clock
type is used to to generate those time pulses. The simulation time settings–the simulation start time, stop time, sampling interval–are configured through the Clock
.
Construction of Clock
Construction of Clock
is done by specifying its start time and final time and the simulation sampling period.
Jusdl.Components.Sources.Clock
— TypeClock(t::Real, dt::Real, tf::Real)
Constructs a Clock
with starting time t
, final time tf
and sampling inteval dt
. When iterated, the Clock
returns its current time.
When constructed, Clock
is not running. To take clock ticks from Clock
, the Clock
must be setted. See take!(clk::Clock)
and set!
Basic Usage of Clocks
A Clock
instance has a Callback list so that a Callback
can be constructed to trigger specific events configured with the time settings. See the following case study.
Let us consider a Clock
with initial time of 0
, sampling interval of 1
and final time of 10
.
julia> using Jusdl # hide
julia> clk = Clock(0., 1., 10.)
Clock(t:0.0, dt:1.0, tf:10.0, paused:false, isrunning:false)
Notice that clk
is not running, since it is not set. Now, let us set it
julia> set!(clk)
Clock(t:0.0, dt:1.0, tf:10.0, paused:false, isrunning:true)
clk
is ready to run, i.e., to be iterated. The following commands generated clock ticks and shows it on the console.
julia> for t in clk
@show t
end
t = 0.0
t = 1.0
t = 2.0
t = 3.0
t = 4.0
t = 5.0
t = 6.0
t = 7.0
t = 8.0
t = 9.0
t = 10.0
At this point, clk
is out of time. The current time of clk
does not advance any more.
julia> take!(clk)
┌ Warning: Clock is out of time.
└ @ Jusdl.Components.Sources ~/.julia/dev/Jusdl/src/components/sources/clock.jl:62
10.0
But, clk
can be reset again.
julia> set!(clk, 0., 1., 10.)
Clock(t:0.0, dt:1.0, tf:10.0, paused:false, isrunning:true)
Consider that we want to configure an alarm. For this, let us consider that when the time of clk
is greater than 5
an alarm message is printed on the console. To this end, we need to construct a Callback
and add it to the callbacks of clk
. (When constructed callback list of clk
is empty.)
julia> condition(clk) = clk.t > 5
condition (generic function with 1 method)
julia> action(clk) = println("Clock time = ", clk.t)
action (generic function with 1 method)
julia> callback = Callback(condition, action)
Callback(condition:condition, action:action)
julia> addcallback(clk, callback)
Clock(t:0.0, dt:1.0, tf:10.0, paused:false, isrunning:true)
Now, let us run clk
by iterating it.
julia> for t in clk
@show t
end
t = 0.0
t = 1.0
t = 2.0
t = 3.0
t = 4.0
t = 5.0
Clock time = 6.0
t = 6.0
Clock time = 7.0
t = 7.0
Clock time = 8.0
t = 8.0
Clock time = 9.0
t = 9.0
Clock time = 10.0
t = 10.0
Note that we, constructed a simple callback. It is of course possible to construct more complex callbacks.
Usage of Clocks with ProgressMeter
It also possible to iterate the Clock
s by using a progress meter. See ProgressMeter for further information for progress meter.
using Jusdl
using ProgressMeter
clk = Clock(0., 0.01, 1.)
set!(clk)
@showprogress for t in clk
end
Note that clk
is just iterated.
Full API
Base.take!
— Methodtake!(clk::Clock)
Takes a values from clk
.
Example
ulia> clk = Clock(0., 0.1, 0.5)
Clock(t:0.0, dt:0.1, tf:0.5, paused:false, isrunning:false)
julia> set!(clk)
Clock(t:0.0, dt:0.1, tf:0.5, paused:false, isrunning:true)
julia> for i = 0 : 5
@show take!(clk)
end
take!(clk) = 0.0
take!(clk) = 0.1
take!(clk) = 0.2
take!(clk) = 0.3
take!(clk) = 0.4
take!(clk) = 0.5
Jusdl.Components.Sources.isrunning
— Functionisrunning(clk::Clock)
Returns true
if clk
if clk
is running.
Jusdl.Components.Sources.ispaused
— Functionispaused(clk::Clock)
Returns true
if clk
is paused. When paused, the currnent time of clk
is not advanced. See also pause!(clk::Clock)
Jusdl.Components.Sources.isoutoftime
— Functionisoutoftime(clk::Clock)
Returns true
if clk
is out of time, i.e., the current time of clk
exceeds its final time.
Jusdl.Components.Sources.set!
— Functionset(clk::Clock, t::Real, dt::Real, tf::Real)
Sets clk
for current clock time t
, sampling time dt
and final time tf
. After the set, it is possible to take clock tick from clk
. See also take!(clk::Clock)
Example
julia> clk = Clock(0., 0.1, 0.5)
Clock(t:0.0, dt:0.1, tf:0.5, paused:false, isrunning:false)
julia> take!(clk)
┌ Warning: Clock is not running.
└ @ Jusdl.Components.Sources ~/.julia/dev/Jusdl/src/components/sources/clock.jl:47
0.0
julia> set!(clk)
Clock(t:0.0, dt:0.1, tf:0.5, paused:false, isrunning:true)
julia> take!(clk)
0.0
Jusdl.Components.Sources.stop!
— Functionstop!(clk::Clock)
Unsets clk
. After the stpp, it is possible to take clock ticks from clk
. See also take!(clk::Clock)
Jusdl.Components.Sources.pause!
— Functionpause!(clk::Clock)
Pauses clk
. When paused, the current time of clk
does not advance.
Example
julia> clk = Clock(0., 0.1, 0.5);
julia> set!(clk);
julia> for i = 1 : 5
i > 3 && pause!(clk)
@show take!(clk)
end
take!(clk) = 0.0
take!(clk) = 0.1
take!(clk) = 0.2
┌ Warning: Clock is paused.
└ @ Jusdl.Components.Sources ~/.julia/dev/Jusdl/src/components/sources/clock.jl:58
take!(clk) = 0.2
┌ Warning: Clock is paused.
└ @ Jusdl.Components.Sources ~/.julia/dev/Jusdl/src/components/sources/clock.jl:58
take!(clk) = 0.2
Base.iterate
— Methoditerate(clk::Clock[, t=clk.t)
Iterationk interface for clk
. clk
can be iterated in a loop.
Example
julia> clk = Clock(0., 0.1, 0.3);
julia> set!(clk)
Clock(t:0.0, dt:0.1, tf:0.3, paused:false, isrunning:true)
julia> for t in clk
@show t
end
t = 0.0
t = 0.1
t = 0.2
t = 0.3