-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.py
86 lines (65 loc) · 2.27 KB
/
mesh.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
class cell:
def __init__(self, idx, x, y, e, n, w, s, isfuel=False):
self.idx = idx
self.x = x
self.y = y
self.e = e
self.n = n
self.w = w
self.s = s
self.fuel = 0
if isfuel:
self.fuel = 10
self.active = False
def get_burn_rate(self, msh):
if not self.active:
return 0
burn_rate = 4
if self.e and msh.cells[self.e].fuel > 0:
burn_rate -= 1
if self.n and msh.cells[self.n].fuel > 0:
burn_rate -= 1
if self.w and msh.cells[self.w].fuel > 0:
burn_rate -= 1
if self.s and msh.cells[self.s].fuel > 0:
burn_rate -= 1
return burn_rate**0.5
def update_active(self, msh):
if self.fuel <= 0:
self.fuel = 0
self.active = False
return
if self.e and msh.cells[self.e].active and msh.cells[self.e].get_burn_rate(msh) > 0:
self.active = True
if self.n and msh.cells[self.n].active and msh.cells[self.n].get_burn_rate(msh) > 0:
self.active = True
if self.w and msh.cells[self.w].active and msh.cells[self.w].get_burn_rate(msh) > 0:
self.active = True
if self.s and msh.cells[self.s].active and msh.cells[self.s].get_burn_rate(msh) > 0:
self.active = True
def update(self, msh, dt):
self.update_active(msh)
if self.active:
self.burn_rate = self.get_burn_rate(msh)
self.fuel -= self.burn_rate * dt
class mesh:
def __init__(self, Nx, Ny):
self.Nx = Nx
self.Ny = Ny
self.cells = [None] * Ny * Nx
for j in range(Ny):
for i in range(Nx):
abs_idx = j * Nx + i
e = j * Nx + (i + 1)
n = (j+1) * Nx + i
w = j * Nx + (i - 1)
s = (j-1) * Nx + i
if j == Ny-1:
n = None
elif j == 0:
s = None
if i == Nx-1:
e = None
elif i == 0:
w = None
self.cells[abs_idx] = cell(abs_idx, i, j, e, n, w, s, False)