-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_flow_inout_v1.py
More file actions
358 lines (317 loc) · 19.1 KB
/
Copy pathpipe_flow_inout_v1.py
File metadata and controls
358 lines (317 loc) · 19.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
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
# Transient internal pipe flow simulation
# Includes basic SIMPLE algorithm
# LIBRARIES
import numpy as np
import matplotlib.pyplot as plt
# CONSTANTS
N_POINTS_Y = 15 # number of points in y direction
AR = 10 # aspect ratio of the pipe
MU = 0.01 # kinematic viscosity
TIME_STEP = 0.001 # time step length
N_TIME_STEPS = 5000 # total time steps
PLOT_EVERY = 50 # plot frames per time step
N_POISSON = 50 # number of pressure poisson iterations
# MAIN FUNCTION FOR THE SIMULATION
def main():
cell_lenght = 1.0 / (N_POINTS_Y-1) # cell lenght
n_points_x = (N_POINTS_Y-1) * AR + 1 # number of points in x direction
x_range = np.linspace(0.0, 1.0*AR, n_points_x) # x coordinates
y_range = np.linspace(0.0, 1.0, N_POINTS_Y) # x coordinates
coordinates_x, coordinates_y = np.meshgrid(x_range, y_range) # 2D mesh domain
# INITIAL CONDITIONS
velocity_x_prev = np.ones((N_POINTS_Y+1, n_points_x)) # initial velocity in x direction
velocity_x_prev[0, :] = -velocity_x_prev[1, :] # upper wall boundary condition
velocity_x_prev[-1, :] = -velocity_x_prev[-2, :] # lower wall boundary condition
velocity_y_prev = np.ones((N_POINTS_Y, n_points_x+1)) # initial velocity in y direction
pressure_prev = np.zeros((N_POINTS_Y+1, n_points_x+1)) # initial uniform zero pressure
# PRE-ALLOCATING ARRAYS
velocity_x_tent = np.zeros_like(velocity_x_prev) # pre-allocated tentative x velocity
velocity_x_next = np.zeros_like(velocity_x_prev) # pre-allocated next x velocity
velocity_y_tent = np.zeros_like(velocity_y_prev) # pre-allocated tentative y velocity
velocity_y_next = np.zeros_like(velocity_y_prev) # pre-allocated next y velocity
plt.figure(figsize=(1.5*AR, 4))
# MAIN TIME LOOP
for iter in range(N_TIME_STEPS):
# UPDATING INTERIOR X VELOCITY WITH MOMENTUM EQUATION
diffusion_x = MU * ( # definition of diffusion equation
(
velocity_x_prev[1:-1, 2: ] # forward stencil point x direction
+
velocity_x_prev[2: , 1:-1] # backward stencil point x direction
+
velocity_x_prev[1:-1, :-2] # forward stencil point y direction
+
velocity_x_prev[ :-2, 1:-1] # backward stencil point y direction
- 4 *
velocity_x_prev[1:-1, 1:-1] # interior velocity field
) / (
cell_lenght**2 # cell lenght squared
)
)
convection_x = ( # definition of convection equation
(
velocity_x_prev[1:-1, 2: ]**2 # forward stencil point x direction
-
velocity_x_prev[1:-1, :-2]**2 # backward stencil point x direction
) / (
2 * cell_lenght
)
+
(
velocity_y_prev[1: ,1:-2] # top left stencil point y velocity
+
velocity_y_prev[1: ,2:-1] # top right stencil point y velocity
+
velocity_y_prev[ :-1, 1:-2] # bottom left stencil point y velocity
+
velocity_y_prev[ :-1, 2:-1] # bottom right stencil point y velocity
) / 4
*
(
velocity_x_prev[2: , 1:-1] # forward stencil point y direction
-
velocity_x_prev[ :-2, 1:-1] # backward stencil point y direction
) / (
2 * cell_lenght # cell lenght squared
)
)
pressure_gradient_x = ( # definition of pressure gradient
(
pressure_prev[1:-1, 2:-1] # forward stencil interior pressure in x
-
pressure_prev[1:-1, 1:-2] # backward stencil interior pressure in x
) / (
cell_lenght # cell lenght
)
)
velocity_x_tent[1:-1, 1:-1] = ( # interior tentative velocity in x direction
velocity_x_prev[1:-1, 1:-1] # previous tentative velocity in x direction
+
TIME_STEP # per time step
*
(
-pressure_gradient_x # negative pressure gradient
+
diffusion_x # effect of diffusion
-
convection_x # effect of convection
)
)
velocity_x_tent[1:-1, 0] = 1.0 # left edge boundary condition
velocity_x_tent[1:-1, -1] = velocity_x_tent[1:-1, -2] # right edge boundary condition
velocity_x_tent[0, :] = -velocity_x_tent[1, :] # bottom edge boundary condition
velocity_x_tent[-1, :] = -velocity_x_tent[-2, :] # top edge boundary condition
# UPDATING INTERIOR X VELOCITY WITH MOMENTUM EQUATION
diffusion_y = MU * ( # definition of diffusion equation
(
velocity_y_prev[1:-1, 2: ] # forward stencil point x direction
+
velocity_y_prev[2: , 1:-1] # backward stencil point x direction
+
velocity_y_prev[1:-1, :-2] # forward stencil point y direction
+
velocity_y_prev[ :-2, 1:-1] # backward stencil point y direction
- 4 *
velocity_y_prev[1:-1, 1:-1] # interior velocity field
) / (
cell_lenght**2 # cell lenght squared
)
)
convection_y = ( # definition of convection equation
(
velocity_x_prev[2:-1, 1: ] # prefactor forward based on x velocity
+
velocity_x_prev[2:-1, :-1] # prefactor backward based on x velocity
+
velocity_x_prev[1:-2, 1: ] # prefactor forward based on x velocity
+
velocity_x_prev[1:-2, :-1] # prefactor backward based on x velocity
) / 4
*
(
velocity_y_prev[1:-1, 2: ] # forward stencil point x direction
-
velocity_y_prev[1:-1, :-2] # backward stencil point x direction
) / (
2 * cell_lenght # cell lenght
)
+
(
velocity_y_prev[2: ,1:-1]**2 # top left stencil point y velocity
-
velocity_y_prev[ :-2 ,1:-1]**2 # top right stencil point y velocity
) / (
2* cell_lenght # cell lenght
)
)
pressure_gradient_y = ( # definition of pressure gradient
(
pressure_prev[2:-1, 1:-1] # forward stencil interior pressure in x
-
pressure_prev[1:-2, 1:-1] # backward stencil interior pressure in x
) / (
cell_lenght # cell lenght
)
)
velocity_y_tent[1:-1, 1:-1] = ( # interior tentative velocity in x direction
velocity_y_prev[1:-1, 1:-1] # previous tentative velocity in x direction
+
TIME_STEP # per time step
*
(
-pressure_gradient_y # negative pressure gradient
+
diffusion_y # effect of diffusion
-
convection_y # effect of convection
)
)
velocity_y_tent[1:-1, 0] = -velocity_y_tent[1:-1, -1] # left edge boundary condition
velocity_y_tent[1:-1, -1] = velocity_y_tent[1:-1, -2] # right edge boundary condition
velocity_y_tent[0, :] = 0.0 # bottom edge boundary condition
velocity_y_tent[-1, :] = 0.0 # top edge boundary condition
# COMPUTING DIVERGENCE FOR PRESSURE POISSON PROBLEM
divergence = ( # definition of divergence
(
velocity_x_tent[1:-1, 1:] # tentative velocity interior forward in x direction
-
velocity_x_tent[1:-1, :-1] # tentative velocity interior backward in x direction
) / (
cell_lenght # cell lenght
)
+
(
velocity_y_tent[1: ,1:-1] # tentative velocity interior forward in y direction
-
velocity_y_tent[ :-1, 1:-1] # tentative velocity interior backward in x direction
) / (
cell_lenght # cell lenght
)
)
density = 1 # fluid density
pressure_poisson_rhs = divergence * density / TIME_STEP # pressure poisson equation right hand side
# SOLVING PRESSURE CORRECTION POISSON EQUATION
pressure_corr_prev = np.zeros_like(pressure_prev) # previous pressure correction
for _ in range(N_POISSON): # iteration for specific poisson iteration count
pressure_corr_next = np.zeros_like(pressure_corr_prev) # next pressure correction
pressure_corr_next[1:-1, 1:-1] = 1/4 * (
pressure_corr_prev[1:-1, 2: ] # interior pressure forward in x direction
+
pressure_corr_prev[2: , 1:-1] # interior pressure forward in y direction
+
pressure_corr_prev[1:-1, :-2] # interior pressure backward in x direction
+
pressure_corr_prev[ :-2, 1:-1] # interior pressure backward in y direction
-
cell_lenght**2 # cell lenght squared
*
pressure_poisson_rhs # pressure poisson right hand side
)
pressure_corr_next[1:-1, 0] = pressure_corr_next[1:-1, 1] # homogenous pressure left boundary condition
pressure_corr_next[1:-1, -1] = -pressure_corr_next[1:-1, -2] # homogenous pressure right boundary condition
pressure_corr_next[0, :] = pressure_corr_next[1, :] # homogenous pressure top boundary condition
pressure_corr_next[-1, :] = pressure_corr_next[-2, :] # homogenous pressure bottom boundary condition
pressure_corr_prev = pressure_corr_next # advance in smoothing to enforce incompressibility
pressure_next = pressure_prev + pressure_corr_next # updatingthe pressure
# UPDATE VELOCITY
pressure_corr_grad_x = ( # pressure correction gradient in x
(
pressure_corr_next[1:-1, 2:-1] # interior pressure forward in x direction
-
pressure_corr_next[1:-1, 1:-2] # interior pressure backward in x direction
) / (
cell_lenght # cell lenght
)
)
velocity_x_next[1:-1, 1:-1] = ( # velocity ıpdate in x direction
velocity_x_tent[1:-1, 1:-1] # tentative velocity in x
-
TIME_STEP # time step lenght
*
pressure_corr_grad_x # pressure correction gradient in x direction
)
pressure_corr_grad_y = ( # pressure correction gradient in y
(
pressure_corr_next[2:-1, 1:-1] # interior pressure forward in y direction
-
pressure_corr_next[1:-2, 1:-1] # interior pressure backward in y direction
) / (
cell_lenght # cell lenght
)
)
velocity_y_next[1:-1, 1:-1] = ( # velocity ıpdate in y direction
velocity_y_tent[1:-1, 1:-1] # tentative velocity in y
-
TIME_STEP # time step lenght
*
pressure_corr_grad_y # pressure correction gradient in y direction
)
velocity_x_next[1:-1, 0] = 1.0 # left edge velocity boundary condition
inflow_mass_rate_next = np.sum(velocity_x_next[1:-1, 0]) # inlet total velocity
outflow_mass_rate_next = np.sum(velocity_x_next[1:-1, -2]) # outlet total velocity
mrr = inflow_mass_rate_next / outflow_mass_rate_next # mass rate ratio
velocity_x_next[1:-1, -1] = velocity_x_next[1:-1, -2] * mrr # right edge velocity boundary condition
velocity_x_next[0, :] = -velocity_x_next[1, :] # bottom edge velocity boundary condition
velocity_x_next[-1, :] = -velocity_x_next[-2, :] # top edge velocity boundary condition
velocity_y_next[1:-1, 0] = -velocity_y_next[1:-1, -1] # left edge velocity boundary condition
velocity_y_next[1:-1, -1] = velocity_y_next[1:-1, -2] # right edge velocity boundary condition
velocity_y_next[0, :] = 0.0 # bottom edge velocity boundary condition
velocity_y_next[-1, :] = 0.0 # top edge velocity boundary condition
# REPEATING THE ITERATIONS
velocity_x_prev = velocity_x_next # advance in time for x velocity
velocity_y_prev = velocity_y_next # advance in time for y velocity
pressure_prev = pressure_next # advance in time for pressure
# VISUALIZATION
if iter % PLOT_EVERY == 0:
velocity_x_vertex_centered = ( # vetices to be plotted in x
(
velocity_x_next[1: , :] # left values
+
velocity_x_next[ :-1, :] # right values
) / 2 # calculating the mean value
)
velocity_y_vertex_centered = ( # vetices to be plotted in y
(
velocity_y_next[:, 1:] # top values
+
velocity_y_next[:, :-1] # bottom values
) / 2 # calculating the mean value
)
plt.contourf( # plotting thr velocity field
coordinates_x,
coordinates_y,
velocity_x_vertex_centered,
levels=10,
cmap='coolwarm'
)
plt.colorbar()
plt.quiver( # plotting the x velocity components
coordinates_x[:, ::6],
coordinates_y[:, ::6],
velocity_x_vertex_centered[:, ::6],
velocity_y_vertex_centered[:, ::6],
alpha=0.4,
)
plt.plot( # velocity parabola
5*cell_lenght + velocity_x_vertex_centered[: ,5],
coordinates_y[:, 5],
color='white'
)
plt.plot( # velocity parabola
40*cell_lenght + velocity_x_vertex_centered[: ,40],
coordinates_y[:, 40],
color='white'
)
plt.plot( # velocity parabola
80*cell_lenght + velocity_x_vertex_centered[: ,80],
coordinates_y[:, 80],
color='white'
)
plt.title(f'Iteration {iter}/{N_TIME_STEPS}')
plt.xlabel("Position along the Pipe")
plt.ylabel("Cross section of the Pipe")
plt.draw()
plt.pause(0.05)
plt.clf()
if __name__ == "__main__":
main()