fieldplot.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2015 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. # Copyright (C) 2013-2015 Konstantin Ladutenko <kostyfisik@gmail.com>
  6. #
  7. # This file is part of python-scattnlay
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # The only additional remark is that we expect that all publications
  20. # describing work using this software, or all commercial products
  21. # using it, cite the following reference:
  22. # [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by
  23. # a multilayered sphere," Computer Physics Communications,
  24. # vol. 180, Nov. 2009, pp. 2348-2354.
  25. #
  26. # You should have received a copy of the GNU General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # Several functions to plot field and streamlines (power flow lines).
  29. import scattnlay
  30. from scattnlay import fieldnlay
  31. from scattnlay import scattnlay
  32. import numpy as np
  33. import cmath
  34. def unit_vector(vector):
  35. """ Returns the unit vector of the vector. """
  36. return vector / np.linalg.norm(vector)
  37. def angle_between(v1, v2):
  38. """ Returns the angle in radians between vectors 'v1' and 'v2'::
  39. >>> angle_between((1, 0, 0), (0, 1, 0))
  40. 1.5707963267948966
  41. >>> angle_between((1, 0, 0), (1, 0, 0))
  42. 0.0
  43. >>> angle_between((1, 0, 0), (-1, 0, 0))
  44. 3.141592653589793
  45. """
  46. v1_u = unit_vector(v1)
  47. v2_u = unit_vector(v2)
  48. angle = np.arccos(np.dot(v1_u, v2_u))
  49. if np.isnan(angle):
  50. if (v1_u == v2_u).all():
  51. return 0.0
  52. else:
  53. return np.pi
  54. return angle
  55. ###############################################################################
  56. def GetFlow3D(x0, y0, z0, max_length, max_angle, x, m, pl):
  57. # Initial position
  58. flow_x = [x0]
  59. flow_y = [y0]
  60. flow_z = [z0]
  61. max_step = x[-1] / 3
  62. min_step = x[0] / 2000
  63. # max_step = min_step
  64. step = min_step * 2.0
  65. if max_step < min_step:
  66. max_step = min_step
  67. coord = np.vstack(([flow_x[-1]], [flow_y[-1]], [flow_z[-1]])).transpose()
  68. terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl)
  69. Ec, Hc = E[0, 0, :], H[0, 0, :]
  70. S = np.cross(Ec, Hc.conjugate()).real
  71. Snorm_prev = S / np.linalg.norm(S)
  72. Sprev = S
  73. length = 0
  74. dpos = step
  75. count = 0
  76. while length < max_length:
  77. count = count + 1
  78. if (count > 4000): # Limit length of the absorbed power streamlines
  79. break
  80. if step < max_step:
  81. step = step * 2.0
  82. r = np.sqrt(flow_x[-1]**2 + flow_y[-1]**2 + flow_z[-1]**2)
  83. while step > min_step:
  84. # Evaluate displacement from previous poynting vector
  85. dpos = step
  86. dx = dpos * Snorm_prev[0]
  87. dy = dpos * Snorm_prev[1]
  88. dz = dpos * Snorm_prev[2]
  89. # Test the next position not to turn\chang size for more than
  90. # max_angle
  91. coord = np.vstack(([flow_x[-1] + dx], [flow_y[-1] + dy],
  92. [flow_z[-1] + dz])).transpose()
  93. terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl)
  94. Ec, Hc = E[0, 0, :], H[0, 0, :]
  95. Eth = max(np.absolute(Ec)) / 1e10
  96. Hth = max(np.absolute(Hc)) / 1e10
  97. for i in xrange(0, len(Ec)):
  98. if abs(Ec[i]) < Eth:
  99. Ec[i] = 0 + 0j
  100. if abs(Hc[i]) < Hth:
  101. Hc[i] = 0 + 0j
  102. S = np.cross(Ec, Hc.conjugate()).real
  103. if not np.isfinite(S).all():
  104. break
  105. Snorm = S / np.linalg.norm(S)
  106. diff = (S - Sprev) / max(np.linalg.norm(S), np.linalg.norm(Sprev))
  107. if np.linalg.norm(diff) < max_angle:
  108. # angle = angle_between(Snorm, Snorm_prev)
  109. # if abs(angle) < max_angle:
  110. break
  111. step = step / 2.0
  112. # 3. Save result
  113. Sprev = S
  114. Snorm_prev = Snorm
  115. dx = dpos * Snorm_prev[0]
  116. dy = dpos * Snorm_prev[1]
  117. dz = dpos * Snorm_prev[2]
  118. length = length + step
  119. flow_x.append(flow_x[-1] + dx)
  120. flow_y.append(flow_y[-1] + dy)
  121. flow_z.append(flow_z[-1] + dz)
  122. return np.array(flow_x), np.array(flow_y), np.array(flow_z)
  123. ###############################################################################
  124. def GetField(crossplane, npts, factor, x, m, pl):
  125. """
  126. crossplane: XZ, YZ, XY
  127. npts: number of point in each direction
  128. factor: ratio of plotting size to outer size of the particle
  129. x: size parameters for particle layers
  130. m: relative index values for particle layers
  131. """
  132. scan = np.linspace(-factor * x[-1], factor * x[-1], npts)
  133. zero = np.zeros(npts * npts, dtype=np.float64)
  134. if crossplane == 'XZ':
  135. coordX, coordZ = np.meshgrid(scan, scan)
  136. coordX.resize(npts * npts)
  137. coordZ.resize(npts * npts)
  138. coordY = zero
  139. coordPlot1 = coordX
  140. coordPlot2 = coordZ
  141. elif crossplane == 'YZ':
  142. coordY, coordZ = np.meshgrid(scan, scan)
  143. coordY.resize(npts * npts)
  144. coordZ.resize(npts * npts)
  145. coordX = zero
  146. coordPlot1 = coordY
  147. coordPlot2 = coordZ
  148. elif crossplane == 'XY':
  149. coordX, coordY = np.meshgrid(scan, scan)
  150. coordX.resize(npts * npts)
  151. coordY.resize(npts * npts)
  152. coordZ = zero
  153. coordPlot1 = coordY
  154. coordPlot2 = coordX
  155. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  156. terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl)
  157. Ec = E[0, :, :]
  158. Hc = H[0, :, :]
  159. P = []
  160. P = np.array(map(lambda n: np.linalg.norm(np.cross(Ec[n], Hc[n])).real,
  161. range(0, len(E[0]))))
  162. # for n in range(0, len(E[0])):
  163. # P.append(np.linalg.norm( np.cross(Ec[n], np.conjugate(Hc[n]) ).real/2 ))
  164. return Ec, Hc, P, coordPlot1, coordPlot2
  165. ###############################################################################
  166. def fieldplot(fig, ax, x, m, WL, comment='', WL_units=' ', crossplane='XZ',
  167. field_to_plot='Pabs', npts=101, factor=2.1, flow_total=11,
  168. is_flow_extend=True, pl=-1, outline_width=1, subplot_label=' '):
  169. Ec, Hc, P, coordX, coordZ = GetField(crossplane, npts, factor, x, m, pl)
  170. Er = np.absolute(Ec)
  171. Hr = np.absolute(Hc)
  172. try:
  173. from matplotlib import cm
  174. from matplotlib.colors import LogNorm
  175. if field_to_plot == 'Pabs':
  176. Eabs_data = np.resize(P, (npts, npts)).T
  177. label = r'$\operatorname{Re}(E \times H)$'
  178. elif field_to_plot == 'Eabs':
  179. Eabs = np.sqrt(Er[:, 0]**2 + Er[:, 1]**2 + Er[:, 2]**2)
  180. Eabs_data = np.resize(Eabs, (npts, npts)).T
  181. label = r'$|E|$'
  182. elif field_to_plot == 'Habs':
  183. Habs = np.sqrt(Hr[:, 0]**2 + Hr[:, 1]**2 + Hr[:, 2]**2)
  184. Eabs_data = np.resize(Habs, (npts, npts)).T
  185. label = r'$|H|$'
  186. elif field_to_plot == 'angleEx':
  187. Eangle = np.angle(Ec[:, 0]) / np.pi * 180
  188. Eabs_data = np.resize(Eangle, (npts, npts)).T
  189. label = r'$arg(E_x)$'
  190. elif field_to_plot == 'angleHy':
  191. Hangle = np.angle(Hc[:, 1]) / np.pi * 180
  192. Eabs_data = np.resize(Hangle, (npts, npts)).T
  193. label = r'$arg(H_y)$'
  194. # Rescale to better show the axes
  195. scale_x = np.linspace(
  196. min(coordX) * WL / 2.0 / np.pi, max(coordX) * WL / 2.0 / np.pi, npts)
  197. scale_z = np.linspace(
  198. min(coordZ) * WL / 2.0 / np.pi, max(coordZ) * WL / 2.0 / np.pi, npts)
  199. # Define scale ticks
  200. min_tick = np.amin(Eabs_data[~np.isnan(Eabs_data)])
  201. #min_tick = 0.1
  202. max_tick = np.amax(Eabs_data[~np.isnan(Eabs_data)])
  203. #max_tick = 60
  204. scale_ticks = np.linspace(min_tick, max_tick, 5)
  205. #scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  206. #scale_ticks = [0.1,0.3,1,3,10, max_tick]
  207. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  208. ax.set_title(label)
  209. # build a rectangle in axes coords
  210. ax.annotate(subplot_label, xy=(0.0, 1.1), xycoords='axes fraction', # fontsize=10,
  211. horizontalalignment='left', verticalalignment='top')
  212. # ax.text(right, top, subplot_label,
  213. # horizontalalignment='right',
  214. # verticalalignment='bottom',
  215. # transform=ax.transAxes)
  216. cax = ax.imshow(Eabs_data, interpolation='nearest', cmap=cm.jet,
  217. origin='lower', vmin=min_tick, vmax=max_tick, extent=(min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  218. # ,norm = LogNorm()
  219. )
  220. ax.axis("image")
  221. # Add colorbar
  222. cbar = fig.colorbar(cax, ticks=[a for a in scale_ticks], ax=ax)
  223. # vertically oriented colorbar
  224. cbar.ax.set_yticklabels(['%3.2f' % (a) for a in scale_ticks])
  225. # pos = list(cbar.ax.get_position().bounds)
  226. #fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  227. lp2 = -10.0
  228. lp1 = -1.0
  229. if crossplane == 'XZ':
  230. ax.set_xlabel('Z, ' + WL_units, labelpad=lp1)
  231. ax.set_ylabel('X, ' + WL_units, labelpad=lp2)
  232. elif crossplane == 'YZ':
  233. ax.set_xlabel('Z, ' + WL_units, labelpad=lp1)
  234. ax.set_ylabel('Y, ' + WL_units, labelpad=lp2)
  235. elif crossplane == 'XY':
  236. ax.set_xlabel('Y, ' + WL_units, labelpad=lp1)
  237. ax.set_ylabel('X, ' + WL_units, labelpad=lp2)
  238. # # This part draws the nanoshell
  239. from matplotlib import patches
  240. from matplotlib.path import Path
  241. for xx in x:
  242. r = xx * WL / 2.0 / np.pi
  243. s1 = patches.Arc((0, 0), 2.0 * r, 2.0 * r, angle=0.0, zorder=1.8,
  244. theta1=0.0, theta2=360.0, linewidth=outline_width, color='black')
  245. ax.add_patch(s1)
  246. #
  247. # for flow in range(0,flow_total):
  248. # flow_x, flow_z = GetFlow(scale_x, scale_z, Ec, Hc,
  249. # min(scale_x)+flow*(scale_x[-1]-scale_x[0])/(flow_total-1),
  250. # min(scale_z),
  251. # #0.0,
  252. # npts*16)
  253. # verts = np.vstack((flow_z, flow_x)).transpose().tolist()
  254. # #codes = [Path.CURVE4]*len(verts)
  255. # codes = [Path.LINETO]*len(verts)
  256. # codes[0] = Path.MOVETO
  257. # path = Path(verts, codes)
  258. # patch = patches.PathPatch(path, facecolor='none', lw=1, edgecolor='yellow')
  259. # ax.add_patch(patch)
  260. if (crossplane == 'XZ' or crossplane == 'YZ') and flow_total > 0:
  261. from matplotlib.path import Path
  262. scanSP = np.linspace(-factor * x[-1], factor * x[-1], npts)
  263. min_SP = -factor * x[-1]
  264. step_SP = 2.0 * factor * x[-1] / (flow_total - 1)
  265. x0, y0, z0 = 0, 0, 0
  266. max_length = factor * x[-1] * 10
  267. # max_length=factor*x[-1]*5
  268. max_angle = np.pi / 160
  269. if is_flow_extend:
  270. rg = range(0, flow_total * 5 + 1)
  271. else:
  272. rg = range(0, flow_total)
  273. for flow in rg:
  274. if crossplane == 'XZ':
  275. if is_flow_extend:
  276. x0 = min_SP * 2 + flow * step_SP
  277. else:
  278. x0 = min_SP + flow * step_SP
  279. z0 = min_SP
  280. # y0 = x[-1]/20
  281. elif crossplane == 'YZ':
  282. if is_flow_extend:
  283. y0 = min_SP * 2 + flow * step_SP
  284. else:
  285. y0 = min_SP + flow * step_SP
  286. z0 = min_SP
  287. # x0 = x[-1]/20
  288. flow_xSP, flow_ySP, flow_zSP = GetFlow3D(
  289. x0, y0, z0, max_length, max_angle, x, m, pl)
  290. if crossplane == 'XZ':
  291. flow_z_plot = flow_zSP * WL / 2.0 / np.pi
  292. flow_f_plot = flow_xSP * WL / 2.0 / np.pi
  293. elif crossplane == 'YZ':
  294. flow_z_plot = flow_zSP * WL / 2.0 / np.pi
  295. flow_f_plot = flow_ySP * WL / 2.0 / np.pi
  296. verts = np.vstack(
  297. (flow_z_plot, flow_f_plot)).transpose().tolist()
  298. codes = [Path.LINETO] * len(verts)
  299. codes[0] = Path.MOVETO
  300. path = Path(verts, codes)
  301. #patch = patches.PathPatch(path, facecolor='none', lw=0.2, edgecolor='white',zorder = 2.7)
  302. patch = patches.PathPatch(
  303. path, facecolor='none', lw=outline_width, edgecolor='white', zorder=1.9, alpha=0.7)
  304. # patch = patches.PathPatch(
  305. # path, facecolor='none', lw=0.7, edgecolor='white', zorder=1.9, alpha=0.7)
  306. ax.add_patch(patch)
  307. # ax.plot(flow_z_plot, flow_f_plot, 'x', ms=2, mew=0.1,
  308. # linewidth=0.5, color='k', fillstyle='none')
  309. finally:
  310. terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(
  311. np.array([x]), np.array([m]))
  312. print("Qabs = " + str(Qabs))
  313. #