-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sim_numpy_jit.py
138 lines (113 loc) · 4.52 KB
/
Sim_numpy_jit.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
from numba import jit
import numpy as np
from SimBase import SimBase
@jit
def advect_velocity(v, v0, b, indexArray, dx, dt):
shape = np.shape(v0[0])
x = indexArray - dt * v0 / dx[:,np.newaxis,np.newaxis]
x = np.array([np.clip(x[0], 0, shape[0] - 1.01),
np.clip(x[1], 0, shape[1] - 1.01)])
indexes = np.array(x, dtype=int)
advectInter = x - indexes
xi = indexes[0]
yi = indexes[1]
s = advectInter[0]
t = advectInter[1]
v[:] = (1 - s) * ((1 - t) * v0[:, xi, yi]
+ t * v0[:, xi, yi + 1]) + s * ((1 - t) * v0[:, xi + 1, yi]
+ t * v0[:, xi + 1, yi + 1])
v[:, b] = v0[:, b]
return v, indexes, advectInter
@jit
def apply_advection(x, x0, xi, s):
xi, yi = xi
s, t = s
x[:] = (1 - s) * ((1 - t) * x0[xi, yi]
+ t * x0[xi, yi + 1]) + s * ((1 - t) * x0[xi + 1, yi]
+ t * x0[xi + 1, yi + 1])
return x
@jit
def divergence(div, v, notb, dx):
div[-1,:] = 0
div[:-1, :] = v[0, 1:, :] * notb[1:, :] / (2 * dx[0])
div[1:, :] -= v[0, :-1, :] * notb[:-1, :] / (2 * dx[0])
div[:, :-1] += v[1, :, 1:] * notb[:, 1:] / (2 * dx[1])
div[:, 1:] -= v[1, :, :-1] * notb[:, :-1] / (2 * dx[1])
#div[self._b] = 0
return div
@jit
def pressure_solve(p, div, b, notb, dx):
p[notb] = 0
bound = 0.0 + b[0:-2,1:-1] + b[2:,1:-1] + b[1:-1,0:-2] + b[1:-1,2:]
for i in range(50):
p[1:-1,1:-1] = 1 / 4 * (p[1:-1,1:-1] * bound
+ p[0:-2,1:-1] * notb[0:-2,1:-1]
+ p[2:,1:-1] * notb[2:,1:-1]
+ p[1:-1,0:-2] * notb[1:-1,0:-2]
+ p[1:-1,2:] * notb[1:-1,2:]
- dx[0] * dx[1] * div[1:-1,1:-1])
return p
@jit
def sub_gradient(v, v0, p, dx):
v[0, 1:-1, :] = v0[0, 1:-1, :] - 1 / (2 * dx[0]) * (p[2:, :] - p[:-2, :])
v[1, :, 1:-1] = v0[1, :, 1:-1] - 1 / (2 * dx[1]) * (p[:, 2:] - p[:, :-2])
return v
@jit
def enforce_slip(v, notb, b):
v[:, b] = 0
right_edge = np.logical_and(notb[:-1,:], b[1:,:])
v[0, :-1,:][right_edge] = v[0, 1:,:][right_edge]
left_edge = np.logical_and(notb[1:,:], b[:-1,:])
v[0, 1:,:][left_edge] = v[0, :-1,:][left_edge]
top_edge = np.logical_and(notb[:,:-1], b[:,1:])
v[1, :,:-1][top_edge] = v[1, :,:-1][top_edge]
bottom_edge = np.logical_and(notb[:,1:], b[:,:-1])
v[1, :, 1:][bottom_edge] = v[1, :, 1:][bottom_edge]
return v
class Sim(SimBase):
def __init__(self, cam_shape, res_multiplier, diffusion, viscosity):
super().__init__(cam_shape, res_multiplier)
self._div = np.zeros(self._shape)
self._p = np.zeros(self._shape)
self._vtmp = np.zeros((2, *self._shape))
self._vtmp2 = np.zeros((2, *self._shape))
self._dtmp = np.zeros(self._shape)
xs = np.arange(0.0, self._shape[0], 1)
ys = np.arange(0.0, self._shape[1], 1)
x, y = np.meshgrid(xs, ys)
self._indexArray = np.array([x.T, y.T])
self._xi = np.zeros_like(self._v, dtype=int)
self._s = np.zeros_like(self._v)
@jit
def step(self, dt, density_arrays):
if False:
# BFECC
self._vtmp2[:], self._xi, self._s = advect_velocity(self._vtmp2, self._v,
self._b, self._indexArray, self._dx, dt)
self._vtmp[:], _, _ = advect_velocity(self._vtmp, self._vtmp2,
self._b, self._indexArray, self._dx, -dt)
self._vtmp2 = 1.3 * self._v - 0.3 * self._vtmp
# Corrected advection
self._vtmp[:], self._xi, self._s = advect_velocity(self._vtmp, self._vtmp2,
self._b, self._indexArray, self._dx, dt)
else:
self._vtmp[:], self._xi, self._s = advect_velocity(self._vtmp, self._v,
self._b, self._indexArray, self._dx, dt)
# remove divergence
self._div[:] = divergence(self._div, self._vtmp, self._notb, self._dx)
self._p[:] = pressure_solve(self._p, self._div, self._b, self._notb, self._dx)
self._v[:] = sub_gradient(self._v, self._vtmp, self._p, self._dx)
# enforce slip at boundary
self._v[:] = enforce_slip(self._v, self._notb, self._b)
for d in density_arrays:
d[:] = apply_advection(self._dtmp, d, self._xi, self._s)
d[self._b] = 0
@jit
def get_pressure_as_rgb(self):
width, height = np.shape(self._p)
rgb = np.zeros((3, width, height))
pmax = max(np.max(self._p), -np.min(self._p))
if pmax > 0:
rgb[2, self._p > 0] = self._p[self._p > 0] / pmax
rgb[0, self._p < 0] = self._p[self._p < 0] / pmax
return rgb