Ejemplo 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,
     )
Ejemplo n.º 2
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])
Ejemplo n.º 3
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,
     )
Ejemplo n.º 4
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])
Ejemplo n.º 5
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.)
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
class TestChebyshev(unittest.TestCase):
    """
    Contains unit tests of the Chebyshev class.
    """
    
    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,
        )
        
    def test_coeffs(self):
        """
        Test that the Chebyshev coeffs property was properly set.
        """
        self.assertEqual(self.chebyshev.coeffs.value.shape, self.coeffs.shape)
        for i in range(self.chebyshev.coeffs.value.shape[0]):
            for j in range(self.chebyshev.coeffs.value.shape[1]):
                C0 = float(self.coeffs[i,j])
                C = float(self.chebyshev.coeffs.value_si[i,j])
                if i == 0 and j == 0: C0 -= 6 # Unit conversion from cm^3/(mol*s) to m^3/(mol*s)
                self.assertAlmostEqual(C0, C, delta=1e-6*C0)
        
    def test_Tmin(self):
        """
        Test that the Chebyshev Tmin property was properly set.
        """
        self.assertAlmostEqual(self.chebyshev.Tmin.value_si, self.Tmin, 6)
        
    def test_Tmax(self):
        """
        Test that the Chebyshev Tmax property was properly set.
        """
        self.assertAlmostEqual(self.chebyshev.Tmax.value_si, self.Tmax, 6)

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

    def test_isPressureDependent(self):
        """
        Test the Chebyshev.isPressureDependent() method.
        
        """
        self.assertTrue(self.chebyshev.isPressureDependent())
    
    def test_getRateCoefficient(self):
        """
        Test the Chebyshev.getRateCoefficient() method.
        """
        Tlist = numpy.array([300,500,1000,1500])
        Plist = numpy.array([1e4,1e5,1e6])
        Kexp = numpy.array([
            [2.29100e+06, 2.58452e+06, 2.57204e+06],
            [1.10198e+06, 2.04037e+06, 2.57428e+06],
            [4.37919e+04, 2.36481e+05, 8.57727e+05],
            [5.20144e+03, 4.10123e+04, 2.50401e+05],
        ])
        for t in range(Tlist.shape[0]):
            for p in range(Plist.shape[0]):
                Kact = self.chebyshev.getRateCoefficient(Tlist[t], Plist[p])
                self.assertAlmostEqual(Kact / Kexp[t,p], 1.0, 4, '{0} != {1} within 4 places'.format(Kexp[t,p], Kact))
        
    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])
        
    def test_pickle(self):
        """
        Test that a Chebyshev object can be pickled and unpickled with no loss
        of information.
        """
        import cPickle
        chebyshev = cPickle.loads(cPickle.dumps(self.chebyshev,-1))
        self.assertEqual(self.chebyshev.coeffs.value.shape[0], chebyshev.coeffs.value.shape[0])
        self.assertEqual(self.chebyshev.coeffs.value.shape[1], chebyshev.coeffs.value.shape[1])
        for i in range(self.chebyshev.coeffs.value.shape[0]):
            for j in range(self.chebyshev.coeffs.value.shape[1]):
                C0 = self.chebyshev.coeffs.value_si[i,j]
                C = chebyshev.coeffs.value_si[i,j]
                self.assertAlmostEqual(C0, C, delta=1e-4*C0)
        self.assertAlmostEqual(self.chebyshev.Tmin.value, chebyshev.Tmin.value, 4)
        self.assertEqual(self.chebyshev.Tmin.units, chebyshev.Tmin.units)
        self.assertAlmostEqual(self.chebyshev.Tmax.value, chebyshev.Tmax.value, 4)
        self.assertEqual(self.chebyshev.Tmax.units, chebyshev.Tmax.units)
        self.assertAlmostEqual(self.chebyshev.Pmin.value, chebyshev.Pmin.value, 4)
        self.assertEqual(self.chebyshev.Pmin.units, chebyshev.Pmin.units)
        self.assertAlmostEqual(self.chebyshev.Pmax.value, chebyshev.Pmax.value, 4)
        self.assertEqual(self.chebyshev.Pmax.units, chebyshev.Pmax.units)
        self.assertEqual(self.chebyshev.comment, chebyshev.comment)

    def test_repr(self):
        """
        Test that a Chebyshev object can be reconstructed from its repr()
        output with no loss of information.
        """
        chebyshev = None
        exec('chebyshev = {0!r}'.format(self.chebyshev))
        self.assertEqual(self.chebyshev.coeffs.value.shape[0], chebyshev.coeffs.value.shape[0])
        self.assertEqual(self.chebyshev.coeffs.value.shape[1], chebyshev.coeffs.value.shape[1])
        for i in range(self.chebyshev.coeffs.value.shape[0]):
            for j in range(self.chebyshev.coeffs.value.shape[1]):
                C0 = self.chebyshev.coeffs.value[i,j]
                C = chebyshev.coeffs.value[i,j]
                self.assertAlmostEqual(C0, C, delta=1e-4*C0)
        self.assertAlmostEqual(self.chebyshev.Tmin.value, chebyshev.Tmin.value, 4)
        self.assertEqual(self.chebyshev.Tmin.units, chebyshev.Tmin.units)
        self.assertAlmostEqual(self.chebyshev.Tmax.value, chebyshev.Tmax.value, 4)
        self.assertEqual(self.chebyshev.Tmax.units, chebyshev.Tmax.units)
        self.assertAlmostEqual(self.chebyshev.Pmin.value, chebyshev.Pmin.value, 4)
        self.assertEqual(self.chebyshev.Pmin.units, chebyshev.Pmin.units)
        self.assertAlmostEqual(self.chebyshev.Pmax.value, chebyshev.Pmax.value, 4)
        self.assertEqual(self.chebyshev.Pmax.units, chebyshev.Pmax.units)
        self.assertEqual(self.chebyshev.comment, chebyshev.comment)
        
    def test_changeRate(self):
        """
        Test the Chebyshev.changeRate() method.
        """
        Tlist = numpy.array([300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500])
        k0list = numpy.array([self.chebyshev.getRateCoefficient(T,1e5) for T in Tlist])
        self.chebyshev.changeRate(2)
        for T, kexp in zip(Tlist, k0list):
            kact = self.chebyshev.getRateCoefficient(T,1e5)
            self.assertAlmostEqual(2*kexp, kact, delta=1e-6*kexp)
Ejemplo n.º 8
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))
Ejemplo n.º 9
0
class TestChebyshev(unittest.TestCase):
    """
    Contains unit tests of the Chebyshev class.
    """
    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,
        )

    def test_coeffs(self):
        """
        Test that the Chebyshev coeffs property was properly set.
        """
        self.assertEqual(self.chebyshev.coeffs.value.shape, self.coeffs.shape)
        for i in range(self.chebyshev.coeffs.value.shape[0]):
            for j in range(self.chebyshev.coeffs.value.shape[1]):
                C0 = float(self.coeffs[i, j])
                C = float(self.chebyshev.coeffs.value_si[i, j])
                if i == 0 and j == 0:
                    C0 -= 6  # Unit conversion from cm^3/(mol*s) to m^3/(mol*s)
                self.assertAlmostEqual(C0, C, delta=1e-6 * C0)

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

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

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

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

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

    def test_isPressureDependent(self):
        """
        Test the Chebyshev.isPressureDependent() method.
        
        """
        self.assertTrue(self.chebyshev.isPressureDependent())

    def test_getRateCoefficient(self):
        """
        Test the Chebyshev.getRateCoefficient() method.
        """
        Tlist = numpy.array([300, 500, 1000, 1500])
        Plist = numpy.array([1e4, 1e5, 1e6])
        Kexp = numpy.array([
            [2.29100e+06, 2.58452e+06, 2.57204e+06],
            [1.10198e+06, 2.04037e+06, 2.57428e+06],
            [4.37919e+04, 2.36481e+05, 8.57727e+05],
            [5.20144e+03, 4.10123e+04, 2.50401e+05],
        ])
        for t in range(Tlist.shape[0]):
            for p in range(Plist.shape[0]):
                Kact = self.chebyshev.getRateCoefficient(Tlist[t], Plist[p])
                self.assertAlmostEqual(
                    Kact / Kexp[t, p], 1.0, 4,
                    '{0} != {1} within 4 places'.format(Kexp[t, p], Kact))

    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])

    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.)

    def test_pickle(self):
        """
        Test that a Chebyshev object can be pickled and unpickled with no loss
        of information.
        """
        import cPickle
        chebyshev = cPickle.loads(cPickle.dumps(self.chebyshev, -1))
        self.assertEqual(self.chebyshev.coeffs.value.shape[0],
                         chebyshev.coeffs.value.shape[0])
        self.assertEqual(self.chebyshev.coeffs.value.shape[1],
                         chebyshev.coeffs.value.shape[1])
        for i in range(self.chebyshev.coeffs.value.shape[0]):
            for j in range(self.chebyshev.coeffs.value.shape[1]):
                C0 = self.chebyshev.coeffs.value_si[i, j]
                C = chebyshev.coeffs.value_si[i, j]
                self.assertAlmostEqual(C0, C, delta=1e-4 * C0)
        self.assertAlmostEqual(self.chebyshev.Tmin.value, chebyshev.Tmin.value,
                               4)
        self.assertEqual(self.chebyshev.Tmin.units, chebyshev.Tmin.units)
        self.assertAlmostEqual(self.chebyshev.Tmax.value, chebyshev.Tmax.value,
                               4)
        self.assertEqual(self.chebyshev.Tmax.units, chebyshev.Tmax.units)
        self.assertAlmostEqual(self.chebyshev.Pmin.value, chebyshev.Pmin.value,
                               4)
        self.assertEqual(self.chebyshev.Pmin.units, chebyshev.Pmin.units)
        self.assertAlmostEqual(self.chebyshev.Pmax.value, chebyshev.Pmax.value,
                               4)
        self.assertEqual(self.chebyshev.Pmax.units, chebyshev.Pmax.units)
        self.assertEqual(self.chebyshev.comment, chebyshev.comment)

    def test_repr(self):
        """
        Test that a Chebyshev object can be reconstructed from its repr()
        output with no loss of information.
        """
        chebyshev = None
        exec('chebyshev = {0!r}'.format(self.chebyshev))
        self.assertEqual(self.chebyshev.coeffs.value.shape[0],
                         chebyshev.coeffs.value.shape[0])
        self.assertEqual(self.chebyshev.coeffs.value.shape[1],
                         chebyshev.coeffs.value.shape[1])
        for i in range(self.chebyshev.coeffs.value.shape[0]):
            for j in range(self.chebyshev.coeffs.value.shape[1]):
                C0 = self.chebyshev.coeffs.value[i, j]
                C = chebyshev.coeffs.value[i, j]
                self.assertAlmostEqual(C0, C, delta=1e-4 * C0)
        self.assertAlmostEqual(self.chebyshev.Tmin.value, chebyshev.Tmin.value,
                               4)
        self.assertEqual(self.chebyshev.Tmin.units, chebyshev.Tmin.units)
        self.assertAlmostEqual(self.chebyshev.Tmax.value, chebyshev.Tmax.value,
                               4)
        self.assertEqual(self.chebyshev.Tmax.units, chebyshev.Tmax.units)
        self.assertAlmostEqual(self.chebyshev.Pmin.value, chebyshev.Pmin.value,
                               4)
        self.assertEqual(self.chebyshev.Pmin.units, chebyshev.Pmin.units)
        self.assertAlmostEqual(self.chebyshev.Pmax.value, chebyshev.Pmax.value,
                               4)
        self.assertEqual(self.chebyshev.Pmax.units, chebyshev.Pmax.units)
        self.assertEqual(self.chebyshev.comment, chebyshev.comment)

    def test_changeRate(self):
        """
        Test the Chebyshev.changeRate() method.
        """
        Tlist = numpy.array([
            300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400,
            1500
        ])
        k0list = numpy.array(
            [self.chebyshev.getRateCoefficient(T, 1e5) for T in Tlist])
        self.chebyshev.changeRate(2)
        for T, kexp in zip(Tlist, k0list):
            kact = self.chebyshev.getRateCoefficient(T, 1e5)
            self.assertAlmostEqual(2 * kexp, kact, delta=1e-6 * kexp)
Ejemplo n.º 10
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))