[关闭]
@rfhongyi 2016-12-11T13:53:56.000000Z 字数 9676 阅读 1178

Exercise_12 : Electric Potentials and Fields: Laplace's Equations

222.png

Problem 5.7

冉峰 弘毅班 2014301020064


Abstract


Background

Introduction


Jacobi Method


Gauss-Sideal Method


Simulatneous over-relaxation(SOR)


The Main Body


Code for Jacobi Method

  1. @author: RF
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from mpl_toolkits.mplot3d import Axes3D
  5. from matplotlib import cm
  6. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  7. class jacobi:
  8. def __init__(self):
  9. pass
  10. def initialization(self,initial_file):
  11. itxt= open(initial_file)
  12. self.lattice_in = []
  13. self.lattice_out = []
  14. for lines in itxt.readlines():
  15. lines=lines.replace("\n","").split(",")
  16. #print (lines[0].split(" ")
  17. self.lattice_in.append(lines[0].split(" "))
  18. self.lattice_out.append(lines[0].split(" "))
  19. itxt.close()
  20. #print (self.lattice_in)
  21. #print (self.initial_lattice)
  22. for i in range(len(self.lattice_in)):
  23. for j in range(len(self.lattice_in[i])):
  24. self.lattice_in[i][j] = float(self.lattice_in[i][j])
  25. self.lattice_out[i][j] = float(self.lattice_out[i][j])
  26. return 0
  27. def evolution(self):
  28. delta = 10
  29. self.N = 0
  30. delta_record = []
  31. while (delta > 0.00001*len(self.lattice_in)*len(self.lattice_in[0])):
  32. delta = 0
  33. for i in range(1,len(self.lattice_in) - 1):
  34. for j in range(1,len(self.lattice_in[i]) - 1):
  35. if (self.lattice_in[i][j] != 1 and self.lattice_in[i][j] != -1):
  36. self.lattice_out[i][j] = 0.25*(self.lattice_in[i - 1][j] + self.lattice_in[i + 1][j] + self.lattice_in[i][j - 1] + self.lattice_in[i][j + 1])
  37. delta+= abs(self.lattice_out[i][j] - self.lattice_in[i][j])
  38. delta_record.append(delta)
  39. for i in range(len(self.lattice_in)):
  40. for j in range(len(self.lattice_in[i])):
  41. self.lattice_in[i][j] = self.lattice_out[i][j]
  42. self.N+= 1
  43. plt.plot(delta_record,label = 'jacobi method')
  44. print (self.N)
  45. print (len(self.lattice_in))
  46. return 0
  47. def electric_field(self):
  48. self.ex = np.zeros([len(self.lattice_in),len(self.lattice_in)])
  49. self.ey = np.zeros([len(self.lattice_in),len(self.lattice_in)])
  50. deltax = 0.3333
  51. deltay = 0.3333
  52. for i in range(1,len(self.lattice_in) - 1):
  53. for j in range(1,len(self.lattice_in[0]) - 1):
  54. self.ey[i][j] = (-(self.lattice_in[i + 1][j] - self.lattice_in[i - 1][j])/(2*deltax))
  55. self.ex[i][j] = (-(self.lattice_in[i][j + 1] - self.lattice_in[i][j - 1])/(2*deltay))
  56. return 0
  57. def plot_field(self):
  58. self.electric_field()
  59. X, Y = np.meshgrid(np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)), np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)))
  60. #plt.figure(figsize = (8,8))
  61. plt.quiver(X, Y, self.ex, self.ey, pivot = 'mid', units='width')
  62. plt.title('Electric field between two metal plates')
  63. #plt.show()
  64. return 0
  65. def plot_contour(self):
  66. X, Y = np.meshgrid(np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)), np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)))
  67. #plt.figure(figsize = (8,8))
  68. CS = plt.contour(X, Y, self.lattice_in, 13)
  69. plt.clabel(CS, inline=1, fontsize=10)
  70. plt.title('Equipotential lines')
  71. #plt.show()
  72. return 0
  73. def plot_surface(self):
  74. fig = plt.figure()
  75. ax = fig.gca(projection='3d')
  76. X, Y = np.meshgrid(np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)), np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)))
  77. surf = ax.plot_surface(X, Y, self.lattice_in, rstride=1, cstride=1,cmap = cm.coolwarm_r,
  78. linewidth=0, antialiased=False)
  79. ax.set_zlim(-1.01, 1.01)
  80. ax.zaxis.set_major_locator(LinearLocator(10))
  81. ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
  82. fig.colorbar(surf, shrink=0.5, aspect=5)
  83. #A = jacobi()
  84. A = SOR()
  85. A.initialization('initial_capacitor_26.txt')
  86. A.evolution()
  87. #plt.figure(figsize = (8,8))
  88. plt.figure(figsize = (16,8))
  89. plt.subplot(121)
  90. A.plot_contour()
  91. plt.subplot(122)
  92. A.plot_field()
  93. #A.plot_surface()
  94. plt.savefig('chapter5_5.7_26.png', dpi = 256)
  95. plt.show()

Code for SOR Method

  1. @author: RF
  2. class SOR(jacobi):
  3. def evolution(self):
  4. delta = 10
  5. self.N = 0
  6. delta_record = []
  7. alpha = 2/(1+np.pi/len(self.lattice_in))
  8. while (delta > 0.00001*len(self.lattice_in)*len(self.lattice_in[0])):
  9. delta = 0
  10. for i in range(1,len(self.lattice_in) - 1):
  11. for j in range(1,len(self.lattice_in[i]) - 1):
  12. if (self.lattice_in[i][j] != 1 and self.lattice_in[i][j] != -1):
  13. self.lattice_out[i][j] = 0.25*(self.lattice_in[i - 1][j] + self.lattice_in[i + 1][j] + self.lattice_in[i][j - 1] + self.lattice_in[i][j + 1])
  14. delta+= abs(self.lattice_out[i][j] - self.lattice_in[i][j])
  15. self.lattice_in[i][j] = alpha * (self.lattice_out[i][j] - self.lattice_in[i][j]) + self.lattice_in[i][j]
  16. #print (self.lattice_out)
  17. delta_record.append(delta)
  18. self.N+= 1
  19. plt.plot(delta_record, label ='SOR algorithm')
  20. print (self.N)
  21. print (len(self.lattice_in))
  22. #print (self.lattice_in)
  23. return 0
  24. class SOR_2(jacobi):
  25. def evolution(self,alpha):
  26. delta = 10
  27. self.N = 0
  28. delta_record = []
  29. #alpha = 2/(1+np.pi/len(self.lattice_in))
  30. while (delta > 0.00001*len(self.lattice_in)*len(self.lattice_in[0])):
  31. delta = 0
  32. for i in range(1,len(self.lattice_in) - 1):
  33. for j in range(1,len(self.lattice_in[i]) - 1):
  34. if (self.lattice_in[i][j] != 1 and self.lattice_in[i][j] != -1):
  35. self.lattice_out[i][j] = 0.25*(self.lattice_in[i - 1][j] + self.lattice_in[i + 1][j] + self.lattice_in[i][j - 1] + self.lattice_in[i][j + 1])
  36. delta+= abs(self.lattice_out[i][j] - self.lattice_in[i][j])
  37. self.lattice_in[i][j] = alpha * (self.lattice_out[i][j] - self.lattice_in[i][j]) + self.lattice_in[i][j]
  38. #print (self.lattice_out)
  39. delta_record.append(delta)
  40. self.N+= 1
  41. #plt.plot(delta_record, label ='SOR algorithm')
  42. print (self.N)
  43. #print (len(self.lattice_in))
  44. #print (self.lattice_in)
  45. return 0

Result

Setting initial value and boundary conditions


Solving the partial differential equation of two capacitor plates


The relationship between the number of iterations and that of grid elements


The Convergence speed


The of SOR algorithm


Conclusion


Thanks For

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注