-
Notifications
You must be signed in to change notification settings - Fork 51
/
FVM_1D_Convection_Diffusion_Upwind.py
189 lines (139 loc) · 5.32 KB
/
FVM_1D_Convection_Diffusion_Upwind.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
import numpy
import matplotlib.pyplot as plt
print("\n")
print("Finite Volume Method\n")
print("Solving 1D Heat Convection Diffusion Equation with Dirichlet Boundary Condition\n")
print("Discretization for Diffusion Term : Central Difference Scheme")
print("Discretization for Convection Term: Upwind Scheme\n")
k = 100
print("Conductivity of the Material:",k,'W/m-K')
rho = 1
print("Density of the Fluid:",rho,'Kg/m3')
cp = 1000
print("Specific Heat Capacity of the Fluid:",cp,'J/Kg-K')
print("\n")
u = float(input('Enter the flow velocity: '))
area = 0.1
print("Cross Section Area of Rod:",area,'m2')
barLength = 5
print("\nLength of the rod:",barLength,'m')
tempLeft = 100
tempRight = 200
print("Temperature at the Left End of the Rod:",tempLeft,'C')
print("Temperature at the Right End of the Rod:",tempRight,'C')
heatSourcePerVol = 1000
print("Heat Source in the Rod:",heatSourcePerVol,'W/m3')
print("\n")
nCell = int(input('Enter the number of Cells for Meshing the Rod: '))
print ('------------------------------------------------')
print (' Creating Mesh')
print ('------------------------------------------------')
#cell coordinates
xFace = numpy.linspace(0, barLength, nCell+1)
#cell centroids
xCentroid = 0.5*(xFace[1:] + xFace[:-1])
#cell length
cellLength = xFace[1:] - xFace[:-1]
#distance between cell centroids
dCentroid = xCentroid[1:] - xCentroid[:-1]
# For the boundary cell on the left, the distance is double the distance
# from the cell centroid to the boundary face
dLeft = 2*(xCentroid[0] - xFace[0])
# For the boundary cell on the right, the distance is double the distance
#from the cell centroid to the boundary cell face
dRight = 2*(xFace[-1] - xCentroid[-1])
# Append these to the vector of distances
dCentroid = numpy.hstack([dLeft, dCentroid, dRight])
#cellVolume
cellVolume = area*cellLength
print ('------------------------------------------------')
print (' Calculating Matrix Coefficients')
print ('------------------------------------------------')
#diffusive flux
DA = area*numpy.divide(k, dCentroid)
#convective flux
velocityVector = u*numpy.ones(nCell+1)
F = velocityVector*rho*area*cp
#peclet no.
Pe = F/DA
#source term Sp
Sp = numpy.zeros(nCell)
Sp[0] = -(2*numpy.copy(DA[0]) + numpy.maximum(numpy.copy(F[0]),0))
Sp[-1] = -(2*numpy.copy(DA[-1]) + numpy.maximum(-numpy.copy(F[-1]),0))
#souce term Su
Su = heatSourcePerVol*cellVolume
Su[0] =Su[0] + tempLeft*(2*numpy.copy(DA[0]) + numpy.maximum(numpy.copy(F[0]),0))
Su[-1] =Su[-1] + tempRight*(2*numpy.copy(DA[-1]) + numpy.maximum(-numpy.copy(F[-1]),0))
#left and right coefficient
aL = numpy.copy(DA[0:-1]) + numpy.maximum(numpy.copy(F[0:-1]),numpy.zeros(nCell))
aR = numpy.copy(DA[0:-1]) + numpy.maximum(-numpy.copy(F[0:-1]),numpy.zeros(nCell))
aL[0] = 0
aR[-1] = 0
#central coeff Ap
aP = numpy.copy(aL) + numpy.copy(aR) - numpy.copy(Sp)
print ('------------------------------------------------')
print (' Assembling Matrices')
print ('------------------------------------------------')
Amatrix = numpy.zeros([nCell, nCell])
Bvector = numpy.zeros(nCell)
for i in range(nCell):
if i == 0:
Amatrix[i,i] = aP[i]
Amatrix[i,i+1] = -1*aR[i]
elif i == nCell - 1:
Amatrix[i,i] = aP[i]
Amatrix[i,i-1] = -1*aL[i]
else:
Amatrix[i,i-1] = -1*aL[i]
Amatrix[i,i] = aP[i]
Amatrix[i,i+1] = -1*aR[i]
Bvector[i] = Su[i]
print('aL:',aL)
print('aR:',aR)
print('aP:',aP)
print('Sp:',Sp)
print('Su:',Su)
print('\nCell Peclet Number:',Pe)
print('\nAmatrix:')
print(Amatrix)
print('\nBvector:',Bvector)
print ('------------------------------------------------')
print (' Solving ...')
print ('------------------------------------------------')
Tvector = numpy.linalg.solve(Amatrix, Bvector)
print ('------------------------------------------------')
print (' Equations Solved')
print ('------------------------------------------------')
print('---------------------------------------------')
print('Solution: Temperature Field')
Tvector = numpy.around(Tvector, decimals = 2)
print(Tvector)
print("\n")
print ('------------------------------------------------')
print (' Plotting ...')
print ('------------------------------------------------')
xPlotting = numpy.hstack([xFace[0], xCentroid, xFace[-1]])
temperaturePlotting = numpy.hstack([tempLeft, Tvector, tempRight])
tickPad = 8
tickPad2 = 16
labelPadY = 10
labelPadX = 8
boxPad = 5
darkBlue = (0.0,0.129,0.2784)
darkRed = (0.7176, 0.0705, 0.207)
fig1 = plt.figure()
ax = fig1.add_subplot()
fig1.tight_layout(pad=boxPad)
ax.plot(xPlotting , temperaturePlotting, 'b-o',linewidth = 2, label='CFD', color=darkBlue)
plt.xlabel(r'$x$ [m]', fontsize=14, labelpad = labelPadX)
plt.ylabel(r'$T$ [$^{\circ}$C]', fontsize=14, labelpad = labelPadY)
plt.yticks(fontsize = 14)
plt.xticks(fontsize = 14)
plt.xlim([xFace[0], xFace[-1]])
leg = plt.legend(fontsize = 14, loc='best', fancybox=False, edgecolor = 'k')
leg.get_frame().set_linewidth(2)
ax.tick_params(which = 'both', direction='in', length=6,width=2, gridOn = False)
ax.yaxis.set_ticks_position('both')
ax.xaxis.set_ticks_position('both')
ax.tick_params(pad=tickPad)
plt.show()