Clock
Causal 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. A Clock instance 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.
julia> using Causal # hide
julia> Clock(0., 1, 10.)
Clock(t:0.0, dt:1.0, tf:10.0, paused:false, isrunning:false)
julia> Clock{Int}(1, 1, 10)
Clock(t:1, dt:1, tf:10, paused:false, isrunning:false)Basic Usage of Clocks
A Clock 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 Causal # 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.0At 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.
└ @ Causal ~/build/zekeriyasari/Causal.jl/src/components/sources/clock.jl:66
10.0But, 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> clk = Clock(0., 1., 10., callbacks=Callback(condition=condition, action=action))
Clock(t:0.0, dt:1.0, tf:10.0, paused:false, isrunning:false)
julia> set!(clk)
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
t = 6.0
Clock time = 6.0
t = 7.0
Clock time = 7.0
t = 8.0
Clock time = 8.0
t = 9.0
Clock time = 9.0
t = 10.0
Clock time = 10.0Note 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 Clocks by using a progress meter. See ProgressMeter for further information for progress meter.
using Causal
using ProgressMeter
clk = Clock(0., 0.01, 1.)
set!(clk)
@showprogress for t in clk
end Note that clk is just iterated.
Full API
Causal.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!
Base.iterate — Functioniterate(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.3Base.take! — Methodtake!(clk::Clock)Takes a values from clk.
Example
julia> 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 in 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.5Causal.isoutoftime — Methodisoutoftime(clk::Clock)Returns true if clk is out of time, i.e., the current time of clk exceeds its final time.
Causal.ispaused — Methodispaused(clk::Clock)Returns true if clk is paused. When paused, the currnent time of clk is not advanced. See also pause!(clk::Clock)
Causal.isrunning — Methodisrunning(clk::Clock)Returns true if clk if clk is running.
Causal.pause! — Methodpause!(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.
└ @ Causal ~/.julia/dev/Causal/src/components/sources/clock.jl:61
take!(clk) = 0.2
┌ Warning: Clock is paused.
└ @ Causal ~/.julia/dev/Causal/src/components/sources/clock.jl:61
take!(clk) = 0.2Causal.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> set!(clk)
Clock(t:0.0, dt:0.1, tf:0.5, paused:false, isrunning:true)
julia> take!(clk)
0.0Causal.stop! — Methodstop!(clk::Clock)Unsets clk. After the stpp, it is possible to take clock ticks from clk. See also take!(clk::Clock)