force-quad.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2018 Konstantin Ladutenko <kostyfisik@gmail.com>
  5. #
  6. # This file is part of python-scattnlay
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # The only additional remark is that we expect that all publications
  19. # describing work using this software, or all commercial products
  20. # using it, cite the following reference:
  21. # [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by
  22. # a multilayered sphere," Computer Physics Communications,
  23. # vol. 180, Nov. 2009, pp. 2348-2354.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. # WIP: try to evaluate forces.
  28. from scattnlay import fieldnlay
  29. from scattnlay import scattnlay
  30. import cmath
  31. import matplotlib.pyplot as plt
  32. import numpy as np
  33. import scattnlay
  34. import quadpy
  35. index_Ag = 4.0
  36. nm = 1.0
  37. WL_units='nm'
  38. x = np.ones((1), dtype = np.float64)
  39. m = np.ones((1), dtype = np.complex128)
  40. core_r = 120
  41. WL = 550
  42. x[0] = 2.0*np.pi*core_r/WL#/4.0*3.0
  43. m[0] = index_Ag/nm
  44. R_st = x[0]*2
  45. #R_st = 0.31
  46. dx = R_st*4.0
  47. charge = 1.0
  48. comment='bulk-NP-WL'+str(WL)+WL_units
  49. #quad_ord = 3
  50. # quad_ord = 19
  51. quad_ord = 131
  52. #coord = quadpy.sphere.Lebedev(3).points
  53. # coord = np.vstack((coordX, coordY, coordZ)).transpose()
  54. def field(coord):
  55. E = []
  56. for rr in coord:
  57. shift = np.array([dx, 0.0, 0.0])
  58. unit = (rr-shift)/np.linalg.norm(rr-shift)
  59. norm = np.linalg.norm(rr-shift)
  60. amp = charge/(4*np.pi*(norm**2))
  61. Eloc = amp*unit
  62. shift = np.array([0.0, 0.0, 0.0])
  63. unit = (rr-shift)/np.linalg.norm(rr-shift)
  64. norm = np.linalg.norm(rr-shift)
  65. amp = charge/(4*np.pi*(norm**2))
  66. Eloc += amp*unit
  67. E.append(Eloc)
  68. E = np.array(E)
  69. return E
  70. def gauss(in_coord):
  71. coord = in_coord.T
  72. E_all = field(coord)
  73. unit_all = coord/ R
  74. P = np.array([
  75. np.dot(E,unit)
  76. for unit,E in zip(unit_all,E_all)
  77. ])
  78. return P.T
  79. def dipole(coord):
  80. H = np.array([0.0,0.0,0.0]*len(coord))
  81. E = field(coord)
  82. return E,H
  83. def force(in_coord):
  84. coord = in_coord.T
  85. terms, Eall, Hall = fieldnlay(np.array([x]), np.array([m]), coord)#, mode_n=-1, mode_type=0)
  86. E_all = Eall[0, :, :]
  87. H_all = Hall[0, :, :]
  88. E_all, H_all = dipole(coord)
  89. # print(coord)
  90. # print(E_all)
  91. # print(H_all)
  92. unit_all = coord/ R
  93. P = np.array([
  94. ( (1.0/(2.0)*(4.0*np.pi))
  95. *np.real(
  96. np.dot(unit,E)*np.conj(E) +
  97. np.dot(unit,H)*np.conj(H) +
  98. (-1.0/2.0)*(np.dot(E,np.conj(E))
  99. +np.dot(H,np.conj(H))
  100. )*unit
  101. )
  102. )
  103. for unit,E,H in zip(unit_all,E_all, H_all)
  104. ])
  105. return P.T
  106. def poynting(in_coord):
  107. coord = in_coord.T
  108. terms, Eall, Hall = fieldnlay(np.array([x]), np.array([m]), coord)
  109. E_all = Eall[0, :, :]
  110. H_all = Hall[0, :, :]
  111. unit_all = coord/ R
  112. P = np.array([
  113. ( ( 1.0/(2.0) )
  114. *np.real(
  115. np.cross(E,np.conj(H))
  116. )
  117. )
  118. for unit,E,H in zip(unit_all,E_all, H_all)
  119. ])
  120. return P.T
  121. # P = np.array(map(lambda n: np.linalg.norm(np.cross(Ec[n], Hc[n])).real,
  122. # range(0, len(E[0]))))
  123. R=R_st
  124. val = quadpy.sphere.integrate(
  125. force
  126. # poynting
  127. ,
  128. [0.0, 0.0, 0.0], R,
  129. quadpy.sphere.Lebedev(quad_ord)
  130. )
  131. print(val)
  132. print("Random increase of integraion sphere radius...")
  133. R=R_st*3.0
  134. val = quadpy.sphere.integrate(
  135. force
  136. # poynting
  137. ,
  138. [0.0, 0.0, 0.0], R,
  139. quadpy.sphere.Lebedev(quad_ord)
  140. )
  141. print(val)
  142. R=R_st
  143. print("\n\nCheck Gauss law")
  144. val = quadpy.sphere.integrate(gauss,
  145. [0.0, 0.0, 0.0], R,
  146. quadpy.sphere.Lebedev(quad_ord)
  147. )
  148. print("Charge: ",val)
  149. print("Random increase of integraion sphere radius...")
  150. R=R_st*3
  151. val = quadpy.sphere.integrate(gauss,
  152. [0.0, 0.0, 0.0], R,
  153. quadpy.sphere.Lebedev(quad_ord)
  154. )
  155. print("Charge: ",val)