Advertisement
shadvoll

DDA_numercial_method_1d

Oct 11th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. import numpy as np
  2. import math as m
  3. import cmath as cm
  4. import scipy.integrate as spint
  5. import scipy
  6. import matplotlib.pyplot as plt
  7. import time
  8.  
  9. # dda problem for dielectric.
  10. # finding polarization for a dieletric with incedent eliectric field
  11. # considered cube NxNxN
  12. N = 10 # number of segments !note, N<20!
  13.  
  14. l = 0.01 # length [m]
  15. eps = 9 # epslion
  16. # freq = 3e8/l / m.sqrt(eps)
  17. # freq = 3e8/l
  18. freq = 12.8e9
  19. k = freq*2*m.pi/(3e8) # k-vector
  20. a = l/N # length of each segment
  21. betta = ( (3*a**3)*(eps - 1) / (4*m.pi *(eps + 2)) )**(-1) + 1j*k**3/(6*m.pi) #alpha^-1
  22.  
  23. # grin function for xx component
  24. def grin(a,b,size):
  25.     i1 = (a % N)
  26.     j1 = (a//N) % N
  27.     k1 = a // N**2
  28.     i2 = (b % N)
  29.     j2 = (b//N) % N
  30.     k2 = b // N**2
  31.     # print(i1,j1,k1)
  32.     # print(i2,j2,k2)
  33.     # print(k)
  34.     R = size*m.sqrt( (i1-i2)**2 + (j1-j2)**2 + (k1-k2)**2)
  35.     mul1 = ( (j2-j1)**2+(k2-k1)**2 + ( 2*(i2-i1)**2-(j2-j1)**2-(k2-k1)**2 )*( -1j/(k*R)+ (k*R)**(-2) ) )*size**2
  36.     return k**2*cm.exp(1j*k*R)/(4*m.pi*R**3) * mul1
  37.  
  38.  
  39.  
  40. start_time = time.time()
  41.  
  42. A = np.zeros((N**3,N**3),dtype = complex)
  43. for i in range(N**3):
  44.     for j in range(N**3):
  45.         if (i == j ):
  46.             A[i][j] = betta
  47.         else:
  48.             A[i][j] = - grin(i,j,a)
  49.  
  50.  
  51. E = np.ones((N,N,N),dtype=complex)
  52. for i in range(N):
  53.     E[:,:,i]= cm.exp(1j*k*a*i)
  54. E_new = np.zeros(N**3,dtype  = complex)
  55. for i in range(N**3):
  56.     E_new[i] = E[i % N][i//N % N][i // N**2]
  57.  
  58. answer = np.linalg.solve(A,E_new)
  59. p = np.zeros((N,N,N),dtype = complex )
  60. for i in range(N**3):
  61.     p[i % N][i//N % N][i // N**2] = answer[i]
  62.  
  63. p = p/a
  64. # plt.plot(scipy.real((E[0,0,:])))
  65. # plt.show()
  66. end_time = time.time()
  67. print(end_time-start_time,"sec")
  68. plt.subplot(311)
  69. # plt.imshow(scipy.real(p[:,:,m.ceil(N/2)-1]))
  70. # plt.imshow(abs(p[:,:,m.ceil(N/2)-1]),aspect = 'auto')
  71. plt.imshow(abs(p[:,:,5]),aspect = 'auto')
  72. plt.title('Z const, Plane (XOY)')
  73. plt.colorbar()
  74. plt.subplot(312)
  75. plt.imshow(abs(p[:,m.ceil(N/2),:]),aspect = 'auto')
  76. plt.colorbar()
  77. plt.title('Y const, Plane (XOZ)')
  78. plt.subplot(313)
  79. plt.imshow(abs(p[m.ceil(N/2),:,:]),aspect = 'auto')
  80. plt.colorbar()
  81. plt.title('X const, Plane (YOZ)')
  82. plt.show()
  83.  
  84. plt.subplot(311)
  85. plt.imshow(scipy.real(p[:,:,5]),aspect = 'auto')
  86. plt.title('Z const, Plane (XOY)')
  87. plt.colorbar()
  88. plt.subplot(312)
  89. plt.imshow(scipy.real(p[:,m.ceil(N/2),:]),aspect = 'auto')
  90. plt.colorbar()
  91. plt.title('Y const, Plane (XOZ)')
  92. plt.subplot(313)
  93. plt.imshow(scipy.real(p[m.ceil(N/2),:,:]),aspect = 'auto')
  94. plt.colorbar()
  95. plt.title('X const, Plane (YOZ)')
  96. plt.show()
  97.  
  98. plt.subplot(311)
  99. plt.imshow(scipy.imag(p[:,:,5]),aspect = 'auto')
  100. plt.title('Z const, Plane (XOY)')
  101. plt.colorbar()
  102. plt.subplot(312)
  103. plt.imshow(scipy.imag(p[:,m.ceil(N/2),:]),aspect = 'auto')
  104. plt.colorbar()
  105. plt.title('Y const, Plane (XOZ)')
  106. plt.subplot(313)
  107. plt.imshow(scipy.imag(p[m.ceil(N/2),:,:]),aspect = 'auto')
  108. plt.colorbar()
  109. plt.title('X const, Plane (YOZ)')
  110. plt.show()
  111. # print(m.ceil(N/2))
  112. print(freq/1e9,"GHz")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement