-
Notifications
You must be signed in to change notification settings - Fork 3
/
exp_play_MPM.py
152 lines (121 loc) · 5.12 KB
/
exp_play_MPM.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
import taichi as ti
import taichi_glsl as ts
import numpy as np
import argparse
from config import mpmCFG, MaType
def parse_args():
parser = argparse.ArgumentParser(description="Run which config")
parser.add_argument("--cfg", help="configure file", type=str)
args = parser.parse_args()
cfg = None
if args.cfg == "Jello-Fall-2D":
import config.config2D.Jello_Fall2D
cfg = config.config2D.Jello_Fall2D
elif args.cfg == "Jello-Fall-3D":
import config.config3D.Jello_Fall3D
cfg = config.config3D.Jello_Fall3D
return mpmCFG(cfg)
if __name__ == '__main__':
m_cfg = parse_args()
from Engine.MPM_solver import MPMSolver
colors = np.array([0xED553B, 0x068587, 0xEEEEF0, 0x8a6844], dtype=np.int32)
solver = MPMSolver(m_cfg)
dim = m_cfg.dim
solver.materialize()
# scheme.Layout.init_cube()
def init_fall_cube():
solver.Layout.add_cube(l_b=ts.vecND(dim, 0.05),
cube_size=ts.vecND(dim, 0.15),
mat=MaType.liquid,
n_p=m_cfg.max_n_particle // 4,
velocity=ts.vecND(dim, 0.0),
color=colors[MaType.liquid]
)
solver.Layout.add_cube(l_b=ts.vecND(dim, 0.3),
cube_size=ts.vecND(dim, 0.15),
mat=MaType.elastic,
n_p=m_cfg.max_n_particle // 4,
velocity=ts.vecND(dim, 0.0),
color=colors[MaType.elastic]
)
solver.Layout.add_cube(l_b=ts.vecND(dim, 0.5),
cube_size=ts.vecND(dim, 0.15),
mat=MaType.sand,
n_p=m_cfg.max_n_particle // 4,
velocity=ts.vecND(dim, 0.0),
color=colors[MaType.sand]
)
solver.Layout.add_cube(l_b=ts.vecND(dim, 0.7),
cube_size=ts.vecND(dim, 0.15),
mat=MaType.snow,
n_p=m_cfg.max_n_particle // 4,
velocity=ts.vecND(dim, 0.0),
color=colors[MaType.snow]
)
def update_jet(total_frame_jet: int, n_jet_p: int):
"""
:param total_frame_jet:
:param n_jet_p:
:return:
"""
tmp_pos = ts.vecND(m_cfg.dim, 0.15)
tmp_pos[0] = 0.0
tmp_vel = ts.vecND(m_cfg.dim, 0.0)
tmp_vel[0] = 1.5
solver.Layout.add_cube(l_b=tmp_pos,
cube_size=ts.vecND(dim, 0.03),
mat=MaType.liquid,
n_p=n_jet_p // total_frame_jet,
velocity=tmp_vel,
color=colors[MaType.liquid]
)
init_fall_cube()
gui = ti.GUI(m_cfg.profile_name, tuple(m_cfg.screen_res), fast_gui=False)
paused = False
# jet_frame = 64
while gui.running:
# if scheme.curFrame < jet_frame:
# update_jet(jet_frame, m_cfg.max_n_particle // 4)
if gui.get_event(ti.GUI.PRESS):
e = gui.event
if e.key == 'p':
paused = not paused
elif e.key == 'r':
solver.reset()
init_fall_cube()
if not paused:
solver.step()
np_x = solver.Layout.p_x.to_numpy()
if m_cfg.dim == 2:
screen_pos = np_x
elif m_cfg.dim == 3:
# screen_x = ((np_x[:, 0] + np_x[:, 2]) / 2 ** 0.5) - 0.2
# screen_y = (np_x[:, 1])
# screen_pos = np.stack([screen_x, screen_y], axis=-1)
np_x -= 0.5
phi, theta = np.radians(28), np.radians(32)
x, y, z = np_x[:, 0], np_x[:, 1], np_x[:, 2]
c, s = np.cos(phi), np.sin(phi)
C, S = np.cos(theta), np.sin(theta)
x, z = x * c + z * s, z * c - x * s
u, v = x, y * C + z * S
screen_pos = np.array([u, v]).swapaxes(0, 1) + 0.5
solver.Layout.update_liquid_color()
gui.circles(screen_pos, radius=1.5, color=solver.Layout.p_color.to_numpy())
if not m_cfg.bool_save:
gui.show() # Change to gui.show(f'{frame:06d}.png') to write images to disk
else:
if solver.curFrame <= m_cfg.save_frame_length:
import os
os.makedirs(m_cfg.save_path, exist_ok=True)
gui.show(os.path.join(m_cfg.save_path, f'{solver.curFrame:06d}.png'))
else:
break
if m_cfg.bool_save_particle:
if solver.curFrame < m_cfg.save_frame_length:
import os
os.makedirs(m_cfg.particle_path, exist_ok=True)
file_name = os.path.join(m_cfg.particle_path, f'{solver.curFrame:06d}.npz')
# print(file_name)
solver.write_particles(file_name)
ti.kernel_profiler_print()