Exemplo n.º 1
0
def get_ammonical_cupric_eqsys():
    NH3_complexes = [
        'CuNH3/2+', 'Cu(NH3)2/2+', 'Cu(NH3)3/2+', 'Cu(NH3)4/2+', 'Cu(NH3)5/2+'
    ]
    OH_complexes = ['Cu2(OH)2/2+', 'Cu(OH)3-', 'Cu(OH)4/2-']
    substances = [
        Solute.from_formula(n)
        for n in ['H+', 'OH-', 'NH4+', 'NH3', 'H2O', 'Cu/2+'] + NH3_complexes +
        OH_complexes + ['Cu(OH)2(s)']
    ]

    (Hp, OHm, NH4p, NH3, H2O, Cupp, CuNH31pp, CuNH32pp, CuNH33pp, CuNH34pp,
     CuNH35pp, Cu2OH2pp, CuOH3m, CuOH4mm,
     CuOH2) = [s.name for s in substances]
    substances[-1].precipitate = True
    init_conc = {
        Hp: 1e-7,
        OHm: 1e-7,
        NH4p: 0,
        NH3: 1.0,
        Cupp: 1e-2,
        CuNH31pp: 0,
        CuNH32pp: 0,
        CuNH33pp: 0,
        CuNH34pp: 0,
        CuNH35pp: 0,
        H2O: 55.5,
        Cu2OH2pp: 0,
        CuOH2: 0,
        CuOH3m: 0,
        CuOH4mm: 0
    }
    H2O_c = init_conc[H2O]
    w_autop = Equilibrium({H2O: 1}, {Hp: 1, OHm: 1}, 10**-14 / H2O_c)
    NH4p_pr = Equilibrium({NH4p: 1}, {Hp: 1, NH3: 1}, 10**-9.26)
    CuOH2_s = Equilibrium({CuOH2: 1}, {Cupp: 1, OHm: 2}, 10**-18.8)
    CuOH_B3 = Equilibrium({CuOH2: 1, OHm: 1}, {CuOH3m: 1}, 10**-3.6)
    CuOH_B4 = Equilibrium({CuOH2: 1, OHm: 2}, {CuOH4mm: 1}, 10**-2.7)
    Cu2OH2 = Equilibrium({
        Cupp: 2,
        H2O: 2
    }, {
        Cu2OH2pp: 1,
        Hp: 2
    }, 10**-10.6 / H2O_c**2)
    CuNH3_B1 = Equilibrium({CuNH31pp: 1}, {Cupp: 1, NH3: 1}, 10**-4.3)
    CuNH3_B2 = Equilibrium({CuNH32pp: 1}, {Cupp: 1, NH3: 2}, 10**-7.9)
    CuNH3_B3 = Equilibrium({CuNH33pp: 1}, {Cupp: 1, NH3: 3}, 10**-10.8)
    CuNH3_B4 = Equilibrium({CuNH34pp: 1}, {Cupp: 1, NH3: 4}, 10**-13.0)
    CuNH3_B5 = Equilibrium({CuNH35pp: 1}, {Cupp: 1, NH3: 5}, 10**-12.4)
    equilibria = (w_autop, NH4p_pr, CuNH3_B1, CuNH3_B2, CuNH3_B3, CuNH3_B4,
                  CuNH3_B5, Cu2OH2, CuOH_B3, CuOH_B4, CuOH2_s)

    new_eqs = CuOH2_s - CuOH_B3, CuOH2_s - CuOH_B4
    skip_subs, skip_eq = (1, 3)
    simpl_subs = substances[:-skip_subs]
    simpl_eq = equilibria[:-skip_eq] + new_eqs
    simpl_c0 = {k.name: init_conc[k.name] for k in substances[:-skip_subs]}
    return EqSystem(simpl_eq, simpl_subs), simpl_c0
Exemplo n.º 2
0
    def test_equilibrate_pH_non_trivial(init_conc, env_T):

        equilibria = {
            'water':
            Equilibrium.from_string(f"H2O = H+ + OH-; {K_H2O / M / M}"),
            'ammonia':
            Equilibrium.from_string(
                f"NH3 + H2O = NH4+ + OH-; {EQUILIBRIUM_CONST['K_NH3'].at(env_T) / M}"
            ),
            'sulfonic_first':
            Equilibrium.from_string(
                f"H2SO3(aq) = H+ + HSO3-; {EQUILIBRIUM_CONST['K_SO2'].at(env_T) / M}"
            ),
            'sulfonic_second':
            Equilibrium.from_string(
                f"HSO3- = H+ + SO3-2; {EQUILIBRIUM_CONST['K_HSO3'].at(env_T) / M}"
            ),
            'carbonic_first':
            Equilibrium.from_string(
                f"H2CO3(aq) = H+ + HCO3-; {EQUILIBRIUM_CONST['K_CO2'].at(env_T) / M}"
            ),
            'carbonic_second':
            Equilibrium.from_string(
                f"HCO3- = H+ + CO3-2; {EQUILIBRIUM_CONST['K_HCO3'].at(env_T) / M}"
            )
        }
        substances = [
            Species.from_formula(f) for f in
            'H2O OH- H+ NH3 NH4+ H2CO3(aq) HCO3- CO3-2 H2SO3(aq) HSO3- SO3-2'.
            split()
        ]
        eqsys = EqSystem(equilibria.values(), substances)

        x, sol, sane = eqsys.root(init_conc)
        assert sol['success'] and sane

        H_idx = 2
        assert substances[H_idx].name == 'H+'
        expected_pH = -np.log10(x[H_idx])

        eqs = {}
        for key in EQUILIBRIUM_CONST.keys():
            eqs[key] = np.full(1, EQUILIBRIUM_CONST[key].at(env_T))

        actual_pH = np.empty(1)
        formulae = Formulae()
        ChemistryMethods.equilibrate_H_body(
            within_tolerance=formulae.trivia.within_tolerance,
            pH2H=formulae.trivia.pH2H,
            H2pH=formulae.trivia.H2pH,
            N_mIII=np.full(1, init_conc['NH3'] * 1e3),
            N_V=np.full(1, init_conc['HNO3(aq)'] * 1e3),
            C_IV=np.full(1, init_conc['H2CO3(aq)'] * 1e3),
            S_IV=np.full(1, init_conc['H2SO3(aq)'] * 1e3),
            S_VI=np.full(1, init_conc['HSO4-'] * 1e3),
            K_HNO3=eqs['K_HNO3'].data,
            K_HCO3=eqs['K_HCO3'].data,
            K_HSO3=eqs['K_HSO3'].data,
            K_HSO4=eqs['K_HSO4'].data,
            K_CO2=eqs['K_CO2'].data,
            K_NH3=eqs['K_NH3'].data,
            K_SO2=eqs['K_SO2'].data,
            cell_id=np.zeros(1, dtype=int),
            # output
            do_chemistry_flag=np.empty(1),
            pH=actual_pH,
            # params
            H_min=formulae.trivia.pH2H(aqueous_chemistry.default_pH_max),
            H_max=formulae.trivia.pH2H(aqueous_chemistry.default_pH_min),
            ionic_strength_threshold=aqueous_chemistry.
            default_ionic_strength_threshold,
            rtol=aqueous_chemistry.default_pH_rtol)

        np.testing.assert_allclose(actual_pH[0], expected_pH, rtol=1e-5)
Exemplo n.º 3
0
# 计算化学式及化学式质量

from chempy import Substance
ferricyanide = Substance.from_formula('Fe(CN)6-3')
ferricyanide.composition == {0: -3, 26: 1, 6: 6, 7: 6}  # 0 for charge

print(ferricyanide.unicode_name)
print(ferricyanide.latex_name + ", " + ferricyanide.html_name)
print('%.3f' % ferricyanide.mass)

# 测量PH值

from collections import defaultdict
from chempy.equilibria import EqSystem
eqsys = EqSystem.from_string("""HCO3- = H+ + CO3-2; 10**-10.3
H2CO3 = H+ + HCO3-; 10**-6.3
H2O = H+ + OH-; 10**-14/55.4
""")
arr, info, sane = eqsys.root(defaultdict(float, {'H2O': 55.4, 'HCO3-': 1e-2}))
conc = dict(zip(eqsys.substances, arr))
from math import log10
print("pH: %.2f" % -log10(conc['H+']))

# 测量离子浓度

from chempy import ReactionSystem  # The rate constants below are arbitrary
rsys = ReactionSystem.from_string("""2 Fe+2 + H2O2 -> 2 Fe+3 + 2 OH-; 42
	   2 Fe+3 + H2O2 -> 2 Fe+2 + O2 + 2 H+; 17
       H+ + OH- -> H2O; 1e10
       H2O -> H+ + OH-; 1e-4""")  # "[H2O]" = 1.0 (actually 55.4 at RT)
from chempy.kinetics.ode import get_odesys
odesys, extra = get_odesys(rsys)
def get_ammonical_cupric_eqsys():
    NH3_complexes = ["CuNH3+2", "Cu(NH3)2+2", "Cu(NH3)3+2", "Cu(NH3)4+2", "Cu(NH3)5+2"]
    OH_complexes = ["Cu2(OH)2+2", "Cu(OH)3-", "Cu(OH)4-2"]
    substances = [
        Species.from_formula(n)
        for n in ["H+", "OH-", "NH4+", "NH3", "H2O", "Cu+2"]
        + NH3_complexes
        + OH_complexes
        + ["Cu(OH)2(s)"]
    ]

    (
        Hp,
        OHm,
        NH4p,
        NH3,
        H2O,
        Cupp,
        CuNH31pp,
        CuNH32pp,
        CuNH33pp,
        CuNH34pp,
        CuNH35pp,
        Cu2OH2pp,
        CuOH3m,
        CuOH4mm,
        CuOH2,
    ) = [s.name for s in substances]
    substances[-1].phase_idx = 1
    init_conc = {
        Hp: 1e-7,
        OHm: 1e-7,
        NH4p: 0,
        NH3: 1.0,
        Cupp: 1e-2,
        CuNH31pp: 0,
        CuNH32pp: 0,
        CuNH33pp: 0,
        CuNH34pp: 0,
        CuNH35pp: 0,
        H2O: 55.5,
        Cu2OH2pp: 0,
        CuOH2: 0,
        CuOH3m: 0,
        CuOH4mm: 0,
    }
    H2O_c = init_conc[H2O]
    w_autop = Equilibrium({H2O: 1}, {Hp: 1, OHm: 1}, 10 ** -14 / H2O_c)
    NH4p_pr = Equilibrium({NH4p: 1}, {Hp: 1, NH3: 1}, 10 ** -9.26)
    CuOH2_s = Equilibrium({CuOH2: 1}, {Cupp: 1, OHm: 2}, 10 ** -18.8)
    CuOH_B3 = Equilibrium({CuOH2: 1, OHm: 1}, {CuOH3m: 1}, 10 ** -3.6)
    CuOH_B4 = Equilibrium({CuOH2: 1, OHm: 2}, {CuOH4mm: 1}, 10 ** -2.7)
    Cu2OH2 = Equilibrium(
        {Cupp: 2, H2O: 2}, {Cu2OH2pp: 1, Hp: 2}, 10 ** -10.6 / H2O_c ** 2
    )
    CuNH3_B1 = Equilibrium({CuNH31pp: 1}, {Cupp: 1, NH3: 1}, 10 ** -4.3)
    CuNH3_B2 = Equilibrium({CuNH32pp: 1}, {Cupp: 1, NH3: 2}, 10 ** -7.9)
    CuNH3_B3 = Equilibrium({CuNH33pp: 1}, {Cupp: 1, NH3: 3}, 10 ** -10.8)
    CuNH3_B4 = Equilibrium({CuNH34pp: 1}, {Cupp: 1, NH3: 4}, 10 ** -13.0)
    CuNH3_B5 = Equilibrium({CuNH35pp: 1}, {Cupp: 1, NH3: 5}, 10 ** -12.4)
    equilibria = (
        w_autop,
        NH4p_pr,
        CuNH3_B1,
        CuNH3_B2,
        CuNH3_B3,
        CuNH3_B4,
        CuNH3_B5,
        Cu2OH2,
        CuOH_B3,
        CuOH_B4,
        CuOH2_s,
    )

    new_eqs = CuOH2_s - CuOH_B3, CuOH2_s - CuOH_B4
    skip_subs, skip_eq = (1, 3)
    simpl_subs = substances[:-skip_subs]
    simpl_eq = equilibria[:-skip_eq] + new_eqs
    simpl_c0 = {k.name: init_conc[k.name] for k in substances[:-skip_subs]}
    return EqSystem(simpl_eq, simpl_subs), simpl_c0