test_py.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import unittest
  2. from scattnlay import mie, mie_mp
  3. from scattnlay import mesomie
  4. # A list of tests for a bulk sphere from
  5. # Hong Du, "Mie-scattering calculation," Appl. Opt. 43, 1951-1956 (2004)
  6. # table 1: sphere size and refractive index
  7. # followed by resulting extinction and scattering efficiencies
  8. test_cases = [
  9. # x, m, Qext, Qsca #, test_name
  10. [0.099, 0.75 + 0j, 7.417859e-06, 7.417859e-06], # ,'a'],
  11. [0.101, 0.75 + 0j, 8.033538e-06, 8.033538e-06], # ,'b'],
  12. [10, 0.75 + 0j, 2.232265, 2.232265], # ,'c'],
  13. [0.055, 1.5 + 1j, 0.10149104, 1.131687e-05], # ,'g'],
  14. [0.056, 1.5 + 1j, 0.1033467, 1.216311e-05], # ,'h'],
  15. [1, 10 + 10j, 2.532993, 2.049405], # ,'k'],
  16. [100, 1.33 + 1e-5j, 2.101321, 2.096594], # ,'e'],
  17. [100, 1.5 + 1j, 2.097502, 1.283697], # ,'i'],
  18. [1000, 0.75 + 0j, 1.997908, 1.997908], # ,'d'],
  19. [100, 10 + 10j, 2.071124, 1.836785], # ,'l'],
  20. [10000, 1.33 + 1e-5j, 2.004089, 1.723857], # ,'f'],
  21. [10000, 1.5 + 1j, 2.004368, 1.236574], # ,'j'],
  22. [10000, 10 + 10j, 2.005914, 1.795393], # ,'m'],
  23. # [1.8263846985116234, 0.02867488311561525+1.2957040351341687j, 3, 3]
  24. ]
  25. class TestStringMethods(unittest.TestCase):
  26. def test_bulk_mesomie(self):
  27. tol = 3e-7
  28. for solver in [mesomie]:
  29. print("Using solver: ", solver)
  30. for case in test_cases:
  31. x = case[0]
  32. m = case[1]
  33. solver.calc_ab(
  34. x, # R
  35. x, # xd
  36. x * m, # xm
  37. 1, # eps_d
  38. m * m, # eps_m
  39. 0, # d_parallel
  40. 0,
  41. ) # d_perp
  42. solver.calc_Q()
  43. Qext = solver.GetQext()
  44. Qsca = solver.GetQsca()
  45. # print(x, m, Qext)
  46. self.assertTrue((case[2] - Qext) / Qext < tol)
  47. self.assertTrue((case[3] - Qsca) / Qsca < tol)
  48. def test_bulk_multilayer(self):
  49. tol = 3e-7
  50. for solver in [mie, mie_mp]:
  51. if solver is None:
  52. continue
  53. print("Using solver: ", solver)
  54. for case in test_cases:
  55. solver.SetLayersSize(case[0])
  56. solver.SetLayersIndex(case[1])
  57. solver.RunMieCalculation()
  58. Qext = solver.GetQext()
  59. Qsca = solver.GetQsca()
  60. if case == test_cases[0]:
  61. print("test case:", case)
  62. print("ext tol:", (case[2] - Qext) / Qext)
  63. print("sca tol:", (case[3] - Qsca) / Qsca)
  64. self.assertTrue((case[2] - Qext) / Qext < tol)
  65. self.assertTrue((case[3] - Qsca) / Qsca < tol)
  66. if __name__ == "__main__":
  67. unittest.main()