field-dielectric-sphere.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 test case calculates the electric field in the
  28. # small dielectric sphere.
  29. import scattnlay
  30. import os
  31. from scattnlay import fieldnlay
  32. import numpy as np
  33. class bcolors:
  34. HEADER = '\033[95m'
  35. OKBLUE = '\033[94m'
  36. OKGREEN = '\033[92m'
  37. WARNING = '\033[93m'
  38. FAIL = '\033[91m'
  39. ENDC = '\033[0m'
  40. BOLD = '\033[1m'
  41. UNDERLINE = '\033[4m'
  42. def is_test_coord_passed(x,m,coord):
  43. terms, E, H = fieldnlay(x, m, coord)
  44. Er = np.absolute(E)
  45. Eabs = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  46. analytic_E = (3/(m[0,0]**2+2)).real
  47. for value in Eabs:
  48. #print(value, value-analytic_E)
  49. if ( value-analytic_E > 10e-7 ):
  50. print(bcolors.FAIL+"Test failed: value="+str(value)+" for m="+str(m[0,0])
  51. +" instead of analytic Eabs="+str(analytic_E))
  52. print("Coords",coord)
  53. return False
  54. return True
  55. def is_test_all_coord_passed(x,m):
  56. npts = 5
  57. scan = np.linspace(0.999*x[0, 0], -0.999*x[0, 0], npts)
  58. zero = np.zeros(npts, dtype = np.float64)
  59. coordZ = np.vstack((zero, zero, scan)).transpose()
  60. coordY = np.vstack((zero, scan, zero)).transpose()
  61. coordX = np.vstack((scan, zero, zero)).transpose()
  62. if (is_test_coord_passed(x,m,coordX)
  63. and is_test_coord_passed(x,m,coordY)
  64. and is_test_coord_passed(x,m,coordY)):
  65. return True
  66. return False
  67. def test_sphere():
  68. path = os.path.dirname(scattnlay.__file__)
  69. print(bcolors.HEADER+"===== Small dielectric sphere test ====="+bcolors.ENDC)
  70. print("Test for python module of Scattnlay: "+scattnlay.__file__)
  71. #Set the sphere
  72. x = np.ones((1, 1), dtype = np.float64)
  73. x[0, 0] = 0.0001
  74. m = np.ones((1, 1), dtype = np.complex128)
  75. m[0, 0] = 1.0
  76. delta_m = 0.712
  77. for n in xrange(0,15):
  78. m[0,0] = 1.0 + delta_m*n
  79. print("Testing index m="+str(m[0,0]))
  80. if not is_test_all_coord_passed(x,m):
  81. print(bcolors.FAIL+"Test for dielectric sphere failed!"+bcolors.ENDC)
  82. return False
  83. print(bcolors.OKGREEN+"All tests for dielectric sphere passed!"+bcolors.ENDC)
  84. return True
  85. test_sphere()