-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluidSimSemiLag.py
More file actions
279 lines (208 loc) · 8.1 KB
/
Copy pathFluidSimSemiLag.py
File metadata and controls
279 lines (208 loc) · 8.1 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sps
from scipy.sparse.linalg import spsolve
from matplotlib import animation
def coord (i, j):
"""
Coordinate mapping from matrix index to 2D x y coordinate.
i is the row entry index, j is the column entry index
"""
return j, i
def interpolate (u, v, x):
"""
Find nearby grid points and return a velocity vector that interpolates those grid points.
u is the horizontal component velocity field
v is the vertical component velocity field
x is the position vector
"""
if x[0] < 0:
x[0] = 0
if x[0] > width-2:
x[0] = width-2
if x[1] < 0:
x[1] = 0
if x[1] > height-2:
x[1] = height-2
xi = int(x[1])
xj = int(x[0])
ax = (x[0] - xj) / h
ay = (x[1] - xi) / h
# Bilinear interpolation in 2D
uij = (1-ax)*(1-ay)*u[xi,xj] + (1-ax)*ay*u[xi+1,xj] + ax*(1-ay)*u[xi,xj+1] + ax*ay*u[xi+1,xj+1]
vij = (1-ax)*(1-ay)*v[xi,xj] + (1-ax)*ay*v[xi+1,xj] + ax*(1-ay)*v[xi,xj+1] + ax*ay*v[xi+1,xj+1]
return uij, vij
def advect (u, v):
"""
Advect the velocity fields using Semi-Lagrangian advection.
u is the horizontal component velocity field
v is the vertical component velocity field
"""
# NOTICE: memory usage might be too high, could optimize
# Store the values from timestep n
un = u
vn = v
for i in range (height):
for j in range (width):
oldpos = coord (i,j) - dt * np.stack((u[i,j], v[i,j]))
u[i,j], v[i,j] = interpolate (un, vn, oldpos)
# Return values for timestep n+1
return u, v
def extforce (u, v):
"""
Apply external forces on the velocity field.
u is the horizontal component velocity field
v is the vertical component velocity field
"""
for i in range (height):
for j in range (width):
u[i,j], v[i,j] = np.stack((u[i,j], v[i,j])) + dt * extacc
return u, v
def index (i, j):
# how we find the index of point i,j in row of matrix A
return i*width + j
def project (u, v):
"""
Calculate pressure field such that velocity field is divergence free.
u is the horizontal component velocity field
v is the vertical component velocity field
"""
# Construct linear system Ap = d
A = sps.lil_matrix ((width*height, width*height))
d = np.zeros ((width*height))
for i in range (1, height-1):
for j in range (1, width-1):
A[index(i,j), index(i,j)] = 4
A[index(i,j), index(i-1,j)] = -1
A[index(i,j), index(i+1,j)] = -1
A[index(i,j), index(i,j-1)] = -1
A[index(i,j), index(i,j+1)] = -1
d[index(i,j)] = -1/h * (u[i,j] - u[i,j-1] + v[i,j] - v[i-1,j])
# Unhandled boundary cases, we assume solid walls that don't move
A[index(0,0), index(0,0)] = 2
A[index(0,0), index(1,0)] = -1
A[index(0,0), index(0,1)] = -1
d[index(0,0)] = -1/h * (u[0,0] + v[0,0])
A[index(height-1,0), index(0,0)] = 2
A[index(height-1,0), index(height-1,1)] = -1
A[index(height-1,0), index(height-2,0)] = -1
d[index(height-1,0)] = -1/h * (u[height-1,0] - v[height-2,0])
A[index(0,width-1), index(0,width-1)] = 2
A[index(0,width-1), index(1,width-1)] = -1
A[index(0,width-1), index(0,width-2)] = -1
d[index(0,width-1)] = -1/h * (-u[0,width-2] + v[0,width-1])
A[index(height-1,width-1), index(height-1,width-1)] = 2
A[index(height-1,width-1), index(height-2,width-1)] = -1
A[index(height-1,width-1), index(height-1,width-2)] = -1
d[index(height-1,width-1)] = -1/h * (-u[height-1,width-2] - v[height-2,width-1])
for i in range (1, height-1):
A[index(i,0), index(i,0)] = 3
A[index(i,0), index(i-1,0)] = -1
A[index(i,0), index(i+1,0)] = -1
A[index(i,0), index(i,1)] = -1
d[index(i,0)] = -1/h * (u[i,0] + v[i,0] - v[i-1,0])
for i in range (1, height-1):
A[index(i,width-1), index(i,width-1)] = 3
A[index(i,width-1), index(i-1,width-1)] = -1
A[index(i,width-1), index(i+1,width-1)] = -1
A[index(i,width-1), index(i,width-2)] = -1
d[index(i,width-1)] = -1/h * (- u[i,width-2] + v[i, width-1] - v[i-1,width-1])
for j in range (1, width-1):
A[index(0,j), index(0,j)] = 3
A[index(0,j), index(1,j)] = -1
A[index(0,j), index(0,j-1)] = -1
A[index(0,j), index(0,j+1)] = -1
d[index(0,j)] = -1/h * (u[0,j] - u[0,j-1] + v[0,j])
for j in range (1, width-1):
A[index(height-1,j), index(height-1,j)] = 3
A[index(height-1,j), index(height-2,j)] = -1
A[index(height-1,j), index(height-1,j-1)] = -1
A[index(height-1,j), index(height-1,j+1)] = -1
d[index(height-1,j)] = -1/h * (u[height-1,j] - u[height-1,j-1] - v[height-2,j])
A = A * dt / (density * h**2)
A = sps.csr_matrix (A)
p = np.reshape(spsolve (A, d), (height, width))
# Calculate new velocity field based on this pressure field
for i in range (height):
for j in range (width):
if (i == height-1 and j == width-1) or (i == height-1 and j == 0) or (i == 0 and j == width-1) or (i == 0 and j == 0):
# Set vertical velocity to movement of solid wall 0
u[i,j] = 0
v[i,j] = 0
elif i == height-1 or i == 0:
u[i,j] = u[i,j] - dt / (density * h) * (p[i,j+1] - p[i,j])
v[i,j] = 0
elif j == width-1 or j == 0:
u[i,j] = 0
v[i,j] = v[i,j] - dt / (density * h) * (p[i+1,j] - p[i,j])
else:
u[i,j] = u[i,j] - dt / (density * h) * (p[i,j+1] - p[i,j])
v[i,j] = v[i,j] - dt / (density * h) * (p[i+1,j] - p[i,j])
# let's get some inflow
u[4:12, 0] = 1
return u, v, p
### 2D fluid simulation with semi lagrangian advection and pressure projection
width = 20
height = 20
timesteps = 50
dt = 0.001
h = 0.01
density = 1000
SLEEPMS = 20
extacc = np.array([0, -9.81]) # External force as acceleration in 2D, here only gravity
# Initialize horizontal and vertical velocity fields and pressure field
u = np.zeros ((height, width))
v = np.zeros ((height, width))
p = np.zeros ((height, width))
# Array that stores temporal evolution of norm of velocity field
q = np.zeros ((timesteps, height, width))
qu = np.zeros ((timesteps, height, width))
qv = np.zeros ((timesteps, height, width))
qp = np.zeros ((timesteps, height, width))
q[0,:,:] = u**2 + v**2
qu[0,:,:] = u
qv[0,:,:] = v
qp[0,:,:] = p
for t in range (1, timesteps):
### Splitting the fluid equations
# Advect the fields
u, v = advect (u, v)
# Body forces
u, v = extforce (u, v)
# Pressure projection
u, v, p = project (u, v)
#p[4:12,4:12] = 1000
q[t,:,:] = u**2 + v**2
qu[t,:,:] = u
qv[t,:,:] = v
qp[t,:,:] = p
fig = plt.figure (1, figsize=(10,10))
ax = fig.add_subplot (221)
axp = fig.add_subplot (222)
axu = fig.add_subplot (223)
axv = fig.add_subplot (224)
#im = ax.quiver (qu[0,:,:], qv[0,:,:])
title = ax.text(0.5, 0.97, "Current time: 0", bbox={'facecolor':'w', 'alpha':0.5, 'pad':2}, transform=ax.transAxes, ha="center")
im = ax.imshow (q[0,:,:])
imp = axp.imshow (qp[0,:,:])
imu = axu.imshow (qu[0,:,:])
imv = axv.imshow (qv[0,:,:])
plt.colorbar (im)
plt.colorbar (imp)
plt.colorbar (imu)
plt.colorbar (imv)
im.axes.figure.canvas.draw()
imp.axes.figure.canvas.draw()
imu.axes.figure.canvas.draw()
imv.axes.figure.canvas.draw()
def animate (t):
title.set_text("Current time: %.5f" % (t*dt))
#im.set_UVC (qu[t,:,:], qv[t,:,:])
im.set_data (q[t,:,:])
imp.set_data (qp[t,:,:])
imu.set_data (qu[t,:,:])
imv.set_data (qv[t,:,:])
return im
anim = animation.FuncAnimation (fig, animate, frames=timesteps, interval=SLEEPMS, blit=False, repeat=True)
anim.save('Fluid.mp4', fps=30)
plt.show ()