Beispiel #1
0
 def test_rng_with_bad_seed_negative_int(self):
     with self.assertRaises(OverflowError):
         rng = polycap.Rng(-523)
Beispiel #2
0
 def test_rng_with_bad_seed_string(self):
     with self.assertRaises(TypeError):
         rng = polycap.Rng("this-is-not-a-seed")
Beispiel #3
0
 def test_rng_without_seed(self):
     self.assertIsInstance(polycap.Rng(), polycap.Rng)
Beispiel #4
0
 def test_rng_with_good_seed(self):
     self.assertIsInstance(polycap.Rng(12345678), polycap.Rng)
Beispiel #5
0
class TestPolycapSource(unittest.TestCase):
    rng = polycap.Rng()

    def test_source_bad_input(self):
        with self.assertRaises(TypeError):
            source = polycap.Source(None, -1,-1,-1,-1,-1,0.3,0.3, 2., np.linspace(1, 25.0, 250))
        with self.assertRaises(ValueError):
            source = polycap.Source(TestPolycapPhoton.description, -1,-1,-1,-1,-1,0.3,0.3, 2., np.linspace(1, 25.0, 250))

    def test_source_get_photon(self):
        source = polycap.Source(TestPolycapPhoton.description, 0.05, 0.1, 0.1, 0.2, 0.2, 0., 0., 0.5, np.linspace(1, 25.0, 250))
        photon = source.get_photon(TestPolycapSource.rng)

    def test_source_bad_get_transmission_efficiencies(self):
        source = polycap.Source(TestPolycapPhoton.description, 2000.0, 0.2065, 0.2065, 0.0, 0.0, 0.0, 0.0, 0.5, np.linspace(1, 25.0, 250))
        with self.assertRaises(TypeError):
            efficiencies = source.get_transmission_efficiencies(-1, None, 1000)
        with self.assertRaises(TypeError):
            efficiencies = source.get_transmission_efficiencies(-1, 1000, False, "extra arg")

    def test_source_good_get_transmission_efficiencies(self):
        VectorTuple = polycap.VectorTuple
        source = polycap.Source(TestPolycapPhoton.description, 2000.0, 0.2065, 0.2065, 0.0, 0.0, 0.0, 0.0, 0.5, np.linspace(1, 25.0, 250))
        efficiencies = source.get_transmission_efficiencies(-1, 10000, leak_calc=False)
        efficiencies.write_hdf5("temp-py.h5")
        self.assertTrue(os.path.exists("temp-py.h5"))
        os.remove("temp-py.h5")
        data = efficiencies.data
        self.assertIsInstance(data, tuple)
        data2 = efficiencies.data
        self.assertIsNot(data, data2)
        self.assertIs(data[0], data2[0])
        self.assertIs(data[1], data2[1])
        start_coords = list(efficiencies.start_coords)
        self.assertEqual(len(start_coords), 10000)
        start_coords = list(efficiencies.start_coords) # do it twice to test caching
        self.assertEqual(len(start_coords), 10000)
        start_direction = list(efficiencies.start_direction)
        start_elecv = list(efficiencies.start_elecv)
        src_start_coords = list(efficiencies.src_start_coords)
        self.assertIsInstance(start_coords[0], VectorTuple)
        self.assertEqual(start_coords[0].z, 0.)
        self.assertIsInstance(start_direction[0], VectorTuple)
        self.assertEqual(start_direction[0].z, 1.)
        self.assertIsInstance(start_elecv[0], VectorTuple)
        self.assertIsInstance(src_start_coords[0], VectorTuple)
        exit_coords = list(efficiencies.exit_coords)
        exit_direction = list(efficiencies.exit_direction)
        exit_elecv = list(efficiencies.exit_elecv)
        self.assertIsInstance(exit_coords[0], VectorTuple)
        self.assertIsInstance(exit_direction[0], VectorTuple)
        self.assertIsInstance(exit_elecv[0], VectorTuple)
        n_refl = list(efficiencies.n_refl)
        exit_weights = list(efficiencies.exit_weights)
        d_travel = list(efficiencies.d_travel)
        self.assertIsInstance(n_refl[0], int)
        self.assertIsInstance(exit_weights[0], np.ndarray)
        self.assertIsInstance(d_travel[0], float)
        
        self.assertEqual(sys.getrefcount(data[0]), 4)

        #fig = plt.figure()
        #ax = fig.add_subplot(111)
        #ax.plot(*data, color='r')
        #plt.draw()
        #plt.show()

        data = data[0]
        del(efficiencies)
        del(data2)

        self.assertEqual(sys.getrefcount(data), 2)

        with self.assertRaises(ValueError):
            data[0] = 6

    def test_source_get_transmission_efficiencies_vs_multiple_photon_launch(self):
        n_photons = 30000
        source = polycap.Source(TestPolycapPhoton.description, 2000.0, 0.2065, 0.2065, 0.0, 0.0, 0.0, 0.0, 0.5, np.linspace(1, 25.0, 10))
        efficiencies = source.get_transmission_efficiencies(-1, n_photons, leak_calc=False)

        weights = np.zeros(10)
        phot_ini = 0
        phot_transm = 0
        myrng = TestPolycapSource.rng
        while phot_transm < n_photons:
            photon = source.get_photon(myrng)
            try:
                myweights = photon.launch(np.linspace(1, 25.0, 10), leak_calc=False)
                if myweights is not None:
                    weights += myweights
                    phot_transm += 1
                phot_ini += 1
            except ValueError:
                pass
        weights = weights[:]/phot_ini # normalize for total initiated photons, including simulated open area (due to photons that interacted with capllary walls at start)
        data = efficiencies.data
        for i in range(10):
            self.assertAlmostEqual(weights[i], data[1][i], delta=0.01)

        del(efficiencies)