test04_field.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2017 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. #
  6. # This file is part of 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 at least one of the following references:
  21. # [1] O. Peña and U. Pal, "Scattering of electromagnetic radiation by
  22. # a multilayered sphere," Computer Physics Communications,
  23. # vol. 180, Nov. 2009, pp. 2348-2354.
  24. # [2] K. Ladutenko, U. Pal, A. Rivera, and O. Peña-Rodríguez, "Mie
  25. # calculation of electromagnetic near-field for a multilayered
  26. # sphere," Computer Physics Communications, vol. 214, May 2017,
  27. # pp. 225-230.
  28. #
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. # This test case calculates the the electric field in the
  32. # XY plane, for a Luneburg lens, as described in:
  33. # B. R. Johnson, Applied Optics 35 (1996) 3286-3296.
  34. # The Luneburg lens is a sphere of radius a, with a
  35. # radially-varying index of refraction, given by:
  36. # m(r) = [2 - (r/a)**2]**(1/2)
  37. # For the calculations, the Luneburg lens was approximated
  38. # as a multilayered sphere with 500 equally spaced layers.
  39. # The refractive index of each layer is defined to be equal to
  40. # m(r) at the midpoint of the layer: ml = [2 - (xm/xL)**2]**(1/2),
  41. # with xm = (xl-1 + xl)/2, for l = 1,2,...,L. The size
  42. # parameter in the lth layer is xl = l*xL/500.
  43. from scattnlay import fieldnlay
  44. import numpy as np
  45. nL = 10
  46. Xmax = 60.0
  47. x = np.array([np.linspace(Xmax/nL, Xmax, nL)], dtype = np.float64)
  48. m = np.array((np.sqrt((2.0 - (x[0]/Xmax - 0.5/nL)**2.0))), dtype = np.complex128)
  49. print "x =", x
  50. print "m =", m
  51. npts = 100
  52. scan = np.linspace(-3*Xmax, 3*Xmax, npts)
  53. coordX, coordY = np.meshgrid(scan, scan)
  54. coordX.resize(npts*npts)
  55. coordY.resize(npts*npts)
  56. coordZ = np.zeros(npts*npts, dtype = np.float64)
  57. #idx = np.where(np.sqrt(coordX**2 + coordY**2) < 10.)
  58. #coordX = np.delete(coordX, idx)
  59. #coordY = np.delete(coordY, idx)
  60. #coordZ = np.delete(coordZ, idx)
  61. terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ)
  62. print "E", E
  63. Er = np.absolute(E)
  64. print "Er", Er
  65. # |E|/|Eo|
  66. Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  67. print "Eh", Eh
  68. result = np.vstack((coordX, coordY, coordZ, Eh)).transpose()
  69. try:
  70. import matplotlib.pyplot as plt
  71. from matplotlib import cm
  72. from matplotlib.colors import LogNorm
  73. min_tick = 0.1
  74. max_tick = 1.0
  75. edata = np.resize(Eh, (npts, npts))
  76. fig = plt.figure()
  77. ax = fig.add_subplot(111)
  78. # Rescale to better show the axes
  79. scale_x = np.linspace(min(coordX), max(coordX), npts)
  80. scale_y = np.linspace(min(coordY), max(coordY), npts)
  81. # Define scale ticks
  82. print np.amin(edata), np.amax(edata)
  83. min_tick = 0.#min(min_tick, np.amin(edata))
  84. max_tick = 10.0#max(max_tick, np.amax(edata))
  85. scale_ticks = np.linspace(min_tick, max_tick, 6)
  86. #scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  87. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  88. cax = ax.imshow(edata, interpolation = 'nearest', cmap = cm.jet,
  89. origin = 'lower', vmin = min_tick, vmax = max_tick,
  90. extent = (min(scale_x), max(scale_x), min(scale_y), max(scale_y)))#,
  91. #norm = LogNorm())
  92. # Add colorbar
  93. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  94. cbar.ax.set_yticklabels(['%3.1e' % (a) for a in scale_ticks]) # vertically oriented colorbar
  95. pos = list(cbar.ax.get_position().bounds)
  96. fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  97. plt.xlabel('X')
  98. plt.ylabel('Y')
  99. # This part draws the lens
  100. from matplotlib import patches
  101. s1 = patches.Arc((0, 0), 2.0*Xmax, 2.0*Xmax, angle=0.0, zorder=2,
  102. theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
  103. ax.add_patch(s1)
  104. # s2 = patches.Arc((0, 0), 2.0*x[0, 1], 2.0*x[0, 1], angle=0.0, zorder=2,
  105. # theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
  106. # ax.add_patch(s2)
  107. # End of drawing
  108. plt.draw()
  109. plt.show()
  110. plt.clf()
  111. plt.close()
  112. finally:
  113. np.savetxt("test04_field.txt", result, fmt = "%.5f")
  114. print result