class TestThirdBody(unittest.TestCase):
    """
    Contains unit tests of the ThirdBody class.
    """
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.arrheniusLow = Arrhenius(
            A=(2.62e+33, "cm^6/(mol^2*s)"),
            n=-4.76,
            Ea=(10.21, "kJ/mol"),
            T0=(1, "K"),
        )
        self.efficiencies = {
            "C": 3,
            "C(=O)=O": 2,
            "CC": 3,
            "O": 6,
            "[Ar]": 0.7,
            "[C]=O": 1.5,
            "[H][H]": 2
        }
        self.Tmin = 300.
        self.Tmax = 2000.
        self.Pmin = 0.01
        self.Pmax = 100.
        self.comment = """H + CH3 -> CH4"""
        self.thirdBody = ThirdBody(
            arrheniusLow=self.arrheniusLow,
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            efficiencies=self.efficiencies,
            comment=self.comment,
        )

    def test_arrheniusLow(self):
        """
        Test that the ThirdBody arrhenius property was properly set.
        """
        self.assertTrue(self.thirdBody.arrheniusLow is self.arrheniusLow)

    def test_Tmin(self):
        """
        Test that the ThirdBody Tmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmin.value_si, self.Tmin, 6)

    def test_Tmax(self):
        """
        Test that the ThirdBody Tmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmax.value_si, self.Tmax, 6)

    def test_Pmin(self):
        """
        Test that the ThirdBody Pmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmin.value_si * 1e-5, self.Pmin,
                               6)

    def test_Pmax(self):
        """
        Test that the ThirdBody Pmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmax.value_si * 1e-5, self.Pmax,
                               6)

    def test_comment(self):
        """
        Test that the ThirdBody comment property was properly set.
        """
        self.assertEqual(self.thirdBody.comment, self.comment)

    def test_isPressureDependent(self):
        """
        Test the ThirdBody.isPressureDependent() method.
        """
        self.assertTrue(self.thirdBody.isPressureDependent())

    def test_getEffectivePressure(self):
        """
        Test the ThirdBody.getEffectivePressure() method.
        """
        P = 1.0
        # Test that each pure bath gas gives the correct effective pressure
        # Create list of species objects
        species = [
            Species(molecule=[mol])
            for mol in self.thirdBody.efficiencies.keys()
        ]
        for mol, eff in self.thirdBody.efficiencies.items():
            for spec in species:
                if spec.isIsomorphic(mol):
                    i = species.index(spec)
                    break
            fractions = numpy.zeros(len(species))
            fractions[i] = 1.0
            Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
            self.assertAlmostEqual(P * eff, Peff)
        # Also test a mixture of bath gases
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.5
        fractions[1] = 0.5
        eff = 0
        for mol in self.thirdBody.efficiencies.keys():
            if species[0].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
            if species[1].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertAlmostEqual(P * eff, Peff)

        # Test the same thing, only with a list of species that are Molecule objects
        species = [
            mol.copy(deep=True) for mol in self.thirdBody.efficiencies.keys()
        ]
        for mol, eff in self.thirdBody.efficiencies.items():
            for spec in species:
                if spec.isIsomorphic(mol):
                    i = species.index(spec)
                    break
            fractions = numpy.zeros(len(species))
            fractions[i] = 1.0
            Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
            self.assertAlmostEqual(P * eff, Peff)
        # Also test a mixture of bath gases
        eff = 0
        for mol in self.thirdBody.efficiencies.keys():
            if species[0].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
            if species[1].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]

        fractions = numpy.zeros(len(species))
        fractions[0] = 0.5
        fractions[1] = 0.5
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertAlmostEqual(P * eff, Peff)

        # Here, test a non-normalized set of fractions (they are still 50% of each)
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.7
        fractions[1] = 0.7
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertAlmostEqual(P * eff, Peff)

    def test_getEffectiveColliderEfficiencies(self):
        """
        Test the getEffectiveColliderEfficiencies() method
        """
        # Create list of molecules
        molecules = [
            Molecule(SMILES=smiles) for smiles in
            ["C", "C(=O)=O", "CC", "O", "[Ar]", "[C]=O", "[H][H]"]
        ]
        methodEfficiencies = self.thirdBody.getEffectiveColliderEfficiencies(
            molecules)
        efficiencies = numpy.array([3, 2, 3, 6, 0.7, 1.5, 2])
        numpy.testing.assert_array_almost_equal(efficiencies,
                                                methodEfficiencies)

        # Use a smaller list of molecules
        molecules = [Molecule(SMILES=smiles) for smiles in ["C", "CC", "[Ar]"]]
        methodEfficiencies = self.thirdBody.getEffectiveColliderEfficiencies(
            molecules)
        efficiencies = numpy.array([3, 3, 0.7])
        numpy.testing.assert_array_almost_equal(efficiencies,
                                                methodEfficiencies)

    def test_getRateCoefficient(self):
        """
        Test the ThirdBody.getRateCoefficient() method.
        """
        Tlist = numpy.array([300, 500, 1000, 1500])
        Plist = numpy.array([1e4, 1e5, 1e6])
        Kexp = numpy.array([
            [2.83508e+08, 2.83508e+09, 2.83508e+10],
            [7.68759e+07, 7.68759e+08, 7.68759e+09],
            [4.84353e+06, 4.84353e+07, 4.84353e+08],
            [7.05740e+05, 7.05740e+06, 7.05740e+07],
        ])
        for t in range(Tlist.shape[0]):
            for p in range(Plist.shape[0]):
                Kact = self.thirdBody.getRateCoefficient(Tlist[t], Plist[p])
                self.assertAlmostEqual(Kact,
                                       Kexp[t, p],
                                       delta=1e-4 * Kexp[t, p])

    def test_pickle(self):
        """
        Test that a ThirdBody object can be successfully pickled and
        unpickled with no loss of information.
        """
        import cPickle
        thirdBody = cPickle.loads(cPickle.dumps(self.thirdBody, -1))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value,
                               thirdBody.arrheniusLow.A.value,
                               delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units,
                         thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value,
                               thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units,
                         thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value,
                               thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units,
                         thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value,
                               thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units,
                         thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value,
                               4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value,
                               4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value,
                               4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value,
                               4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        efficiencies = {}
        for mol, eff in self.thirdBody.efficiencies.iteritems():
            efficiencies[mol.toSMILES()] = eff
        pickled_efficiencies = {}
        for mol, eff in thirdBody.efficiencies.iteritems():
            pickled_efficiencies[mol.toSMILES()] = eff
        self.assertEqual(efficiencies, pickled_efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)

    def test_repr(self):
        """
        Test that a ThirdBody object can be successfully reconstructed
        from its repr() output with no loss of information.
        """
        thirdBody = None
        exec('thirdBody = {0!r}'.format(self.thirdBody))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value,
                               thirdBody.arrheniusLow.A.value,
                               delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units,
                         thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value,
                               thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units,
                         thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value,
                               thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units,
                         thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value,
                               thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units,
                         thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value,
                               4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value,
                               4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value,
                               4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value,
                               4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        efficiencies = {}
        for mol, eff in self.thirdBody.efficiencies.iteritems():
            efficiencies[mol.toSMILES()] = eff
        pickled_efficiencies = {}
        for mol, eff in thirdBody.efficiencies.iteritems():
            pickled_efficiencies[mol.toSMILES()] = eff
        self.assertEqual(efficiencies, pickled_efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)

    def test_changeRate(self):
        """
        Test the ThirdBody.changeRate() method.
        """
        Tlist = numpy.array([
            300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400,
            1500
        ])
        k0list = numpy.array(
            [self.thirdBody.getRateCoefficient(T, 1e5) for T in Tlist])
        self.thirdBody.changeRate(2)
        for T, kexp in zip(Tlist, k0list):
            kact = self.thirdBody.getRateCoefficient(T, 1e5)
            self.assertAlmostEqual(2 * kexp, kact, delta=1e-6 * kexp)
Exemplo n.º 2
0
class TestThirdBody(unittest.TestCase):
    """
    Contains unit tests of the ThirdBody class.
    """
    
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.arrheniusLow = Arrhenius(
            A = (2.62e+33,"cm^6/(mol^2*s)"), 
            n = -4.76, 
            Ea = (10.21,"kJ/mol"), 
            T0 = (1,"K"),
        )
        self.efficiencies = {"C": 3, "C(=O)=O": 2, "CC": 3, "O": 6, "[Ar]": 0.7, "[C]=O": 1.5, "[H][H]": 2}
        self.Tmin = 300.
        self.Tmax = 2000.
        self.Pmin = 0.01
        self.Pmax = 100.
        self.comment = """H + CH3 -> CH4"""
        self.thirdBody = ThirdBody(
            arrheniusLow = self.arrheniusLow,
            Tmin = (self.Tmin,"K"),
            Tmax = (self.Tmax,"K"),
            Pmin = (self.Pmin,"bar"),
            Pmax = (self.Pmax,"bar"),
            efficiencies = self.efficiencies,
            comment = self.comment,
        )
        
    def test_arrheniusLow(self):
        """
        Test that the ThirdBody arrhenius property was properly set.
        """
        self.assertTrue(self.thirdBody.arrheniusLow is self.arrheniusLow)
        
    def test_Tmin(self):
        """
        Test that the ThirdBody Tmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmin.value_si, self.Tmin, 6)
        
    def test_Tmax(self):
        """
        Test that the ThirdBody Tmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmax.value_si, self.Tmax, 6)

    def test_Pmin(self):
        """
        Test that the ThirdBody Pmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmin.value_si*1e-5, self.Pmin, 6)
        
    def test_Pmax(self):
        """
        Test that the ThirdBody Pmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmax.value_si*1e-5, self.Pmax, 6)
        
    def test_comment(self):
        """
        Test that the ThirdBody comment property was properly set.
        """
        self.assertEqual(self.thirdBody.comment, self.comment)

    def test_isPressureDependent(self):
        """
        Test the ThirdBody.isPressureDependent() method.
        """
        self.assertTrue(self.thirdBody.isPressureDependent())
    
    def test_getEffectivePressure(self):
        """
        Test the ThirdBody.getEffectivePressure() method.
        """
        P = 1.0
        # Test that each pure bath gas gives the correct effective pressure
        species = self.efficiencies.keys()
        for spec, eff in self.efficiencies.items():
            fractions = numpy.zeros(len(species))
            i = species.index(spec)
            fractions[i] = 1.0
            Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
            self.assertEqual(P * eff, Peff)
        # Also test a mixture of bath gases
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.5
        fractions[1] = 0.5
        eff = 0.5 * self.efficiencies[species[0]] + 0.5 * self.efficiencies[species[1]]
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertEqual(P * eff, Peff)
            
    def test_getRateCoefficient(self):
        """
        Test the ThirdBody.getRateCoefficient() method.
        """
        Tlist = numpy.array([300,500,1000,1500])
        Plist = numpy.array([1e4,1e5,1e6])
        Kexp = numpy.array([
            [2.83508e+08, 2.83508e+09, 2.83508e+10],
            [7.68759e+07, 7.68759e+08, 7.68759e+09],
            [4.84353e+06, 4.84353e+07, 4.84353e+08],
            [7.05740e+05, 7.05740e+06, 7.05740e+07],
        ])
        for t in range(Tlist.shape[0]):
            for p in range(Plist.shape[0]):
                Kact = self.thirdBody.getRateCoefficient(Tlist[t], Plist[p])
                self.assertAlmostEqual(Kact, Kexp[t,p], delta=1e-4*Kexp[t,p])

    def test_pickle(self):
        """
        Test that a ThirdBody object can be successfully pickled and
        unpickled with no loss of information.
        """
        import cPickle
        thirdBody = cPickle.loads(cPickle.dumps(self.thirdBody))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value, thirdBody.arrheniusLow.A.value, delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units, thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value, thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units, thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value, thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units, thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value, thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units, thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value, 4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value, 4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value, 4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value, 4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        self.assertEqual(self.thirdBody.efficiencies, thirdBody.efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)

    def test_repr(self):
        """
        Test that a ThirdBody object can be successfully reconstructed
        from its repr() output with no loss of information.
        """
        thirdBody = None
        exec('thirdBody = {0!r}'.format(self.thirdBody))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value, thirdBody.arrheniusLow.A.value, delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units, thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value, thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units, thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value, thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units, thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value, thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units, thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value, 4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value, 4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value, 4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value, 4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        self.assertEqual(self.thirdBody.efficiencies, thirdBody.efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)
Exemplo n.º 3
0
class TestThirdBody(unittest.TestCase):
    """
    Contains unit tests of the ThirdBody class.
    """
    
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.arrheniusLow = Arrhenius(
            A = (2.62e+33,"cm^6/(mol^2*s)"), 
            n = -4.76, 
            Ea = (10.21,"kJ/mol"), 
            T0 = (1,"K"),
        )
        self.efficiencies = {"C": 3, "C(=O)=O": 2, "CC": 3, "O": 6, "[Ar]": 0.7, "[C]=O": 1.5, "[H][H]": 2}
        self.Tmin = 300.
        self.Tmax = 2000.
        self.Pmin = 0.01
        self.Pmax = 100.
        self.comment = """H + CH3 -> CH4"""
        self.thirdBody = ThirdBody(
            arrheniusLow = self.arrheniusLow,
            Tmin = (self.Tmin,"K"),
            Tmax = (self.Tmax,"K"),
            Pmin = (self.Pmin,"bar"),
            Pmax = (self.Pmax,"bar"),
            efficiencies = self.efficiencies,
            comment = self.comment,
        )
        
    def test_arrheniusLow(self):
        """
        Test that the ThirdBody arrhenius property was properly set.
        """
        self.assertTrue(self.thirdBody.arrheniusLow is self.arrheniusLow)
        
    def test_Tmin(self):
        """
        Test that the ThirdBody Tmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmin.value_si, self.Tmin, 6)
        
    def test_Tmax(self):
        """
        Test that the ThirdBody Tmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmax.value_si, self.Tmax, 6)

    def test_Pmin(self):
        """
        Test that the ThirdBody Pmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmin.value_si*1e-5, self.Pmin, 6)
        
    def test_Pmax(self):
        """
        Test that the ThirdBody Pmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmax.value_si*1e-5, self.Pmax, 6)
        
    def test_comment(self):
        """
        Test that the ThirdBody comment property was properly set.
        """
        self.assertEqual(self.thirdBody.comment, self.comment)

    def test_isPressureDependent(self):
        """
        Test the ThirdBody.isPressureDependent() method.
        """
        self.assertTrue(self.thirdBody.isPressureDependent())
    
    def test_getEffectivePressure(self):
        """
        Test the ThirdBody.getEffectivePressure() method.
        """
        P = 1.0
        # Test that each pure bath gas gives the correct effective pressure
        # Create list of species objects 
        species = [Species(molecule=[mol]) for mol in self.thirdBody.efficiencies.keys()]
        for mol, eff in self.thirdBody.efficiencies.items():
            for spec in species:
                if spec.isIsomorphic(mol):
                    i = species.index(spec)
                    break
            fractions = numpy.zeros(len(species))
            fractions[i] = 1.0
            Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
            self.assertAlmostEqual(P * eff, Peff)
        # Also test a mixture of bath gases
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.5
        fractions[1] = 0.5
        eff = 0
        for mol in self.thirdBody.efficiencies.keys():
            if species[0].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
            if species[1].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertAlmostEqual(P * eff, Peff)
        
        # Test the same thing, only with a list of species that are Molecule objects
        species = [mol.copy(deep=True) for mol in self.thirdBody.efficiencies.keys()]
        for mol, eff in self.thirdBody.efficiencies.items():
            for spec in species:
                if spec.isIsomorphic(mol):
                    i = species.index(spec)
                    break
            fractions = numpy.zeros(len(species))
            fractions[i] = 1.0
            Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
            self.assertAlmostEqual(P * eff, Peff)
        # Also test a mixture of bath gases
        eff = 0
        for mol in self.thirdBody.efficiencies.keys():
            if species[0].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
            if species[1].isIsomorphic(mol):
                eff += 0.5 * self.thirdBody.efficiencies[mol]
        
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.5
        fractions[1] = 0.5
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertAlmostEqual(P * eff, Peff)
        
        # Here, test a non-normalized set of fractions (they are still 50% of each)
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.7
        fractions[1] = 0.7
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertAlmostEqual(P * eff, Peff)
        
    def test_getEffectiveColliderEfficiencies(self):
        """
        Test the getEffectiveColliderEfficiencies() method
        """
        # Create list of molecules
        molecules = [Molecule(SMILES=smiles) for smiles in ["C", "C(=O)=O", "CC", "O", "[Ar]", "[C]=O", "[H][H]"]]
        methodEfficiencies = self.thirdBody.getEffectiveColliderEfficiencies(molecules)
        efficiencies = numpy.array([3, 2, 3, 6, 0.7, 1.5, 2])
        numpy.testing.assert_array_almost_equal(efficiencies, methodEfficiencies)
        
        # Use a smaller list of molecules
        molecules = [Molecule(SMILES=smiles) for smiles in ["C", "CC", "[Ar]"]]
        methodEfficiencies = self.thirdBody.getEffectiveColliderEfficiencies(molecules)
        efficiencies = numpy.array([3, 3, 0.7])
        numpy.testing.assert_array_almost_equal(efficiencies, methodEfficiencies)

    def test_getRateCoefficient(self):
        """
        Test the ThirdBody.getRateCoefficient() method.
        """
        Tlist = numpy.array([300,500,1000,1500])
        Plist = numpy.array([1e4,1e5,1e6])
        Kexp = numpy.array([
            [2.83508e+08, 2.83508e+09, 2.83508e+10],
            [7.68759e+07, 7.68759e+08, 7.68759e+09],
            [4.84353e+06, 4.84353e+07, 4.84353e+08],
            [7.05740e+05, 7.05740e+06, 7.05740e+07],
        ])
        for t in range(Tlist.shape[0]):
            for p in range(Plist.shape[0]):
                Kact = self.thirdBody.getRateCoefficient(Tlist[t], Plist[p])
                self.assertAlmostEqual(Kact, Kexp[t,p], delta=1e-4*Kexp[t,p])

    def test_pickle(self):
        """
        Test that a ThirdBody object can be successfully pickled and
        unpickled with no loss of information.
        """
        import cPickle
        thirdBody = cPickle.loads(cPickle.dumps(self.thirdBody,-1))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value, thirdBody.arrheniusLow.A.value, delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units, thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value, thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units, thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value, thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units, thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value, thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units, thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value, 4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value, 4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value, 4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value, 4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        efficiencies = {}
        for mol, eff in self.thirdBody.efficiencies.iteritems():
            efficiencies[mol.toSMILES()] = eff
        pickled_efficiencies = {}
        for mol, eff in thirdBody.efficiencies.iteritems():
            pickled_efficiencies[mol.toSMILES()] = eff
        self.assertEqual(efficiencies, pickled_efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)

    def test_repr(self):
        """
        Test that a ThirdBody object can be successfully reconstructed
        from its repr() output with no loss of information.
        """
        thirdBody = None
        exec('thirdBody = {0!r}'.format(self.thirdBody))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value, thirdBody.arrheniusLow.A.value, delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units, thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value, thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units, thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value, thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units, thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value, thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units, thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value, 4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value, 4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value, 4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value, 4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        efficiencies = {}
        for mol, eff in self.thirdBody.efficiencies.iteritems():
            efficiencies[mol.toSMILES()] = eff
        pickled_efficiencies = {}
        for mol, eff in thirdBody.efficiencies.iteritems():
            pickled_efficiencies[mol.toSMILES()] = eff
        self.assertEqual(efficiencies, pickled_efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)
        
    def test_changeRate(self):
        """
        Test the ThirdBody.changeRate() method.
        """
        Tlist = numpy.array([300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500])
        k0list = numpy.array([self.thirdBody.getRateCoefficient(T,1e5) for T in Tlist])
        self.thirdBody.changeRate(2)
        for T, kexp in zip(Tlist, k0list):
            kact = self.thirdBody.getRateCoefficient(T,1e5)
            self.assertAlmostEqual(2*kexp, kact, delta=1e-6*kexp)
Exemplo n.º 4
0
class TestThirdBody(unittest.TestCase):
    """
    Contains unit tests of the ThirdBody class.
    """
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.arrheniusLow = Arrhenius(
            A=(2.62e+33, "cm^6/(mol^2*s)"),
            n=-4.76,
            Ea=(10.21, "kJ/mol"),
            T0=(1, "K"),
        )
        self.efficiencies = {
            "C": 3,
            "C(=O)=O": 2,
            "CC": 3,
            "O": 6,
            "[Ar]": 0.7,
            "[C]=O": 1.5,
            "[H][H]": 2
        }
        self.Tmin = 300.
        self.Tmax = 2000.
        self.Pmin = 0.01
        self.Pmax = 100.
        self.comment = """H + CH3 -> CH4"""
        self.thirdBody = ThirdBody(
            arrheniusLow=self.arrheniusLow,
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            efficiencies=self.efficiencies,
            comment=self.comment,
        )

    def test_arrheniusLow(self):
        """
        Test that the ThirdBody arrhenius property was properly set.
        """
        self.assertTrue(self.thirdBody.arrheniusLow is self.arrheniusLow)

    def test_Tmin(self):
        """
        Test that the ThirdBody Tmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmin.value_si, self.Tmin, 6)

    def test_Tmax(self):
        """
        Test that the ThirdBody Tmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Tmax.value_si, self.Tmax, 6)

    def test_Pmin(self):
        """
        Test that the ThirdBody Pmin property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmin.value_si * 1e-5, self.Pmin,
                               6)

    def test_Pmax(self):
        """
        Test that the ThirdBody Pmax property was properly set.
        """
        self.assertAlmostEqual(self.thirdBody.Pmax.value_si * 1e-5, self.Pmax,
                               6)

    def test_comment(self):
        """
        Test that the ThirdBody comment property was properly set.
        """
        self.assertEqual(self.thirdBody.comment, self.comment)

    def test_isPressureDependent(self):
        """
        Test the ThirdBody.isPressureDependent() method.
        """
        self.assertTrue(self.thirdBody.isPressureDependent())

    def test_getEffectivePressure(self):
        """
        Test the ThirdBody.getEffectivePressure() method.
        """
        P = 1.0
        # Test that each pure bath gas gives the correct effective pressure
        species = self.efficiencies.keys()
        for spec, eff in self.efficiencies.items():
            fractions = numpy.zeros(len(species))
            i = species.index(spec)
            fractions[i] = 1.0
            Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
            self.assertEqual(P * eff, Peff)
        # Also test a mixture of bath gases
        fractions = numpy.zeros(len(species))
        fractions[0] = 0.5
        fractions[1] = 0.5
        eff = 0.5 * self.efficiencies[species[0]] + 0.5 * self.efficiencies[
            species[1]]
        Peff = self.thirdBody.getEffectivePressure(P, species, fractions)
        self.assertEqual(P * eff, Peff)

    def test_getRateCoefficient(self):
        """
        Test the ThirdBody.getRateCoefficient() method.
        """
        Tlist = numpy.array([300, 500, 1000, 1500])
        Plist = numpy.array([1e4, 1e5, 1e6])
        Kexp = numpy.array([
            [2.83508e+08, 2.83508e+09, 2.83508e+10],
            [7.68759e+07, 7.68759e+08, 7.68759e+09],
            [4.84353e+06, 4.84353e+07, 4.84353e+08],
            [7.05740e+05, 7.05740e+06, 7.05740e+07],
        ])
        for t in range(Tlist.shape[0]):
            for p in range(Plist.shape[0]):
                Kact = self.thirdBody.getRateCoefficient(Tlist[t], Plist[p])
                self.assertAlmostEqual(Kact,
                                       Kexp[t, p],
                                       delta=1e-4 * Kexp[t, p])

    def test_pickle(self):
        """
        Test that a ThirdBody object can be successfully pickled and
        unpickled with no loss of information.
        """
        import cPickle
        thirdBody = cPickle.loads(cPickle.dumps(self.thirdBody, -1))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value,
                               thirdBody.arrheniusLow.A.value,
                               delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units,
                         thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value,
                               thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units,
                         thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value,
                               thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units,
                         thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value,
                               thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units,
                         thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value,
                               4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value,
                               4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value,
                               4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value,
                               4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        self.assertEqual(self.thirdBody.efficiencies, thirdBody.efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)

    def test_repr(self):
        """
        Test that a ThirdBody object can be successfully reconstructed
        from its repr() output with no loss of information.
        """
        thirdBody = None
        exec('thirdBody = {0!r}'.format(self.thirdBody))
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.A.value,
                               thirdBody.arrheniusLow.A.value,
                               delta=1e0)
        self.assertEqual(self.thirdBody.arrheniusLow.A.units,
                         thirdBody.arrheniusLow.A.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.n.value,
                               thirdBody.arrheniusLow.n.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.n.units,
                         thirdBody.arrheniusLow.n.units, 4)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.Ea.value,
                               thirdBody.arrheniusLow.Ea.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.Ea.units,
                         thirdBody.arrheniusLow.Ea.units)
        self.assertAlmostEqual(self.thirdBody.arrheniusLow.T0.value,
                               thirdBody.arrheniusLow.T0.value, 4)
        self.assertEqual(self.thirdBody.arrheniusLow.T0.units,
                         thirdBody.arrheniusLow.T0.units)
        self.assertAlmostEqual(self.thirdBody.Tmin.value, thirdBody.Tmin.value,
                               4)
        self.assertEqual(self.thirdBody.Tmin.units, thirdBody.Tmin.units)
        self.assertAlmostEqual(self.thirdBody.Tmax.value, thirdBody.Tmax.value,
                               4)
        self.assertEqual(self.thirdBody.Tmax.units, thirdBody.Tmax.units)
        self.assertAlmostEqual(self.thirdBody.Pmin.value, thirdBody.Pmin.value,
                               4)
        self.assertEqual(self.thirdBody.Pmin.units, thirdBody.Pmin.units)
        self.assertAlmostEqual(self.thirdBody.Pmax.value, thirdBody.Pmax.value,
                               4)
        self.assertEqual(self.thirdBody.Pmax.units, thirdBody.Pmax.units)
        self.assertEqual(self.thirdBody.efficiencies, thirdBody.efficiencies)
        self.assertEqual(self.thirdBody.comment, thirdBody.comment)

    def test_changeRate(self):
        """
        Test the ThirdBody.changeRate() method.
        """
        Tlist = numpy.array([
            300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400,
            1500
        ])
        k0list = numpy.array(
            [self.thirdBody.getRateCoefficient(T, 1e5) for T in Tlist])
        self.thirdBody.changeRate(2)
        for T, kexp in zip(Tlist, k0list):
            kact = self.thirdBody.getRateCoefficient(T, 1e5)
            self.assertAlmostEqual(2 * kexp, kact, delta=1e-6 * kexp)