Exemplo n.º 1
0
 def setUp(self):
     """
     A function run before each unit test in this class.
     """
     self.Tmin = 300.
     self.Tmax = 2000.
     self.Pmin = 0.01
     self.Pmax = 100.
     self.coeffs = numpy.array([
         [11.67723, 0.729281, -0.11984, 0.00882175],
         [-1.02669, 0.853639, -0.0323485, -0.027367],
         [-0.447011, 0.244144, 0.0559122, -0.0101723],
         [-0.128261, 0.0111596, 0.0281176, 0.00604353],
         [-0.0117034, -0.0235646, 0.00061009, 0.00401309],
         [0.0155433, -0.0136846, -0.00463048, -0.000261353],
     ])
     self.comment = """acetyl + O2 -> acetylperoxy"""
     self.chebyshev = Chebyshev(
         coeffs=self.coeffs,
         kunits="cm^3/(mol*s)",
         Tmin=(self.Tmin, "K"),
         Tmax=(self.Tmax, "K"),
         Pmin=(self.Pmin, "bar"),
         Pmax=(self.Pmax, "bar"),
         comment=self.comment,
     )
Exemplo n.º 2
0
 def test_fitToData2(self):
     """
     Test the Chebyshev.fitToData() method throws error without enough degrees of freedom.
     
     Here only 3 temperatures are given, but the polynomial desired has 6 parameters.
     """
     Tdata = numpy.array([300, 1200, 2000])
     Pdata = numpy.array([1e5, 3e5, 1e6, 3e7])
     nT = len(Tdata)
     nP = len(Pdata)
     kdata = numpy.zeros((nT, nP))
     for t in range(nT):
         for p in range(nP):
             kdata[t, p] = self.chebyshev.getRateCoefficient(
                 Tdata[t], Pdata[p])
     with self.assertRaises(KineticsError):
         Chebyshev().fitToData(Tdata,
                               Pdata,
                               kdata,
                               kunits="cm^3/(mol*s)",
                               degreeT=12,
                               degreeP=8,
                               Tmin=300,
                               Tmax=2000,
                               Pmin=0.1,
                               Pmax=10.)
Exemplo n.º 3
0
 def test_fitToData(self):
     """
     Test the Chebyshev.fitToData() method.
     """
     Tdata = numpy.array([
         300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400,
         1500, 1600, 1700, 1800, 1900, 2000
     ])
     Pdata = numpy.array([3e3, 1e4, 3e4, 1e5, 3e5, 1e6, 3e7])
     nT = len(Tdata)
     nP = len(Pdata)
     kdata = numpy.zeros((nT, nP))
     for t in range(nT):
         for p in range(nP):
             kdata[t, p] = self.chebyshev.getRateCoefficient(
                 Tdata[t], Pdata[p]) * 1e6
     chebyshev = Chebyshev().fitToData(Tdata,
                                       Pdata,
                                       kdata,
                                       kunits="cm^3/(mol*s)",
                                       degreeT=6,
                                       degreeP=4,
                                       Tmin=300,
                                       Tmax=2000,
                                       Pmin=0.1,
                                       Pmax=10.)
     for t in range(nT):
         for p in range(nP):
             kfit = chebyshev.getRateCoefficient(Tdata[t], Pdata[p]) * 1e6
             self.assertAlmostEqual(kfit,
                                    kdata[t, p],
                                    delta=1e-4 * kdata[t, p])
Exemplo n.º 4
0
    def test_mark_duplicate_reactions(self):
        """Test that we can properly mark duplicate reactions for Chemkin."""
        s1 = Species().from_smiles('CC')
        s2 = Species().from_smiles('[CH3]')
        s3 = Species().from_smiles('[OH]')
        s4 = Species().from_smiles('C[CH2]')
        s5 = Species().from_smiles('O')
        s6 = Species().from_smiles('[H]')

        # Try initializing with duplicate=False
        reaction_list = [
            Reaction(reactants=[s1], products=[s2, s2], duplicate=False, kinetics=Arrhenius()),
            Reaction(reactants=[s1], products=[s2, s2], duplicate=False, kinetics=Arrhenius()),
            Reaction(reactants=[s1, s3], products=[s4, s5], duplicate=False, kinetics=Arrhenius()),
            Reaction(reactants=[s1, s3], products=[s4, s5], duplicate=False, kinetics=Chebyshev()),
            Reaction(reactants=[s1], products=[s4, s6], duplicate=False, kinetics=Arrhenius(), reversible=False),
            Reaction(reactants=[s1], products=[s4, s6], duplicate=False, kinetics=Arrhenius(), reversible=False),
            Reaction(reactants=[s5], products=[s3, s6], duplicate=False, kinetics=Arrhenius(), reversible=False),
            Reaction(reactants=[s3, s6], products=[s5], duplicate=False, kinetics=Arrhenius(), reversible=False),
        ]

        expected_flags = [True, True, False, False, True, True, False, False]

        mark_duplicate_reactions(reaction_list)
        duplicate_flags = [rxn.duplicate for rxn in reaction_list]

        self.assertEqual(duplicate_flags, expected_flags)

        # Try initializing with duplicate=True
        reaction_list = [
            Reaction(reactants=[s1], products=[s2, s2], duplicate=True, kinetics=Arrhenius()),
            Reaction(reactants=[s1], products=[s2, s2], duplicate=True, kinetics=Arrhenius()),
            Reaction(reactants=[s1, s3], products=[s4, s5], duplicate=True, kinetics=Arrhenius()),
            Reaction(reactants=[s1, s3], products=[s4, s5], duplicate=True, kinetics=Chebyshev()),
            Reaction(reactants=[s1], products=[s4, s6], duplicate=True, kinetics=Arrhenius(), reversible=False),
            Reaction(reactants=[s1], products=[s4, s6], duplicate=True, kinetics=Arrhenius(), reversible=False),
            Reaction(reactants=[s5], products=[s3, s6], duplicate=True, kinetics=Arrhenius(), reversible=False),
            Reaction(reactants=[s3, s6], products=[s5], duplicate=True, kinetics=Arrhenius(), reversible=False),
        ]

        mark_duplicate_reactions(reaction_list)
        duplicate_flags = [rxn.duplicate for rxn in reaction_list]

        self.assertEqual(duplicate_flags, expected_flags)
Exemplo n.º 5
0
    def test_process_duplicate_reactions(self):
        """
        Test that duplicate reactions are handled correctly when
        loading a Chemkin file.
        """
        s1 = Species().from_smiles('CC')
        s2 = Species().from_smiles('[CH3]')
        s3 = Species().from_smiles('[OH]')
        s4 = Species().from_smiles('C[CH2]')
        s5 = Species().from_smiles('O')
        r1 = Reaction(reactants=[s1],
                      products=[s2, s2],
                      duplicate=False,
                      kinetics=Arrhenius())
        r2 = Reaction(reactants=[s1, s3],
                      products=[s4, s5],
                      duplicate=True,
                      kinetics=Arrhenius())
        r3 = Reaction(reactants=[s1, s3],
                      products=[s4, s5],
                      duplicate=True,
                      kinetics=Arrhenius())
        r4 = Reaction(reactants=[s1, s3],
                      products=[s4, s5],
                      duplicate=False,
                      kinetics=Arrhenius())
        r5 = LibraryReaction(reactants=[s1, s3],
                             products=[s4, s5],
                             duplicate=True,
                             kinetics=Arrhenius(),
                             library='lib1')
        r6 = LibraryReaction(reactants=[s1, s3],
                             products=[s4, s5],
                             duplicate=True,
                             kinetics=Arrhenius(),
                             library='lib2')
        r7 = LibraryReaction(reactants=[s1, s3],
                             products=[s4, s5],
                             duplicate=True,
                             kinetics=Chebyshev(),
                             library='lib1')
        r8 = LibraryReaction(reactants=[s1, s3],
                             products=[s4, s5],
                             duplicate=True,
                             kinetics=Arrhenius(),
                             library='lib1')
        r9 = LibraryReaction(
            reactants=[s1, s3],
            products=[s4, s5],
            duplicate=False,
            kinetics=MultiArrhenius(
                arrhenius=[Arrhenius(), Arrhenius()]),
            library='lib1')
        reaction_list_with_duplicate = [r1, r2, r3]
        reaction_list_with_duplicate2 = [r1, r2, r3]
        reaction_list_unmarked_duplicate = [r1, r2, r4]
        reaction_list_unequal_libraries = [r1, r5, r6]
        reaction_list_mixed_kinetics = [r1, r5, r7]
        reaction_list_mergeable = [r1, r5, r8]
        reaction_list_merged = [r1, r9]

        # Test that duplicates are not removed for non-library reactions
        _process_duplicate_reactions(reaction_list_with_duplicate)
        self.assertEqual(reaction_list_with_duplicate,
                         reaction_list_with_duplicate2)

        # Test that unmarked duplicate reactions are detected if both
        # reactions are p-dep or p-indep
        self.assertRaisesRegexp(ChemkinError,
                                'Encountered unmarked duplicate reaction',
                                _process_duplicate_reactions,
                                reaction_list_unmarked_duplicate)

        # Test that unequal libraries are recognized
        self.assertRaisesRegexp(ChemkinError, 'from different libraries',
                                _process_duplicate_reactions,
                                reaction_list_unequal_libraries)

        # Test that an error is raised for reactions with kinetics
        # that cannot be merged
        self.assertRaisesRegexp(ChemkinError,
                                'Mixed kinetics for duplicate reaction',
                                _process_duplicate_reactions,
                                reaction_list_mixed_kinetics)

        # Test that duplicate library reactions are merged successfully
        _process_duplicate_reactions(reaction_list_mergeable)
        self.assertEqual(len(reaction_list_mergeable),
                         len(reaction_list_merged))
        self.assertEqual(reaction_list_mergeable[0], reaction_list_merged[0])
        rtest = reaction_list_mergeable[1]
        rtrue = reaction_list_merged[1]
        self.assertEqual(rtest.reactants, rtrue.reactants)
        self.assertEqual(rtest.products, rtrue.products)
        self.assertEqual(rtest.duplicate, rtrue.duplicate)
        self.assertEqual(rtest.library, rtrue.library)
        self.assertTrue(isinstance(rtest.kinetics, MultiArrhenius))
        self.assertTrue(
            all(isinstance(k, Arrhenius) for k in rtest.kinetics.arrhenius))
Exemplo n.º 6
0
    def test_is_identical_to(self):
        """
        Test the Chebyshev.is_identical_to() method.
        """
        # Trivial case, compare to a KineticsModel
        from rmgpy.kinetics.model import KineticsModel
        self.assertFalse(self.chebyshev.is_identical_to(KineticsModel()))

        # Compare to identical Chebyshev
        new_chebyshev = Chebyshev(
            coeffs=self.coeffs,
            kunits="cm^3/(mol*s)",
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertTrue(self.chebyshev.is_identical_to(new_chebyshev))

        # Compare to Chebyshev with different Tmin/Tmax
        new_chebyshev = Chebyshev(
            coeffs=self.coeffs,
            kunits="cm^3/(mol*s)",
            Tmin=(200, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertFalse(self.chebyshev.is_identical_to(new_chebyshev))

        new_chebyshev = Chebyshev(
            coeffs=self.coeffs,
            kunits="cm^3/(mol*s)",
            Tmin=(self.Tmin, "K"),
            Tmax=(2500, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertFalse(self.chebyshev.is_identical_to(new_chebyshev))

        # Compare to Chebyshev with different degreeT/degreeP
        new_chebyshev = Chebyshev(
            coeffs=self.coeffs[0:-1, :],  # Remove one T dimension
            kunits="cm^3/(mol*s)",
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertFalse(self.chebyshev.is_identical_to(new_chebyshev))

        new_chebyshev = Chebyshev(
            coeffs=self.coeffs[:, 0:-1],  # Remove one P dimension
            kunits="cm^3/(mol*s)",
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertFalse(self.chebyshev.is_identical_to(new_chebyshev))

        # Compare to Chebyshev with different units
        new_chebyshev = Chebyshev(
            coeffs=self.coeffs,
            kunits="m^3/(mol*s)",
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertFalse(self.chebyshev.is_identical_to(new_chebyshev))

        # Compare to Chebyshev with slightly different coefficients
        new_chebyshev = Chebyshev(
            coeffs=np.copy(self.coeffs) * 0.01,
            kunits="cm^3/(mol*s)",
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            Pmin=(self.Pmin, "bar"),
            Pmax=(self.Pmax, "bar"),
            comment=self.comment,
        )
        self.assertFalse(self.chebyshev.is_identical_to(new_chebyshev))