-
Notifications
You must be signed in to change notification settings - Fork 1
/
firespread_multipdevs.cr
228 lines (194 loc) · 5.41 KB
/
firespread_multipdevs.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require "../src/quartz"
class HeatCell < Quartz::MultiComponent::Component
T_AMBIENT = 27.0f32
T_IGNITE = 300.0f32
T_GENERATE = 500.0f32
T_BURNED = 60.0f32
TIMESTEP = 1
RADIUS = 3
TMP_DIFF = T_AMBIENT
state do
var temperature : Float32 = T_AMBIENT
var ignite_time : Float64 = Float64::INFINITY
var phase : Symbol = :inactive
var old_temp : Float32 = T_AMBIENT
var surrounding_temps : Hash(Quartz::Name, Float32) = Hash(Quartz::Name, Float32).new(
default_value: T_AMBIENT.to_f32
)
var time : Float64 = 0.0
end
getter x : Int32 = 0
getter y : Int32 = 0
def initialize(name, state, @x, @y)
super(name, state)
end
def initialize(name, @x, @y)
super(name)
end
def new_phase
if temperature > T_IGNITE || (temperature > T_BURNED && phase == :burning)
:burning
elsif temperature < T_BURNED && phase == :burning
:burned
else
:unburned
end
end
def time_advance : Quartz::Duration
case phase
when :inactive, :burned
Quartz::Duration::INFINITY
else # when :unburned, :burning
Quartz.duration(1)
end
end
def internal_transition : Hash(Quartz::Name, Quartz::Any)
proposed_states = Hash(Quartz::Name, Quartz::Any).new
new_old_temp = old_temp
sum = influencers.map { |i| surrounding_temps[i.name] }.reduce { |acc, i| acc + i }
if (temperature - old_temp).abs > TMP_DIFF
influencees.each do |j|
next if j == self
proposed_states[j.name] = Quartz::Any.new(temperature)
end
new_old_temp = temperature
end
ct = time + self.time_advance.to_f
new_temp = case phase
when :burning
0.98689 * temperature + 0.0031 * sum + 2.74 * Math.exp(-0.19 * (ct * TIMESTEP - ignite_time)) + 0.213
when :unburned
0.98689 * temperature + 0.0031 * sum + 0.213
else
temperature
end
n_phase = self.new_phase
new_ignite_time = ct * TIMESTEP if phase == :unburned && n_phase == :burning
nstate = HeatCell::State.new(
old_temp: new_old_temp,
temperature: new_temp.to_f32,
phase: n_phase,
ignite_time: new_ignite_time || ignite_time,
surrounding_temps: surrounding_temps,
time: time
)
proposed_states[self.name] = Quartz::Any.new(nstate)
proposed_states
end
def reaction_transition(states)
temps = surrounding_temps
influence = false
states.each_with_index do |tuple, i|
influencer, val = tuple
case influencer
when self.name
self.state = val.raw.as(HeatCell::State)
else
influence = true
temps[influencer] = val.as_f32
end
end
surrounding_temps = temps
if influence && phase == :inactive
@phase = :unburned
end
end
end
class HeatMultiPDEVS < Quartz::MultiComponent::Model
getter rows : Int32 = 0
getter columns : Int32 = 0
getter cells : Array(Array(HeatCell))
def initialize(name, filepath)
super(name)
@cells = Array(Array(HeatCell)).new
file = File.new(filepath, "r")
y = 0
file.each_line do |l|
x = 0
row = l.split(/[ ]+/).map(&.to_i).map do |value|
name = "cell_#{x}_#{y}"
cell = if value > HeatCell::T_AMBIENT
phase = value >= HeatCell::T_IGNITE ? :burning : :unburned
state = HeatCell::State.new(temperature: value.to_f32, ignite_time: 0f64, phase: phase)
HeatCell.new(name, state, x, y)
else
HeatCell.new(name, x, y)
end
self << cell
x += 1
cell
end
cells << row
y += 1
end
@rows = cells.size
@columns = cells.first.size
# set neighbors
cells.each do |row|
row.each do |cell|
cell.influencees << cell
cell.influencers << cell
((cell.x - 1)..(cell.x + 1)).each do |x|
((cell.y - 1)..(cell.y + 1)).each do |y|
if x >= 0 && y >= 0 && x < columns && y < rows
if x != cell.x || y != cell.y
neighbor = cells[y][x]
cell.influencers << neighbor
cell.influencees << neighbor
end
end
end
end
end
end
end
end
class Consolify
include Quartz::Observer
CLR = "\033c"
@rows : Int32
@columns : Int32
@sim : Quartz::Simulation
def initialize(model : HeatMultiPDEVS, @sim)
@rows = model.rows
@columns = model.columns
model.add_observer(self)
end
def update(model, info)
if model.is_a?(HeatMultiPDEVS)
model = model.as(HeatMultiPDEVS)
puts CLR
i = 0
while i < @rows
j = 0
while j < @columns
case model.cells[i][j].phase
when :inactive, :unburned
print "❀ "
when :burning
print "◼ "
else # :burned
print " "
end
j += 1
end
print "\n"
i += 1
end
print "\n\nt=#{info[:time]}\n"
STDOUT.flush
sleep 0.01
end
end
end
CLR = "\033c"
CLI = true
filepath = if ARGV.size == 1
ARGV.first
else
"examples/init/fire25_25.txt"
end
model = HeatMultiPDEVS.new(:heat, filepath)
simulation = Quartz::Simulation.new(model, duration: Quartz::Duration.new(600))
Consolify.new(model, simulation) if CLI
simulation.simulate