-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
stable_fluid.py
395 lines (323 loc) · 10.9 KB
/
stable_fluid.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# References:
# http://developer.download.nvidia.com/books/HTML/gpugems/gpugems_ch38.html
# https://github.com/PavelDoGreat/WebGL-Fluid-Simulation
# https://www.bilibili.com/video/BV1ZK411H7Hc?p=4
# https://github.com/ShaneFX/GAMES201/tree/master/HW01
import argparse
import numpy as np
import taichi as ti
# How to run:
# `python stable_fluid.py`: use the jacobi iteration to solve the linear system.
# `python stable_fluid.py -S`: use a sparse matrix to do so.
parser = argparse.ArgumentParser()
parser.add_argument(
"-S",
"--use-sp-mat",
action="store_true",
help="Solve Poisson's equation by using a sparse matrix",
)
parser.add_argument(
"-a",
"--arch",
required=False,
default="cpu",
dest="arch",
type=str,
help="The arch (backend) to run this example on",
)
args, unknowns = parser.parse_known_args()
res = 512
dt = 0.03
p_jacobi_iters = 500 # 40 for a quicker but less accurate result
f_strength = 10000.0
curl_strength = 0
time_c = 2
maxfps = 60
dye_decay = 1 - 1 / (maxfps * time_c)
force_radius = res / 2.0
debug = False
use_sparse_matrix = args.use_sp_mat
arch = args.arch
if arch in ["x64", "cpu", "arm64"]:
ti.init(arch=ti.cpu)
elif arch in ["cuda", "gpu"]:
ti.init(arch=ti.cuda)
else:
raise ValueError("Only CPU and CUDA backends are supported for now.")
if use_sparse_matrix:
print("Using sparse matrix")
else:
print("Using jacobi iteration")
_velocities = ti.Vector.field(2, float, shape=(res, res))
_new_velocities = ti.Vector.field(2, float, shape=(res, res))
velocity_divs = ti.field(float, shape=(res, res))
velocity_curls = ti.field(float, shape=(res, res))
_pressures = ti.field(float, shape=(res, res))
_new_pressures = ti.field(float, shape=(res, res))
_dye_buffer = ti.Vector.field(3, float, shape=(res, res))
_new_dye_buffer = ti.Vector.field(3, float, shape=(res, res))
class TexPair:
def __init__(self, cur, nxt):
self.cur = cur
self.nxt = nxt
def swap(self):
self.cur, self.nxt = self.nxt, self.cur
velocities_pair = TexPair(_velocities, _new_velocities)
pressures_pair = TexPair(_pressures, _new_pressures)
dyes_pair = TexPair(_dye_buffer, _new_dye_buffer)
if use_sparse_matrix:
# use a sparse matrix to solve Poisson's pressure equation.
@ti.kernel
def fill_laplacian_matrix(A: ti.types.sparse_matrix_builder()):
for i, j in ti.ndrange(res, res):
row = i * res + j
center = 0.0
if j != 0:
A[row, row - 1] += -1.0
center += 1.0
if j != res - 1:
A[row, row + 1] += -1.0
center += 1.0
if i != 0:
A[row, row - res] += -1.0
center += 1.0
if i != res - 1:
A[row, row + res] += -1.0
center += 1.0
A[row, row] += center
N = res * res
K = ti.linalg.SparseMatrixBuilder(N, N, max_num_triplets=N * 6)
F_b = ti.ndarray(ti.f32, shape=N)
fill_laplacian_matrix(K)
L = K.build()
solver = ti.linalg.SparseSolver(solver_type="LLT")
solver.analyze_pattern(L)
solver.factorize(L)
@ti.func
def sample(qf, u, v):
I = ti.Vector([int(u), int(v)])
I = ti.max(0, ti.min(res - 1, I))
return qf[I]
@ti.func
def lerp(vl, vr, frac):
# frac: [0.0, 1.0]
return vl + frac * (vr - vl)
@ti.func
def bilerp(vf, p):
u, v = p
s, t = u - 0.5, v - 0.5
# floor
iu, iv = ti.floor(s), ti.floor(t)
# fract
fu, fv = s - iu, t - iv
a = sample(vf, iu, iv)
b = sample(vf, iu + 1, iv)
c = sample(vf, iu, iv + 1)
d = sample(vf, iu + 1, iv + 1)
return lerp(lerp(a, b, fu), lerp(c, d, fu), fv)
# 3rd order Runge-Kutta
@ti.func
def backtrace(vf: ti.template(), p, dt_: ti.template()):
v1 = bilerp(vf, p)
p1 = p - 0.5 * dt_ * v1
v2 = bilerp(vf, p1)
p2 = p - 0.75 * dt_ * v2
v3 = bilerp(vf, p2)
p -= dt_ * ((2 / 9) * v1 + (1 / 3) * v2 + (4 / 9) * v3)
return p
@ti.kernel
def advect(vf: ti.template(), qf: ti.template(), new_qf: ti.template()):
for i, j in vf:
p = ti.Vector([i, j]) + 0.5
p = backtrace(vf, p, dt)
new_qf[i, j] = bilerp(qf, p) * dye_decay
@ti.kernel
def apply_impulse(vf: ti.template(), dyef: ti.template(), imp_data: ti.types.ndarray()):
g_dir = -ti.Vector([0, 9.8]) * 300
for i, j in vf:
omx, omy = imp_data[2], imp_data[3]
mdir = ti.Vector([imp_data[0], imp_data[1]])
dx, dy = (i + 0.5 - omx), (j + 0.5 - omy)
d2 = dx * dx + dy * dy
# dv = F * dt
factor = ti.exp(-d2 / force_radius)
dc = dyef[i, j]
a = dc.norm()
momentum = (mdir * f_strength * factor + g_dir * a / (1 + a)) * dt
v = vf[i, j]
vf[i, j] = v + momentum
# add dye
if mdir.norm() > 0.5:
dc += ti.exp(-d2 * (4 / (res / 15) ** 2)) * ti.Vector([imp_data[4], imp_data[5], imp_data[6]])
dyef[i, j] = dc
@ti.kernel
def divergence(vf: ti.template()):
for i, j in vf:
vl = sample(vf, i - 1, j)
vr = sample(vf, i + 1, j)
vb = sample(vf, i, j - 1)
vt = sample(vf, i, j + 1)
vc = sample(vf, i, j)
if i == 0:
vl.x = -vc.x
if i == res - 1:
vr.x = -vc.x
if j == 0:
vb.y = -vc.y
if j == res - 1:
vt.y = -vc.y
velocity_divs[i, j] = (vr.x - vl.x + vt.y - vb.y) * 0.5
@ti.kernel
def vorticity(vf: ti.template()):
for i, j in vf:
vl = sample(vf, i - 1, j)
vr = sample(vf, i + 1, j)
vb = sample(vf, i, j - 1)
vt = sample(vf, i, j + 1)
velocity_curls[i, j] = (vr.y - vl.y - vt.x + vb.x) * 0.5
@ti.kernel
def pressure_jacobi(pf: ti.template(), new_pf: ti.template()):
for i, j in pf:
pl = sample(pf, i - 1, j)
pr = sample(pf, i + 1, j)
pb = sample(pf, i, j - 1)
pt = sample(pf, i, j + 1)
div = velocity_divs[i, j]
new_pf[i, j] = (pl + pr + pb + pt - div) * 0.25
@ti.kernel
def subtract_gradient(vf: ti.template(), pf: ti.template()):
for i, j in vf:
pl = sample(pf, i - 1, j)
pr = sample(pf, i + 1, j)
pb = sample(pf, i, j - 1)
pt = sample(pf, i, j + 1)
vf[i, j] -= 0.5 * ti.Vector([pr - pl, pt - pb])
@ti.kernel
def enhance_vorticity(vf: ti.template(), cf: ti.template()):
# anti-physics visual enhancement...
for i, j in vf:
cl = sample(cf, i - 1, j)
cr = sample(cf, i + 1, j)
cb = sample(cf, i, j - 1)
ct = sample(cf, i, j + 1)
cc = sample(cf, i, j)
force = ti.Vector([abs(ct) - abs(cb), abs(cl) - abs(cr)]).normalized(1e-3)
force *= curl_strength * cc
vf[i, j] = ti.min(ti.max(vf[i, j] + force * dt, -1e3), 1e3)
@ti.kernel
def copy_divergence(div_in: ti.template(), div_out: ti.types.ndarray()):
for I in ti.grouped(div_in):
div_out[I[0] * res + I[1]] = -div_in[I]
@ti.kernel
def apply_pressure(p_in: ti.types.ndarray(), p_out: ti.template()):
for I in ti.grouped(p_out):
p_out[I] = p_in[I[0] * res + I[1]]
def solve_pressure_sp_mat():
copy_divergence(velocity_divs, F_b)
x = solver.solve(F_b)
apply_pressure(x, pressures_pair.cur)
def solve_pressure_jacobi():
for _ in range(p_jacobi_iters):
pressure_jacobi(pressures_pair.cur, pressures_pair.nxt)
pressures_pair.swap()
def step(mouse_data):
advect(velocities_pair.cur, velocities_pair.cur, velocities_pair.nxt)
advect(velocities_pair.cur, dyes_pair.cur, dyes_pair.nxt)
velocities_pair.swap()
dyes_pair.swap()
apply_impulse(velocities_pair.cur, dyes_pair.cur, mouse_data)
divergence(velocities_pair.cur)
if curl_strength:
vorticity(velocities_pair.cur)
enhance_vorticity(velocities_pair.cur, velocity_curls)
if use_sparse_matrix:
solve_pressure_sp_mat()
else:
solve_pressure_jacobi()
subtract_gradient(velocities_pair.cur, pressures_pair.cur)
if debug:
divergence(velocities_pair.cur)
div_s = np.sum(velocity_divs.to_numpy())
print(f"divergence={div_s}")
class MouseDataGen:
def __init__(self):
self.prev_mouse = None
self.prev_color = None
def __call__(self, gui):
# [0:2]: normalized delta direction
# [2:4]: current mouse xy
# [4:7]: color
mouse_data = np.zeros(8, dtype=np.float32)
if gui.is_pressed(ti.GUI.LMB):
mxy = np.array(gui.get_cursor_pos(), dtype=np.float32) * res
if self.prev_mouse is None:
self.prev_mouse = mxy
# Set lower bound to 0.3 to prevent too dark colors
self.prev_color = (np.random.rand(3) * 0.7) + 0.3
else:
mdir = mxy - self.prev_mouse
mdir = mdir / (np.linalg.norm(mdir) + 1e-5)
mouse_data[0], mouse_data[1] = mdir[0], mdir[1]
mouse_data[2], mouse_data[3] = mxy[0], mxy[1]
mouse_data[4:7] = self.prev_color
self.prev_mouse = mxy
else:
self.prev_mouse = None
self.prev_color = None
return mouse_data
def reset():
velocities_pair.cur.fill(0)
pressures_pair.cur.fill(0)
dyes_pair.cur.fill(0)
def main():
global debug, curl_strength
visualize_d = True # visualize dye (default)
visualize_v = False # visualize velocity
visualize_c = False # visualize curl
paused = False
gui = ti.GUI("Stable Fluid", (res, res))
md_gen = MouseDataGen()
while gui.running:
if gui.get_event(ti.GUI.PRESS):
e = gui.event
if e.key == ti.GUI.ESCAPE:
break
elif e.key == "r":
paused = False
reset()
elif e.key == "s":
if curl_strength:
curl_strength = 0
else:
curl_strength = 7
elif e.key == "v":
visualize_v = True
visualize_c = False
visualize_d = False
elif e.key == "d":
visualize_d = True
visualize_v = False
visualize_c = False
elif e.key == "c":
visualize_c = True
visualize_d = False
visualize_v = False
elif e.key == "p":
paused = not paused
elif e.key == "d":
debug = not debug
# Debug divergence:
# print(max((abs(velocity_divs.to_numpy().reshape(-1)))))
if not paused:
mouse_data = md_gen(gui)
step(mouse_data)
if visualize_c:
vorticity(velocities_pair.cur)
gui.set_image(velocity_curls.to_numpy() * 0.03 + 0.5)
elif visualize_d:
gui.set_image(dyes_pair.cur)
elif visualize_v:
gui.set_image(velocities_pair.cur.to_numpy() * 0.01 + 0.5)
gui.show()
if __name__ == "__main__":
main()