test_py.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # 'water r=1mkm scattnlay 2020/04/22']
  25. [2.03575204, 1.4558642 + 0.20503704j, 1.952484, 0.9391477],
  26. ]
  27. class TestStringMethods(unittest.TestCase):
  28. def test_bulk_mesomie(self):
  29. tol = 3e-7
  30. for solver in [mesomie]:
  31. print("Using solver: ", solver)
  32. for case in test_cases:
  33. x = case[0]
  34. m = case[1]
  35. solver.calc_ab(
  36. x, # R
  37. x, # xd
  38. x * m, # xm
  39. 1, # eps_d
  40. m * m, # eps_m
  41. 0, # d_parallel
  42. 0,
  43. ) # d_perp
  44. solver.calc_Q()
  45. Qext = solver.GetQext()
  46. Qsca = solver.GetQsca()
  47. # print(x, m, Qext)
  48. self.assertTrue((case[2] - Qext) / Qext < tol)
  49. self.assertTrue((case[3] - Qsca) / Qsca < tol)
  50. def test_bulk_multilayer(self):
  51. tol = 3e-7
  52. for solver in [mie, mie_mp]:
  53. if solver is None:
  54. continue
  55. print("Using solver: ", solver)
  56. for case in test_cases:
  57. solver.SetLayersSize(case[0])
  58. solver.SetLayersIndex(case[1])
  59. solver.RunMieCalculation()
  60. Qext = solver.GetQext()
  61. Qsca = solver.GetQsca()
  62. if case == test_cases[0]:
  63. print("test case:", case)
  64. print("ext tol:", (case[2] - Qext) / Qext)
  65. print("sca tol:", (case[3] - Qsca) / Qsca)
  66. self.assertTrue((case[2] - Qext) / Qext < tol)
  67. self.assertTrue((case[3] - Qsca) / Qsca < tol)
  68. if __name__ == "__main__":
  69. unittest.main()