scattcoeffs.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2015 Ovidio Peña Rodríguez <ovidio@bytesfall.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. # This is a test against the program n-mie (version 3a) for the test case
  28. # distributed by them (extended for x up to 100)
  29. # n-mie is based in the algorithm described in:
  30. # Wu Z.P., Wang Y.P.
  31. # Electromagnetic scattering for multilayered spheres:
  32. # recursive algorithms
  33. # Radio Science 1991. V. 26. P. 1393-1401.
  34. # Voshchinnikov N.V., Mathis J.S.
  35. # Calculating Cross Sections of Composite Interstellar Grains
  36. # Astrophys. J. 1999. V. 526. #1.
  37. # The test consist in 5 layers with the following parameters
  38. # m1=1.8 i1.7
  39. # m2=0.8 i0.7
  40. # m3=1.2 i0.09
  41. # m4=2.8 i0.2
  42. # m5=1.5 i0.4
  43. # v1/Vt=0.1
  44. # v2/Vt=0.26
  45. # v3/Vt=0.044
  46. # v4/Vt=0.3666
  47. from scattnlay import scattcoeffs
  48. import numpy as np
  49. size = np.arange(0.25, 100.25, 0.25)
  50. x = np.ones((len(size), 5), dtype = np.float64)
  51. x[:, 0] = 0.1**(1.0/3.0)*size
  52. x[:, 1] = 0.36**(1.0/3.0)*size
  53. x[:, 2] = 0.404**(1.0/3.0)*size
  54. x[:, 3] = 0.7706**(1.0/3.0)*size
  55. x[:, 4] = size
  56. m = np.ones((len(size), 5), dtype = np.complex128)
  57. m[:, 0] *= 1.8 + 1.7j
  58. m[:, 1] *= 0.8 + 0.7j
  59. m[:, 2] *= 1.2 + 0.09j
  60. m[:, 3] *= 2.8 + 0.2j
  61. m[:, 4] *= 1.5 + 0.4j
  62. terms, an, bn = scattcoeffs(x, m, 105)
  63. result = np.vstack((x[:, 4], an[:, 0].real, an[:, 0].imag, an[:, 1].real, an[:, 1].imag, an[:, 2].real, an[:, 2].imag,
  64. bn[:, 0].real, bn[:, 0].imag, bn[:, 1].real, bn[:, 1].imag, bn[:, 2].real, bn[:, 2].imag)).transpose()
  65. try:
  66. import matplotlib.pyplot as plt
  67. plt.figure(1)
  68. for i in range(3):
  69. plt.subplot(310 + i + 1)
  70. plt.plot(x[:, 4], an[:, i].real, label = "Re(a$_%i$)" % (i + 1))
  71. plt.plot(x[:, 4], bn[:, i].real, label = "Re(b$_%i$)" % (i + 1))
  72. plt.plot(x[:, 4], an[:, i].imag, label = "Im(a$_%i$)" % (i + 1))
  73. plt.plot(x[:, 4], bn[:, i].imag, label = "Im(b$_%i$)" % (i + 1))
  74. plt.ylabel('n = %i' % (i + 1))
  75. plt.legend()
  76. plt.xlabel('X')
  77. plt.show()
  78. finally:
  79. np.savetxt("scattcoeffs.txt", result, fmt = "%.5f")
  80. print result