test_py.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(x, # R
  34. x, # xd
  35. x * m, # xm
  36. 1, # eps_d
  37. m * m, # eps_m
  38. 0, # d_parallel
  39. 0) # d_perp
  40. solver.calc_Q()
  41. Qext = solver.GetQext()
  42. Qsca = solver.GetQsca()
  43. # print(x, m, Qext)
  44. self.assertTrue((case[2]-Qext)/Qext < tol)
  45. self.assertTrue((case[3]-Qsca)/Qsca < tol)
  46. def test_bulk_multilayer(self):
  47. tol = 3e-7
  48. for solver in [mie, mie_mp]:
  49. if solver is None:
  50. continue
  51. print('Using solver: ', solver)
  52. for case in test_cases:
  53. solver.SetLayersSize(case[0])
  54. solver.SetLayersIndex(case[1])
  55. solver.RunMieCalculation()
  56. Qext = solver.GetQext()
  57. Qsca = solver.GetQsca()
  58. self.assertTrue((case[2]-Qext)/Qext < tol)
  59. self.assertTrue((case[3]-Qsca)/Qsca < tol)
  60. if __name__ == '__main__':
  61. unittest.main()