-
Notifications
You must be signed in to change notification settings - Fork 22
/
view.py
49 lines (38 loc) · 953 Bytes
/
view.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
import taichi as ti
import numpy as np
from matplotlib import cm
import os
cmap = cm.get_cmap('magma')
res = 256, 64, 64
#res = 1024, 256, 1
rho = ti.field(float, res)
vel = ti.Vector.field(3, float, res)
img = ti.field(float, (res[0], res[1]))
def load(frame):
path = f'/tmp/{frame:06d}.npz'
if not os.path.exists(path):
return False
with np.load(path) as data:
rho.from_numpy(data['rho'])
vel.from_numpy(data['vel'])
return True
@ti.func
def color(x, y, z):
return vel[x, y, z].norm() * 5
@ti.kernel
def render():
for x, y in img:
ret = 0.0
cnt = 0
for z in range(res[2] // 4, max(1, res[2] * 3 // 4)):
ret += color(x, y, z)
cnt += 1
img[x, y] = ret / cnt
frame = 0
while load(frame):
print('render for', frame)
render()
im = cmap(img.to_numpy())
ti.imshow(im)
ti.imwrite(im, f'/tmp/{frame:06d}.png')
frame += 1