[关闭]
@2014301020054 2016-12-12T01:32:25.000000Z 字数 9051 阅读 649

Exercise_12: Electric Potential and Fields

python physics 2014301020054 陈佩卓


Abstract


Background

Laplace's Equations

Numerically, we get that:

Specially, for the 2-dimensional case:

Jacobi Method

Gauss-Sideal Method

Worth mentioning,whilw the performance if the Gauss-Seidel akgorithm is better than that of the Jacobi method,the imporvement is only modest.It turns out that the number of iterations required for convergence with the Gauss-Siedel method is smaller,but only by a factor of 2.
But the benefits in Gauss-Seidel method is obvious,cause there is no need for us to save the old values seperately.In other words, the updating can be performed in place with using an algorithm of the form.

Simulatneous over-relaxation(SOR)

where is a factor that measures how much we "over-relax".Choosing tuekds the Gauss-Seidel method.It turns out that if , the method does bit converge.The best choice of is:


Main Body

Problem 5.7

Write two problems to slove the capacitor problem of Figures 5.6 and 5.7, one using the Jacobi method and one using the SOR algorithm. For a fixed accuracy (as set by the convergence test) compare the number of iterations, , that each algorithm requires as a function of the number of the grid elelments, . Show that for the Jacobi method ~, while with SOR ~.

Codes

For Jacobi method:

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

For SOR Method:

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

Solving the partial differential equation of two capacitor plates

Then, enlarging the grids to 26*26 leads to smoother equipotential curve and a finer structure of electric field.

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

The Convergence speed

The of SOR algorithm


Conclusion


Acknowledgement

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