-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sim_Stam03.py
133 lines (102 loc) · 4.99 KB
/
Sim_Stam03.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
import numpy as np
#import FluidSim
class StamFluidSim():
_inner = np.s_[1:-1,1:-1]
def __init__(self, shape, diffusion, viscosity):
self._v = np.zeros((2, *shape), dtype=np.float32)
self._b = np.zeros(shape, dtype=bool)
self._tmp = np.zeros((2, *shape), dtype=np.float32)
print('v=', np.shape(self._v))
print('b=', np.shape(self._b))
print('tmp=', np.shape(self._tmp))
self._diff = diffusion
self._visc = viscosity
xs = np.arange(0.0, shape[0] + 0.0, 1)
ys = np.arange(0.0, shape[1] + 0.0, 1)
x, y = np.meshgrid(xs, ys)
self._indexArray = np.array([x.T, y.T], dtype=np.float32)
def _set_bnd(self, x, f):
x[self._b] = 0
if f==2:
dfall = np.logical_and(self._b[:,:-1], np.logical_not(self._b[:,1:]))
x[:,:-1][dfall] += -x[:,1:][dfall]
ufall = np.logical_and(np.logical_not(self._b[:,:-1]), self._b[:,1:])
x[:,1:][ufall] += -x[:,:-1][ufall]
elif f==1:
lfall = np.logical_and(np.logical_not(self._b[:-1,:]), self._b[1:,:])
x[1:,:][lfall] += -x[:-1,:][lfall]
rfall = np.logical_and(self._b[:-1,:], np.logical_not(self._b[1:,:]))
x[:-1,:][rfall] += -x[1:,:][rfall]
else:
dfall = np.logical_and(self._b[:,:-1], np.logical_not(self._b[:,1:]))
x[:,:-1][dfall] = x[:,1:][dfall]
ufall = np.logical_and(np.logical_not(self._b[:,:-1]), self._b[:,1:])
x[:,1:][ufall] = x[:,:-1][ufall]
lfall = np.logical_and(np.logical_not(self._b[:-1,:]), self._b[1:,:])
x[1:,:][lfall] = x[:-1,:][lfall]
rfall = np.logical_and(self._b[:-1,:], np.logical_not(self._b[1:,:]))
x[:-1,:][rfall] = x[1:,:][rfall]
def _lin_solve(self, x, xp, a, c, f):
x[:] = 0
for i in range(10):
#print('lin_solve', i, x[10,10])
x[StamFluidSim._inner] = 1 / c * (xp[StamFluidSim._inner]
+ a * (x[0:-2,1:-1] + x[2:,1:-1] + x[1:-1,0:-2] + x[1:-1,2:]))
self._set_bnd(x, f)
def _diffuse(self, x, xp, diff, dt, f):
#print('diffuse', x[10,10])
a = dt * diff * np.prod(np.shape(x))
self._lin_solve(x, xp, a, 1 + 4 * a, f)
def _advect(self, d, d0, u, v, dt, f):
shape = np.shape(d)
dt0 = dt * np.sqrt(np.prod(shape))
x = np.clip(self._indexArray[0] - dt0 * u, 0, shape[0] - 1.01)
y = np.clip(self._indexArray[1] - dt0 * v, 0, shape[1] - 1.01)
xi = np.array(x, dtype=int)
yi = np.array(y, dtype=int)
s = x - xi
t = y - yi
d[:] = (1 - s) * ((1 - t) * d0[xi, yi] + t * d0[xi, yi + 1]) \
+ s * ((1 - t) * d0[xi + 1, yi] + t * d0[xi + 1, yi + 1])
self._set_bnd(d, f)
def _project(self, u, v, div, p):
shape = np.shape(u)
N = np.sqrt(np.prod(shape))
div[StamFluidSim._inner] = (-0.5 / N) * (u[2:, 1:-1] - u[:-2, 1:-1]
+ v[1:-1, 2:] - v[1:-1, :-2])
p[StamFluidSim._inner] = 0
self._set_bnd(div, 0)
self._set_bnd(p, 0)
self._lin_solve(p, div, 1, 4, 0)
u[StamFluidSim._inner] -= 0.5 * N * (p[2:, 1:-1] - p[:-2, 1:-1])
v[StamFluidSim._inner] -= 0.5 * N * (p[1:-1, 2:] - p[1:-1, :-2])
self._set_bnd(u, 1)
self._set_bnd(v, 2)
def _dens_step(self, dt, x):
#add_source(x, x0, dt)
self._diffuse(self._tmp[0], x, self._diff.current, dt, 0)
self._advect(x, self._tmp[0], self._v[0], self._v[1], dt, 0)
def _vel_step(self, dt):
# add_source(u, u0, dt)
# add_source(v, v0, dt)
# print('prediffuse', self._tmp[0,10,10])
self._diffuse(self._tmp[0], self._v[0], self._visc.current, dt, 1)
# print('postdiffuse', self._tmp[0, 10, 10])
self._diffuse(self._tmp[1], self._v[1], self._visc.current, dt, 2)
self._project(self._tmp[0], self._tmp[1], self._v[0], self._v[1])
# print('postproject', self._tmp[0, 10, 10])
self._advect(self._v[0], self._tmp[0], self._tmp[0], self._tmp[1], dt, 1)
self._advect(self._v[1], self._tmp[1], self._tmp[0], self._tmp[1], dt, 2)
self._project(self._v[0], self._v[1], self._tmp[0], self._tmp[1])
def set_boundary(self, cell_ocupation):
self._b = cell_ocupation
def set_velocity(self, cells_to_set, cell_velocity):
self._v[cells_to_set] = cell_velocity
def reset(self):
self._v[:] = 0
def step(self, dt, density_arrays):
self._vel_step(dt)
for d in density_arrays:
self._dens_step(dt, d)
def get_velocity(self):
return self._v