-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_cfd_simulation.py
executable file
·349 lines (284 loc) · 10.6 KB
/
run_cfd_simulation.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
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
"""
Solves the incompressible Navier Stokes equations in a lid-driven cavity
scenario using Finite Differences, explicit timestepping and Chorin's Projection.
Momentum: ∂u/∂t + (u ⋅ ∇) u = − 1/ρ ∇p + ν ∇²u + f
Incompressibility: ∇ ⋅ u = 0
u: Velocity (2d vector)
p: Pressure
f: Forcing (here =0)
ν: Kinematic Viscosity
ρ: Density
t: Time
∇: Nabla operator (defining nonlinear convection, gradient and divergence)
∇²: Laplace Operator
----
Lid-Driven Cavity Scenario:
------>>>>> u_top
1 +-------------------------------------------------+
| |
| * * |
| * * * * |
0.8 | |
| * |
| * * |
| * * |
0.6 | * |
u = 0 | * * | u = 0
v = 0 | * | v = 0
| * |
| * * * |
0.4 | |
| |
| * * * |
| * * |
0.2 | * * |
| * |
| * * * * * |
| * |
0 +-------------------------------------------------+
0 0.2 0.4 0.6 0.8 1
u = 0
v = 0
* Velocity and pressure have zero initial condition.
* Homogeneous Dirichlet Boundary Conditions everywhere except for horizontal
velocity at top. It is driven by an external flow.
-----
Solution strategy: (Projection Method: Chorin's Splitting)
1. Solve Momentum equation without pressure gradient for tentative velocity
(with given Boundary Conditions)
∂u/∂t + (u ⋅ ∇) u = ν ∇²u
2. Solve pressure poisson equation for pressure at next point in time
(with homogeneous Neumann Boundary Conditions everywhere except for
the top, where it is homogeneous Dirichlet)
∇²p = ρ/Δt ∇ ⋅ u
3. Correct the velocities (and again enforce the Velocity Boundary Conditions)
u ← u − Δt/ρ ∇ p
-----
Expected Outcome: After some time a swirling motion will take place
1 +-------------------------------------------------+
| |
| |
| |
0.8 | |
| *-->* |
| ****** ****** |
| ** ** |
0.6 | * * |
| * * |
| * * |
| * * |
| * * |
0.4 | * * |
| ** ** |
| ****** ****** |
| *<--* |
0.2 | |
| |
| |
| |
0 +-------------------------------------------------+
0 0.2 0.4 0.6 0.8 1
------
Strategy in index notation
u = [u, v]
x = [x, y]
1. Solve tentative velocity + velocity BC
∂u/∂t + u ∂u/∂x + v ∂u/∂y = ν ∂²u/∂x² + ν ∂²u/∂y²
∂v/∂t + u ∂v/∂x + v ∂v/∂y = ν ∂²v/∂x² + ν ∂²v/∂y²
2. Solve pressure poisson + pressure BC
∂²p/∂x² + ∂²p/∂y² = ρ/Δt (∂u/∂x + ∂v/∂y)
3. Correct velocity + velocity BC
u ← u − Δt/ρ ∂p/∂x
v ← v − Δt/ρ ∂p/∂y
------
IMPORTANT: Take care to select a timestep that ensures stability
"""
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
N_POINTS = 41
DOMAIN_SIZE = 1.0
N_ITERATIONS = 500
TIME_STEP_LENGTH = 0.001
KINEMATIC_VISCOSITY = 0.1
DENSITY = 1.0
HORIZONTAL_VELOCITY_TOP = 1.0
N_PRESSURE_POISSON_ITERATIONS = 50
STABILITY_SAFETY_FACTOR = 0.5
def main():
element_length = DOMAIN_SIZE / (N_POINTS - 1)
x = np.linspace(0.0, DOMAIN_SIZE, N_POINTS)
y = np.linspace(0.0, DOMAIN_SIZE, N_POINTS)
X, Y = np.meshgrid(x, y)
u_prev = np.zeros_like(X)
v_prev = np.zeros_like(X)
p_prev = np.zeros_like(X)
def central_difference_x(f):
diff = np.zeros_like(f)
diff[1:-1, 1:-1] = (
f[1:-1, 2: ]
-
f[1:-1, 0:-2]
) / (
2 * element_length
)
return diff
def central_difference_y(f):
diff = np.zeros_like(f)
diff[1:-1, 1:-1] = (
f[2: , 1:-1]
-
f[0:-2, 1:-1]
) / (
2 * element_length
)
return diff
def laplace(f):
diff = np.zeros_like(f)
diff[1:-1, 1:-1] = (
f[1:-1, 0:-2]
+
f[0:-2, 1:-1]
-
4
*
f[1:-1, 1:-1]
+
f[1:-1, 2: ]
+
f[2: , 1:-1]
) / (
element_length**2
)
return diff
maximum_possible_time_step_length = (
0.5 * element_length**2 / KINEMATIC_VISCOSITY
)
if TIME_STEP_LENGTH > STABILITY_SAFETY_FACTOR * maximum_possible_time_step_length:
raise RuntimeError("Stability is not guarenteed")
for _ in tqdm(range(N_ITERATIONS)):
d_u_prev__d_x = central_difference_x(u_prev)
d_u_prev__d_y = central_difference_y(u_prev)
d_v_prev__d_x = central_difference_x(v_prev)
d_v_prev__d_y = central_difference_y(v_prev)
laplace__u_prev = laplace(u_prev)
laplace__v_prev = laplace(v_prev)
# Perform a tentative step by solving the momentum equation without the
# pressure gradient
u_tent = (
u_prev
+
TIME_STEP_LENGTH * (
-
(
u_prev * d_u_prev__d_x
+
v_prev * d_u_prev__d_y
)
+
KINEMATIC_VISCOSITY * laplace__u_prev
)
)
v_tent = (
v_prev
+
TIME_STEP_LENGTH * (
-
(
u_prev * d_v_prev__d_x
+
v_prev * d_v_prev__d_y
)
+
KINEMATIC_VISCOSITY * laplace__v_prev
)
)
# Velocity Boundary Conditions: Homogeneous Dirichlet BC everywhere
# except for the horizontal velocity at the top, which is prescribed
u_tent[0, :] = 0.0
u_tent[:, 0] = 0.0
u_tent[:, -1] = 0.0
u_tent[-1, :] = HORIZONTAL_VELOCITY_TOP
v_tent[0, :] = 0.0
v_tent[:, 0] = 0.0
v_tent[:, -1] = 0.0
v_tent[-1, :] = 0.0
d_u_tent__d_x = central_difference_x(u_tent)
d_v_tent__d_y = central_difference_y(v_tent)
# Compute a pressure correction by solving the pressure-poisson equation
rhs = (
DENSITY / TIME_STEP_LENGTH
*
(
d_u_tent__d_x
+
d_v_tent__d_y
)
)
for _ in range(N_PRESSURE_POISSON_ITERATIONS):
p_next = np.zeros_like(p_prev)
p_next[1:-1, 1:-1] = 1/4 * (
+
p_prev[1:-1, 0:-2]
+
p_prev[0:-2, 1:-1]
+
p_prev[1:-1, 2: ]
+
p_prev[2: , 1:-1]
-
element_length**2
*
rhs[1:-1, 1:-1]
)
# Pressure Boundary Conditions: Homogeneous Neumann Boundary
# Conditions everywhere except for the top, where it is a
# homogeneous Dirichlet BC
p_next[:, -1] = p_next[:, -2]
p_next[0, :] = p_next[1, :]
p_next[:, 0] = p_next[:, 1]
p_next[-1, :] = 0.0
p_prev = p_next
d_p_next__d_x = central_difference_x(p_next)
d_p_next__d_y = central_difference_y(p_next)
# Correct the velocities such that the fluid stays incompressible
u_next = (
u_tent
-
TIME_STEP_LENGTH / DENSITY
*
d_p_next__d_x
)
v_next = (
v_tent
-
TIME_STEP_LENGTH / DENSITY
*
d_p_next__d_y
)
# Velocity Boundary Conditions: Homogeneous Dirichlet BC everywhere
# except for the horizontal velocity at the top, which is prescribed
u_next[0, :] = 0.0
u_next[:, 0] = 0.0
u_next[:, -1] = 0.0
u_next[-1, :] = HORIZONTAL_VELOCITY_TOP
v_next[0, :] = 0.0
v_next[:, 0] = 0.0
v_next[:, -1] = 0.0
v_next[-1, :] = 0.0
# Advance in time
u_prev = u_next
v_prev = v_next
p_prev = p_next
# The [::2, ::2] selects only every second entry (less cluttering plot)
plt.style.use("dark_background")
plt.figure()
plt.contourf(X[::2, ::2], Y[::2, ::2], p_next[::2, ::2], cmap="coolwarm")
plt.colorbar()
plt.quiver(X[::2, ::2], Y[::2, ::2], u_next[::2, ::2], v_next[::2, ::2], color="black")
# plt.streamplot(X[::2, ::2], Y[::2, ::2], u_next[::2, ::2], v_next[::2, ::2], color="black")
plt.xlim((0, 1))
plt.ylim((0, 1))
plt.show()
if __name__ == "__main__":
main()