Browse Source

scattnlay tests for python against data from Du's paper

Konstantin Ladutenko 2 years ago
parent
commit
61184be0fa
4 changed files with 35 additions and 15 deletions
  1. 2 1
      setup.py
  2. 1 1
      tests/test_cases.hpp
  3. 30 13
      tests/test_py.py
  4. 2 0
      tox.ini

+ 2 - 1
setup.py

@@ -85,7 +85,8 @@ def run_setup(extensions):
           platforms='any',
           packages=['scattnlay'],  # , 'scattnlay_dp', 'scattnlay_mp'],
           test_suite="tests",
-          ext_modules=extensions
+          ext_modules=extensions,
+          install_requires=['numpy']
           )
 
 

+ 1 - 1
tests/test_cases.hpp

@@ -21,7 +21,7 @@ std::vector<std::tuple<double, std::complex<double>, std::string> >
         {1000, {0.75, 0}, "Hong Du testcase:d"},
         // passes but takes too long
         // {10000, {1.33, 1e-5}, "Hong Du testcase:f"},
-        // fails in any precision, TODO fixme
+        // fails for nearfield in any precision, TODO fixme
         // {100, {10, 10}, "Hong Du testcase:l"},
         // {10000, {1.5, 1}, "Hong Du testcase:j"},
         // {10000, {10, 10}, "Hong Du testcase:m"},

+ 30 - 13
tests/test_py.py

@@ -1,21 +1,38 @@
 import unittest
+import numpy as np
+from scattnlay import scattnlay, mie, mie_mp
 
+test_cases = [
+    # x, {Re(m), Im(m)}, Qext, Qsca, test_name
+    [0.099, 0.75+0j, 7.417859e-06, 7.417859e-06],  # ,'a'],
+    [0.101, 0.75+0j, 8.033538e-06, 8.033538e-06],  # ,'b'],
+    [10, 0.75+0j, 2.232265, 2.232265],  # ,'c'],
+    [0.055, 1.5+1j, 0.10149104, 1.131687e-05],  # ,'g'],
+    [0.056, 1.5+1j, 0.1033467, 1.216311e-05],  # ,'h'],
+    [1, 10+10j, 2.532993, 2.049405],  # ,'k'],
+    [100, 1.33+1e-5j, 2.101321, 2.096594],  # ,'e'],
+    [100, 1.5+1j, 2.097502, 1.283697],  # ,'i'],
+    [1000, 0.75+0j, 1.997908, 1.997908],  # ,'d'],
+    [100, 10+10j, 2.071124, 1.836785],  # ,'l'],
+    [10000, 1.33+1e-5j, 2.004089, 1.723857],  # ,'f'],
+    [10000, 1.5+1j, 2.004368, 1.236574],  # ,'j'],
+    [10000, 10+10j, 2.005914, 1.795393],  # ,'m'],
+]
 
-class TestStringMethods(unittest.TestCase):
-
-    def test_upper(self):
-        self.assertEqual('foo'.upper(), 'FOO')
 
-    def test_isupper(self):
-        self.assertTrue('FOO'.isupper())
-        self.assertFalse('Foo'.isupper())
+class TestStringMethods(unittest.TestCase):
 
-    def test_split(self):
-        s = 'hello world'
-        self.assertEqual(s.split(), ['hello', 'world'])
-        # check that s.split fails when the separator is not a string
-        with self.assertRaises(TypeError):
-            s.split(2)
+    def test_bulk(self):
+        tol = 3e-7
+        for solver in [mie, mie_mp]:
+            for case in test_cases:
+                solver.SetLayersSize(case[0])
+                solver.SetLayersIndex(case[1])
+                solver.RunMieCalculation()
+                Qext = solver.GetQext()
+                Qsca = solver.GetQsca()
+                self.assertTrue((case[2]-Qext)/Qext < tol)
+                self.assertTrue((case[3]-Qsca)/Qsca < tol)
 
 
 if __name__ == '__main__':

+ 2 - 0
tox.ini

@@ -8,5 +8,7 @@ envlist = py38, py310
 skip_missing_interpreters=true
 
 [testenv]
+deps =
+    numpy
 commands =
     python -m unittest tests/test_py.py