Beispiel #1
0
class TestCustom(TestElementary):

    # probe O + H2 <=> H + OH
    _cls = ct.CustomReaction
    _equation = 'H2 + O <=> H + OH'
    _rate_obj = ct.CustomRate(lambda T: 38.7 * T**2.7 * exp(-3150.15428 / T))
    _index = 2
    _type = "custom-rate-function"

    def setUp(self):
        # need to overwrite rate to ensure correct type ('method' is not compatible with Func1)
        self._rate = lambda T: 38.7 * T**2.7 * exp(-3150.15428 / T)

    def test_no_rate(self):
        rxn = self._cls(equation=self._equation, kinetics=self.gas)
        with self.assertRaisesRegex(
                ct.CanteraError, "Custom rate function is not initialized."):
            rxn.rate(self.gas.T)

        gas2 = ct.Solution(thermo='IdealGas',
                           kinetics='GasKinetics',
                           species=self.species,
                           reactions=[rxn])
        gas2.TPX = self.gas.TPX

        with self.assertRaisesRegex(
                ct.CanteraError, "Custom rate function is not initialized."):
            gas2.forward_rate_constants

    def test_from_func(self):
        f = ct.Func1(self._rate)
        rxn = ct.CustomReaction(equation=self._equation,
                                rate=f,
                                kinetics=self.gas)
        self.check_rxn(rxn)

    def test_rate_func(self):
        f = ct.Func1(self._rate)
        rate = ct.CustomRate(f)
        self.assertNear(rate(self.gas.T),
                        self.gas.forward_rate_constants[self._index])

    def test_custom(self):
        rxn = ct.CustomReaction(
            equation=self._equation,
            rate=lambda T: 38.7 * T**2.7 * exp(-3150.15428 / T),
            kinetics=self.gas)
        self.check_rxn(rxn)
class TestCustom(ReactionTests, utilities.CanteraTest):
    # test Custom reaction

    # probe O + H2 <=> H + OH
    _cls = ct.CustomReaction
    _equation = "H2 + O <=> H + OH"
    _rate_obj = ct.CustomRate(lambda T: 38.7 * T**2.7 * exp(-3150.15428/T))
    _index = 0
    _type = "custom-rate-function"
    _legacy = False
    _yaml = None

    def setUp(self):
        # need to overwrite rate to ensure correct type ("method" is not compatible with Func1)
        super().setUp()
        self._rate = lambda T: 38.7 * T**2.7 * exp(-3150.15428/T)

    def test_roundtrip(self):
        # overload default tester for round trip
        pass

    def test_raises_invalid_rate(self):
        # check exception for instantiation from keywords / invalid rate
        with self.assertRaises(TypeError):
            self.from_rate(tuple())
        with self.assertRaises(TypeError):
            self.from_rate("spam")

    def test_from_func1(self):
        # check instantiation from keywords / rate provided as func1
        f = ct.Func1(self._rate)
        rxn = self.from_rate(f)
        self.check_rxn(rxn)

    def test_rate_func(self):
        # check result of rate expression
        f = ct.Func1(self._rate)
        rate = ct.CustomRate(f)
        self.assertNear(rate(self.gas.T), self.gas.forward_rate_constants[self._index])

    def test_custom_lambda(self):
        # check instantiation from keywords / rate provided as lambda function
        rxn = self.from_rate(lambda T: 38.7 * T**2.7 * exp(-3150.15428/T))
        self.check_rxn(rxn)
Beispiel #3
0
 def test_rate_func(self):
     f = ct.Func1(self._rate)
     rate = ct.CustomRate(f)
     self.assertNear(rate(self.gas.T), self.gas.forward_rate_constants[self._index])
Beispiel #4
0
 def test_rate_func(self):
     # check result of rate expression
     f = ct.Func1(self._rate)
     rate = ct.CustomRate(f)
     self.assertNear(rate(self.gas.T), self.gas.forward_rate_constants[self._index])