예제 #1
0
파일: nasaTest.py 프로젝트: yplitw/RMG-Py
 def setUp(self):
     """
     A function run before each unit test in this class.
     """
     self.coeffs_low = [
         4.03055, -0.00214171, 4.90611e-05, -5.99027e-08, 2.38945e-11,
         -11257.6, 3.5613
     ]
     self.coeffs_high = [
         -0.307954, 0.0245269, -1.2413e-05, 3.07724e-09, -3.01467e-13,
         -10693, 22.628
     ]
     self.Tmin = 300.
     self.Tmax = 3000.
     self.Tint = 650.73
     self.E0 = -782292.  # J/mol.
     self.comment = "C2H6"
     self.nasa = NASA(
         polynomials=[
             NASAPolynomial(coeffs=self.coeffs_low,
                            Tmin=(self.Tmin, "K"),
                            Tmax=(self.Tint, "K")),
             NASAPolynomial(coeffs=self.coeffs_high,
                            Tmin=(self.Tint, "K"),
                            Tmax=(self.Tmax, "K")),
         ],
         Tmin=(self.Tmin, "K"),
         Tmax=(self.Tmax, "K"),
         E0=(self.E0, "J/mol"),
         comment=self.comment,
     )
예제 #2
0
 def test_nasa_as_dict_minimal(self):
     """
     Test that NASA.as_dict does not contain empty, optional attributes
     """
     nasa_dict = NASA().as_dict()
     keys = list(nasa_dict.keys())
     self.assertNotIn('Tmin', keys)
     self.assertNotIn('Tmax', keys)
     self.assertNotIn('E0', keys)
     self.assertNotIn('Cp0', keys)
     self.assertNotIn('CpInf', keys)
     self.assertNotIn('label', keys)
     self.assertNotIn('comment', keys)
예제 #3
0
 def test_species_atomic_nasa_polynomial(self):
     """
     Test loading a atom with NASA polynomials
     """
     label0 = "H(1)"
     kwargs = {
         "structure":
         SMILES('[H]'),
         "thermo":
         NASA(polynomials=[
             NASAPolynomial(coeffs=[2.5, 0, 0, 0, 0, 25473.7, -0.446683],
                            Tmin=(200, 'K'),
                            Tmax=(1000, 'K')),
             NASAPolynomial(coeffs=[2.5, 0, 0, 0, 0, 25473.7, -0.446683],
                            Tmin=(1000, 'K'),
                            Tmax=(6000, 'K'))
         ],
              Tmin=(200, 'K'),
              Tmax=(6000, 'K'),
              comment="""Thermo library: FFCM1(-)"""),
         "energyTransferModel":
         SingleExponentialDown(alpha0=(3.5886, 'kJ/mol'),
                               T0=(300, 'K'),
                               n=0.85)
     }
     spc0 = species(label0, **kwargs)
     self.assertEqual(spc0.label, label0)
     self.assertEqual(spc0.smiles, '[H]')
     self.assertTrue(spc0.has_statmech())
     self.assertEqual(spc0.thermo, kwargs['thermo'])
예제 #4
0
    def test_generate_isotopomers(self):
        """
        Test that the generation of isotopomers with N isotopes works.
        """
        from rmgpy.thermo.nasa import NASAPolynomial, NASA

        spc = Species().from_smiles('CC')

        polynomial = NASAPolynomial(coeffs=[1., 1., 1., 1., 1., 1., 1.],
                                    Tmin=(200, 'K'), Tmax=(1600, 'K'), E0=(1., 'kJ/mol'),
                                    comment='made up thermo')

        spc.thermo = NASA(polynomials=[polynomial], Tmin=(200, 'K'),
                          Tmax=(1600, 'K'), E0=(1., 'kJ/mol'),
                          comment='made up thermo')

        spcs = generate_isotopomers(spc, 0)
        self.assertEquals(len(spcs), 0)

        spcs = generate_isotopomers(spc)
        self.assertEquals(len(spcs), 1)

        spcs = generate_isotopomers(spc, 2)
        self.assertEquals(len(spcs), 2)

        spcs = generate_isotopomers(spc, 3)
        self.assertEquals(len(spcs), 2)
예제 #5
0
 def test_species_polyatomic_nasa_polynomial(self):
     """
     Test loading a species with NASA polynomials
     """
     label0 = "benzyl"
     kwargs = {
         "structure":
         SMILES('[c]1ccccc1'),
         "thermo":
         NASA(polynomials=[
             NASAPolynomial(coeffs=[
                 2.78632, 0.00784632, 7.97887e-05, -1.11617e-07,
                 4.39429e-11, 39695, 11.5114
             ],
                            Tmin=(100, 'K'),
                            Tmax=(943.73, 'K')),
             NASAPolynomial(coeffs=[
                 13.2455, 0.0115667, -2.49996e-06, 4.66496e-10,
                 -4.12376e-14, 35581.1, -49.6793
             ],
                            Tmin=(943.73, 'K'),
                            Tmax=(5000, 'K'))
         ],
              Tmin=(100, 'K'),
              Tmax=(5000, 'K'),
              comment="""Thermo library: Fulvene_H + radical(CbJ)"""),
         "energyTransferModel":
         SingleExponentialDown(alpha0=(3.5886, 'kJ/mol'),
                               T0=(300, 'K'),
                               n=0.85)
     }
     spc0 = species(label0, **kwargs)
     self.assertEqual(spc0.label, label0)
     self.assertTrue(spc0.has_statmech())
     self.assertEqual(spc0.thermo, kwargs['thermo'])
예제 #6
0
    def test_correct_entropy(self):
        """
        Tests that correctEntropy effectively makes the isotopomer have the same
        thermo as isotopeless with the change in entropy corresponding to the
        symmetry difference.

        This example compares propane to a asymmetrically labeled propane.
        """
        from rmgpy.thermo.nasa import NASA, NASAPolynomial
        from copy import deepcopy
        from rmgpy import constants
        propanei = Species().from_adjacency_list("""
1  C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2  C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3  C u0 p0 c0 i13 {2,S} {9,S} {10,S} {11,S}
4  H u0 p0 c0 {1,S}
5  H u0 p0 c0 {1,S}
6  H u0 p0 c0 {1,S}
7  H u0 p0 c0 {2,S}
8  H u0 p0 c0 {2,S}
9  H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
""")
        propane = Species().from_adjacency_list("""
1  C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2  C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3  C u0 p0 c0 {2,S} {9,S} {10,S} {11,S}
4  H u0 p0 c0 {1,S}
5  H u0 p0 c0 {1,S}
6  H u0 p0 c0 {1,S}
7  H u0 p0 c0 {2,S}
8  H u0 p0 c0 {2,S}
9  H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
""")

        # get thermo of propane
        original_thermo = NASA(polynomials=[NASAPolynomial(
            coeffs=[1.10564, 0.0315339, -1.48274e-05, 3.39621e-09, -2.97953e-13, -14351.9, 18.775],
            Tmin=(100, 'K'), Tmax=(3370.6, 'K')), NASAPolynomial(
            coeffs=[-1.45473, 0.0241202, -6.87667e-06, 9.03634e-10, -4.48389e-14, -6688.59, 43.0459],
            Tmin=(3370.6, 'K'), Tmax=(5000, 'K'))], Tmin=(100, 'K'), Tmax=(5000, 'K'),
            comment="""Thermo group additivity estimation: group(Cs-CsCsHH) + gauche(Cs(CsCsRR)) + other(R) + group(Cs-CsHHH) + gauche(Cs(Cs(CsRR)RRR)) + other(R) + group(Cs-CsHHH) + gauche(Cs(Cs(CsRR)RRR)) + other(R)""")
        propane.thermo = deepcopy(original_thermo)
        propanei.thermo = deepcopy(original_thermo)

        correct_entropy(propanei, propane)

        self.assertAlmostEqual(propane.get_enthalpy(298), propanei.get_enthalpy(298))
        self.assertAlmostEqual(propanei.get_entropy(298) - propane.get_entropy(298), constants.R * np.log(2))
예제 #7
0
    def test_make_nasa(self):
        """
        Test that a NASA object can be reconstructed from a dictionary (also test NASAPolynomial by extension)
        """
        nasa_dict = self.nasa.as_dict()
        new_nasa = NASA.__new__(NASA)
        class_dictionary = {'ScalarQuantity': ScalarQuantity,
                            'np_array': np.array,
                            'NASA': NASA,
                            'NASAPolynomial': NASAPolynomial,
                            }

        new_nasa.make_object(nasa_dict, class_dictionary)
 def setUp(self):
     """
     A function run before each unit test in this class.
     """
     octyl_pri = Species(label="", thermo=NASA(polynomials=[
         NASAPolynomial(coeffs=[-0.772759,0.093255,-5.84447e-05,1.8557e-08,-2.37127e-12,-3926.9,37.6131], Tmin=(298,'K'), Tmax=(1390,'K')),
         NASAPolynomial(coeffs=[25.051,0.036948,-1.25765e-05,1.94628e-09,-1.12669e-13,-13330.1,-102.557], Tmin=(1390,'K'), Tmax=(5000,'K'))
         ],
         Tmin=(298,'K'), Tmax=(5000,'K'), Cp0=(33.2579,'J/(mol*K)'), CpInf=(577.856,'J/(mol*K)'),
         comment="""Thermo library: JetSurF0.2"""), molecule=[Molecule(SMILES="[CH2]CCCCCCC")])
     octyl_sec = Species(label="", thermo=NASA(polynomials=[
         NASAPolynomial(coeffs=[-0.304233,0.0880077,-4.90743e-05,1.21858e-08,-8.87773e-13,-5237.93,36.6583], Tmin=(298,'K'), Tmax=(1383,'K')),
         NASAPolynomial(coeffs=[24.9044,0.0366394,-1.2385e-05,1.90835e-09,-1.10161e-13,-14713.5,-101.345], Tmin=(1383,'K'), Tmax=(5000,'K'))
         ],
         Tmin=(298,'K'), Tmax=(5000,'K'), Cp0=(33.2579,'J/(mol*K)'), CpInf=(577.856,'J/(mol*K)'),
         comment="""Thermo library: JetSurF0.2"""), molecule=[Molecule(SMILES="CC[CH]CCCCC")])
     ethane = Species(label="", thermo=ThermoData(
         Tdata=([300,400,500,600,800,1000,1500],'K'), Cpdata=([10.294,12.643,14.933,16.932,20.033,22.438,26.281],'cal/(mol*K)'), H298=(12.549,'kcal/mol'), S298=(52.379,'cal/(mol*K)'),
         Cp0=(33.2579,'J/(mol*K)'), CpInf=(133.032,'J/(mol*K)'), comment="""Thermo library: CH"""),
         molecule=[Molecule(SMILES="C=C")])
     decyl = Species(label="", thermo=NASA(polynomials=[
         NASAPolynomial(coeffs=[-1.31358,0.117973,-7.51843e-05,2.43331e-08,-3.17523e-12,-9689.68,43.501], Tmin=(298,'K'), Tmax=(1390,'K')),
         NASAPolynomial(coeffs=[31.5697,0.0455818,-1.54995e-05,2.39711e-09,-1.3871e-13,-21573.8,-134.709], Tmin=(1390,'K'), Tmax=(5000,'K'))
         ],
         Tmin=(298,'K'), Tmax=(5000,'K'), Cp0=(33.2579,'J/(mol*K)'), CpInf=(719.202,'J/(mol*K)'),
         comment="""Thermo library: JetSurF0.2"""), molecule=[Molecule(SMILES="[CH2]CCCCCCCCC")])
     self.database = SolvationDatabase()
     self.database.load(os.path.join(settings['database.directory'], 'solvation'))
     self.solvent = 'octane'
     diffusionLimiter.enable(self.database.getSolventData(self.solvent), self.database)
     self.T = 298
     self.uni_reaction = Reaction(reactants=[octyl_pri], products=[octyl_sec])
     self.uni_reaction.kinetics = Arrhenius(A=(2.0, '1/s'), n=0, Ea=(0,'kJ/mol'))
     self.bi_uni_reaction = Reaction(reactants=[octyl_pri, ethane], products=[decyl])
     self.bi_uni_reaction.kinetics = Arrhenius(A=(1.0E-22, 'cm^3/molecule/s'), n=0, Ea=(0,'kJ/mol'))
     self.intrinsic_rates = {
         self.uni_reaction: self.uni_reaction.kinetics.getRateCoefficient(self.T, P=100e5),
         self.bi_uni_reaction: self.bi_uni_reaction.kinetics.getRateCoefficient(self.T, P=100e5),
         }
예제 #9
0
파일: nasaTest.py 프로젝트: jbarlow3/RMG-Py
 def setUp(self):
     """
     A function run before each unit test in this class.
     """
     self.coeffs_low = [4.03055,-0.00214171,4.90611e-05,-5.99027e-08,2.38945e-11,-11257.6,3.5613]
     self.coeffs_high = [-0.307954,0.0245269,-1.2413e-05,3.07724e-09,-3.01467e-13,-10693,22.628]
     self.Tmin = 300.
     self.Tmax = 3000.
     self.Tint = 650.73
     self.comment = "C2H6"
     self.nasa = NASA(
         polynomials = [
             NASAPolynomial(coeffs=self.coeffs_low, Tmin=(self.Tmin,"K"), Tmax=(self.Tint,"K")),
             NASAPolynomial(coeffs=self.coeffs_high, Tmin=(self.Tint,"K"), Tmax=(self.Tmax,"K")),
         ],
         Tmin = (self.Tmin,"K"),
         Tmax = (self.Tmax,"K"),
         comment = self.comment,
     )
예제 #10
0
파일: nasaTest.py 프로젝트: yplitw/RMG-Py
class TestNASA(unittest.TestCase):
    """
    Contains unit tests of the MultiNASA class.
    """
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.coeffs_low = [
            4.03055, -0.00214171, 4.90611e-05, -5.99027e-08, 2.38945e-11,
            -11257.6, 3.5613
        ]
        self.coeffs_high = [
            -0.307954, 0.0245269, -1.2413e-05, 3.07724e-09, -3.01467e-13,
            -10693, 22.628
        ]
        self.Tmin = 300.
        self.Tmax = 3000.
        self.Tint = 650.73
        self.E0 = -782292.  # J/mol.
        self.comment = "C2H6"
        self.nasa = NASA(
            polynomials=[
                NASAPolynomial(coeffs=self.coeffs_low,
                               Tmin=(self.Tmin, "K"),
                               Tmax=(self.Tint, "K")),
                NASAPolynomial(coeffs=self.coeffs_high,
                               Tmin=(self.Tint, "K"),
                               Tmax=(self.Tmax, "K")),
            ],
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            E0=(self.E0, "J/mol"),
            comment=self.comment,
        )

    def tearDown(self):
        """
        Reset the database & liquid parameters for solution
        """
        import rmgpy.data.rmg
        rmgpy.data.rmg.database = None

        from rmgpy.rmg.model import Species as DifferentSpecies
        DifferentSpecies.solventData = None
        DifferentSpecies.solventName = None
        DifferentSpecies.solventStructure = None
        DifferentSpecies.solventViscosity = None

    def test_polyLow(self):
        """
        Test that the NASA low-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly1.coeffs), len(self.coeffs_low))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, self.coeffs_low):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value_si, self.Tmin)
        self.assertEqual(self.nasa.poly1.Tmax.value_si, self.Tint)

    def test_polyHigh(self):
        """
        Test that the NASA high-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly2.coeffs), len(self.coeffs_high))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, self.coeffs_high):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value_si, self.Tint)
        self.assertEqual(self.nasa.poly2.Tmax.value_si, self.Tmax)

    def test_Tmin(self):
        """
        Test that the NASA Tmin property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.Tmin.value_si / self.Tmin, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmin, self.Tmin))

    def test_Tmax(self):
        """
        Test that the NASA Tmax property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.Tmax.value_si / self.Tmax, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

    def test_E0(self):
        """
        Test that the NASA E0 property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.E0.value_si / self.E0, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

    def test_Comment(self):
        """
        Test that the NASA comment property was properly set.
        """
        self.assertEqual(self.nasa.comment, self.comment)

    def test_isTemperatureValid(self):
        """
        Test the NASA.isTemperatureValid() method.
        """
        Tdata = [200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000]
        validdata = [
            False, True, True, True, True, True, True, True, True, True
        ]
        for T, valid in zip(Tdata, validdata):
            valid0 = self.nasa.isTemperatureValid(T)
            self.assertEqual(valid0, valid)

    def test_getHeatCapacity(self):
        """
        Test the NASA.getHeatCapacity() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        Cpexplist = numpy.array([
            7.80157, 10.5653, 12.8213, 14.5817, 15.9420, 16.9861, 17.78645,
            18.4041, 18.8883
        ]) * constants.R
        for T, Cpexp in zip(Tlist, Cpexplist):
            Cpact = self.nasa.getHeatCapacity(T)
            self.assertAlmostEqual(Cpexp / Cpact, 1.0, 4,
                                   '{0} != {1}'.format(Cpexp, Cpact))

    def test_getEnthalpy(self):
        """
        Test the NASA.getEnthalpy() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        Hexplist = numpy.array([
            -22.7613, -12.1027, -6.14236, -2.16615, 0.743456, 2.99256, 4.79397,
            6.27334, 7.51156
        ]) * constants.R * Tlist
        for T, Hexp in zip(Tlist, Hexplist):
            Hact = self.nasa.getEnthalpy(T)
            self.assertAlmostEqual(Hexp / Hact, 1.0, 3,
                                   '{0} != {1}'.format(Hexp, Hact))

    def test_getEntropy(self):
        """
        Test the NASA.getEntropy() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        Sexplist = numpy.array([
            29.6534, 33.3516, 36.7131, 39.7715, 42.5557, 45.0952, 47.4179,
            49.5501, 51.5152
        ]) * constants.R
        for T, Sexp in zip(Tlist, Sexplist):
            Sact = self.nasa.getEntropy(T)
            self.assertAlmostEqual(Sexp / Sact, 1.0, 4,
                                   '{0} != {1}'.format(Sexp, Sact))

    def test_getFreeEnergy(self):
        """
        Test the NASA.getFreeEnergy() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        for T in Tlist:
            Gexp = self.nasa.getEnthalpy(T) - T * self.nasa.getEntropy(T)
            Gact = self.nasa.getFreeEnergy(T)
            self.assertAlmostEqual(Gexp / Gact, 1.0, 4,
                                   '{0} != {1}'.format(Gexp, Gact))

    def test_pickle(self):
        """
        Test that a NASA object can be pickled and unpickled with no loss of
        information.
        """
        import cPickle
        nasa = cPickle.loads(cPickle.dumps(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_repr(self):
        """
        Test that a NASA object can be reconstructed from its repr() output
        with no loss of information.
        """
        nasa = None
        exec('nasa = {0!r}'.format(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_toCantera(self):
        """
        Test that conversion to a Cantera NasaPoly2 object works
        """
        nasapoly2 = self.nasa.toCantera()
        # NasaPoly2 units use J/kmol rather than J/mol
        self.assertAlmostEqual(self.nasa.getEnthalpy(900),
                               nasapoly2.h(900) / 1000, 1)
        self.assertAlmostEqual(self.nasa.getEntropy(700),
                               nasapoly2.s(700) / 1000, 1)

    def testToNASA(self):
        """
        Test if the entropy computed from other thermo implementations is close to what NASA computes.
        """

        from rmgpy import settings
        from rmgpy.data.rmg import RMGDatabase, database
        from rmgpy.species import Species

        # Load databases
        database = RMGDatabase()
        database.loadThermo(os.path.join(settings['database.directory'],
                                         'thermo'),
                            thermoLibraries=['Narayanaswamy'])
        database.loadSolvation(
            os.path.join(settings['database.directory'], 'solvation'))

        spc = Species().fromSMILES('CC')
        spc.getThermoData()

        T = 1350.  # not 298K!

        # nasa to thermodata
        nasa = spc.thermo
        Snasa = nasa.getEntropy(T)

        td = nasa.toThermoData()
        Std = td.getEntropy(T)

        self.assertAlmostEqual(Snasa, Std, -1)
        self.assertEqual(td.comment, nasa.comment)

        # thermodata to nasa
        nasa = td.toNASA(Tmin=100.0, Tmax=5000.0, Tint=1000.0)
        Snasa = nasa.getEntropy(T)

        self.assertAlmostEqual(Snasa, Std, -1)
        self.assertEqual(td.comment, nasa.comment)

        # wilhoit to nasa
        wilhoit = nasa.toWilhoit()
        nasa = wilhoit.toNASA(Tmin=100.0, Tmax=5000.0, Tint=1000.0)
        Snasa = nasa.getEntropy(T)

        self.assertAlmostEqual(Snasa, Std, -1)
        self.assertEqual(wilhoit.comment, nasa.comment)
예제 #11
0
class TestNASA(unittest.TestCase):
    """
    Contains unit tests of the MultiNASA class.
    """

    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.coeffs_low = [4.03055,-0.00214171,4.90611e-05,-5.99027e-08,2.38945e-11,-11257.6,3.5613]
        self.coeffs_high = [-0.307954,0.0245269,-1.2413e-05,3.07724e-09,-3.01467e-13,-10693,22.628]
        self.Tmin = 300.
        self.Tmax = 3000.
        self.Tint = 650.73
        self.E0 = -782292. # J/mol.
        self.comment = "C2H6"
        self.nasa = NASA(
            polynomials = [
                NASAPolynomial(coeffs=self.coeffs_low, Tmin=(self.Tmin,"K"), Tmax=(self.Tint,"K")),
                NASAPolynomial(coeffs=self.coeffs_high, Tmin=(self.Tint,"K"), Tmax=(self.Tmax,"K")),
            ],
            Tmin = (self.Tmin,"K"),
            Tmax = (self.Tmax,"K"),
            E0 = (self.E0, "J/mol"),
            comment = self.comment,
        )
    
    def test_polyLow(self):
        """
        Test that the NASA low-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly1.coeffs), len(self.coeffs_low))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, self.coeffs_low):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value_si, self.Tmin)
        self.assertEqual(self.nasa.poly1.Tmax.value_si, self.Tint)
    
    def test_polyHigh(self):
        """
        Test that the NASA high-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly2.coeffs), len(self.coeffs_high))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, self.coeffs_high):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value_si, self.Tint)
        self.assertEqual(self.nasa.poly2.Tmax.value_si, self.Tmax)
    
    def test_Tmin(self):
        """
        Test that the NASA Tmin property was properly set.
        """
        self.assertAlmostEqual(self.nasa.Tmin.value_si / self.Tmin, 1.0, 6, '{0} != {1} within 6 places'.format(self.nasa.Tmin, self.Tmin))
    
    def test_Tmax(self):
        """
        Test that the NASA Tmax property was properly set.
        """
        self.assertAlmostEqual(self.nasa.Tmax.value_si / self.Tmax, 1.0, 6, '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

    def test_E0(self):
        """
        Test that the NASA E0 property was properly set.
        """
        self.assertAlmostEqual(self.nasa.E0.value_si / self.E0, 1.0, 6, '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))
        
    def test_Comment(self):
        """
        Test that the NASA comment property was properly set.
        """
        self.assertEqual(self.nasa.comment, self.comment)

    def test_isTemperatureValid(self):
        """
        Test the NASA.isTemperatureValid() method.
        """
        Tdata = [200,400,600,800,1000,1200,1400,1600,1800,2000]
        validdata = [False,True,True,True,True,True,True,True,True,True]
        for T, valid in zip(Tdata, validdata):
            valid0 = self.nasa.isTemperatureValid(T)
            self.assertEqual(valid0, valid)
        
    def test_getHeatCapacity(self):
        """
        Test the NASA.getHeatCapacity() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        Cpexplist = numpy.array([7.80157, 10.5653, 12.8213, 14.5817, 15.9420, 16.9861, 17.78645, 18.4041, 18.8883]) * constants.R
        for T, Cpexp in zip(Tlist, Cpexplist):
            Cpact = self.nasa.getHeatCapacity(T)
            self.assertAlmostEqual(Cpexp / Cpact, 1.0, 4, '{0} != {1}'.format(Cpexp, Cpact))
        
    def test_getEnthalpy(self):
        """
        Test the NASA.getEnthalpy() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        Hexplist = numpy.array([-22.7613, -12.1027, -6.14236, -2.16615, 0.743456, 2.99256, 4.79397, 6.27334, 7.51156]) * constants.R * Tlist
        for T, Hexp in zip(Tlist, Hexplist):
            Hact = self.nasa.getEnthalpy(T)
            self.assertAlmostEqual(Hexp / Hact, 1.0, 3, '{0} != {1}'.format(Hexp, Hact))
        
    def test_getEntropy(self):
        """
        Test the NASA.getEntropy() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        Sexplist = numpy.array([29.6534, 33.3516, 36.7131, 39.7715, 42.5557, 45.0952, 47.4179, 49.5501, 51.5152]) * constants.R
        for T, Sexp in zip(Tlist, Sexplist):
            Sact = self.nasa.getEntropy(T)
            self.assertAlmostEqual(Sexp / Sact, 1.0, 4, '{0} != {1}'.format(Sexp, Sact))

    def test_getFreeEnergy(self):
        """
        Test the NASA.getFreeEnergy() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        for T in Tlist:
            Gexp = self.nasa.getEnthalpy(T) - T * self.nasa.getEntropy(T)
            Gact = self.nasa.getFreeEnergy(T)
            self.assertAlmostEqual(Gexp / Gact, 1.0, 4, '{0} != {1}'.format(Gexp, Gact))
    
    def test_pickle(self):
        """
        Test that a NASA object can be pickled and unpickled with no loss of
        information.
        """
        import cPickle
        nasa = cPickle.loads(cPickle.dumps(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_repr(self):
        """
        Test that a NASA object can be reconstructed from its repr() output
        with no loss of information.
        """
        nasa = None
        exec('nasa = {0!r}'.format(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)
        
    def test_toCantera(self):
        """
        Test that conversion to a Cantera NasaPoly2 object works
        """
        nasapoly2 = self.nasa.toCantera()
        # NasaPoly2 units use J/kmol rather than J/mol
        self.assertAlmostEqual(self.nasa.getEnthalpy(900), nasapoly2.h(900)/1000, 1)
        self.assertAlmostEqual(self.nasa.getEntropy(700), nasapoly2.s(700)/1000, 1)

    def testToNASA(self):
        """
        Test if the entropy computed from other thermo implementations is close to what NASA computes.
        """

        from rmgpy import settings
        from rmgpy.data.rmg import RMGDatabase, database
        from rmgpy.species import Species

        # Load databases
        database = RMGDatabase()
        database.loadThermo(os.path.join(settings['database.directory'], 'thermo'),thermoLibraries=['Narayanaswamy'])
        database.loadSolvation(os.path.join(settings['database.directory'], 'solvation'))

        spc = Species().fromSMILES('CC')
        spc.getThermoData()

        T = 1350.# not 298K!

        # nasa to thermodata
        nasa = spc.thermo
        Snasa = nasa.getEntropy(T)

        td = nasa.toThermoData()
        Std = td.getEntropy(T)

        self.assertAlmostEqual(Snasa, Std, -1)
        self.assertEqual(td.comment,nasa.comment)


        # thermodata to nasa
        nasa = td.toNASA(Tmin=100.0, Tmax=5000.0, Tint=1000.0)
        Snasa = nasa.getEntropy(T)

        self.assertAlmostEqual(Snasa, Std, -1)
        self.assertEqual(td.comment,nasa.comment)

        # wilhoit to nasa
        wilhoit=nasa.toWilhoit()
        nasa = wilhoit.toNASA(Tmin=100.0, Tmax=5000.0, Tint=1000.0)
        Snasa = nasa.getEntropy(T)

        self.assertAlmostEqual(Snasa, Std, -1)
        self.assertEqual(wilhoit.comment,nasa.comment)

        # nasa to wilhoi performed in wilhoitTest
예제 #12
0
 def setUp(self):
     """
     A function run before each unit test in this class.
     """
     octyl_pri = Species(
         label="",
         thermo=NASA(polynomials=[
             NASAPolynomial(coeffs=[
                 -0.772759, 0.093255, -5.84447e-05, 1.8557e-08,
                 -2.37127e-12, -3926.9, 37.6131
             ],
                            Tmin=(298, 'K'),
                            Tmax=(1390, 'K')),
             NASAPolynomial(coeffs=[
                 25.051, 0.036948, -1.25765e-05, 1.94628e-09, -1.12669e-13,
                 -13330.1, -102.557
             ],
                            Tmin=(1390, 'K'),
                            Tmax=(5000, 'K'))
         ],
                     Tmin=(298, 'K'),
                     Tmax=(5000, 'K'),
                     Cp0=(33.2579, 'J/(mol*K)'),
                     CpInf=(577.856, 'J/(mol*K)'),
                     comment="""Thermo library: JetSurF0.2"""),
         molecule=[Molecule(SMILES="[CH2]CCCCCCC")])
     octyl_sec = Species(
         label="",
         thermo=NASA(polynomials=[
             NASAPolynomial(coeffs=[
                 -0.304233, 0.0880077, -4.90743e-05, 1.21858e-08,
                 -8.87773e-13, -5237.93, 36.6583
             ],
                            Tmin=(298, 'K'),
                            Tmax=(1383, 'K')),
             NASAPolynomial(coeffs=[
                 24.9044, 0.0366394, -1.2385e-05, 1.90835e-09, -1.10161e-13,
                 -14713.5, -101.345
             ],
                            Tmin=(1383, 'K'),
                            Tmax=(5000, 'K'))
         ],
                     Tmin=(298, 'K'),
                     Tmax=(5000, 'K'),
                     Cp0=(33.2579, 'J/(mol*K)'),
                     CpInf=(577.856, 'J/(mol*K)'),
                     comment="""Thermo library: JetSurF0.2"""),
         molecule=[Molecule(SMILES="CC[CH]CCCCC")])
     ethane = Species(label="",
                      thermo=ThermoData(
                          Tdata=([300, 400, 500, 600, 800, 1000,
                                  1500], 'K'),
                          Cpdata=([
                              10.294, 12.643, 14.933, 16.932, 20.033,
                              22.438, 26.281
                          ], 'cal/(mol*K)'),
                          H298=(12.549, 'kcal/mol'),
                          S298=(52.379, 'cal/(mol*K)'),
                          Cp0=(33.2579, 'J/(mol*K)'),
                          CpInf=(133.032, 'J/(mol*K)'),
                          comment="""Thermo library: CH"""),
                      molecule=[Molecule(SMILES="C=C")])
     decyl = Species(label="",
                     thermo=NASA(polynomials=[
                         NASAPolynomial(coeffs=[
                             -1.31358, 0.117973, -7.51843e-05, 2.43331e-08,
                             -3.17523e-12, -9689.68, 43.501
                         ],
                                        Tmin=(298, 'K'),
                                        Tmax=(1390, 'K')),
                         NASAPolynomial(coeffs=[
                             31.5697, 0.0455818, -1.54995e-05, 2.39711e-09,
                             -1.3871e-13, -21573.8, -134.709
                         ],
                                        Tmin=(1390, 'K'),
                                        Tmax=(5000, 'K'))
                     ],
                                 Tmin=(298, 'K'),
                                 Tmax=(5000, 'K'),
                                 Cp0=(33.2579, 'J/(mol*K)'),
                                 CpInf=(719.202, 'J/(mol*K)'),
                                 comment="""Thermo library: JetSurF0.2"""),
                     molecule=[Molecule(SMILES="[CH2]CCCCCCCCC")])
     acetone = Species(label="",
                       thermo=NASA(polynomials=[
                           NASAPolynomial(coeffs=[
                               3.75568, 0.0264934, -6.55661e-05,
                               1.94971e-07, -1.82059e-10, -27905.3, 9.0162
                           ],
                                          Tmin=(10, 'K'),
                                          Tmax=(422.477, 'K')),
                           NASAPolynomial(coeffs=[
                               0.701289, 0.0344988, -1.9736e-05,
                               5.48052e-09, -5.92612e-13, -27460.6, 23.329
                           ],
                                          Tmin=(422.477, 'K'),
                                          Tmax=(3000, 'K'))
                       ],
                                   Tmin=(10, 'K'),
                                   Tmax=(3000, 'K'),
                                   E0=(-232.025, 'kJ/mol'),
                                   Cp0=(33.2579, 'J/(mol*K)'),
                                   CpInf=(232.805, 'J/(mol*K)')),
                       molecule=[Molecule(SMILES="CC(=O)C")])
     peracetic_acid = Species(label="",
                              thermo=NASA(polynomials=[
                                  NASAPolynomial(coeffs=[
                                      3.81786, 0.016419, 3.32204e-05,
                                      -8.98403e-08, 6.63474e-11, -42057.8,
                                      9.65245
                                  ],
                                                 Tmin=(10, 'K'),
                                                 Tmax=(354.579, 'K')),
                                  NASAPolynomial(coeffs=[
                                      2.75993, 0.0283534, -1.72659e-05,
                                      5.08158e-09, -5.77773e-13, -41982.8,
                                      13.6595
                                  ],
                                                 Tmin=(354.579, 'K'),
                                                 Tmax=(3000, 'K'))
                              ],
                                          Tmin=(10, 'K'),
                                          Tmax=(3000, 'K'),
                                          E0=(-349.698, 'kJ/mol'),
                                          Cp0=(33.2579, 'J/(mol*K)'),
                                          CpInf=(199.547, 'J/(mol*K)')),
                              molecule=[Molecule(SMILES="CC(=O)OO")])
     acetic_acid = Species(label="",
                           thermo=NASA(polynomials=[
                               NASAPolynomial(coeffs=[
                                   3.97665, 0.00159915, 8.5542e-05,
                                   -1.76486e-07, 1.20201e-10, -53911.5,
                                   8.99309
                               ],
                                              Tmin=(10, 'K'),
                                              Tmax=(375.616, 'K')),
                               NASAPolynomial(coeffs=[
                                   1.57088, 0.0272146, -1.67357e-05,
                                   5.01453e-09, -5.82273e-13, -53730.7,
                                   18.2442
                               ],
                                              Tmin=(375.616, 'K'),
                                              Tmax=(3000, 'K'))
                           ],
                                       Tmin=(10, 'K'),
                                       Tmax=(3000, 'K'),
                                       E0=(-448.245, 'kJ/mol'),
                                       Cp0=(33.2579, 'J/(mol*K)'),
                                       CpInf=(182.918, 'J/(mol*K)')),
                           molecule=[Molecule(SMILES="CC(=O)O")])
     criegee = Species(label="",
                       thermo=NASA(polynomials=[
                           NASAPolynomial(coeffs=[
                               3.23876, 0.0679583, -3.35611e-05,
                               7.91519e-10, 3.13038e-12, -77986, 13.6438
                           ],
                                          Tmin=(10, 'K'),
                                          Tmax=(1053.46, 'K')),
                           NASAPolynomial(coeffs=[
                               9.84525, 0.0536795, -2.86165e-05,
                               7.39945e-09, -7.48482e-13, -79977.6, -21.4187
                           ],
                                          Tmin=(1053.46, 'K'),
                                          Tmax=(3000, 'K'))
                       ],
                                   Tmin=(10, 'K'),
                                   Tmax=(3000, 'K'),
                                   E0=(-648.47, 'kJ/mol'),
                                   Cp0=(33.2579, 'J/(mol*K)'),
                                   CpInf=(457.296, 'J/(mol*K)')),
                       molecule=[Molecule(SMILES="CC(=O)OOC(C)(O)C")])
     self.database = SolvationDatabase()
     self.database.load(
         os.path.join(settings['database.directory'], 'solvation'))
     self.solvent = 'octane'
     diffusionLimiter.enable(self.database.getSolventData(self.solvent),
                             self.database)
     self.T = 298
     self.uni_reaction = Reaction(reactants=[octyl_pri],
                                  products=[octyl_sec])
     self.uni_reaction.kinetics = Arrhenius(A=(2.0, '1/s'),
                                            n=0,
                                            Ea=(0, 'kJ/mol'))
     self.bi_uni_reaction = Reaction(reactants=[octyl_pri, ethane],
                                     products=[decyl])
     self.bi_uni_reaction.kinetics = Arrhenius(A=(1.0E-22,
                                                  'cm^3/molecule/s'),
                                               n=0,
                                               Ea=(0, 'kJ/mol'))
     self.tri_bi_reaction = Reaction(
         reactants=[acetone, peracetic_acid, acetic_acid],
         products=[criegee, acetic_acid])
     self.tri_bi_reaction.kinetics = Arrhenius(A=(1.07543e-11,
                                                  'cm^6/(mol^2*s)'),
                                               n=5.47295,
                                               Ea=(-38.5379, 'kJ/mol'))
     self.intrinsic_rates = {
         self.uni_reaction:
         self.uni_reaction.kinetics.getRateCoefficient(self.T, P=100e5),
         self.bi_uni_reaction:
         self.bi_uni_reaction.kinetics.getRateCoefficient(self.T, P=100e5),
         self.tri_bi_reaction:
         self.tri_bi_reaction.kinetics.getRateCoefficient(self.T, P=100e5),
     }
예제 #13
0
class TestNASA(unittest.TestCase):
    """
    Contains unit tests of the MultiNASA class.
    """
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.coeffs_low = [
            4.03055, -0.00214171, 4.90611e-05, -5.99027e-08, 2.38945e-11,
            -11257.6, 3.5613
        ]
        self.coeffs_high = [
            -0.307954, 0.0245269, -1.2413e-05, 3.07724e-09, -3.01467e-13,
            -10693, 22.628
        ]
        self.Tmin = 300.
        self.Tmax = 3000.
        self.Tint = 650.73
        self.E0 = -782292.  # J/mol.
        self.comment = "C2H6"
        self.nasa = NASA(
            polynomials=[
                NASAPolynomial(coeffs=self.coeffs_low,
                               Tmin=(self.Tmin, "K"),
                               Tmax=(self.Tint, "K")),
                NASAPolynomial(coeffs=self.coeffs_high,
                               Tmin=(self.Tint, "K"),
                               Tmax=(self.Tmax, "K")),
            ],
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            E0=(self.E0, "J/mol"),
            comment=self.comment,
        )

    def tearDown(self):
        """
        Reset the database & liquid parameters for solution
        """
        import rmgpy.data.rmg
        rmgpy.data.rmg.database = None

    def test_poly_low(self):
        """
        Test that the NASA low-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly1.coeffs), len(self.coeffs_low))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, self.coeffs_low):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value_si, self.Tmin)
        self.assertEqual(self.nasa.poly1.Tmax.value_si, self.Tint)

    def test_poly_high(self):
        """
        Test that the NASA high-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly2.coeffs), len(self.coeffs_high))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, self.coeffs_high):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value_si, self.Tint)
        self.assertEqual(self.nasa.poly2.Tmax.value_si, self.Tmax)

    def test_temperature_min(self):
        """
        Test that the NASA Tmin property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.Tmin.value_si / self.Tmin, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmin, self.Tmin))

    def test_temperature_max(self):
        """
        Test that the NASA Tmax property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.Tmax.value_si / self.Tmax, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

    def test_e0(self):
        """
        Test that the NASA E0 property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.E0.value_si / self.E0, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

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

    def test_is_temperature_valid(self):
        """
        Test the NASA.is_temperature_valid() method.
        """
        Tdata = [200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000]
        valid_data = [
            False, True, True, True, True, True, True, True, True, True
        ]
        for T, valid in zip(Tdata, valid_data):
            valid0 = self.nasa.is_temperature_valid(T)
            self.assertEqual(valid0, valid)

    def test_get_heat_capacity(self):
        """
        Test the NASA.get_heat_capacity() method.
        """
        Tlist = np.array([400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        cp_exp_list = np.array([
            7.80157, 10.5653, 12.8213, 14.5817, 15.9420, 16.9861, 17.78645,
            18.4041, 18.8883
        ]) * constants.R
        for T, cp_exp in zip(Tlist, cp_exp_list):
            cp_act = self.nasa.get_heat_capacity(T)
            self.assertAlmostEqual(cp_exp / cp_act, 1.0, 4,
                                   '{0} != {1}'.format(cp_exp, cp_act))

    def test_get_enthalpy(self):
        """
        Test the NASA.get_enthalpy() method.
        """
        Tlist = np.array([400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        h_exp_list = np.array([
            -22.7613, -12.1027, -6.14236, -2.16615, 0.743456, 2.99256, 4.79397,
            6.27334, 7.51156
        ]) * constants.R * Tlist
        for T, h_exp in zip(Tlist, h_exp_list):
            h_act = self.nasa.get_enthalpy(T)
            self.assertAlmostEqual(h_exp / h_act, 1.0, 3,
                                   '{0} != {1}'.format(h_exp, h_act))

    def test_get_entropy(self):
        """
        Test the NASA.get_entropy() method.
        """
        Tlist = np.array([400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        s_exp_list = np.array([
            29.6534, 33.3516, 36.7131, 39.7715, 42.5557, 45.0952, 47.4179,
            49.5501, 51.5152
        ]) * constants.R
        for T, s_exp in zip(Tlist, s_exp_list):
            s_act = self.nasa.get_entropy(T)
            self.assertAlmostEqual(s_exp / s_act, 1.0, 4,
                                   '{0} != {1}'.format(s_exp, s_act))

    def test_get_free_energy(self):
        """
        Test the NASA.get_free_energy() method.
        """
        Tlist = np.array([400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        for T in Tlist:
            g_exp = self.nasa.get_enthalpy(T) - T * self.nasa.get_entropy(T)
            g_act = self.nasa.get_free_energy(T)
            self.assertAlmostEqual(g_exp / g_act, 1.0, 4,
                                   '{0} != {1}'.format(g_exp, g_act))

    def test_pickle(self):
        """
        Test that a NASA object can be pickled and unpickled with no loss of
        information.
        """
        import pickle
        nasa = pickle.loads(pickle.dumps(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_repr(self):
        """
        Test that a NASA object can be reconstructed from its repr() output
        with no loss of information.
        """
        namespace = {}
        exec('nasa = {0!r}'.format(self.nasa), globals(), namespace)
        self.assertIn('nasa', namespace)
        nasa = namespace['nasa']
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_to_cantera(self):
        """
        Test that conversion to a Cantera NasaPoly2 object works
        """
        nasapoly2 = self.nasa.to_cantera()
        # NasaPoly2 units use J/kmol rather than J/mol
        self.assertAlmostEqual(self.nasa.get_enthalpy(900),
                               nasapoly2.h(900) / 1000, 1)
        self.assertAlmostEqual(self.nasa.get_entropy(700),
                               nasapoly2.s(700) / 1000, 1)

    def test_to_nasa(self):
        """
        Test if the entropy computed from other thermo implementations is close to what NASA computes.
        """

        from rmgpy import settings
        from rmgpy.data.rmg import RMGDatabase
        from rmgpy.species import Species

        # Load databases
        database = RMGDatabase()
        database.load_thermo(os.path.join(settings['database.directory'],
                                          'thermo'),
                             thermo_libraries=['Narayanaswamy'])
        database.load_solvation(
            os.path.join(settings['database.directory'], 'solvation'))

        spc = Species().from_smiles('CC')
        spc.get_thermo_data()

        T = 1350.  # not 298K!

        # nasa to thermodata
        nasa = spc.thermo
        s_nasa = nasa.get_entropy(T)

        td = nasa.to_thermo_data()
        s_td = td.get_entropy(T)

        self.assertAlmostEqual(s_nasa, s_td, -1)
        self.assertEqual(td.comment, nasa.comment)

        # thermodata to nasa
        nasa = td.to_nasa(Tmin=100.0, Tmax=5000.0, Tint=1000.0)
        s_nasa = nasa.get_entropy(T)

        self.assertAlmostEqual(s_nasa, s_td, -1)
        self.assertEqual(td.comment, nasa.comment)

        # wilhoit to nasa
        wilhoit = nasa.to_wilhoit()
        nasa = wilhoit.to_nasa(Tmin=100.0, Tmax=5000.0, Tint=1000.0)
        s_nasa = nasa.get_entropy(T)

        self.assertAlmostEqual(s_nasa, s_td, -1)
        self.assertEqual(wilhoit.comment, nasa.comment)

        # nasa to wilhoi performed in wilhoitTest

    def test_nasa_as_dict_full(self):
        """
        Test that NASA.as_dict functions properly with all attributes
        """
        nasa_dict = self.nasa.as_dict()
        self.assertEqual(nasa_dict['E0']['value'], self.E0)
        self.assertEqual(nasa_dict['Tmin']['value'], self.Tmin)
        self.assertEqual(nasa_dict['Tmax']['value'], self.Tmax)
        self.assertEqual(nasa_dict['comment'], self.comment)
        self.assertTupleEqual(
            tuple(nasa_dict['polynomials']['polynomial1']['coeffs']['object']),
            tuple(self.coeffs_low))
        self.assertTupleEqual(
            tuple(nasa_dict['polynomials']['polynomial2']['coeffs']['object']),
            tuple(self.coeffs_high))
        self.assertEqual(
            nasa_dict['polynomials']['polynomial1']['Tmin']['value'],
            self.Tmin)
        self.assertEqual(
            nasa_dict['polynomials']['polynomial1']['Tmax']['value'],
            self.Tint)
        self.assertEqual(
            nasa_dict['polynomials']['polynomial2']['Tmin']['value'],
            self.Tint)
        self.assertEqual(
            nasa_dict['polynomials']['polynomial2']['Tmax']['value'],
            self.Tmax)

    def test_nasa_as_dict_minimal(self):
        """
        Test that NASA.as_dict does not contain empty, optional attributes
        """
        nasa_dict = NASA().as_dict()
        keys = list(nasa_dict.keys())
        self.assertNotIn('Tmin', keys)
        self.assertNotIn('Tmax', keys)
        self.assertNotIn('E0', keys)
        self.assertNotIn('Cp0', keys)
        self.assertNotIn('CpInf', keys)
        self.assertNotIn('label', keys)
        self.assertNotIn('comment', keys)

    def test_nasa_polynomial_as_dict(self):
        """
        Test that NASAPolynomial.as_dict returns all non-empty, non-redundant attributes properly.
        """
        nasa_poly_dict = self.nasa.polynomials[0].as_dict()
        self.assertEqual(
            nasa_poly_dict, {
                'coeffs': {
                    'object': [
                        4.03055, -0.00214171, 4.90611e-05, -5.99027e-08,
                        2.38945e-11, -11257.6, 3.5613
                    ],
                    'class':
                    'np_array'
                },
                'Tmax': {
                    'units': 'K',
                    'class': 'ScalarQuantity',
                    'value': 650.73
                },
                'Tmin': {
                    'units': 'K',
                    'class': 'ScalarQuantity',
                    'value': 300.0
                },
                'class': 'NASAPolynomial'
            })

    def test_make_nasa(self):
        """
        Test that a NASA object can be reconstructed from a dictionary (also test NASAPolynomial by extension)
        """
        nasa_dict = self.nasa.as_dict()
        new_nasa = NASA.__new__(NASA)
        class_dictionary = {
            'ScalarQuantity': ScalarQuantity,
            'np_array': np.array,
            'NASA': NASA,
            'NASAPolynomial': NASAPolynomial,
        }

        new_nasa.make_object(nasa_dict, class_dictionary)
예제 #14
0
파일: nasaTest.py 프로젝트: thackray/RMG-Py
class TestNASA(unittest.TestCase):
    """
    Contains unit tests of the MultiNASA class.
    """
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.coeffs_low = [
            4.03055, -0.00214171, 4.90611e-05, -5.99027e-08, 2.38945e-11,
            -11257.6, 3.5613
        ]
        self.coeffs_high = [
            -0.307954, 0.0245269, -1.2413e-05, 3.07724e-09, -3.01467e-13,
            -10693, 22.628
        ]
        self.Tmin = 300.
        self.Tmax = 3000.
        self.Tint = 650.73
        self.E0 = -782292.  # J/mol.
        self.comment = "C2H6"
        self.nasa = NASA(
            polynomials=[
                NASAPolynomial(coeffs=self.coeffs_low,
                               Tmin=(self.Tmin, "K"),
                               Tmax=(self.Tint, "K")),
                NASAPolynomial(coeffs=self.coeffs_high,
                               Tmin=(self.Tint, "K"),
                               Tmax=(self.Tmax, "K")),
            ],
            Tmin=(self.Tmin, "K"),
            Tmax=(self.Tmax, "K"),
            E0=(self.E0, "J/mol"),
            comment=self.comment,
        )

    def test_polyLow(self):
        """
        Test that the NASA low-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly1.coeffs), len(self.coeffs_low))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, self.coeffs_low):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value_si, self.Tmin)
        self.assertEqual(self.nasa.poly1.Tmax.value_si, self.Tint)

    def test_polyHigh(self):
        """
        Test that the NASA high-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly2.coeffs), len(self.coeffs_high))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, self.coeffs_high):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value_si, self.Tint)
        self.assertEqual(self.nasa.poly2.Tmax.value_si, self.Tmax)

    def test_Tmin(self):
        """
        Test that the NASA Tmin property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.Tmin.value_si / self.Tmin, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmin, self.Tmin))

    def test_Tmax(self):
        """
        Test that the NASA Tmax property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.Tmax.value_si / self.Tmax, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

    def test_E0(self):
        """
        Test that the NASA E0 property was properly set.
        """
        self.assertAlmostEqual(
            self.nasa.E0.value_si / self.E0, 1.0, 6,
            '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))

    def test_Comment(self):
        """
        Test that the NASA comment property was properly set.
        """
        self.assertEqual(self.nasa.comment, self.comment)

    def test_isTemperatureValid(self):
        """
        Test the NASA.isTemperatureValid() method.
        """
        Tdata = [200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000]
        validdata = [
            False, True, True, True, True, True, True, True, True, True
        ]
        for T, valid in zip(Tdata, validdata):
            valid0 = self.nasa.isTemperatureValid(T)
            self.assertEqual(valid0, valid)

    def test_getHeatCapacity(self):
        """
        Test the NASA.getHeatCapacity() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        Cpexplist = numpy.array([
            7.80157, 10.5653, 12.8213, 14.5817, 15.9420, 16.9861, 17.78645,
            18.4041, 18.8883
        ]) * constants.R
        for T, Cpexp in zip(Tlist, Cpexplist):
            Cpact = self.nasa.getHeatCapacity(T)
            self.assertAlmostEqual(Cpexp / Cpact, 1.0, 4,
                                   '{0} != {1}'.format(Cpexp, Cpact))

    def test_getEnthalpy(self):
        """
        Test the NASA.getEnthalpy() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        Hexplist = numpy.array([
            -22.7613, -12.1027, -6.14236, -2.16615, 0.743456, 2.99256, 4.79397,
            6.27334, 7.51156
        ]) * constants.R * Tlist
        for T, Hexp in zip(Tlist, Hexplist):
            Hact = self.nasa.getEnthalpy(T)
            self.assertAlmostEqual(Hexp / Hact, 1.0, 3,
                                   '{0} != {1}'.format(Hexp, Hact))

    def test_getEntropy(self):
        """
        Test the NASA.getEntropy() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        Sexplist = numpy.array([
            29.6534, 33.3516, 36.7131, 39.7715, 42.5557, 45.0952, 47.4179,
            49.5501, 51.5152
        ]) * constants.R
        for T, Sexp in zip(Tlist, Sexplist):
            Sact = self.nasa.getEntropy(T)
            self.assertAlmostEqual(Sexp / Sact, 1.0, 4,
                                   '{0} != {1}'.format(Sexp, Sact))

    def test_getFreeEnergy(self):
        """
        Test the NASA.getFreeEnergy() method.
        """
        Tlist = numpy.array(
            [400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000])
        for T in Tlist:
            Gexp = self.nasa.getEnthalpy(T) - T * self.nasa.getEntropy(T)
            Gact = self.nasa.getFreeEnergy(T)
            self.assertAlmostEqual(Gexp / Gact, 1.0, 4,
                                   '{0} != {1}'.format(Gexp, Gact))

    def test_pickle(self):
        """
        Test that a NASA object can be pickled and unpickled with no loss of
        information.
        """
        import cPickle
        nasa = cPickle.loads(cPickle.dumps(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_repr(self):
        """
        Test that a NASA object can be reconstructed from its repr() output
        with no loss of information.
        """
        nasa = None
        exec('nasa = {0!r}'.format(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.E0.value, nasa.E0.value)
        self.assertEqual(self.nasa.E0.units, nasa.E0.units)
        self.assertEqual(self.nasa.comment, nasa.comment)
예제 #15
0
파일: nasaTest.py 프로젝트: jbarlow3/RMG-Py
class TestNASA(unittest.TestCase):
    """
    Contains unit tests of the MultiNASA class.
    """

    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        self.coeffs_low = [4.03055,-0.00214171,4.90611e-05,-5.99027e-08,2.38945e-11,-11257.6,3.5613]
        self.coeffs_high = [-0.307954,0.0245269,-1.2413e-05,3.07724e-09,-3.01467e-13,-10693,22.628]
        self.Tmin = 300.
        self.Tmax = 3000.
        self.Tint = 650.73
        self.comment = "C2H6"
        self.nasa = NASA(
            polynomials = [
                NASAPolynomial(coeffs=self.coeffs_low, Tmin=(self.Tmin,"K"), Tmax=(self.Tint,"K")),
                NASAPolynomial(coeffs=self.coeffs_high, Tmin=(self.Tint,"K"), Tmax=(self.Tmax,"K")),
            ],
            Tmin = (self.Tmin,"K"),
            Tmax = (self.Tmax,"K"),
            comment = self.comment,
        )
    
    def test_polyLow(self):
        """
        Test that the NASA low-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly1.coeffs), len(self.coeffs_low))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, self.coeffs_low):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value_si, self.Tmin)
        self.assertEqual(self.nasa.poly1.Tmax.value_si, self.Tint)
    
    def test_polyHigh(self):
        """
        Test that the NASA high-temperature polynomial was properly set.
        """
        self.assertEqual(len(self.nasa.poly2.coeffs), len(self.coeffs_high))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, self.coeffs_high):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value_si, self.Tint)
        self.assertEqual(self.nasa.poly2.Tmax.value_si, self.Tmax)
    
    def test_Tmin(self):
        """
        Test that the NASA Tmin property was properly set.
        """
        self.assertAlmostEqual(self.nasa.Tmin.value_si / self.Tmin, 1.0, 6, '{0} != {1} within 6 places'.format(self.nasa.Tmin, self.Tmin))
    
    def test_Tmax(self):
        """
        Test that the NASA Tmax property was properly set.
        """
        self.assertAlmostEqual(self.nasa.Tmax.value_si / self.Tmax, 1.0, 6, '{0} != {1} within 6 places'.format(self.nasa.Tmax, self.Tmax))
    
    def test_Comment(self):
        """
        Test that the NASA comment property was properly set.
        """
        self.assertEqual(self.nasa.comment, self.comment)

    def test_isTemperatureValid(self):
        """
        Test the NASA.isTemperatureValid() method.
        """
        Tdata = [200,400,600,800,1000,1200,1400,1600,1800,2000]
        validdata = [False,True,True,True,True,True,True,True,True,True]
        for T, valid in zip(Tdata, validdata):
            valid0 = self.nasa.isTemperatureValid(T)
            self.assertEqual(valid0, valid)
        
    def test_getHeatCapacity(self):
        """
        Test the NASA.getHeatCapacity() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        Cpexplist = numpy.array([7.80157, 10.5653, 12.8213, 14.5817, 15.9420, 16.9861, 17.78645, 18.4041, 18.8883]) * constants.R
        for T, Cpexp in zip(Tlist, Cpexplist):
            Cpact = self.nasa.getHeatCapacity(T)
            self.assertAlmostEqual(Cpexp / Cpact, 1.0, 4, '{0} != {1}'.format(Cpexp, Cpact))
        
    def test_getEnthalpy(self):
        """
        Test the NASA.getEnthalpy() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        Hexplist = numpy.array([-22.7613, -12.1027, -6.14236, -2.16615, 0.743456, 2.99256, 4.79397, 6.27334, 7.51156]) * constants.R * Tlist
        for T, Hexp in zip(Tlist, Hexplist):
            Hact = self.nasa.getEnthalpy(T)
            self.assertAlmostEqual(Hexp / Hact, 1.0, 3, '{0} != {1}'.format(Hexp, Hact))
        
    def test_getEntropy(self):
        """
        Test the NASA.getEntropy() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        Sexplist = numpy.array([29.6534, 33.3516, 36.7131, 39.7715, 42.5557, 45.0952, 47.4179, 49.5501, 51.5152]) * constants.R
        for T, Sexp in zip(Tlist, Sexplist):
            Sact = self.nasa.getEntropy(T)
            self.assertAlmostEqual(Sexp / Sact, 1.0, 4, '{0} != {1}'.format(Sexp, Sact))

    def test_getFreeEnergy(self):
        """
        Test the NASA.getFreeEnergy() method.
        """
        Tlist = numpy.array([400,600,800,1000,1200,1400,1600,1800,2000])
        for T in Tlist:
            Gexp = self.nasa.getEnthalpy(T) - T * self.nasa.getEntropy(T)
            Gact = self.nasa.getFreeEnergy(T)
            self.assertAlmostEqual(Gexp / Gact, 1.0, 4, '{0} != {1}'.format(Gexp, Gact))
    
    def test_pickle(self):
        """
        Test that a NASA object can be pickled and unpickled with no loss of
        information.
        """
        import cPickle
        nasa = cPickle.loads(cPickle.dumps(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.comment, nasa.comment)

    def test_repr(self):
        """
        Test that a NASA object can be reconstructed from its repr() output
        with no loss of information.
        """
        nasa = None
        exec('nasa = {0!r}'.format(self.nasa))
        self.assertEqual(len(self.nasa.poly1.coeffs), len(nasa.poly1.coeffs))
        for coeff0, coeff in zip(self.nasa.poly1.coeffs, nasa.poly1.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly1.Tmin.value, nasa.poly1.Tmin.value)
        self.assertEqual(self.nasa.poly1.Tmin.units, nasa.poly1.Tmin.units)
        self.assertEqual(self.nasa.poly1.Tmax.value, nasa.poly1.Tmax.value)
        self.assertEqual(self.nasa.poly1.Tmax.units, nasa.poly1.Tmax.units)
        self.assertEqual(self.nasa.poly1.comment, nasa.poly1.comment)
        self.assertEqual(len(self.nasa.poly2.coeffs), len(nasa.poly2.coeffs))
        for coeff0, coeff in zip(self.nasa.poly2.coeffs, nasa.poly2.coeffs):
            self.assertAlmostEqual(coeff / coeff0, 1.0, 6)
        self.assertEqual(self.nasa.poly2.Tmin.value, nasa.poly2.Tmin.value)
        self.assertEqual(self.nasa.poly2.Tmin.units, nasa.poly2.Tmin.units)
        self.assertEqual(self.nasa.poly2.Tmax.value, nasa.poly2.Tmax.value)
        self.assertEqual(self.nasa.poly2.Tmax.units, nasa.poly2.Tmax.units)
        self.assertEqual(self.nasa.poly2.comment, nasa.poly2.comment)
        self.assertEqual(self.nasa.Tmin.value, nasa.Tmin.value)
        self.assertEqual(self.nasa.Tmin.units, nasa.Tmin.units)
        self.assertEqual(self.nasa.Tmax.value, nasa.Tmax.value)
        self.assertEqual(self.nasa.Tmax.units, nasa.Tmax.units)
        self.assertEqual(self.nasa.comment, nasa.comment)