用Gauss-Seidel和ghost点解Laplace方程的边界点

2024-09-28 22:02:02 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np
import matplotlib.pyplot as plt

#Error

error = 0

#Set Maximum Iteration

maxIter = 500

#Set Dimension and Delta

lenX = lenY = 1
delta = 0.1


#Initial guess of interior grid

Tguess = 0

#Set meshgrid

X,Y = np.meshgrid(np.arange(0.1,1.2,delta),np.arange(0.1,1.2,delta))

Tx = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2]

#Texact
Texact = np.empty((13,13))
Texact.fill(Tguess)
for jx in np.arange(0.1,1.3,0.1) :
    for jy in np.arange(0.1,1.3,0.1):
        Texact = ((np.cos((np.pi)*jx))*(np.sinh((np.pi)*jy)))/(np.sinh(np.pi))

#Set array size and set the interior value with Tguess

T = np.empty((13,13))
T.fill(Tguess)
Tnew = np.empty((13,13))
Tnew.fill(Tguess)

#Set boundary condition
T[0:1 , 11: ] = np.cos ((np.pi))
T[1:1, : ] = T [ 11 :11 , : ]    
T[1 : 1 , 1 : 1] = 0

#Iteration           
print ("Please wait for a moment")
nof = 0
Tnew = T
Tnew = T .copy()
while (error < 10**(-7)) :
    for i in (0,1,2,3,4,5,6,7,8,9,10,11,12,13):
        for j in (0,1,2,3,4,5,6,7,8,9,10,11,12,13):
            Tnew[i,j]= (0.25 * (T[i+1,j] + Tnew[i-1,j])+(0.25 *( T[i,j+1] + Tnew[i,j-1])))
            T[0 , j ] = T [1 ,j ]
            T[12 , j ] = T[10,j]
            error = np.abs((Texact) - (Tnew))
            nof = nof + 1

print(nof)                          
print("Iteration finished")

我正试图用我说的方法解拉普拉斯方程 但是我的代码没有收敛。 问题域为1-1,网格大小为11*11。 我们必须使用在上一次迭代中更新的每个点的数量,特别是之前更新的i-1和j-1,并使用这些数量来Tnew。 我希望迭代次数和错误次数必须小于10**-2,但我的代码中没有。 在域的顶部有dirichlet b.c,它是cos(pi),它的底部必须是0。 左右壁必须是Neumann b.c.(T对x的导数必须是0在这种情况下,我们必须使用高斯点i+1和i-1,它应该用一个我不知道如何编写的函数进行更新,Neumann b.c.是Ti+1-Ti-1=0。 我们的域名是1-1,11分差0.1步


Tags: infornppierrorfillemptydelta