field-dielectric-sphere.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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,coordX,coordY,coordZ):
  43. terms, E, H = fieldnlay(x, m, coordX,coordY,coordZ)
  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. if (is_test_coord_passed(x,m,scan, zero, zero)
  60. and is_test_coord_passed(x,m,zero, scan, zero)
  61. and is_test_coord_passed(x,m,zero, zero, scan)):
  62. return True
  63. return False
  64. def test_sphere():
  65. path = os.path.dirname(scattnlay.__file__)
  66. print(bcolors.HEADER+"===== Small dielectric sphere test ====="+bcolors.ENDC)
  67. print("Test for python module of Scattnlay: "+scattnlay.__file__)
  68. #Set the sphere
  69. x = np.ones((1, 1), dtype = np.float64)
  70. x[0, 0] = 0.0001
  71. m = np.ones((1, 1), dtype = np.complex128)
  72. m[0, 0] = 1.0
  73. delta_m = 0.712
  74. for n in xrange(0,15):
  75. m[0,0] = 1.0 + delta_m*n
  76. print("Testing index m="+str(m[0,0]))
  77. if not is_test_all_coord_passed(x,m):
  78. print(bcolors.FAIL+"Test for dielectric sphere failed!"+bcolors.ENDC)
  79. return False
  80. print(bcolors.OKGREEN+"All tests for dielectric sphere passed!"+bcolors.ENDC)
  81. return True
  82. test_sphere()