-
Notifications
You must be signed in to change notification settings - Fork 17
/
fluid_simulator.py
153 lines (132 loc) · 5.09 KB
/
fluid_simulator.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
import taichi as ti
from advection import advect_kk_scheme, advect_upwind
from boundary_condition import get_boundary_condition
from solver import CipMacSolver, DyeCipMacSolver, DyeMacSolver, MacSolver
from pressure_updater import RedBlackSorPressureUpdater
from vorticity_confinement import VorticityConfinement
from visualization import visualize_norm, visualize_pressure, visualize_vorticity
@ti.data_oriented
class FluidSimulator:
def __init__(self, solver):
self._solver = solver
self.rgb_buf = ti.Vector.field(3, ti.f32, shape=solver._resolution) # image buffer
self._wall_color = ti.Vector([0.5, 0.7, 0.5])
def step(self):
self._solver.update()
def get_norm_field(self):
self._to_norm(self.rgb_buf, *self._solver.get_fields()[:2])
return self.rgb_buf
def get_pressure_field(self):
self._to_pressure(self.rgb_buf, self._solver.get_fields()[1])
return self.rgb_buf
def get_vorticity_field(self):
self._to_vorticity(self.rgb_buf, self._solver.get_fields()[0])
return self.rgb_buf
def field_to_numpy(self):
fields = self._solver.get_fields()
return {"v": fields[0].to_numpy(), "p": fields[1].to_numpy()}
@ti.kernel
def _to_norm(self, rgb_buf: ti.template(), vc: ti.template(), pc: ti.template()):
for i, j in rgb_buf:
rgb_buf[i, j] = 0.2 * visualize_norm(vc[i, j])
rgb_buf[i, j] += 0.002 * visualize_pressure(pc[i, j])
if self._solver.is_wall(i, j):
rgb_buf[i, j] = self._wall_color
@ti.kernel
def _to_pressure(self, rgb_buf: ti.template(), pc: ti.template()):
for i, j in rgb_buf:
rgb_buf[i, j] = 0.04 * visualize_pressure(pc[i, j])
if self._solver.is_wall(i, j):
rgb_buf[i, j] = self._wall_color
@ti.kernel
def _to_vorticity(self, rgb_buf: ti.template(), vc: ti.template()):
for i, j in rgb_buf:
rgb_buf[i, j] = 0.005 * visualize_vorticity(vc, i, j, self._solver.dx)
if self._solver.is_wall(i, j):
rgb_buf[i, j] = self._wall_color
@staticmethod
def create(num, resolution, dt, dx, re, vor_eps, scheme):
boundary_condition = get_boundary_condition(num, resolution, True)
vorticity_confinement = (
VorticityConfinement(boundary_condition, dt, dx, vor_eps)
if vor_eps is not None
else None
)
pressure_updater = RedBlackSorPressureUpdater(
boundary_condition, dt, dx, relaxation_factor=1.3, n_iter=2
)
if scheme == "cip":
solver = CipMacSolver(
boundary_condition, pressure_updater, dt, dx, re, vorticity_confinement
)
elif scheme == "upwind":
solver = MacSolver(
boundary_condition,
pressure_updater,
advect_upwind,
dt,
dx,
re,
vorticity_confinement,
)
elif scheme == "kk":
solver = MacSolver(
boundary_condition,
pressure_updater,
advect_kk_scheme,
dt,
dx,
re,
vorticity_confinement,
)
return FluidSimulator(solver)
@ti.data_oriented
class DyeFluidSimulator(FluidSimulator):
def get_dye_field(self):
self._to_dye(self.rgb_buf, self._solver.get_fields()[2])
return self.rgb_buf
def field_to_numpy(self):
fields = self._solver.get_fields()
return {"v": fields[0].to_numpy(), "p": fields[1].to_numpy(), "dye": fields[2].to_numpy()}
@ti.kernel
def _to_dye(self, rgb_buf: ti.template(), dye: ti.template()):
for i, j in rgb_buf:
rgb_buf[i, j] = dye[i, j]
if self._solver.is_wall(i, j):
rgb_buf[i, j] = self._wall_color
@staticmethod
def create(num, resolution, dt, dx, re, vor_eps, scheme):
boundary_condition = get_boundary_condition(num, resolution, False)
vorticity_confinement = (
VorticityConfinement(boundary_condition, dt, dx, vor_eps)
if vor_eps is not None
else None
)
pressure_updater = RedBlackSorPressureUpdater(
boundary_condition, dt, dx, relaxation_factor=1.3, n_iter=2
)
if scheme == "cip":
solver = DyeCipMacSolver(
boundary_condition, pressure_updater, dt, dx, re, vorticity_confinement
)
elif scheme == "upwind":
solver = DyeMacSolver(
boundary_condition,
pressure_updater,
advect_upwind,
dt,
dx,
re,
vorticity_confinement,
)
elif scheme == "kk":
solver = DyeMacSolver(
boundary_condition,
pressure_updater,
advect_kk_scheme,
dt,
dx,
re,
vorticity_confinement,
)
return DyeFluidSimulator(solver)