Ejemplo n.º 1
0
def test_get_native__first_step(solve):
    integrator = 'cvode'
    forgive = 20

    def k(num):
        return "MassAction(unique_keys=('k%d',))" % num

    lines = [  # fictitious isomerization
        "CNO -> ONC; %s" % k(1),
        "ONC -> NCO; %s" % k(2),
        "NCO -> CON; %s" % k(3)
    ]
    rsys = ReactionSystem.from_string('\n'.join(lines), 'CNO ONC NCO CON')
    odesys, extra = get_odesys(rsys, include_params=False)
    if len(solve) > 0:
        from pyodesys.symbolic import PartiallySolvedSystem
        odesys = PartiallySolvedSystem(odesys, extra['linear_dependencies'](solve))
    c0 = defaultdict(float, {'CNO': .7})
    rate_coeffs = (1e78, 2, 3.)
    args = (5, c0, dict(zip('k1 k2 k3'.split(), rate_coeffs)))
    kwargs = dict(integrator=integrator, atol=1e-9, rtol=1e-9, nsteps=1000)
    native = get_native(rsys, odesys, integrator)

    h0 = extra['max_euler_step_cb'](0, *args[1:])
    xout1, yout1, info1 = odesys.integrate(*args, first_step=h0, **kwargs)
    xout2, yout2, info2 = native.integrate(*args, **kwargs)
    ref1 = decay_get_Cref(rate_coeffs, [c0[key] for key in native.names], xout1)
    ref2 = decay_get_Cref(rate_coeffs, [c0[key] for key in native.names], xout2)
    allclose_kw = dict(atol=kwargs['atol']*forgive, rtol=kwargs['rtol']*forgive)

    assert np.allclose(yout1[:, :3], ref1, **allclose_kw)

    assert info2['success']
    assert info2['nfev'] > 10 and info2['nfev'] > 1 and info2['time_cpu'] < 10 and info2['time_wall'] < 10
    assert np.allclose(yout2[:, :3], ref2, **allclose_kw)
Ejemplo n.º 2
0
def test_get_odesys__linear_dependencies__PartiallySolvedSystem(preferred):
    import sympy
    from pyodesys.symbolic import PartiallySolvedSystem
    rsys = ReactionSystem.from_string('\n'.join(
        ['H2O -> H+ + OH-; 1e-4', 'OH- + H+ -> H2O; 1e10']))
    odesys, extra = get_odesys(rsys)
    c0 = {'H2O': 0, 'H+': 2e-7, 'OH-': 3e-7}
    h0max = extra['max_euler_step_cb'](0, c0)
    analytic_factory = extra['linear_dependencies']()
    y0 = {k: sympy.Symbol(k + '0') for k in rsys.substances}
    analytic_factory(None, {odesys[k]: v for k, v in y0.items()}, None, sympy)
    psys = PartiallySolvedSystem(odesys, analytic_factory)
    xout, yout, info = psys.integrate(1,
                                      c0,
                                      atol=1e-12,
                                      rtol=1e-10,
                                      first_step=h0max * 1e-12,
                                      integrator='cvode')
    c_reac = c0['H+'], c0['OH-']
    H2O_ref = binary_rev(xout, 1e10, 1e-4, c0['H2O'], max(c_reac), min(c_reac))
    assert np.allclose(yout[:, psys.names.index('H2O')], H2O_ref)
    assert np.allclose(yout[:, psys.names.index('H+')],
                       c0['H+'] + c0['H2O'] - H2O_ref)
    assert np.allclose(yout[:, psys.names.index('OH-')],
                       c0['OH-'] + c0['H2O'] - H2O_ref)
Ejemplo n.º 3
0
def test_get_native__a_substance_no_composition(solve):
    rsys = ReactionSystem.from_string('\n'.join(['H2O -> H2O+ + e-(aq); 1e-8', 'e-(aq) + H2O+ -> H2O; 1e10']))
    odesys, extra = get_odesys(rsys)
    c0 = {'H2O': 0, 'H2O+': 2e-9, 'e-(aq)': 3e-9}
    if len(solve) > 0:
        from pyodesys.symbolic import PartiallySolvedSystem
        odesys = PartiallySolvedSystem(odesys, extra['linear_dependencies'](solve))
    odesys = get_native(rsys, odesys, 'gsl')
    xout, yout, info = odesys.integrate(1, c0, atol=1e-15, rtol=1e-15, integrator='gsl')
    c_reac = c0['H2O+'], c0['e-(aq)']
    H2O_ref = binary_rev(xout, 1e10, 1e-4, c0['H2O'], max(c_reac), min(c_reac))
    assert np.allclose(yout[:, odesys.names.index('H2O')], H2O_ref)
    assert np.allclose(yout[:, odesys.names.index('H2O+')], c0['H2O+'] + c0['H2O'] - H2O_ref)
    assert np.allclose(yout[:, odesys.names.index('e-(aq)')], c0['e-(aq)'] + c0['H2O'] - H2O_ref)
Ejemplo n.º 4
0
def test_get_odesys__linear_dependencies__PartiallySolvedSystem(preferred):
    import sympy
    from pyodesys.symbolic import PartiallySolvedSystem
    rsys = ReactionSystem.from_string('\n'.join(['H2O -> H+ + OH-; 1e-4', 'OH- + H+ -> H2O; 1e10']))
    odesys, extra = get_odesys(rsys)
    c0 = {'H2O': 0, 'H+': 2e-7, 'OH-': 3e-7}
    h0max = extra['max_euler_step_cb'](0, c0)
    analytic_factory = extra['linear_dependencies']()
    y0 = {k: sympy.Symbol(k+'0') for k in rsys.substances}
    analytic_factory(None, {odesys[k]: v for k, v in y0.items()}, None, sympy)
    psys = PartiallySolvedSystem(odesys, analytic_factory)
    xout, yout, info = psys.integrate(1, c0, atol=1e-12, rtol=1e-10, first_step=h0max*1e-12,
                                      integrator='cvode')
    c_reac = c0['H+'], c0['OH-']
    H2O_ref = binary_rev(xout, 1e10, 1e-4, c0['H2O'], max(c_reac), min(c_reac))
    assert np.allclose(yout[:, psys.names.index('H2O')], H2O_ref)
    assert np.allclose(yout[:, psys.names.index('H+')], c0['H+'] + c0['H2O'] - H2O_ref)
    assert np.allclose(yout[:, psys.names.index('OH-')], c0['OH-'] + c0['H2O'] - H2O_ref)
Ejemplo n.º 5
0
def test_get_native__a_substance_no_composition(solve):
    rsys = ReactionSystem.from_string("\n".join(
        ["H2O -> H2O+ + e-(aq); 1e-8", "e-(aq) + H2O+ -> H2O; 1e10"]))
    odesys, extra = get_odesys(rsys)
    c0 = {"H2O": 0, "H2O+": 2e-9, "e-(aq)": 3e-9}
    if len(solve) > 0:
        from pyodesys.symbolic import PartiallySolvedSystem

        odesys = PartiallySolvedSystem(odesys,
                                       extra["linear_dependencies"](solve))
    odesys = get_native(rsys, odesys, "gsl")
    xout, yout, info = odesys.integrate(1,
                                        c0,
                                        atol=1e-15,
                                        rtol=1e-15,
                                        integrator="gsl")
    c_reac = c0["H2O+"], c0["e-(aq)"]
    H2O_ref = binary_rev(xout, 1e10, 1e-4, c0["H2O"], max(c_reac), min(c_reac))
    assert np.allclose(yout[:, odesys.names.index("H2O")], H2O_ref)
    assert np.allclose(yout[:, odesys.names.index("H2O+")],
                       c0["H2O+"] + c0["H2O"] - H2O_ref)
    assert np.allclose(yout[:, odesys.names.index("e-(aq)")],
                       c0["e-(aq)"] + c0["H2O"] - H2O_ref)
Ejemplo n.º 6
0
def test_get_odesys__linear_dependencies__PartiallySolvedSystem(preferred):
    import sympy
    from pyodesys.symbolic import PartiallySolvedSystem

    rsys = ReactionSystem.from_string(
        "\n".join(["H2O -> H+ + OH-; 1e-4", "OH- + H+ -> H2O; 1e10"])
    )
    odesys, extra = get_odesys(rsys)
    c0 = {"H2O": 0, "H+": 2e-7, "OH-": 3e-7}
    h0max = extra["max_euler_step_cb"](0, c0)
    analytic_factory = extra["linear_dependencies"]()
    y0 = {k: sympy.Symbol(k + "0") for k in rsys.substances}
    analytic_factory(None, {odesys[k]: v for k, v in y0.items()}, None, sympy)
    psys = PartiallySolvedSystem(odesys, analytic_factory)
    xout, yout, info = psys.integrate(
        1, c0, atol=1e-12, rtol=1e-10, first_step=h0max * 1e-12, integrator="cvode"
    )
    c_reac = c0["H+"], c0["OH-"]
    H2O_ref = binary_rev(xout, 1e10, 1e-4, c0["H2O"], max(c_reac), min(c_reac))
    assert np.allclose(yout[:, psys.names.index("H2O")], H2O_ref)
    assert np.allclose(yout[:, psys.names.index("H+")], c0["H+"] + c0["H2O"] - H2O_ref)
    assert np.allclose(
        yout[:, psys.names.index("OH-")], c0["OH-"] + c0["H2O"] - H2O_ref
    )