-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid_fluid.py
107 lines (90 loc) · 3.17 KB
/
grid_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
import numpy as np
import taichi as ti
@ti.func
def sample_lerp(data: ti.template(), normed_pos: ti.template()):
pos = normed_pos * ti.Vector([width, height])
x, y = int(pos.x), int(pos.y)
tx, ty = pos.x - x, pos.y - y
val00 = data[x, y]
val10 = data[x + 1, y]
val01 = data[x, y + 1]
val11 = data[x + 1, y + 1]
val0 = ti.math.mix(val00, val10, tx)
val1 = ti.math.mix(val01, val11, tx)
return ti.math.mix(val0, val1, ty)
@ti.kernel
def compute_pressure(parity: int):
for i in range(1, width - 1):
for j in range(1, height - 1):
if (i + j) % 2 == parity:
x0 = pressure[i - 1, j]
x1 = pressure[i + 1, j]
y0 = pressure[i, j - 1]
y1 = pressure[i, j + 1]
div = divergence[i, j]
pressure[i, j] = (x0 + x1 + y0 + y1 - div) / 4
@ti.kernel
def add_force(cursor: ti.template()):
for i in range(1, width - 1):
for j in range(1, height - 1):
pos = ti.Vector([i / width, j / height])
if ti.math.distance(pos, cursor) < 0.1:
velocity[i, j] = ti.Vector([0.0, -0.2])
@ti.kernel
def advect(dt: float):
for i in range(1, width - 1):
for j in range(1, height - 1):
offset = velocity[i, j] * dt
pos = ti.Vector([i / width, j / height])
velocity[i, j] = sample_lerp(velocity, pos - offset)
@ti.kernel
def compute_divergence():
for i in range(1, width - 1):
for j in range(1, height - 1):
x0 = velocity[i - 1, j].x
x1 = velocity[i + 1, j].x
y0 = velocity[i, j - 1].y
y1 = velocity[i, j + 1].y
dx = (x1 - x0) / 2.0
dy = (y1 - y0) / 2.0
divergence[i, j] = (dx + dy) * 1.9
@ti.kernel
def subtract_pressure_gradient():
for i in range(1, width - 1):
for j in range(1, height - 1):
x0 = pressure[i - 1, j]
x1 = pressure[i + 1, j]
y0 = pressure[i, j - 1]
y1 = pressure[i, j + 1]
dx = (x1 - x0) / 2.0
dy = (y1 - y0) / 2.0
grad = ti.Vector([dx, dy])
velocity[i, j] -= grad
@ti.kernel
def render():
for i, j in colors:
colors[i, j].xy = ti.abs(velocity[i, j] * 5)
colors[i, j].z = ti.abs(pressure[i, j] * 5)
if __name__ == '__main__':
ti.init(arch=ti.vulkan)
width, height = 1024, 1024
window = ti.ui.Window("Grid-based fluid", (width, height), vsync=True)
canvas = window.get_canvas()
colors = ti.Vector.field(3, dtype=float, shape=(width, height))
velocity = ti.Vector.field(2, dtype=float, shape=(width, height))
divergence = ti.field(dtype=float, shape=(width, height))
pressure = ti.field(dtype=float, shape=(width, height))
frame = 0
while window.running:
emitter_pos = ti.Vector([0.5 + np.sin(frame * 0.04) * 0.2, 0.7])
add_force(emitter_pos)
advect(0.05)
compute_divergence()
pressure.fill(0.0)
for i in range(20):
compute_pressure(parity=i%2)
subtract_pressure_gradient()
render()
canvas.set_image(colors)
window.show()
frame += 1