def test_engine_nap_hco3m_higher_T_closed():
    EXPECTED = {
        "sc": (1310.8, -15.0),
        "I": (15.032e-3, 0.1),
        "pH": (8.2, 1.0),
        "DIC": (15 * 1e-3, -1),
    }
    TK = 80.0 + 273.15
    cNap = 15e-3
    cHCO3m = 15e-3
    carbone_total = 15e-3
    args = (np.array([cNap, cHCO3m]), TK, carbone_total)

    feed_compounds = ["Na+", "HCO3-"]
    # initial_feed_mass_balance = None
    # element_mass_balance = ['Na']
    # closing_equation_type = ClosingEquationType.CARBONE_TOTAL

    sys_eq = create_equilibrium(feed_compounds)
    # closing_equation_type, element_mass_balance,
    # initial_feed_mass_balance)

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    assert_solution_result(solution, EXPECTED)
def test_engine_baco3_cacl2_solve_5_2_mM_closed():
    EXPECTED = {  # AQION
        "sc": (1099.5, 15),  # FIXME: add parameters for BaCO3 conductivity
        "I": (15.457e-3, 15.0),
        "pH": (10.71, 2.0),
        "DIC": (5e-3, -1),
        "SI-CaCO3-Calcite": (2.30, 10.0),
        "SI-BaCO3": (2.99, 10.0),
        # Include Vaterite, Aragonite, Amorph, Ikaite (6H2O)
    }

    sys_eq = pyequion.create_equilibrium(feed_compounds=["CaCl2", "BaCO3"],
                                         initial_feed_mass_balance=["Cl-"])

    TK = 25.0 + 273.15
    cCaCl2 = 2e-3
    cBaCO3 = 5e-3
    DIC = 5e-3

    args = (np.array([cCaCl2, cBaCO3]), TK, DIC)
    solution = pyequion.solve_equilibrium(sys_eq, args=args)

    pyequion.print_solution(solution)

    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Calcite"], EXPECTED["SI-CaCO3-Calcite"])
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Witherite"], EXPECTED["SI-BaCO3"])

    solution = pyequion.solve_equilibrium(sys_eq,
                                          args=args,
                                          allow_precipitation=True)

    pyequion.print_solution(solution)
def test_engine_baco3_nahco3_solve_1mM_each_closed():
    EXPECTED = {  # AQION
        "sc": (300.1, 15),  # FIXME: add parameters for BaCO3 conductivity
        "I": (4.16e-3, 4.0),
        "pH": (10.04, 2.0),
        "DIC": (2e-3, -1),
        "SI": (2.08, 3.0),
    }
    TK = 25.0 + 273.15
    DIC = EXPECTED["DIC"][0]
    cNaHCO3 = 1e-3
    cBaCO3 = 1e-3
    args = (np.array([cNaHCO3, cBaCO3]), TK, DIC)

    feed_compounds = ["NaHCO3", "BaCO3"]
    # initial_feed_mass_balance = None
    # element_mass_balance = ['Na', 'Ba']
    # closing_equation_type = ClosingEquationType.CARBONE_TOTAL

    sys_eq = create_equilibrium(feed_compounds)

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Witherite"], EXPECTED["SI"])
def test_engine_baco3_solve_1mM_open():
    EXPECTED = {  # AQION
        "sc": (198.5, 15),  # FIXME: add parameters for BaCO3 conductivity
        "I": (2.9652e-3, 4.0),
        "pH": (8.48, 2.0),
        "DIC": (1.97e-3, 5.0),
        "SI": (0.85, 3.0),
    }
    TK = 25.0 + 273.15
    cFeed = 1e-3
    args = (np.array([cFeed]), TK, pyequion.pCO2_ref)

    feed_compounds = ["BaCO3"]
    # initial_feed_mass_balance = None
    # element_mass_balance = ['Ba']
    closing_equation_type = ClosingEquationType.OPEN

    sys_eq = create_equilibrium(feed_compounds,
                                closing_equation_type=closing_equation_type)

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Witherite"], EXPECTED["SI"])
def test_engine_caco3_cacl2_pitzer():
    TK = 25.0 + 273.15
    cNaHCO3 = 80e-3
    cCaCl2 = 33.25e-3
    args = (np.array([cNaHCO3, cCaCl2]), TK, pyequion.pCO2_ref)

    feed_compounds = ["CaCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    closing_equation_type = ClosingEquationType.OPEN

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    x_guess = np.full(sys_eq.idx_control.idx["size"], -1e-2)

    solution = solve_equilibrium(
        sys_eq,
        x_guess=x_guess,
        args=args,
        # jac=nahco3_residual_jacobian
        setup_log_gamma_func=pyequion.setup_log_gamma_pitzer,
        calc_log_gamma=pyequion.calc_log_gamma_pitzer,
    )
    pyequion.print_solution(solution)
    print(solution.specie_names)
    assert np.isclose(9.327, solution.pH,
                      1e-2)  # 9.45 is D.H, check for Pitzer with phreeqc
def test_engine_zemaits_na2so4_caso4():
    # flake8: noqa
    "Was not able to get good aggrement"
    EXPECTED = {  # AQION
        "sc": (1099.5, 15),  # FIXME: add parameters for BaCO3 conductivity
        "I": (15.457e-3, 15.0),
        "pH": (10.71, 2.0),
        "DIC": (5e-3, -1),
        "SI-CaCO3-Calcite": (2.30, 10.0),
        "SI-BaCO3": (2.99, 10.0),
        # Include Vaterite, Aragonite, Amorph, Ikaite (6H2O)
    }

    sys_eq = pyequion.create_equilibrium(feed_compounds=["Na2SO4", "CaSO4"])

    TK = 25.0 + 273.15
    cNa2SO4 = 94.8e-3
    cCaSO4 = 21.8e-3

    args = (np.array([cNa2SO4, cCaSO4]), TK, np.nan)

    # solution = pyequion.solve_equilibrium(sys_eq, args=args)

    # pyequion.print_solution(solution)

    # assert_solution_result(solution, EXPECTED)
    # assert compare_with_expected_perc_tuple(solution.saturation_index['Calcite'], EXPECTED['SI-CaCO3-Calcite'])
    # assert compare_with_expected_perc_tuple(solution.saturation_index['Witherite'], EXPECTED['SI-BaCO3'])

    solution = pyequion.solve_equilibrium(sys_eq,
                                          args=args,
                                          allow_precipitation=True)

    pyequion.print_solution(solution)
def test_engine_mix_solve_1_and_5_closed():
    EXPECTED = {
        "sc": (1272.0, -1),
        "I": (15.885e-3, 1.0),
        "pH": (7.92, 1.0),
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 1e-3
    cCaCl2 = 5e-3
    carbone_total = cNaHCO3  # 1.34e+01 * 1e-3
    args = (
        np.array([cNaHCO3, cCaCl2]),
        TK,
        carbone_total,
    )  # Instead of pCO2->DIC

    feed_compounds = ["NaHCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    # closing_equation_type = ClosingEquationType.CARBONE_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds, initial_feed_mass_balance=initial_feed_mass_balance)

    x_guess = np.full(sys_eq.idx_control.idx["size"], -1e-4)

    solution = solve_equilibrium(
        sys_eq,
        x_guess=x_guess,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
def test_engine_nahco3_solve_15mM_open():
    EXPECTED = {
        "sc": (1336.4, -15.0),
        "I": (16.073e-3, 0.1),
        "pH": (9.24, 1.0),
        "DIC": (1.34e01 * 1e-3, -1),
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 15e-3
    args = (np.array([cNaHCO3]), TK, pyequion.pCO2_ref)

    feed_compounds = ["NaHCO3"]
    initial_feed_mass_balance = None
    element_mass_balance = None
    closing_equation_type = ClosingEquationType.OPEN

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        element_mass_balance,
        initial_feed_mass_balance,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    assert_solution_result(solution, EXPECTED)
def test_engine_water_co2_solve_fixed_pH_closing_equation():
    EXPECTED = {  # from-aqion
        "sc": (8.2, -15.0),
        "I": (0.021e-3, 1.0),
        "pH": (4.68, 1.0),
        "DIC": (1e-3, -1),
    }
    TK = 25.0 + 273.15
    # cFeed = 15e-3
    pH_fixed = EXPECTED["pH"][0]  # mM
    args = (np.array([]), TK, pH_fixed)

    feed_compounds = ["CO2"]
    initial_feed_mass_balance = None
    element_mass_balance = []
    closing_equation_type = ClosingEquationType.PH
    # closing_equation_type=ClosingEquationType.NONE

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        element_mass_balance,
        initial_feed_mass_balance,
    )

    x_guess = np.full(sys_eq.idx_control.idx["size"], -2)

    solution, fsol, _ = solve_equilibrium(sys_eq,
                                          args=args,
                                          x_guess=x_guess,
                                          ret_fsol=True)
    print(fsol.message)
    assert_solution_result(solution, EXPECTED)
    pass
def test_engine_water_co2_solve_closed():
    EXPECTED = {  # from-aqion
        "sc": (8.2, -15.0),
        "I": (0.021e-3, 1.0),
        "pH": (4.68, 1.0),
        "DIC": (1e-3, -1),
    }
    TK = 25.0 + 273.15
    # cFeed = 15e-3
    carbone_total = EXPECTED["DIC"][0]  # mM
    args = (np.array([]), TK, carbone_total)

    feed_compounds = ["CO2"]
    initial_feed_mass_balance = None
    element_mass_balance = []
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        element_mass_balance,
        initial_feed_mass_balance,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    assert_solution_result(solution, EXPECTED)
    pass
def test_engine_activity_external_debye_huckel():
    EXPECTED = {
        "pH": (8.2, 1.0),
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 15e-3
    cCaCl2 = 0.02e-3
    carbone_total = cNaHCO3
    c_feed = np.array([cNaHCO3, cCaCl2])
    args = (c_feed, TK, carbone_total)  # Instead of pCO2->DIC

    feed_compounds = ["NaHCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    element_mass_balance = ["Na", "Ca"]
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        element_mass_balance,
        initial_feed_mass_balance,
    )

    # setup_log_gamma(sys_eq, TK, c_feed)

    x_guess = np.full(sys_eq.idx_control.idx["size"], -1e-4)

    solution = solve_equilibrium(sys_eq,
                                 x_guess=x_guess,
                                 args=args
                                 # jac=nahco3_residual_jacobian
                                 )
    pyequion.print_solution(solution)
    assert np.isclose(EXPECTED["pH"][0], solution.pH, 1e-2)
Пример #12
0
def for_report_code():

    sys_eq = pyequion.create_equilibrium(
        ["Na+", "HCO3-", "Ca++", "Cl-"],
        fixed_elements=["Cl-"],
        possible_aqueous_reactions_in=sys_reactions_simpler,
    )
    pyequion.save_res_to_file(sys_eq, "./eq_nahco3_caco3.py", "res")
    return
def test_engine_mix_solve_1_and_5_closed_precipitation():
    EXPECTED = {
        "sc": (6977.5, -1),
        "I": (99.779e-3, 1.0),
        "pH": (8.85, 1.0),
        "SI": {
            "Calcite": (0.00, 1.0),
            "Aragonite": (-0.14, 1.0),
            "Vaterite": (-0.57, 1.0),
        },
        "sat-conc": {
            "Calcite": (80.0e-3, 1.0),
            "Aragonite": (0.0, -1.0),
            "Vaterite": (0.0, -1.0),
        },
    }
    TK = 25.0 + 273.15
    cCaCO3 = 80e-3
    cCaCl2 = 33.25e-3
    carbone_total = cCaCO3  # 1.34e+01 * 1e-3
    args = (
        np.array([cCaCO3, cCaCl2]),
        TK,
        carbone_total,
    )  # Instead of pCO2->DIC

    feed_compounds = ["CaCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    allow_precipitation = True
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    # This was with an old API, the create_equilibrium is created without solid reaction and
    #  only in the solution function the code is adjust to treat the precipitation
    # x_guess = np.full(sys_eq.idx_control.idx['size'], -1e-1)

    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
        allow_precipitation=allow_precipitation,
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
    for tag in EXPECTED["SI"]:
        assert compare_with_expected_perc_tuple(solution.saturation_index[tag],
                                                EXPECTED["SI"][tag])
        assert compare_with_expected_perc_tuple(
            solution.preciptation_conc[tag], EXPECTED["sat-conc"][tag])
Пример #14
0
def get_caco3_nahco3_equilibrium():
    feed_compounds = ["Na+", "HCO3-", "Ca++", "Cl-"]
    fixed_elements = ["Cl-"]
    # element_mass_balance = ['Na', 'Ca']
    # closing_equation_type = pyequion.ClosingEquationType.CARBON_TOTAL

    sys_eq = pyequion.create_equilibrium(
        feed_compounds,
        fixed_elements=fixed_elements,
        # possible_aqueous_reactions_in=sys_reactions_simpler,
    )
    return sys_eq
Пример #15
0
def test_creating_solution_result_from_x():

    feed_compounds = ["Na+", "HCO3-", "Ca++", "Cl-"]
    fixed_elements = ["Cl-"]

    sys_eq = pyequion.create_equilibrium(
        feed_compounds,
        fixed_elements=fixed_elements,
    )

    comps = {"Na+": 10, "HCO3-": 10, "Ca++": 5.0, "Cl-": 10.0}
    solution_exp = pyequion.solve_solution(comps, sys_eq)
    pyequion.print_solution(solution_exp)

    x = solution_exp.x

    sys_eq2 = pyequion.create_equilibrium(
        feed_compounds,
        fixed_elements=fixed_elements,
    )

    solution_new = pyequion.get_solution_from_x(sys_eq2, x, comps)

    assert np.isclose(solution_exp.pH, solution_new.pH)
def test_engine_with_parenthesis_Mn():
    EXPECTED = {
        "sc": (1336.4, -15.0),
        "I": (17.959e-3, 3.0),
        "pH": (9.18, 1.0),
        "DIC": (1.56e02 * 1e-3, 1.0),
        "SI": {
            "Pyrochroite": (0.61, 1.0),
            "Rhodochrosite": (4.65, 50.0),  # error in thisone...
        },
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 150e-3
    args = (np.array([cNaHCO3]), TK, pyequion.pCO2_ref)

    feed_compounds = ["MnCO3"]
    initial_feed_mass_balance = None
    # initial_feed_mass_balance = ['Cl-']
    element_mass_balance = None
    closing_equation_type = ClosingEquationType.OPEN

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        element_mass_balance,
        initial_feed_mass_balance,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )

    meanMnCO3 = pyequion.get_mean_activity_coeff(solution, feed_compounds[0])
    assert np.isclose(meanMnCO3,
                      solution.gamma[sys_eq.idx_control.idx["Mn++"]], 1e-2)
    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Pyrochroite"],
        EXPECTED["SI"]["Pyrochroite"])
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Rhodochrosite"],
        EXPECTED["SI"]["Rhodochrosite"],
    )
def test_engine_mix_closed_add_IS():
    EXPECTED = {
        "sc": (1314.9, 15),
        "I": (15.083e-3, 1.0),
        "pH": (8.2, 1.0),
        "DIC": (1.5e01 * 1e-3, -1),
        "SI": {
            "Calcite": (-0.53, 5.0),
            "Aragonite": (-0.67, 5.0),
            "Halite": (-7.91, 5.0),
        },
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 15e-3
    cCaCl2 = 0.02e-3
    carbone_total = cNaHCO3
    args = (
        np.array([cNaHCO3, cCaCl2]),
        TK,
        carbone_total,
    )  # Instead of pCO2->DIC

    feed_compounds = ["NaHCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]

    sys_eq = create_equilibrium(
        feed_compounds, initial_feed_mass_balance=initial_feed_mass_balance)

    x_guess = np.full(sys_eq.idx_control.idx["size"], -1e-4)

    solution = solve_equilibrium(
        sys_eq,
        x_guess=x_guess,
        args=args,
        # jac=nahco3_residual_jacobian
    )

    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Halite"], EXPECTED["SI"]["Halite"])
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Calcite"], EXPECTED["SI"]["Calcite"])
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Aragonite"], EXPECTED["SI"]["Aragonite"])
def main_cacl2_meancoeff():

    sys_eq = pyequion.create_equilibrium(feed_compounds=["CaCl2"],
                                         initial_feed_mass_balance=["Cl-"])

    cIn_span = np.geomspace(1e-3, 3.0, 61)
    methods = ["debye", "bromley", "sit", "pitzer"]

    solutions = {
        key: [
            pyequion.solve_solution({"CaCl2": c * 1e3},
                                    sys_eq,
                                    activity_model_type=key) for c in cIn_span
        ]
        for key in methods
    }
    gamma_means = {
        key: np.array([
            pyequion.get_mean_activity_coeff(sol, "CaCl2")
            for sol in solutions[key]
        ])
        for key in methods
    }

    I = {key: np.array([sol.I for sol in solutions[key]]) for key in methods}

    I_exp_cnv = conv_c2I_X1A2(data_exp_raw_collected[:, 0])

    plot_opts = {
        "debye": "-k",
        "bromley": ":k",
        "sit": "-.k",
        "pitzer": "--k",
    }
    plt.figure()
    plt.plot(I_exp_cnv, data_exp_raw_collected[:, 1], "sk", label="exp")
    for key in methods:
        plt.plot(I[key], gamma_means[key], plot_opts[key], label=key)
    plt.legend()

    plt.show()

    return
def test_engine_mix_closed_add_IS_dic_modified():
    EXPECTED = {
        "sc": (1339.4, 15),
        "I": (16.102e-3, 1.0),
        "pH": (9.24, 1.0),
        "DIC": (1.34e01 * 1e-3, -1),
        "SI": (0.20, 200.0),  # FIXME: High error!
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 15e-3
    cCaCl2 = 0.02e-3
    carbone_total = EXPECTED["DIC"][0]  # 1.34e+01 * 1e-3
    args = (
        np.array([cNaHCO3, cCaCl2]),
        TK,
        carbone_total,
    )  # Instead of pCO2->DIC

    feed_compounds = ["NaHCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    x_guess = np.full(sys_eq.idx_control.idx["size"], -1e-4)

    solution = solve_equilibrium(
        sys_eq,
        x_guess=x_guess,
        args=args,
        # jac=nahco3_residual_jacobian
    )

    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Calcite"], EXPECTED["SI"])
def test_engine_nap_hco3m_Capp_clm_closed():
    EXPECTED = {
        "sc": (1339.4, -200),
        "I": (16.102e-3, 1.0),
        "pH": (9.24, 1.0),
        "DIC": (1.34e01 * 1e-3, -1),
    }
    TK = 25.0 + 273.15
    cNap = 15e-3
    cHCO3m = 15e-3
    cCapp = 0.02e-3
    cClm = 0.02e-3
    carbone_total = EXPECTED["DIC"][0]
    # args = (np.array([cNap, cHCO3m, cBaqpp]), TK, carbone_total)

    feed_compounds = ["Na+", "HCO3-", "Ca++", "Cl-"]
    # initial_feed_mass_balance = ['Cl-']
    fixed_elements = ["Cl-"]
    allow_precipitation = False
    # element_mass_balance = ['Na', 'Ca']
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        # closing_equation_type, element_mass_balance,
        closing_equation_type,
        allow_precipitation=allow_precipitation,
        fixed_elements=fixed_elements,
    )

    args = (np.array([cNap, cHCO3m, cCapp, cClm]), TK, carbone_total)
    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
def test_engine_cacl2_solve_open_0p01():
    EXPECTED = {
        "sc": (1336.4, -200),
        "I": (16.073e-3, 200),  # ONLY PH KOWN
        "pH": (5.61, 1.0),
        "DIC": (1.34e01 * 1e-3, -1),
    }
    TK = 25.0 + 273.15
    c_feed = 5 * 1e-3  # Forcing to be an array
    args = (np.array([c_feed]), TK, pyequion.pCO2_ref)
    """
    Big Issue: How to automate the known-mass-balance
    - A specie is removed (CaCl2)
    - A specie is still a variable (Ca)
    - A specie is placed as known (Cl)
    """

    feed_compounds = ["CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    closing_equation_type = ClosingEquationType.OPEN

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    idx = sys_eq.idx_control.idx
    x_guess = np.full(idx["size"], -1e-3)
    x_guess[idx["Ca++"]] = np.log10(c_feed)
    solution = solve_equilibrium(
        sys_eq,
        x_guess=x_guess,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
def test_engine_greg_andr_caso4():

    sys_eq = pyequion.create_equilibrium(feed_compounds=["CaSO4"])

    TK = 25.0 + 273.15
    # cNa2SO4 = 94.8e-3
    cCaSO4 = 15.6e-3

    args = (np.array([cCaSO4]), TK, np.nan)
    solution = pyequion.solve_equilibrium(sys_eq, args=args)

    pyequion.print_solution(solution)

    # assert_solution_result(solution, EXPECTED)
    # assert compare_with_expected_perc_tuple(solution.saturation_index['Calcite'], EXPECTED['SI-CaCO3-Calcite'])
    # assert compare_with_expected_perc_tuple(solution.saturation_index['Witherite'], EXPECTED['SI-BaCO3'])

    solution = pyequion.solve_equilibrium(sys_eq,
                                          args=args,
                                          allow_precipitation=True)

    pyequion.print_solution(solution)
def main_check_jit():
    pyequion.jit_compile_functions()

    sys_eq = pyequion.create_equilibrium(feed_compounds=["CaCl2"],
                                         initial_feed_mass_balance=["Cl-"])
    solution = pyequion.solve_solution(
        {"CaCl2": 1 * 1e3},
        sys_eq,
        activity_model_type="debye",
    )
    print("end first call")

    tstart = time.time()
    solution = pyequion.solve_solution(
        {"CaCl2": 1 * 1e3},
        sys_eq,
        activity_model_type="debye",
    )
    print("Elapsed time = ", time.time() - tstart, solution.pH)

    args = (np.array([1.0]), 25 + 273.15, np.nan)
    tstart = time.time()
    solution = pyequion.solve_equilibrium(sys_eq, args=args)
    print("Elapsed time = ", time.time() - tstart, solution.pH)

    quit()

    tstart = time.time()
    cIn_span = np.geomspace(1e-3, 3.0, 1)
    gamma_mean = np.empty_like(cIn_span)
    for i, c in enumerate(cIn_span):
        solution = pyequion.solve_solution(
            {"CaCl2": c * 1e3},
            sys_eq,
            activity_model_type="debye",
        )
        gamma_mean[i] = pyequion.get_mean_activity_coeff(solution, "CaCl2")  # pylint: disable=unsupported-assignment-operation
    print("Elapsed time = ", time.time() - tstart)
    print(gamma_mean)
def test_engine_mix_default_feed_mb_closed():
    EXPECTED = {
        "sc": (1339.4, 15),
        "I": (16.102e-3, 1.0),
        "pH": (9.24, 1.0),
        "DIC": (1.34e01 * 1e-3, -1),
    }
    TK = 25.0 + 273.15
    cNaHCO3 = 15e-3
    cCaCl2 = 0.02e-3
    carbone_total = EXPECTED["DIC"][0]  # 1.34e+01 * 1e-3
    args = (
        np.array([cNaHCO3, cCaCl2]),
        TK,
        carbone_total,
    )  # Instead of pCO2->DIC

    feed_compounds = ["NaHCO3", "CaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    # element_mass_balance = ['Na', 'Ca']
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        element_mass_balance=None,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    x_guess = np.full(sys_eq.idx_control.idx["size"], -1e-4)

    solution = solve_equilibrium(
        sys_eq,
        x_guess=x_guess,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    assert_solution_result(solution, EXPECTED)
def test_two_irreversible_cacl2_bacl2():
    EXPECTED = {
        "sc": (3417.7, -1),
        "I": (43.02e-3, 3.0),
        "pH": (7.91, 1.0),
    }
    feed_compounds = ["NaHCO3", "CaCl2", "BaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    # closing_eq = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds, initial_feed_mass_balance=initial_feed_mass_balance)

    TK = 25.0 + 273.15
    cNaHCO3 = 15e-3
    cCaCl2 = 5e-3
    cBaCl2 = 5e-3
    carbone_total = cNaHCO3
    args = (np.array([cNaHCO3, cCaCl2, cBaCl2]), TK, carbone_total)
    solution = solve_equilibrium(sys_eq, args=args)
    pyequion.print_solution(solution)

    assert_solution_result(solution, EXPECTED)
def test_engine_baco3_cacl2_nahco3_solve_1mM_each_closed():
    EXPECTED = {  # AQION
        "sc": (484.5, 15),  # FIXME: add parameters for BaCO3 conductivity
        "I": (6.213e-3, 15.0),
        "pH": (9.89, 2.0),
        "DIC": (2e-3, -1),
        "SI-Calcite": (1.73, 5.0),
        "SI-BaCO3": (1.91, 5.0),
    }
    TK = 25.0 + 273.15
    DIC = EXPECTED["DIC"][0]
    cCaCl2 = 1e-3
    cBaCO3 = 1e-3
    cNaHCO3 = 1e-3
    args = (np.array([cCaCl2, cBaCO3, cNaHCO3]), TK, DIC)

    feed_compounds = ["CaCl2", "BaCO3", "NaHCO3"]
    initial_feed_mass_balance = ["Cl-"]
    # element_mass_balance = ['Ca', 'Ba', 'Na']

    sys_eq = create_equilibrium(
        feed_compounds,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Calcite"], EXPECTED["SI-Calcite"])
    assert compare_with_expected_perc_tuple(
        solution.saturation_index["Witherite"], EXPECTED["SI-BaCO3"])
def test_engine_nap_hco3m_capp_clm_open():
    EXPECTED = {
        "sc": (1525.3, 25),  # FIXME
        "I": (20.3e-3, 3.0),
        "pH": (8.06, 1.0),
        "DIC": (15.0 * 1e-3, 5),
        "IS(s)": (2.39, -1),  # TODO nao incluido ainda
    }
    TK = 25.0 + 273.15
    cNap = 15e-3
    cHCO3m = 15e-3
    cBapp = 2e-3
    cClpp = 2 * 2e-3
    carbone_total = 15e-3
    # args = (np.array([cNap, cHCO3m, cBaqpp]), TK, carbone_total)
    args = (np.array([cNap, cHCO3m, cBapp, cClpp]), TK, carbone_total)

    feed_compounds = ["Na+", "HCO3-", "Ca++", "Cl-"]
    fixed_elements = ["Cl-"]
    closing_equation_type = ClosingEquationType.CARBON_TOTAL

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        # element_mass_balance,
        fixed_elements=fixed_elements,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
def test_engine_nap_hco3m_bacl2_open():
    EXPECTED = {
        "sc": (1525.3, 25),  # FIXME
        "I": (20.776e-3, 3.0),
        "pH": (9.22, 1.0),
        "DIC": (13.2 * 1e-3, 5),
        "IS(s)": (2.39, -1),  # TODO nao incluido ainda
    }
    TK = 25.0 + 273.15
    cNap = 15e-3
    cHCO3m = 15e-3
    cBaqpp = 2e-3
    # carbone_total = 15e-3
    # args = (np.array([cNap, cHCO3m, cBaqpp]), TK, carbone_total)
    args = (np.array([cNap, cHCO3m, cBaqpp]), TK, pyequion.pCO2_ref)

    feed_compounds = ["Na+", "HCO3-", "BaCl2"]
    initial_feed_mass_balance = ["Cl-"]
    # element_mass_balance = ['Na', 'Ba']
    closing_equation_type = ClosingEquationType.OPEN

    sys_eq = create_equilibrium(
        feed_compounds,
        closing_equation_type,
        # element_mass_balance,
        initial_feed_mass_balance=initial_feed_mass_balance,
    )

    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
def test_engine_nap_hco3m_Capp_clm_check_hco3_conc_closed():
    EXPECTED = {
        "sc": (1272.0, -1),
        "I": (15.885e-3, 1.0),
        "pH": (7.92, 1.0),
    }
    TK = 25.0 + 273.15
    cNap = 1e-3
    cHCO3m = 1e-3
    cCapp = 5e-3
    cClm = 10e-3  # CAREFUL HERE-> !! STOIC CL*2
    carbone_total = cHCO3m
    # args = (np.array([cNap, cHCO3m, cBaqpp]), TK, carbone_total)

    feed_compounds = ["Na+", "HCO3-", "Ca++", "Cl-"]
    # initial_feed_mass_balance = ['Cl-']
    fixed_elements = ["Cl-"]
    allow_precipitation = False
    # element_mass_balance = ['Na', 'Ca']

    sys_eq = create_equilibrium(
        feed_compounds,
        # element_mass_balance,
        allow_precipitation=allow_precipitation,
        fixed_elements=fixed_elements,
    )

    args = (np.array([cNap, cHCO3m, cCapp, cClm]), TK, carbone_total)
    # x_guess = np.full(nahco3_eq.idx.size, -1.0)
    solution = solve_equilibrium(
        sys_eq,
        args=args,
        # jac=nahco3_residual_jacobian
    )
    pyequion.print_solution(solution)
    assert_solution_result(solution, EXPECTED)
Пример #30
0
"""Pyequion sample

- Simple usage
"""
import os
import time
import numpy as np

os.environ["NUMBA_DISABLE_JIT"] = "0"
import pyequion

# %%
# CREATING SYSTEM FOR No-JOT (Regular Mode)
esys = pyequion.create_equilibrium(["NaHCO3", "CaCl2"])

t01 = time.time()
sol = pyequion.solve_solution(
    {
        "NaHCO3": 50,
        "CaCl2": 10
    },
    esys,
)
print("Not JIT Elapsed = {}".format(time.time() - t01))

print("NO JIT LOOP")
Tspan = np.linspace(24.0, 28.0, 101)
t01 = time.time()
sol_span = [
    pyequion.solve_solution({
        "NaHCO3": 50,