-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFT_Solver_2D_numpy.py
More file actions
465 lines (377 loc) · 17 KB
/
FFT_Solver_2D_numpy.py
File metadata and controls
465 lines (377 loc) · 17 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import numpy as np
from tqdm import tqdm
from numpy.fft import rfft2, irfft2
from timeit import default_timer as timer
class Solver2D_FFT:
def advect_BFECC2D_Periodic(self, input_matrix, U, V):
# Forward warp
x = self.X - self.dt * U
y = self.Y - self.dt * V
x_floor = np.floor(x).astype(int)
y_floor = np.floor(y).astype(int)
s = x - x_floor
t = y - y_floor
i0 = (self.Nx + (x_floor % self.Nx)) % self.Nx
i1 = (i0 + 1) % self.Nx
j0 = (self.Ny + (y_floor %self. Ny)) % self.Ny
j1 = (j0 + 1) % self.Ny
U00 = input_matrix[j0, i0]
U01 = input_matrix[j0, i1]
U10 = input_matrix[j1, i0]
U11 = input_matrix[j1, i1]
forward_warp = (1-s) * ((1-t) * U00 + t * U10) + s * ((1-t) * U01 + t * U11)
# Backward warp
backward_warp = self.advect_Linear2D_Periodic(forward_warp, -U, -V)
# Warp error
corrected = 0.5 * (3 * input_matrix - backward_warp)
# Final warp
advected_result = self.advect_Linear2D_Periodic(corrected, U, V)
# Limiter
sampling_stack = np.stack((U00,U01,U10,U11), axis = 2)
min_sampling = np.min(sampling_stack, axis = 2)
max_sampling = np.max(sampling_stack, axis = 2)
advected_result = np.clip(advected_result,min_sampling,max_sampling)
return advected_result
def advect_MacCormack2D_Periodic(self, input_matrix, U, V):
# Forward warp with min max of sampling coordinates extracted for limiter
x = self.X - self.dt * U
y = self.Y - self.dt * V
x_floor = np.floor(x).astype(int)
y_floor = np.floor(y).astype(int)
s = x - x_floor
t = y - y_floor
i0 = (self.Nx + (x_floor % self.Nx)) % self.Nx
i1 = (i0 + 1) % self.Nx
j0 = (self.Ny + (y_floor % self.Ny)) % self.Ny
j1 = (j0 + 1) % self.Ny
U00 = input_matrix[j0, i0]
U01 = input_matrix[j0, i1]
U10 = input_matrix[j1, i0]
U11 = input_matrix[j1, i1]
forward_warp = (1-s) * ((1-t) * U00 + t * U10) + s * ((1-t) * U01 + t * U11)
# Backward warp
backward_warp = self.advect_Linear2D_Periodic(forward_warp, -U, -V)
# Correct
advected_result = forward_warp + 0.5 * (input_matrix - backward_warp)
# Limiter
sampling_stack = np.stack((U00,U01,U10,U11), axis = 2)
min_sampling = np.min(sampling_stack, axis = 2)
max_sampling = np.max(sampling_stack, axis = 2)
advected_result = np.clip(advected_result,min_sampling,max_sampling)
return advected_result
def advect_Fedkiw2D_Periodic(self, input_matrix, U, V):
x = self.X - self.dt * U
y = self.Y - self.dt * V
x_floor = np.floor(x).astype(int)
y_floor = np.floor(y).astype(int)
s = x - x_floor
t = y - y_floor
# Extract sampling coordinate
i0 = (self.Nx + (x_floor % self.Nx)) % self.Nx
i_n1 = (i0 - 1) % self.Nx
i1 = (i0 + 1) % self.Nx
i2 = (i1 + 1) % self.Nx
j0 = (self.Ny + (y_floor % self.Ny)) % self.Ny
j_n1 = (j0 - 1) % self.Ny
j1 = (j0 + 1) % self.Ny
j2 = (j1 + 1) % self.Ny
# Sample coordinates
# i - 1
q_jn1_in1 = input_matrix[j_n1, i_n1] # j - 1
q_j0_in1 = input_matrix[j0, i_n1] # j
q_j1_in1 = input_matrix[j1, i_n1] # j + 1
q_j2_in1 = input_matrix[j2, i_n1] # j + 2
# i
q_jn1_i0 = input_matrix[j_n1, i0] # j - 1
q_j0_i0 = input_matrix[j0, i0] # j, j
q_j1_i0 = input_matrix[j1, i0] # j + 1
q_j2_i0 = input_matrix[j2, i0] # j + 2
# i + 1
q_jn1_i1 = input_matrix[j_n1, i1] # j - 1
q_j0_i1 = input_matrix[j0, i1] # j
q_j1_i1 = input_matrix[j1, i1] # j + 1
q_j2_i1 = input_matrix[j2, i1] # j + 2
# i + 2
q_jn1_i2 = input_matrix[j_n1, i2] # j - 1
q_j0_i2 = input_matrix[j0, i2] # j
q_j1_i2 = input_matrix[j1, i2] # j + 1
q_j2_i2 = input_matrix[j2, i2] # j + 2
# y-axis interpolation
# in1
delQ = q_j1_in1 - q_j0_in1
di = (q_j1_in1 - q_jn1_in1)/2
di_plus1 = (q_j2_in1 - q_j0_in1)/2
# Monotonic correction
sign_delQ = np.sign(delQ)
sign_di = np.sign(di)
sign_di_plus1 = np.sign(di_plus1)
mask_di = (sign_di != sign_delQ)
mask_di_plus1 = (sign_di_plus1 != sign_delQ)
di[mask_di] = 0
di_plus1[mask_di_plus1] = 0
# Interp
q_in1 = q_j0_in1 + di*t + (3 * delQ - 2 * di - di_plus1)*t**2 + (-2*delQ + di + di_plus1)*t**3
# i0
delQ = q_j1_i0 - q_j0_i0
di = (q_j1_i0 - q_jn1_i0)/2
di_plus1 = (q_j2_i0 - q_j0_i0)/2
# Monotonic correction
sign_delQ = np.sign(delQ)
sign_di = np.sign(di)
sign_di_plus1 = np.sign(di_plus1)
mask_di = (sign_di != sign_delQ)
mask_di_plus1 = (sign_di_plus1 != sign_delQ)
di[mask_di] = 0
di_plus1[mask_di_plus1] = 0
# Interp
q_i0 = q_j0_i0 + di*t + (3 * delQ - 2 * di - di_plus1)*t**2 + (-2*delQ + di + di_plus1)*t**3
# i1
delQ = q_j1_i1 - q_j0_i1
di = (q_j1_i1 - q_jn1_i1)/2
di_plus1 = (q_j2_i1 - q_j0_i1)/2
# Monotonic correction
sign_delQ = np.sign(delQ)
sign_di = np.sign(di)
sign_di_plus1 = np.sign(di_plus1)
mask_di = (sign_di != sign_delQ)
mask_di_plus1 = (sign_di_plus1 != sign_delQ)
di[mask_di] = 0
di_plus1[mask_di_plus1] = 0
# Interp
q_i1 = q_j0_i1 + di*t + (3 * delQ - 2 * di - di_plus1)*t**2 + (-2*delQ + di + di_plus1)*t**3
# i2
delQ = q_j1_i2 - q_j0_i2
di = (q_j1_i2 - q_jn1_i2)/2
di_plus1 = (q_j2_i2 - q_j0_i2)/2
# Monotonic correction
sign_delQ = np.sign(delQ)
sign_di = np.sign(di)
sign_di_plus1 = np.sign(di_plus1)
mask_di = (sign_di != sign_delQ)
mask_di_plus1 = (sign_di_plus1 != sign_delQ)
di[mask_di] = 0
di_plus1[mask_di_plus1] = 0
# Interp
q_i2 = q_j0_i2 + di*t + (3 * delQ - 2 * di - di_plus1)*t**2 + (-2*delQ + di + di_plus1)*t**3
# x-axis interpolation
delQ = q_i1 - q_i0
di = (q_i1 - q_in1)/2
di_plus1 = (q_i2 - q_i0)/2
# Monotonic correction
sign_delQ = np.sign(delQ)
sign_di = np.sign(di)
sign_di_plus1 = np.sign(di_plus1)
mask_di = (sign_di != sign_delQ)
mask_di_plus1 = (sign_di_plus1 != sign_delQ)
di[mask_di] = 0
di_plus1[mask_di_plus1] = 0
# Interp
advected_result = q_i0 + di*s + (3 * delQ - 2 * di - di_plus1)*s**2 + (-2*delQ + di + di_plus1)*s**3
return advected_result
def advect_Linear2D_Periodic(self, input_matrix, U, V):
x = self.X - self.dt * U
y = self.Y - self.dt * V
x_floor = np.floor(x).astype(int)
y_floor = np.floor(y).astype(int)
s = x - x_floor
t = y - y_floor
i0 = (self.Nx + (x_floor % self.Nx)) % self.Nx
i1 = (i0 + 1) % self.Nx
j0 = (self.Ny + (y_floor % self.Ny)) % self.Ny
j1 = (j0 + 1) % self.Ny
U00 = input_matrix[j0, i0]
U01 = input_matrix[j0, i1]
U10 = input_matrix[j1, i0]
U11 = input_matrix[j1, i1]
advected_result = (1-s) * ((1-t) * U00 + t * U10) + s * ((1-t) * U01 + t * U11)
return advected_result
def advect_Cubic2D_Periodic(self, input_matrix, U, V):
x = self.X - self.dt * U
y = self.Y - self.dt * V
x_floor = np.floor(x).astype(np.int16)
y_floor = np.floor(y).astype(np.int16)
s = x - x_floor
t = y - y_floor
# Interpolation weights x
s_sq = s**2
s_cub = s_sq*s
s_Wn1 = -1/3 * s + 1/2 * s_sq - 1/6 * s_cub
s_W0 = 1 - s_sq + 1/2 * (s_cub - s)
s_W1 = s + 1/2 * (s_sq - s_cub)
s_W2 = 1/6 * (s_cub - s)
# Interpolation weights y
t_sq = t**2
t_cub = t_sq*t
t_Wn1 = -1/3 * t + 1/2 * t_sq - 1/6 * t_cub
t_W0 = 1 - t_sq + 1/2 * (t_cub - t)
t_W1 = t + 1/2 * (t_sq - t_cub)
t_W2 = 1/6 * (t_cub - t)
# Extract sampling coordinate
i0 = (self.Nx + (x_floor % self.Nx)) % self.Nx
i_n1 = (i0 - 1) % self.Nx
i1 = (i0 + 1) % self.Nx
i2 = (i1 + 1) % self.Nx
j0 = (self.Ny + (y_floor % self.Ny)) % self.Ny
j_n1 = (j0 - 1) % self.Ny
j1 = (j0 + 1) % self.Ny
j2 = (j1 + 1) % self.Ny
# Sample coordinates
# j - 1
q_jn1_in1 = input_matrix[j_n1, i_n1] # i - 1 for s_Wn1
q_j0_in1 = input_matrix[j0, i_n1] # i for s_W0
q_j1_in1 = input_matrix[j1, i_n1] # i + 1 for s_W1
q_j2_in1 = input_matrix[j2, i_n1] # i + 2 for s_W2
# j
q_jn1_i0 = input_matrix[j_n1, i0] # i - 1 for s_Wn1
q_j0_i0 = input_matrix[j0, i0] # i, j for s_W0
q_j1_i0 = input_matrix[j1, i0] # i + 1 for s_W1
q_j2_i0 = input_matrix[j2, i0] # i + 2 for s_W2
# j + 1
q_jn1_i1 = input_matrix[j_n1, i1] # i - 1 for s_Wn1
q_j0_i1 = input_matrix[j0, i1] # i for s_W0
q_j1_i1 = input_matrix[j1, i1] # i + 1 for s_W1
q_j2_i1 = input_matrix[j2, i1] # i + 2 for s_W2
# j + 2
q_jn1_i2 = input_matrix[j_n1, i2] # i - 1 for s_Wn1
q_j0_i2 = input_matrix[j0, i2] # i for s_W0
q_j1_i2 = input_matrix[j1, i2] # i + 1 for s_W1
q_j2_i2 = input_matrix[j2, i2] # i + 2 for s_W2
# y-axis interpolation
q_in1 = t_Wn1 * q_jn1_in1 + t_W0 * q_j0_in1 + t_W1 * q_j1_in1 + t_W2 * q_j2_in1
q_i0 = t_Wn1 * q_jn1_i0 + t_W0 * q_j0_i0 + t_W1 * q_j1_i0 + t_W2 * q_j2_i0
q_i1 = t_Wn1 * q_jn1_i1 + t_W0 * q_j0_i1 + t_W1 * q_j1_i1 + t_W2 * q_j2_i1
q_i2 = t_Wn1 * q_jn1_i2 + t_W0 * q_j0_i2 + t_W1 * q_j1_i2 + t_W2 * q_j2_i2
# x-axis interpolation
advected_result = s_Wn1 * q_in1 + s_W0 * q_i0 + s_W1 * q_i1 + s_W2 * q_i2
return advected_result
def __init__(self,
Nx, Ny,
dt, n_iter,
Nu = 0, Nu_scalar = 0, C_dissip = 0,
VC_weight = 0,
advection_type = 'cubic'):
valid_advection_type = {'linear','cubic','bfecc','maccormack','fedkiw'}
assert advection_type in valid_advection_type, f'Invalid input: {advection_type}. Must be one of: {valid_advection_type}.'
# Interpolation method for advection
if advection_type == 'cubic':
self.advect = self.advect_Cubic2D_Periodic
elif advection_type == 'linear':
self.advect = self.advect_Linear2D_Periodic
elif advection_type == 'bfecc':
self.advect = self.advect_BFECC2D_Periodic
elif advection_type == 'maccormack':
self.advect = self.advect_MacCormack2D_Periodic
elif advection_type == 'fedkiw':
self.advect = self.advect_Fedkiw2D_Periodic
else:
raise ValueError('Unknown advection type') # Program stops with ValueError
# Dimensions and grid
self.Nx = Nx
self.Ny = Ny
self.X, self.Y = np.meshgrid(np.arange(0,Nx),np.arange(0,Ny), indexing='xy')
# Iterations and timestep
self.n_iter = n_iter
self.dt = dt
# Viscosities and dissipation constants
self.Nu = Nu
self.Nu_scalar = Nu_scalar
self.C_dissip = C_dissip
# Vorticity confinement parameter
self.VC_weight = VC_weight
self.advection_type = advection_type
self.elapsed_time = None
def print_parameters(self):
print('----- Parameter summary -----')
print(f'Grid resolution: X={self.Nx}, Y={self.Ny}')
print(f'Iterations: {self.n_iter}')
print(f'dt: {self.dt}')
print(f'Fluid viscosity: {self.Nu}')
print(f'Scalar viscosity: {self.Nu_scalar}')
print(f'Scalar dissipation: {self.C_dissip}')
print(f'Advection: {self.advection_type}')
print(f'Vorticity confinement: {self.VC_weight}')
def calc_vorticity_2D(self,U,V): # curl = vorticty = 2X the angular velocity
dU_dy, dU_dx = np.gradient(U)
dV_dy, dV_dx = np.gradient(V)
result = dV_dx - dU_dy
return result
def vorticity_confinement_2D(self,U,V):
omega_z = self.calc_vorticity_2D(U,V) # 2D Vorticity
omega = np.abs(omega_z) # |vorticity|
eta_y, eta_x = np.gradient(omega) # nabla |vorticity|
norm = np.sqrt(eta_x**2 + eta_y**2) + 0.0000001
Nx = eta_x / norm
Ny = eta_y / norm
fx = self.VC_weight * Ny * omega_z
fy = self.VC_weight * -Nx * omega_z
return fx, fy
def simulate_2D(self,U_force,V_force,S_force,n_iter_force):
# Forcing function
assert U_force.shape == V_force.shape, 'Mismatch in U and V forcing function dimensions'
assert U_force.shape[0:1] == S_force.shape[0:1], 'Mismatch in velocity and density forcing function dimensions'
assert (U_force.shape[0], U_force.shape[1]) == (self.Ny, self.Nx), \
'Forcing function dimensions do not match the simulation domain'
assert n_iter_force <= self.n_iter, 'Forcing exceeds max iteration'
# time_vector = np.arange(0,self.T,self.dt)
U_full = np.zeros((self.Ny,self.Nx,self.n_iter))
V_full = np.zeros((self.Ny,self.Nx,self.n_iter))
S_full = np.zeros((self.Ny,self.Nx,self.n_iter))
# Setup wavenumbers
kx = np.fft.rfftfreq(self.Nx) * self.Nx
ky = np.fft.fftfreq(self.Ny) * self.Ny
kx, ky = np.meshgrid(kx, ky, indexing='xy')
k_squared = kx**2 + ky**2
k_squared[0, 0] = 1 # Avoid division by zero at the zero frequency
kx_norm = kx / k_squared
ky_norm = ky / k_squared
U = np.zeros((self.Ny,self.Nx))
V = np.zeros((self.Ny,self.Nx))
S = np.zeros((self.Ny,self.Nx))
# Precompute constant denominators
vel_diffu_denom = (1 + self.Nu * self.dt * k_squared)
scal_diffu_denom = (1 + self.Nu_scalar * self.dt * k_squared)
diss_denom = (1 + self.dt * self.C_dissip)
start_time = timer()
for i in tqdm(range(self.n_iter)): # [!] dt as a step + enumerate?
# *** Velocity step ***
# -------------- Force velocity --------------
if i < n_iter_force:
U += self.dt * U_force
V += self.dt * V_force
S += self.dt * S_force
# -------------- Vorticity confinement --------------
if self.VC_weight > 0:
f_vort_x, f_vort_y = self.vorticity_confinement_2D(U,V)
U += self.dt * f_vort_x
V += self.dt * f_vort_y
# -------------- Advect --------------
U_0 = U
V_0 = V
U = self.advect(U, U_0, V_0) # Cubic polynomial
V = self.advect(V, U_0, V_0)
U_fft = rfft2(U)
V_fft = rfft2(V)
# -------------- Diffuse velocity --------------
U_fft /= vel_diffu_denom
V_fft /= vel_diffu_denom
# -------------- Project --------------
div_fft = (U_fft * kx + V_fft * ky) # (* 1j) - Compute divergence
U = irfft2(U_fft - div_fft * kx_norm)
V = irfft2(V_fft - div_fft * ky_norm)
# *** Density step ***
# -------------- Advect --------------
S = self.advect(S, U, V) # Cubic polynomial
# -------------- Diffuse density --------------
S_fft = rfft2(S)
S_fft /= scal_diffu_denom
S = irfft2(S_fft)
# -------------- Dissipate density --------------
S /= diss_denom
# -------------- Accumulate timestep result --------------
U_full[:,:,i] = U
V_full[:,:,i] = V
S_full[:,:,i] = S
end_time = timer()
self.elapsed_time = end_time - start_time
print(f'Simulation complete in: {self.elapsed_time} seconds')
return U_full, V_full, S_full