def test_get_odesys_3(): M = u.molar s = u.second mol = u.mol m = u.metre substances = list(map(Substance, 'H2O H+ OH-'.split())) dissociation = Reaction({'H2O': 1}, {'H+': 1, 'OH-': 1}, 2.47e-5 / s) recombination = Reaction({'H+': 1, 'OH-': 1}, {'H2O': 1}, 1.37e11 / M / s) rsys = ReactionSystem([dissociation, recombination], substances) odesys = get_odesys(rsys, include_params=True, unit_registry=SI_base_registry, output_conc_unit=M)[0] c0 = {'H2O': 55.4 * M, 'H+': 1e-7 * M, 'OH-': 1e-4 * mol / m**3} x, y, p = odesys.pre_process(-42 * u.second, rsys.as_per_substance_array(c0, unit=M)) fout = odesys.f_cb(x, y, p) time_unit = get_derived_unit(SI_base_registry, 'time') conc_unit = get_derived_unit(SI_base_registry, 'concentration') r1 = to_unitless(55.4 * 2.47e-5 * M / s, conc_unit / time_unit) r2 = to_unitless(1e-14 * 1.37e11 * M / s, conc_unit / time_unit) assert abs(fout[0] - r2 + r1) < 1e-10 assert abs(fout[1] - r1 + r2) < 1e-10 assert abs(fout[2] - r1 + r2) < 1e-10
def test_get_odesys__with_units(): a = Substance('A') b = Substance('B') molar = u.molar second = u.second r = Reaction({'A': 2}, {'B': 1}, param=1e-3 / molar / second) rsys = ReactionSystem([r], [a, b]) odesys = get_odesys(rsys, include_params=True, unit_registry=SI_base_registry)[0] c0 = {'A': 13 * u.mol / u.metre**3, 'B': .2 * u.molar} conc_unit = get_derived_unit(SI_base_registry, 'concentration') t = np.linspace(0, 10) * u.hour xout, yout, info = odesys.integrate(t, rsys.as_per_substance_array( c0, unit=conc_unit), atol=1e-10, rtol=1e-12) t_unitless = to_unitless(xout, u.second) Aref = dimerization_irrev(t_unitless, 1e-6, 13.0) # Aref = 1/(1/13 + 2*1e-6*t_unitless) yref = np.zeros((xout.size, 2)) yref[:, 0] = Aref yref[:, 1] = 200 + (13 - Aref) / 2 print((yout - yref * conc_unit) / yout) assert allclose(yout, yref * conc_unit)
def test_get_odesys__with_units(): a = Substance('A') b = Substance('B') molar = u.molar second = u.second r = Reaction({'A': 2}, {'B': 1}, param=1e-3/molar/second) rsys = ReactionSystem([r], [a, b]) odesys = get_odesys(rsys, include_params=True, unit_registry=SI_base_registry)[0] c0 = { 'A': 13*u.mol / u.metre**3, 'B': .2 * u.molar } conc_unit = get_derived_unit(SI_base_registry, 'concentration') t = np.linspace(0, 10)*u.hour xout, yout, info = odesys.integrate( t, rsys.as_per_substance_array(c0, unit=conc_unit), atol=1e-10, rtol=1e-12) t_unitless = to_unitless(xout, u.second) Aref = dimerization_irrev(t_unitless, 1e-6, 13.0) # Aref = 1/(1/13 + 2*1e-6*t_unitless) yref = np.zeros((xout.size, 2)) yref[:, 0] = Aref yref[:, 1] = 200 + (13-Aref)/2 print((yout - yref*conc_unit)/yout) assert allclose(yout, yref*conc_unit)
def test_get_odesys_3(): M = u.molar s = u.second mol = u.mol m = u.metre substances = list(map(Substance, 'H2O H+ OH-'.split())) dissociation = Reaction({'H2O': 1}, {'H+': 1, 'OH-': 1}, 2.47e-5/s) recombination = Reaction({'H+': 1, 'OH-': 1}, {'H2O': 1}, 1.37e11/M/s) rsys = ReactionSystem([dissociation, recombination], substances) odesys = get_odesys( rsys, include_params=True, unit_registry=SI_base_registry, output_conc_unit=M)[0] c0 = {'H2O': 55.4*M, 'H+': 1e-7*M, 'OH-': 1e-4*mol/m**3} x, y, p = odesys.pre_process(-42*u.second, rsys.as_per_substance_array(c0, unit=M)) fout = odesys.f_cb(x, y, p) time_unit = get_derived_unit(SI_base_registry, 'time') conc_unit = get_derived_unit(SI_base_registry, 'concentration') r1 = to_unitless(55.4*2.47e-5*M/s, conc_unit/time_unit) r2 = to_unitless(1e-14*1.37e11*M/s, conc_unit/time_unit) assert abs(fout[0] - r2 + r1) < 1e-10 assert abs(fout[1] - r1 + r2) < 1e-10 assert abs(fout[2] - r1 + r2) < 1e-10
def test_get_odesys_1(): k = .2 a = Substance('A') b = Substance('B') r = Reaction({'A': 1}, {'B': 1}, param=k) rsys = ReactionSystem([r], [a, b]) odesys = get_odesys(rsys, include_params=True)[0] c0 = { 'A': 1.0, 'B': 3.0, } t = np.linspace(0.0, 10.0) xout, yout, info = odesys.integrate(t, rsys.as_per_substance_array(c0)) yref = np.zeros((t.size, 2)) yref[:, 0] = np.exp(-k * t) yref[:, 1] = 4 - np.exp(-k * t) assert np.allclose(yout, yref)
def test_get_odesys_1(): k = .2 a = Substance('A') b = Substance('B') r = Reaction({'A': 1}, {'B': 1}, param=k) rsys = ReactionSystem([r], [a, b]) odesys = get_odesys(rsys, include_params=True)[0] c0 = { 'A': 1.0, 'B': 3.0, } t = np.linspace(0.0, 10.0) xout, yout, info = odesys.integrate(t, rsys.as_per_substance_array(c0)) yref = np.zeros((t.size, 2)) yref[:, 0] = np.exp(-k*t) yref[:, 1] = 4 - np.exp(-k*t) assert np.allclose(yout, yref)
def test_get_odesys_2(): g = Radiolytic([3.14]) a = Substance('A') b = Substance('B') r = Reaction({'A': 1}, {'B': 1}, param=g) rsys = ReactionSystem([r], [a, b]) odesys = get_odesys(rsys, include_params=True)[0] c0 = { 'A': 1.0, 'B': 3.0, } t = np.linspace(0.0, .1) xout, yout, info = odesys.integrate(t, rsys.as_per_substance_array(c0), {'doserate': 2.72, 'density': .998}) yref = np.zeros((t.size, 2)) k = 3.14*2.72*.998 yref[:, 0] = 1 - k*t yref[:, 1] = 3 + k*t assert np.allclose(yout, yref)
def test_get_odesys__time_dep_temperature(): import sympy as sp def refA(t, A0, A, Ea_over_R, T0, dTdt): T = (T0 + dTdt * t) d_Ei = sp.Ei(-Ea_over_R / T0).n(100).round(90) - sp.Ei( -Ea_over_R / T).n(100).round(90) d_Texp = T0 * sp.exp(-Ea_over_R / T0) - T * sp.exp(-Ea_over_R / T) return A0 * sp.exp(A / dTdt * (Ea_over_R * d_Ei + d_Texp)).n(30) params = A0, A, Ea_over_R, T0, dTdt = [13, 1e10, 56e3 / 8, 273, 2] B0 = 11 rate = ArrheniusMassAction([A, Ea_over_R]) rxn = Reaction({'A': 1}, {'B': 3}, rate) rsys = ReactionSystem([rxn], 'A B') rt = RampedTemp([T0, dTdt], ('init_temp', 'ramp_rate')) odesys = get_odesys(rsys, False, substitutions={'temperature': rt})[0] conc = {'A': A0, 'B': B0} x, y, p = odesys.pre_process([2, 5, 10], conc) fout = odesys.f_cb(x, y, p) def r(t): return A * np.exp(-Ea_over_R / (T0 + dTdt * t)) * A0 # refA(t, *params) ref = np.array([[-r(2), -r(5), -r(10)], [3 * r(2), 3 * r(5), 3 * r(10)]]).T assert np.allclose(fout, ref) for ramp_rate in [2, 3, 4]: xout, yout, info = odesys.integrate(10, rsys.as_per_substance_array(conc), {'ramp_rate': ramp_rate}, atol=1e-10, rtol=1e-12) params[-1] = ramp_rate Aref = np.array([float(refA(t, *params)) for t in xout]) # Aref = 1/(1/13 + 2*1e-6*t_unitless) yref = np.zeros((xout.size, 2)) yref[:, 0] = Aref yref[:, 1] = B0 + 3 * (A0 - Aref) assert allclose(yout, yref)
def test_get_odesys_2(): g = Radiolytic([3.14]) a = Substance('A') b = Substance('B') r = Reaction({'A': 1}, {'B': 1}, param=g) rsys = ReactionSystem([r], [a, b]) odesys = get_odesys(rsys, include_params=True)[0] c0 = { 'A': 1.0, 'B': 3.0, } t = np.linspace(0.0, .1) xout, yout, info = odesys.integrate(t, rsys.as_per_substance_array(c0), { 'doserate': 2.72, 'density': .998 }) yref = np.zeros((t.size, 2)) k = 3.14 * 2.72 * .998 yref[:, 0] = 1 - k * t yref[:, 1] = 3 + k * t assert np.allclose(yout, yref)