field.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # This test case calculates the differential scattering
  3. # cross section from a Luneburg lens, as described in:
  4. # B. R. Johnson, Applied Optics 35 (1996) 3286-3296.
  5. # The Luneburg lens is a sphere of radius a, with a
  6. # radially-varying index of refraction, given by:
  7. # m(r) = [2 - (r/a)**1]**(1/2)
  8. # For the calculations, the Luneburg lens was approximated
  9. # as a multilayered sphere with 500 equally spaced layers.
  10. # The refractive index of each layer is defined to be equal to
  11. # m(r) at the midpoint of the layer: ml = [2 - (xm/xL)**1]**(1/2),
  12. # with xm = (xl-1 + xl)/2, for l = 1,2,...,L. The size
  13. # parameter in the lth layer is xl = l*xL/500. According to
  14. # geometrical optics theory, the differential cross section
  15. # can be expressed as:
  16. # d(Csca)/d(a**2*Omega) = cos(Theta)
  17. # The differential cross section from wave optics is:
  18. # d(Csca)/d(a**2*Omega) = S11(Theta)/x**2
  19. from scattnlay import fieldnlay
  20. import numpy as np
  21. x = np.ones((1, 1), dtype = np.float64)
  22. x[0, 0] = 1.
  23. m = np.ones((1, 1), dtype = np.complex128)
  24. m[0, 0] = (0.05 + 2.070j)/1.46
  25. npts = 1001
  26. scan = np.linspace(-3.0*x[0, 0], 3.0*x[0, 0], npts)
  27. coordX, coordY = np.meshgrid(scan, scan)
  28. coordX.resize(npts*npts)
  29. coordY.resize(npts*npts)
  30. coordZ = np.zeros(npts*npts, dtype = np.float64)
  31. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  32. terms, E, H = fieldnlay(x, m, coord)
  33. Er = np.absolute(E)
  34. # |E|/|Eo|
  35. Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  36. result = np.vstack((coordX, coordY, coordZ, Eh)).transpose()
  37. try:
  38. import matplotlib.pyplot as plt
  39. from matplotlib import cm
  40. from matplotlib.colors import LogNorm
  41. min_tick = 0.1
  42. max_tick = 1.0
  43. edata = np.resize(Eh, (npts, npts))
  44. fig = plt.figure()
  45. ax = fig.add_subplot(111)
  46. # Rescale to better show the axes
  47. scale_x = np.linspace(min(coordX), max(coordX), npts)
  48. scale_y = np.linspace(min(coordY), max(coordY), npts)
  49. # Define scale ticks
  50. min_tick = max(0.5, min(min_tick, np.amin(edata)))
  51. max_tick = max(max_tick, np.amax(edata))
  52. scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  53. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  54. cax = ax.imshow(edata, interpolation = 'bicubic', cmap = cm.afmhot,
  55. origin = 'lower', vmin = min_tick, vmax = max_tick,
  56. extent = (min(scale_x), max(scale_x), min(scale_y), max(scale_y)),
  57. norm = LogNorm())
  58. # Add colorbar
  59. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  60. cbar.ax.set_yticklabels(['%3.1e' % (a) for a in scale_ticks]) # vertically oriented colorbar
  61. pos = list(cbar.ax.get_position().bounds)
  62. fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  63. plt.xlabel('X')
  64. plt.ylabel('Y')
  65. plt.draw()
  66. plt.show()
  67. plt.clf()
  68. plt.close()
  69. finally:
  70. np.savetxt("field.txt", result, fmt = "%.5f")
  71. print result