-
Notifications
You must be signed in to change notification settings - Fork 1
/
lotka_volterra.cr
73 lines (58 loc) · 1.91 KB
/
lotka_volterra.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require "../src/quartz"
class LotkaVolterra < Quartz::DTSS::AtomicModel
delta 10, milli # euler integration step
state do
var x : Float64 = 1.0
var y : Float64 = 1.0
var alpha : Float64 = 5.2 # prey reproduction rate
var beta : Float64 = 3.4 # predator per prey mortality rate
var gamma : Float64 = 2.1 # predator mortality rate
var delta : Float64 = 1.4 # predator per prey reproduction rate
end
def transition(_messages)
dxdt = ((x * alpha) - (beta * x * y))
dydt = (-(gamma * y) + (delta * x * y))
self.x += self.time_delta.to_f * dxdt
self.y += self.time_delta.to_f * dydt
end
def output
end
end
class Tracer
include Quartz::Hooks::Notifiable
include Quartz::Observer
@file : File?
SPACES = 30
def initialize(model, notifier)
notifier.subscribe(Quartz::Hooks::PRE_INIT, self)
notifier.subscribe(Quartz::Hooks::POST_SIMULATION, self)
model.add_observer(self)
end
def notify(hook)
case hook
when Quartz::Hooks::PRE_INIT
@file = File.new("lotkavolterra.dat", "w+")
@file.not_nil!.printf("%-#{SPACES}s %-#{SPACES}s %-#{SPACES}s\n", 't', 'x', 'y')
when Quartz::Hooks::POST_SIMULATION
@file.not_nil!.close
@file = nil
else
# no-op
end
end
def update(model, info)
if model.is_a?(LotkaVolterra)
lotka = model.as(LotkaVolterra)
time = info[:time].as(Quartz::TimePoint)
@file.not_nil!.printf("%-#{SPACES}s %-#{SPACES}s %-#{SPACES}s\n", time.to_s, lotka.x, lotka.y)
end
end
end
model = LotkaVolterra.new(:LotkaVolterra)
sim = Quartz::Simulation.new(model, scheduler: :binary_heap, duration: Quartz.duration(10))
Tracer.new(model, sim.notifier)
sim.simulate
puts sim.transition_stats[:TOTAL]
puts sim.elapsed_secs
puts "Dataset written to 'lotkavolterra.dat'."
puts "Run 'gnuplot -e \"plot 'lotkavolterra.dat' u 1:2 w l t 'preys', '' u 1:3 w l t 'predators'; pause -1;\"' to graph output"