示例#1
0
 def test_time(self):
     """
     Similar to FloatWithUnitTest.test_time.
     Check whether EnergyArray and FloatWithUnit have same behavior.
     """
     # here there's a minor difference because we have a ndarray with dtype=np.int.
     a = TimeArray(20, "h")
     self.assertAlmostEqual(a.to("s"), 3600 * 20)
     #Test left and right multiplication.
     self.assertEqual(str(a * 3), "60 h")
     self.assertEqual(str(3 * a), "60 h")
示例#2
0
 def test_time(self):
     """
     Similar to FloatWithUnitTest.test_time.
     Check whether EnergyArray and FloatWithUnit have same behavior.
     """
     # here there's a minor difference because we have a ndarray with
     # dtype=np.int.
     a = TimeArray(20, "h")
     self.assertAlmostEqual(a.to("s"), 3600 * 20)
     #Test left and right multiplication.
     self.assertEqual(str(a * 3), "60 h")
     self.assertEqual(str(3 * a), "60 h")
示例#3
0
    def test_array_algebra(self):
        ene_ha = EnergyArray([1, 2], "Ha")
        ene_ev = EnergyArray([1, 2], "eV")
        time_s = TimeArray([1, 2], "s")

        e1 = ene_ha.copy()
        e1 += 1
        e2 = ene_ha.copy()
        e2 -= 1
        e3 = ene_ha.copy()
        #e3 /= 2
        e4 = ene_ha.copy()
        e4 *= 2

        objects_with_unit = [
            ene_ha + ene_ev,
            ene_ha - ene_ev,
            3 * ene_ha,
            ene_ha * 3,
            ene_ha / 3,
            3 / ene_ha,
            ene_ha * time_s,
            ene_ha / ene_ev,
            ene_ha.copy(),
            ene_ha[0:1],
            e1,
            e2,
            e3,
            e4,
        ]

        for i, obj in enumerate(objects_with_unit):
            #print(i, obj.unit)
            self.assertTrue(hasattr(obj, "unit"))
            #self.assertTrue(str(obj.unit) == "Ha")

        objects_without_unit = [
            # Here we could return a FloatWithUnit object but I prefer this
            # a bare scalar since FloatWithUnit extends float while we could
            # have an int.
            ene_ha[0],
        ]

        for obj in objects_without_unit:
            self.assertFalse(hasattr(obj, "unit"))

        with self.assertRaises(UnitError):
            ene_ha + time_s