forked from gforsyth/CFD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnschannel.py
143 lines (117 loc) · 4.26 KB
/
nschannel.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
##Channel Flow Navier Stokes
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
import os
import time
plt.ion()
##variable declarations
nx = 41
ny = 41
nt = 10
nit=50
c = 1
dx = 2.0/(nx-1)
dy = 2.0/(ny-1)
x = np.linspace(0,2,nx)
y = np.linspace(0,2,ny)
Y,X = np.meshgrid(y,x)
##physical variables
rho = 1
nu = .1
F = 1
dt = .01
#initial conditions
u = np.zeros((ny,nx)) ##create a XxY vector of 0's
un = np.zeros((ny,nx)) ##create a XxY vector of 0's
v = np.zeros((ny,nx)) ##create a XxY vector of 0's
vn = np.zeros((ny,nx)) ##create a XxY vector of 0's
p = np.ones((ny,nx)) ##create a XxY vector of 0's
pn = np.ones((ny,nx)) ##create a XxY vector of 0's
b = np.zeros((ny,nx))
udiff = 1
stepcount = 0
while udiff > .001:
un[:] = u[:]
vn[:] = v[:]
b[1:-1,1:-1]=rho*(1/dt*((u[2:,1:-1]-u[0:-2,1:-1])/(2*dx)+(v[1:-1,2:]-v[1:-1,0:-2])/(2*dy))-\
((u[2:,1:-1]-u[0:-2,1:-1])/(2*dx))**2-\
2*((u[1:-1,2:]-u[1:-1,0:-2])/(2*dy)*(v[2:,1:-1]-v[0:-2,1:-1])/(2*dx))-\
((v[1:-1,2:]-v[1:-1,0:-2])/(2*dy))**2)
####Periodic BC Pressure @ x = 2
b[-1,1:-1]=rho*(1/dt*((u[0,1:-1]-u[-2,1:-1])/(2*dx)+(v[-1,2:]-v[-1,0:-2])/(2*dy))-\
((u[0,1:-1]-u[-2,1:-1])/(2*dx))**2-\
2*((u[-1,2:]-u[-1,0:-2])/(2*dy)*(v[0,1:-1]-v[-2,1:-1])/(2*dx))-\
((v[-1,2:]-v[-1,0:-2])/(2*dy))**2)
####Periodic BC Pressure @ x = 0
b[0,1:-1]=rho*(1/dt*((u[1,1:-1]-u[-1,1:-1])/(2*dx)+(v[0,2:]-v[0,0:-2])/(2*dy))-\
((u[1,1:-1]-u[-1,1:-1])/(2*dx))**2-\
2*((u[0,2:]-u[0,0:-2])/(2*dy)*(v[1,1:-1]-v[-1,1:-1])/(2*dx))-\
((v[0,2:]-v[0,0:-2])/(2*dy))**2)
for q in range(nit):
pn[:]=p[:]
p[1:-1,1:-1] = ((pn[2:,1:-1]+pn[0:-2,1:-1])*dy**2+(pn[1:-1,2:]+pn[1:-1,0:-2])*dx**2)/\
(2*(dx**2+dy**2)) -\
dx**2*dy**2/(2*(dx**2+dy**2))*b[1:-1,1:-1]
####Periodic BC Pressure @ x = 2
p[-1,1:-1] = ((pn[0,1:-1]+pn[-2,1:-1])*dy**2+(pn[-1,2:]+pn[-1,0:-2])*dx**2)/\
(2*(dx**2+dy**2)) -\
dx**2*dy**2/(2*(dx**2+dy**2))*b[-1,1:-1]
####Periodic BC Pressure @ x = 0
p[0,1:-1] = ((pn[1,1:-1]+pn[-1,1:-1])*dy**2+(pn[0,2:]+pn[0,0:-2])*dx**2)/\
(2*(dx**2+dy**2)) -\
dx**2*dy**2/(2*(dx**2+dy**2))*b[0,1:-1]
####Wall boundary conditions, pressure
p[-1,:] =p[-2,:] ##dp/dy = 0 at y = 2
p[0,:] = p[1,:] ##dp/dy = 0 at y = 0
u[1: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]-2*un[0,1:-1]+un[0,0:-2]))+F*dt
####Periodic BC v @ x = 2
v[-1,1:-1] = vn[-1,1:-1]-\
un[-1,1:-1]*dt/dx*(vn[-1,1:-1]-vn[-2,1:-1])-\
vn[-1,1:-1]*dt/dy*(vn[-1,1:-1]-vn[-1,0:-2])-\
dt/(2*rho*dy)*(p[-1,2:]-p[-1,0:-2])+\
nu*(dt/dx**2*(vn[0,1:-1]-2*vn[-1,1:-1]+vn[-2,1:-1])+\
(dt/dy**2*(vn[-1,2:]-2*vn[-1,1:-1]+vn[-1,0:-2])))
####Periodic BC v @ x = 0
v[0,1:-1] = vn[0,1:-1]-\
un[0,1:-1]*dt/dx*(vn[0,1:-1]-vn[-1,1:-1])-\
vn[0,1:-1]*dt/dy*(vn[0,1:-1]-vn[0,0:-2])-\
dt/(2*rho*dy)*(p[0,2:]-p[0,0:-2])+\
nu*(dt/dx**2*(vn[1,1:-1]-2*vn[0,1:-1]+vn[-1,1:-1])+\
(dt/dy**2*(vn[0,2:]-2*vn[0,1:-1]+vn[0,0:-2])))
####Wall BC: u,v = 0 @ y = 0,2
u[:,0] = 0
u[:,-1] = 0
v[:,0] = 0
v[:,-1]=0
udiff = (np.sum(u)-np.sum(un))/np.sum(u)
stepcount += 1
plt.quiver(X[::3, ::3], Y[::3, ::3], u[::3, ::3], v[::3, ::3])
plt.show()