def _check(T=273.15 * u.kelvin): result = cv['Be']({ 'temperature': T, 'molar_gas_constant': R }, backend=Backend()) ref = 0.7342617587256584 * u.joule / u.gram / u.kelvin assert abs(to_unitless((result - ref) / ref)) < 1e-10
def test_GibbsEqConst__units(): R, T = dc.molar_gas_constant, 298.15 * du.K DH = -4e3 * du.J / du.mol DS = 16 * du.J / du.K / du.mol be = Backend() gee = GibbsEqConst([DH / R, DS / R]) res = gee.eq_const({'temperature': T}, backend=be) ref = be.exp(-(DH - T * DS) / (R * T)) assert allclose(res, ref)
def test_TPolyInLog10MassAction__units(): Mps = u.molar/u.second kunit = 1/u.molar**2/u.second p = MassAction(Constant(kunit)*ShiftedLog10TPoly([2, 0.3, .2, .03, .004])) lgT = Log10('temperature'/Constant(u.K)) r = Reaction({'A': 2, 'B': 1}, {'C': 1}, p, {'B': 1}) res = p({'A': 11*u.molar, 'B': 13*u.molar, 'temperature': 298.15*u.K, 'log10_temperature': lgT}, backend=Backend(), reaction=r) _T = math.log10(298.15) - 2 ref = (.3 + .2*_T + 0.03 * _T**2 + 0.004 * _T**3) * 13 * 11**2 * Mps assert abs(res - ref) < 1e-15*Mps
def test_TPolyInLog10MassAction__units(): Mps = u.molar / u.second kunit = 1 / u.molar**2 / u.second r = TPolyInLog10MassAction( [u.K, 2, 0.3 * kunit, .2 * kunit, .03 * kunit, .004 * kunit]) Reaction({'A': 2, 'B': 1}, {'C': 1}, r, {'B': 1}) res = r({ 'A': 11 * u.molar, 'B': 13 * u.molar, 'temperature': 298.15 * u.K }, backend=Backend()) _T = math.log10(298.15) - 2 ref = .3 + .2 * _T + 0.03 * _T**2 + 0.004 * _T**3 assert abs(res - ref * 13 * 11**2 * Mps) < 1e-15
def test_MassAction__subclass_from_callback__units(): def rate_coeff(variables, all_args, backend, **kwargs): return all_args[0] * backend.exp( all_args[1] / variables["temperature"]) CustomMassAction = MassAction.subclass_from_callback( rate_coeff, cls_attrs=dict(parameter_keys=("temperature", ), nargs=2)) k1 = CustomMassAction([2.1e10 / u.molar**2 / u.second, -5132.2 * u.kelvin]) rxn = Reaction({"H2": 2, "O2": 1}, {"H2O": 2}, k1) variables = { "temperature": 491.67 * u.rankine, "H2": 7000 * u.mol / u.metre**3, "O2": 13 * u.molar, } cma = rxn.rate_expr() res = cma(variables, backend=Backend(), reaction=rxn) ref = 7 * 7 * 13 * 2.1e10 * math.exp(-5132.2 / 273.15) * u.molar / u.second assert allclose(res, ref)
def test_GibbsEqConst__nested(): class TExpr(Expr): argument_names = ('heat_capacity',) parameter_keys = ('energy',) def __call__(self, variables, backend=None): heat_capacity, = self.all_args(variables, backend=backend) energy, = self.all_params(variables, backend=backend) return energy/heat_capacity R = 8.314 * du.J/du.K/du.mol T = TExpr([10.0 * du.J/du.K]) dH, dS = -4e3 * du.J/du.mol, 16 * du.J/du.K/du.mol gee = GibbsEqConst([dH/R, dS/R]) be = Backend() Tref = 298.15 * du.K ref = be.exp(-(dH - Tref*dS)/(R*Tref)) assert be.abs((gee.eq_const({'energy': 2981.5 * du.J, 'temperature': T}, backend=be) - ref)/ref) < 1e-14
def test_TPolyInLog10MassAction__units(): Mps = u.molar / u.second kunit = 1 / u.molar**2 / u.second p = MassAction( Constant(kunit) * ShiftedLog10TPoly([2, 0.3, 0.2, 0.03, 0.004])) lgT = Log10("temperature" / Constant(u.K)) r = Reaction({"A": 2, "B": 1}, {"C": 1}, p, {"B": 1}) res = p( { "A": 11 * u.molar, "B": 13 * u.molar, "temperature": 298.15 * u.K, "log10_temperature": lgT, }, backend=Backend(), reaction=r, ) _T = math.log10(298.15) - 2 ref = (0.3 + 0.2 * _T + 0.03 * _T**2 + 0.004 * _T**3) * 13 * 11**2 * Mps assert abs(res - ref) < 1e-15 * Mps
def test_MassAction__subclass_from_callback__units(): def rate_coeff(variables, all_args, backend): return all_args[0] * backend.exp( all_args[1] / variables['temperature']) CustomMassAction = MassAction.subclass_from_callback( rate_coeff, cls_attrs=dict(parameter_keys=('temperature', ), nargs=2)) k1 = CustomMassAction([2.1e10 / u.molar**2 / u.second, -5132.2 * u.kelvin], rxn=Reaction({ 'H2': 2, 'O2': 1 }, {'H2O': 2})) res = k1( { 'temperature': 491.67 * u.rankine, 'H2': 7000 * u.mol / u.metre**3, 'O2': 13 * u.molar }, backend=Backend()) ref = 7 * 7 * 13 * 2.1e10 * math.exp(-5132.2 / 273.15) * u.molar / u.second assert allclose(res, ref)