-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFVM_UpWind.py
72 lines (59 loc) · 1.28 KB
/
FVM_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
#Solve FVM UpWind convection-diffusion 1D equation
import numpy as np
import matplotlib.pyplot as plt
n=6
a=np.zeros((n,n))
F=0.1
D=0.5
phiA=1
phiB=0
for i in range(n):
for j in range(n):
if i==j:
a[i,j]=F+2*D
if i==j-1:
a[i,j]=-D
if i==j+1:
a[i,j]=-F-D
a[0,0]=F+(3*D)
a[-1,-1]=F+(3*D)
b=np.zeros((n,1)).reshape(n,1)
b[0]=phiA*(F+ 2*D)
b[-1]=2*D*phiB
print(b)
print(a)
#Gauss Sidel iteration method
#LU decomposition
U=a.copy()
L=a.copy()
for i in range (0,n):
for j in range (0,n):
if i>=j:
U[i,j]=0
for i in range (0,n):
for j in range (0,n):
if j>i:
L[i,j]=0
Linv=np.linalg.inv(L)
T=np.dot(-Linv,U)
C=np.dot(Linv,b)
####Solution####
x_init=np.zeros(n).reshape(n,1)
epsilon=0.000000001
conv_criteria=np.array([epsilon]*n).reshape(n,1)
###Iteration process
iteration_number=7
x=[None]*n
for i in range(1,iteration_number):
x[0]=np.dot(T,x_init) + C
x[i]=np.dot(T,x[i-1]) + C
x.append(x[i])
check_conv=np.less(x[i]-x[i-1],conv_criteria)
if check_conv.any()==True:
print('CONVERGED!')
break
print(x[-1])
#postProcessing
#plt.plot(x[-n+1])
plt.plot(x[-1])
#plt.show()