1# -*- coding: utf-8 -*- 2 3try: 4 import numpy as np 5except ImportError: 6 np = None 7else: 8 from ..stoich import ( 9 get_coeff_mtx, 10 decompose_yields, 11 ) 12from chempy.units import units_library, allclose, _sum 13from ..testing import requires 14 15 16@requires("numpy") 17def test_get_coeff_mtx(): 18 r = [({"A": 1}, {"B": 1}), ({"A": 1, "B": 1}, {"C": 2})] 19 A = get_coeff_mtx("ABC", r) 20 Aref = np.array([[-1, -1], [1, -1], [0, 2]]) 21 assert np.allclose(A, Aref) 22 23 24@requires("numpy") 25def test_decompose_yields_1(): 26 from chempy import Reaction 27 28 gamma_yields = { 29 "OH-": 0.5, 30 "H2O2": 0.7, 31 "OH": 2.7, 32 "H2": 0.45, 33 "H": 0.66, 34 "H+": 3.1, 35 "HO2": 0.02, 36 "e-(aq)": 2.6, 37 } 38 39 rxns = [ 40 Reaction({"H2O": 1}, {"H+": 1, "OH-": 1}), 41 Reaction({"H2O": 1}, {"H+": 1, "e-(aq)": 1, "OH": 1}), 42 Reaction({"H2O": 1}, {"H": 2, "H2O2": 1}, inact_reac={"H2O": 1}), 43 Reaction({"H2O": 1}, {"H2": 1, "H2O2": 1}, inact_reac={"H2O": 1}), 44 Reaction({"H2O": 1}, {"H2": 1, "OH": 2}, inact_reac={"H2O": 1}), 45 Reaction({"H2O": 1}, {"H2": 3, "HO2": 2}, inact_reac={"H2O": 3}), 46 ] 47 48 k = decompose_yields(gamma_yields, rxns) 49 k_ref = [0.5, 2.6, 0.33, 0.37, 0.05, 0.01] 50 51 assert np.allclose(k, k_ref) 52 53 G_H2O = sum(rxn.net_stoich(["H2O"])[0] * k[i] for i, rxn in enumerate(rxns)) 54 55 assert abs(G_H2O + 4.64) < 1e-3 56 57 58@requires(units_library) 59def test_decompose_yields__units_1(): 60 from chempy import Reaction 61 from chempy.units import default_units as u 62 63 gamma_yields = { 64 "OH-": 0.5 * u.per100eV, 65 "H2O2": 0.7 * u.per100eV, 66 "OH": 2.7 * u.per100eV, 67 "H2": 0.45 * u.per100eV, 68 "H": 0.66 * u.per100eV, 69 "H+": 3.1 * u.per100eV, 70 "HO2": 0.02 * u.per100eV, 71 "e-(aq)": 2.6 * u.per100eV, 72 } 73 74 rxns = [ 75 Reaction({"H2O": 1}, {"H+": 1, "OH-": 1}), 76 Reaction({"H2O": 1}, {"H+": 1, "e-(aq)": 1, "OH": 1}), 77 Reaction({"H2O": 1}, {"H": 2, "H2O2": 1}, inact_reac={"H2O": 1}), 78 Reaction({"H2O": 1}, {"H2": 1, "H2O2": 1}, inact_reac={"H2O": 1}), 79 Reaction({"H2O": 1}, {"H2": 1, "OH": 2}, inact_reac={"H2O": 1}), 80 Reaction({"H2O": 1}, {"H2": 3, "HO2": 2}, inact_reac={"H2O": 3}), 81 ] 82 83 k = decompose_yields(gamma_yields, rxns) 84 k_ref = [0.5, 2.6, 0.33, 0.37, 0.05, 0.01] * u.per100eV 85 86 assert allclose(k, k_ref) 87 88 G_H2O = [rxn.net_stoich(["H2O"])[0] * k[i] for i, rxn in enumerate(rxns)] 89 ref = 4.64 * u.per100eV 90 assert abs((_sum(G_H2O) + ref) / ref) < 1e-3 91 92 93@requires("numpy") 94def test_decompose_yields_2(): 95 from chempy import Reaction 96 97 yields = {"B": 3.0, "C": 24.0} 98 rxns = [ 99 Reaction({"A": 1}, {"B": 1, "C": 1}, inact_reac={"A": 1}), 100 Reaction({"A": 1}, {"C": 3}), 101 ] 102 k = decompose_yields(yields, rxns) 103 k_ref = [3, 7] 104 105 rtol = 1e-12 106 for a, b in zip(k, k_ref): 107 assert abs(a - b) < abs(a * rtol) 108