-
Notifications
You must be signed in to change notification settings - Fork 4
/
particle_system.py
201 lines (175 loc) · 8.23 KB
/
particle_system.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
import taichi as ti
import numpy as np
from functools import reduce
@ti.data_oriented
class ParticleSystem:
def __init__(self, res):
self.res = res
self.dim = len(res)
assert self.dim > 1
self.screen_to_world_ratio = 50
self.bound = np.array(res) / self.screen_to_world_ratio
# Material
self.material_boundary = 0
self.material_fluid = 1
self.particle_radius = 0.05 # particle radius
self.particle_diameter = 2 * self.particle_radius
self.support_radius = self.particle_radius * 4.0 # support radius
self.m_V = 0.8 * self.particle_diameter ** self.dim
self.particle_max_num = 2 ** 15
self.particle_max_num_per_cell = 100
self.particle_max_num_neighbor = 100
self.particle_num = ti.field(int, shape=())
# Grid related properties
self.grid_size = self.support_radius
self.grid_num = np.ceil(np.array(res) / self.grid_size).astype(int)
self.grid_particles_num = ti.field(int)
self.grid_particles = ti.field(int)
self.padding = self.grid_size
# Particle related properties
self.x = ti.Vector.field(self.dim, dtype=float)
self.v = ti.Vector.field(self.dim, dtype=float)
self.density = ti.field(dtype=float)
self.pressure = ti.field(dtype=float)
self.material = ti.field(dtype=int)
self.color = ti.field(dtype=int)
self.particle_neighbors = ti.field(int)
self.particle_neighbors_num = ti.field(int)
self.particles_node = ti.root.dense(ti.i, self.particle_max_num)
self.particles_node.place(self.x, self.v, self.density, self.pressure, self.material, self.color)
self.particles_node.place(self.particle_neighbors_num)
self.particle_node = self.particles_node.dense(ti.j, self.particle_max_num_neighbor)
self.particle_node.place(self.particle_neighbors)
index = ti.ij if self.dim == 2 else ti.ijk
grid_node = ti.root.dense(index, self.grid_num)
grid_node.place(self.grid_particles_num)
cell_index = ti.k if self.dim == 2 else ti.l
cell_node = grid_node.dense(cell_index, self.particle_max_num_per_cell)
cell_node.place(self.grid_particles)
@ti.func
def add_particle(self, p, x, v, density, pressure, material, color):
self.x[p] = x
self.v[p] = v
self.density[p] = density
self.pressure[p] = pressure
self.material[p] = material
self.color[p] = color
@ti.kernel
def add_particles(self, new_particles_num: int,
new_particles_positions: ti.ext_arr(),
new_particles_velocity: ti.ext_arr(),
new_particle_density: ti.ext_arr(),
new_particle_pressure: ti.ext_arr(),
new_particles_material: ti.ext_arr(),
new_particles_color: ti.ext_arr()):
for p in range(self.particle_num[None], self.particle_num[None] + new_particles_num):
v = ti.Vector.zero(float, self.dim)
x = ti.Vector.zero(float, self.dim)
for d in ti.static(range(self.dim)):
v[d] = new_particles_velocity[p - self.particle_num[None], d]
x[d] = new_particles_positions[p - self.particle_num[None], d]
self.add_particle(p, x, v,
new_particle_density[p - self.particle_num[None]],
new_particle_pressure[p - self.particle_num[None]],
new_particles_material[p - self.particle_num[None]],
new_particles_color[p - self.particle_num[None]])
self.particle_num[None] += new_particles_num
@ti.func
def pos_to_index(self, pos):
return (pos / self.grid_size).cast(int)
@ti.func
def is_valid_cell(self, cell):
# Check whether the cell is in the grid
flag = True
for d in ti.static(range(self.dim)):
flag = flag and (0 <= cell[d] < self.grid_num[d])
return flag
@ti.kernel
def allocate_particles_to_grid(self):
for p in range(self.particle_num[None]):
cell = self.pos_to_index(self.x[p])
offset = self.grid_particles_num[cell].atomic_add(1)
self.grid_particles[cell, offset] = p
@ti.kernel
def search_neighbors(self):
for p_i in range(self.particle_num[None]):
# Skip boundary particles
if self.material[p_i] == self.material_boundary:
continue
center_cell = self.pos_to_index(self.x[p_i])
cnt = 0
for offset in ti.grouped(ti.ndrange(*((-1, 2),) * self.dim)):
if cnt >= self.particle_max_num_neighbor:
break
cell = center_cell + offset
if not self.is_valid_cell(cell):
break
for j in range(self.grid_particles_num[cell]):
p_j = self.grid_particles[cell, j]
distance = (self.x[p_i] - self.x[p_j]).norm()
if p_i != p_j and distance < self.support_radius:
self.particle_neighbors[p_i, cnt] = p_j
cnt += 1
self.particle_neighbors_num[p_i] = cnt
def initialize_particle_system(self):
self.grid_particles_num.fill(0)
self.particle_neighbors.fill(-1)
self.allocate_particles_to_grid()
self.search_neighbors()
@ti.kernel
def copy_to_numpy_nd(self, np_arr: ti.ext_arr(), src_arr: ti.template()):
for i in range(self.particle_num[None]):
for j in ti.static(range(self.dim)):
np_arr[i, j] = src_arr[i][j]
@ti.kernel
def copy_to_numpy(self, np_arr: ti.ext_arr(), src_arr: ti.template()):
for i in range(self.particle_num[None]):
np_arr[i] = src_arr[i]
def dump(self):
np_x = np.ndarray((self.particle_num[None], self.dim), dtype=np.float32)
self.copy_to_numpy_nd(np_x, self.x)
np_v = np.ndarray((self.particle_num[None], self.dim), dtype=np.float32)
self.copy_to_numpy_nd(np_v, self.v)
np_material = np.ndarray((self.particle_num[None],), dtype=np.int32)
self.copy_to_numpy(np_material, self.material)
np_color = np.ndarray((self.particle_num[None],), dtype=np.int32)
self.copy_to_numpy(np_color, self.color)
return {
'position': np_x,
'velocity': np_v,
'material': np_material,
'color': np_color
}
def add_cube(self,
lower_corner,
cube_size,
material,
color=0xFFFFFF,
density=None,
pressure=None,
velocity=None):
num_dim = []
for i in range(self.dim):
num_dim.append(
np.arange(lower_corner[i], lower_corner[i] + cube_size[i],
self.particle_radius))
num_new_particles = reduce(lambda x, y: x * y,
[len(n) for n in num_dim])
assert self.particle_num[
None] + num_new_particles <= self.particle_max_num
new_positions = np.array(np.meshgrid(*num_dim,
sparse=False,
indexing='ij'),
dtype=np.float32)
new_positions = new_positions.reshape(-1,
reduce(lambda x, y: x * y, list(new_positions.shape[1:]))).transpose()
print("new position shape ", new_positions.shape)
if velocity is None:
velocity = np.full_like(new_positions, 0)
else:
velocity = np.array([velocity for _ in range(num_new_particles)], dtype=np.float32)
material = np.full_like(np.zeros(num_new_particles), material)
color = np.full_like(np.zeros(num_new_particles), color)
density = np.full_like(np.zeros(num_new_particles), density if density is not None else 1000.)
pressure = np.full_like(np.zeros(num_new_particles), pressure if pressure is not None else 0.)
self.add_particles(num_new_particles, new_positions, velocity, density, pressure, material, color)