/
board.py
282 lines (224 loc) · 8.57 KB
/
board.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from threading import Thread
from ipycanvas import MultiCanvas, hold_canvas
from ipywidgets import (Button, FloatSlider, IntSlider, HTML, Label,
HBox, VBox, Layout)
from .toposim import TopographySimulator
from .particles import Particles
from .buckets import Buckets
class Board:
def __init__(self, scale=3, buckets_height=150):
self.scale = scale
self.buckets_height = buckets_height
self.toposim = TopographySimulator()
self.particles = Particles(self.toposim, scale)
self.buckets = Buckets(self.particles, scale)
self.setup_canvas()
self.setup_play_widgets()
self.setup_particles_widgets()
self.setup_toposim_widgets()
self.setup_layout()
self.process = None
self._running = False
def setup_canvas(self):
# canvas 0: topography
# canvas 1: particles
# canvas 2: buckets
self.canvas = MultiCanvas(
ncanvases=3,
width=self.scale * self.toposim.shape[0],
height=self.scale * self.toposim.shape[1] + self.buckets_height
)
self.canvas.on_client_ready(self.redraw)
def setup_play_widgets(self):
self.play_widgets = {
'start': Button(description="Start", icon='play'),
'stop': Button(description="Stop/Reset", icon='stop',
disabled=True)
}
self.play_widgets['start'].on_click(self.start)
self.play_widgets['stop'].on_click(self.stop)
def setup_particles_widgets(self):
self.particles_labels = {
'size': Label(value='Number of particles'),
'speed': Label(value='Particle "speed"')
}
self.particles_widgets = {
'size': IntSlider(
value=10000, min=500, max=15000, step=500
),
'speed': FloatSlider(
value=0.5, min=0.1, max=1., step=0.1
)
}
self.particles_widgets['size'].observe(
self.on_change_size, names='value'
)
self.particles_widgets['speed'].observe(
self.on_change_speed, names='value'
)
def on_change_size(self, change):
self.particles.n_particles = change.new
self.initialize()
def on_change_speed(self, change):
self.particles.speed_factor = change.new
def setup_toposim_widgets(self):
self.toposim_labels = {
'kf': Label(value='River incision coefficient'),
'g': Label(value='River transport coefficient'),
'kd': Label(value='Hillslope diffusivity'),
'p': Label(value='Flow partition exponent'),
'u': Label(value='Plateau uplift rate')
}
self.toposim_widgets = {
'kf': FloatSlider(
value=1e-4, min=5e-5, max=3e-4, step=1e-5,
readout_format='.1e'
),
'g': FloatSlider(
value=1., min=0.5, max=1.5, step=0.1,
readout_format='.1f'
),
'kd': FloatSlider(
value=0.02, min=0., max=0.1, step=0.01,
),
'p': FloatSlider(
value=1., min=0., max=10., step=0.2,
readout_format='.1f'
),
'u': FloatSlider(
value=0., min=0., max=1e-3, step=1e-5,
readout_format='.1e'
)
}
def set_erosion_params(self):
self.toposim.set_erosion_params(
kf=self.toposim_widgets['kf'].value,
g=self.toposim_widgets['g'].value,
kd=self.toposim_widgets['kd'].value,
p=self.toposim_widgets['p'].value,
u=self.toposim_widgets['u'].value
)
def setup_layout(self):
play_box = HBox(tuple(self.play_widgets.values()))
particles_hboxes = []
for k in self.particles_widgets:
self.particles_labels[k].layout = Layout(width='200px')
self.particles_widgets[k].layout = Layout(width='200px')
particles_hboxes.append(
HBox([self.particles_labels[k], self.particles_widgets[k]])
)
particles_label = HTML(value='<b>Particles parameters</b>')
particles_box = VBox(particles_hboxes)
particles_box.layout = Layout(grid_gap='6px')
toposim_hboxes = []
for k in self.toposim_widgets:
self.toposim_labels[k].layout = Layout(width='200px')
self.toposim_widgets[k].layout = Layout(width='200px')
toposim_hboxes.append(
HBox([self.toposim_labels[k], self.toposim_widgets[k]])
)
toposim_label = HTML(
value='<b>Landscape evolution model parameters</b>'
)
toposim_box = VBox(toposim_hboxes)
toposim_box.layout = Layout(grid_gap='6px')
control_box = VBox((
play_box,
particles_label,
particles_box,
toposim_label,
toposim_box
))
control_box.layout = Layout(grid_gap='10px')
self.main_box = HBox((self.canvas, control_box))
self.main_box.layout = Layout(grid_gap='30px')
def initialize(self):
self.toposim.initialize()
self.particles.initialize()
self.buckets.initialize()
self.redraw()
def run(self):
while self._running and not self.buckets.all_in_buckets:
self.set_erosion_params()
self.toposim.run_step()
self.particles.run_step()
self.buckets.run_step()
self.redraw()
self.draw_winner()
self.play_widgets['stop'].description = "Reset"
self.play_widgets['stop'].icon = "retweet"
def toggle_disabled(self):
for w in self.play_widgets.values():
w.disabled = not w.disabled
w = self.particles_widgets['size']
w.disabled = not w.disabled
def start(self, b):
self.process = Thread(target=self.run)
self._running = True
self.process.start()
self.toggle_disabled()
def stop(self, b):
self._running = False
self.process.join()
self.reset()
self.toggle_disabled()
def reset(self):
self.toposim.reset()
self.particles.reset()
self.buckets.reset()
self.redraw()
self.play_widgets['stop'].description = "Stop/Reset"
self.play_widgets['stop'].icon = "stop"
def redraw(self):
self.draw_topography()
self.draw_particles()
self.draw_buckets()
def draw_topography(self):
with hold_canvas(self.canvas[0]):
self.canvas[0].save()
self.canvas[0].scale(self.scale)
self.canvas[0].clear()
self.canvas[0].put_image_data(
self.toposim.shaded_topography, 0, 0
)
self.canvas[0].restore()
def draw_particles(self):
x, y = self.particles.positions
with hold_canvas(self.canvas[1]):
self.canvas[1].clear()
self.canvas[1].global_alpha = 0.4
self.canvas[1].fill_style = '#3378b8'
self.canvas[1].fill_rects(x, y, self.particles.sizes)
def draw_buckets(self):
xsize, ysize = self.canvas[2].width, self.canvas[2].height
with hold_canvas(self.canvas[2]):
self.canvas[2].clear()
self.canvas[2].font = '20px serif'
self.canvas[2].fill_style = 'black'
for i, x in enumerate(self.buckets.x_separators[0:-1]):
self.canvas[2].fill_text(f"{i+1:02d}", x + 15, ysize - 120)
self.canvas[2].fill_rects(
self.buckets.x_separators,
self.toposim.shape[0] * self.scale,
1, self.buckets_height
)
self.canvas[2].fill_style = '#3378b8'
self.canvas[2].fill_rects(
self.buckets.x_separators + 5,
ysize - self.buckets.bar_heights,
self.buckets.bar_width - 10,
self.buckets.bar_heights
)
self.canvas[2].fill_style = 'black'
self.canvas[2].fill_rect(0, ysize - 3, xsize, 3)
self.canvas[2].fill_rect(0, ysize - 155, xsize, 10)
def draw_winner(self):
xsize, ysize = self.canvas[2].width, self.canvas[2].height
winner = self.buckets.count.argmax()
self.canvas[2].font = '50px serif'
self.canvas[2].fill_style = '#3378b8'
self.canvas[2].fill_text(f"{winner+1:02d} wins!",
xsize // 3 , ysize // 2.5)
def show(self):
self.initialize()
return self.main_box