-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpressure_updater.py
101 lines (77 loc) · 3.17 KB
/
pressure_updater.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
from abc import ABCMeta, abstractmethod
import taichi as ti
from differentiation import sample
class PressureUpdater(metaclass=ABCMeta):
def __init__(self, boundary_condition, dt, dx):
self._bc = boundary_condition
self.dt = dt
self.dx = dx
@abstractmethod
def update(self, p, v_current):
pass
@ti.func
def predict_p(pc, vc, i, j, dt, dx):
sub_x = sample(vc, i + 1, j) - sample(vc, i - 1, j)
sub_y = sample(vc, i, j + 1) - sample(vc, i, j - 1)
pred_p = (
0.25
* (
sample(pc, i + 1, j)
+ sample(pc, i - 1, j)
+ sample(pc, i, j + 1)
+ sample(pc, i, j - 1)
)
+ (sub_x.x**2 + sub_y.y**2 + (sub_y.x * sub_x.y)) / 8.0
- dx * (sub_x.x + sub_y.y) / (8 * dt)
)
return pred_p
@ti.data_oriented
class JacobiPressureUpdater(PressureUpdater):
"""Jacobi Method"""
def __init__(self, boundary_condition, dt, dx, n_iter):
super().__init__(boundary_condition, dt, dx)
self._n_iter = n_iter
def update(self, p, v_current):
for _ in range(self._n_iter):
self._bc.set_pressure_boundary_condition(p.current)
self._update(p.next, p.current, v_current)
p.swap()
@ti.kernel
def _update(self, p_next: ti.template(), p_current: ti.template(), v_current: ti.template()):
for i, j in p_next:
if not self._bc.is_wall(i, j):
p_next[i, j] = predict_p(p_current, v_current, i, j, self.dt, self.dx)
@ti.data_oriented
class RedBlackSorPressureUpdater(PressureUpdater):
"""Red-Black SOR Method"""
def __init__(self, boundary_condition, dt, dx, relaxation_factor, n_iter):
super().__init__(boundary_condition, dt, dx)
self._n_iter = n_iter
self._relaxation_factor = relaxation_factor
def update(self, p, v_current):
for _ in range(self._n_iter):
self._bc.set_pressure_boundary_condition(p.current)
self._update(p.next, p.current, v_current)
p.swap()
def _update(self, p_next, p_current, v_current):
# 圧力のFieldは1つでも良いがインターフェイスの統一のために2つ受け取るようにしている
# Fieldを1つしか使わない場合はpn, pcを同じFieldとして与えれば良い
self._update_pressures_odd(p_next, p_current, v_current)
self._update_pressures_even(p_next, p_next, v_current)
@ti.kernel
def _update_pressures_odd(self, pn: ti.template(), pc: ti.template(), vc: ti.template()):
for i, j in pn:
if (i + j) % 2 == 1:
if self._bc.is_fluid_domain(i, j):
pn[i, j] = self._pn_ij(pc, vc, i, j)
@ti.kernel
def _update_pressures_even(self, pn: ti.template(), pc: ti.template(), vc: ti.template()):
for i, j in pn:
if (i + j) % 2 == 0:
if self._bc.is_fluid_domain(i, j):
pn[i, j] = self._pn_ij(pc, vc, i, j)
@ti.func
def _pn_ij(self, pc, vc, i, j):
return (1.0 - self._relaxation_factor) * pc[i, j] + self._relaxation_factor * predict_p(
pc, vc, i, j, self.dt, self.dx
)