def test_equilibrium_CH4_CO2_H2S_liq_gas(temperature, pressure,
                                         num_regression):
    """
    This test checks the capability of solving a ternary mixture with
    @param Temperature
        temperature in Kelvin which will be used to compute equilibrium
    @param Pressure
        pressure in bar which will be used to compute equilibrium
    """

    db = Database("supcrt98.xml")

    editor = ChemicalEditor(db)

    eos_params = CubicEOSParams(
        phase_identification_method=PhaseIdentificationMethod.
        GibbsEnergyAndEquationOfStateMethod, )

    editor.addGaseousPhase(["CH4(g)", "H2S(g)",
                            "CO2(g)"]).setChemicalModelPengRobinson(eos_params)
    editor.addLiquidPhase(["CH4(liq)", "H2S(liq)", "CO2(liq)"
                           ]).setChemicalModelPengRobinson(eos_params)

    system = ChemicalSystem(editor)

    problem = EquilibriumProblem(system)

    problem.setTemperature(temperature, "K")
    problem.setPressure(pressure, "bar")
    problem.add("CH4(g)", 0.60, "mol")
    problem.add("H2S(g)", 0.35, "mol")
    problem.add("CO2(g)", 0.05, "mol")

    solver = EquilibriumSolver(problem.system())

    options = EquilibriumOptions()
    options.hessian = GibbsHessian.Exact
    options.nonlinear.max_iterations = 100
    options.optimum.max_iterations = 200
    options.optimum.ipnewton.step = StepMode.Conservative

    solver.setOptions(options)

    state = ChemicalState(system)

    result = solver.solve(state, problem)

    assert result.optimum.succeeded

    species_amount = {
        "CH4(g)": np.asarray([state.speciesAmount("CH4(g)")]),
        "H2S(g)": np.asarray([state.speciesAmount("H2S(g)")]),
        "CO2(g)": np.asarray([state.speciesAmount("CO2(g)")]),
        "CH4(liq)": np.asarray([state.speciesAmount("CH4(liq)")]),
        "H2S(liq)": np.asarray([state.speciesAmount("H2S(liq)")]),
        "CO2(liq)": np.asarray([state.speciesAmount("CO2(liq)")]),
    }

    num_regression.check(species_amount)
示例#2
0
def test_add_phases_right_use():
    """Test the normal use of addAqueousPhase, addGaseousPhase and addMineralPhase."""
    database = Database("supcrt98.xml")

    # Check adding phase by giving an string with all species
    list_of_aqueous_species_expected = r"H2O\(l\)\s*H\+\s*OH-\s*HCO3-\s*CO2\(aq\)\s*CO3--\s*"
    list_of_gaseous_species_expected = r"H2O\(g\)\s*CO2\(g\)\s*"
    list_of_liquid_species_expected = r"H2O\(liq\)\s*CO2\(liq\)\s*"
    list_of_mineral_species_expected = r"Graphite\s*"

    editor1 = ChemicalEditor(database)
    editor1.addAqueousPhase("H2O(l) H+ OH- HCO3- CO2(aq) CO3--")
    editor1.addGaseousPhase("H2O(g) CO2(g)")
    editor1.addLiquidPhase("H2O(liq) CO2(liq)")
    editor1.addMineralPhase("Graphite")

    aqueous_phase_1 = editor1.aqueousPhase()
    gaseous_phase_1 = editor1.gaseousPhase()
    liquid_phase_1 = editor1.liquidPhase()
    mineral_phases_1 = editor1.mineralPhases()

    aqueous_species_added_1 = _getting_species_names(aqueous_phase_1)
    gaseous_species_added_1 = _getting_species_names(gaseous_phase_1)
    liquid_species_added_1 = _getting_species_names(liquid_phase_1)
    mineral_species_added_1 = _getting_species_names(mineral_phases_1[0])

    assert re.match(list_of_aqueous_species_expected, aqueous_species_added_1)
    assert re.match(list_of_gaseous_species_expected, gaseous_species_added_1)
    assert re.match(list_of_liquid_species_expected, liquid_species_added_1)
    assert re.match(list_of_mineral_species_expected, mineral_species_added_1)

    # Check adding phase by giving a list of string with all species
    editor2 = ChemicalEditor(database)
    editor2.addAqueousPhase(
        ["H2O(l)", "H+", "OH-", "HCO3-", "CO2(aq)", "CO3--"])
    editor2.addGaseousPhase(["H2O(g)", "CO2(g)"])
    editor2.addLiquidPhase(["H2O(liq)", "CO2(liq)"])
    editor2.addMineralPhase(["Graphite"])

    aqueous_phase_2 = editor2.aqueousPhase()
    gaseous_phase_2 = editor2.gaseousPhase()
    liquid_phase_2 = editor2.liquidPhase()
    mineral_phases_2 = editor2.mineralPhases()

    aqueous_species_added_2 = _getting_species_names(aqueous_phase_2)
    gaseous_species_added_2 = _getting_species_names(gaseous_phase_2)
    liquid_species_added_2 = _getting_species_names(liquid_phase_2)
    mineral_species_added_2 = _getting_species_names(mineral_phases_2[0])

    assert re.match(list_of_aqueous_species_expected, aqueous_species_added_2)
    assert re.match(list_of_gaseous_species_expected, gaseous_species_added_2)
    assert re.match(list_of_liquid_species_expected, liquid_species_added_2)
    assert re.match(list_of_mineral_species_expected, mineral_species_added_2)
def test_equilibrium_H2S_liq_gas(temperature, pressure, num_regression):
    db = Database("supcrt98.xml")

    editor = ChemicalEditor(db)

    eos_params = CubicEOSParams(
        model=CubicEOSModel.PengRobinson,
        phase_identification_method=PhaseIdentificationMethod.
        GibbsEnergyAndEquationOfStateMethod,
    )

    editor.addGaseousPhase(["H2S(g)"]).setChemicalModelCubicEOS(eos_params)
    editor.addLiquidPhase(["H2S(liq)"]).setChemicalModelCubicEOS(eos_params)

    system = ChemicalSystem(editor)

    problem = EquilibriumProblem(system)

    problem.setTemperature(temperature, "K")
    problem.setPressure(pressure, "Pa")
    problem.add("H2S(g)", 1.0, "mol")

    solver = EquilibriumSolver(problem.system())

    options = EquilibriumOptions()
    options.hessian = GibbsHessian.Exact
    options.nonlinear.max_iterations = 100
    options.optimum.max_iterations = 200
    options.optimum.ipnewton.step = StepMode.Conservative
    options.optimum.tolerance = 1e-17
    solver.setOptions(options)

    state = ChemicalState(system)

    result = solver.solve(state, problem)

    assert result.optimum.succeeded

    species_amounts = {
        "H2S(g)": np.asarray([state.speciesAmount("H2S(g)")]),
        "H2S(liq)": np.asarray([state.speciesAmount("H2S(liq)")]),
    }

    num_regression.check(species_amounts)
示例#4
0
def test_chemical_editor_create_system():
    expected = [
        "H2O(l)", "H+", "OH-", "H2O(g)", "CO2(g)", "H2O(liq)", "CO2(liq)",
        "Graphite"
    ]

    database = Database("supcrt98.xml")

    editor = ChemicalEditor(database)

    editor.addAqueousPhase("H2O(l) H+ OH-")
    editor.addGaseousPhase("H2O(g) CO2(g)")
    editor.addLiquidPhase("H2O(liq) CO2(liq)")
    editor.addMineralPhase("Graphite")

    system = ChemicalSystem(editor)

    species_name = []
    for specie in system.species():
        species_name.append(specie.name())

    assert species_name == expected
def test_equilibrium_CH4_H2S_CO2_H2O_liq_gas_aq(temperature, pressure,
                                                num_regression):
    """
    This test checks the capability of solving a system that has CH4, H2S,
    CO2, H2O with
    @param Temperature
        temperature in Kelvin which will be used to compute equilibrium
    @param Pressure
        pressure in bar which will be used to compute equilibrium
    """

    db = Database("supcrt98.xml")

    editor = ChemicalEditor(db)

    eos_params = CubicEOSParams(
        phase_identification_method=PhaseIdentificationMethod.
        GibbsEnergyAndEquationOfStateMethod, )

    editor.addAqueousPhase(["CO2(aq)", "H2S(aq)", "H2O(l)"])
    editor.addGaseousPhase(["CH4(g)", "CO2(g)", "H2S(g)",
                            "H2O(g)"]).setChemicalModelCubicEOS(eos_params)
    editor.addLiquidPhase(["CH4(liq)", "CO2(liq)", "H2S(liq)",
                           "H2O(liq)"]).setChemicalModelCubicEOS(eos_params)

    system = ChemicalSystem(editor)

    problem = EquilibriumProblem(system)

    problem.setTemperature(temperature, "K")
    problem.setPressure(pressure, "bar")
    problem.add("H2O(g)", 0.50, "mol")
    problem.add("CO2(g)", 0.05, "mol")
    problem.add("H2S(g)", 0.40, "mol")
    problem.add("CH4(g)", 0.05, "mol")

    # This is a workaround to avoid an Eigen assertion when in Debug:
    # `DenseBase::resize() does not actually allow to resize.`, triggered by `y(iee) = optimum_state.y * RT;`
    problem.add("Z", 1e-15, "mol")

    solver = EquilibriumSolver(problem.system())

    options = EquilibriumOptions()
    options.hessian = GibbsHessian.Exact
    options.nonlinear.max_iterations = 100
    options.optimum.max_iterations = 200
    options.optimum.ipnewton.step = StepMode.Conservative
    options.optimum.tolerance = 1e-14
    solver.setOptions(options)

    state = ChemicalState(system)

    result = solver.solve(state, problem)

    assert result.optimum.succeeded

    species_amount = {
        "CO2(aq)": np.asarray([state.speciesAmount("CO2(g)")]),
        "H2S(aq)": np.asarray([state.speciesAmount("H2S(aq)")]),
        "H2O(l)": np.asarray([state.speciesAmount("H2O(l)")]),
        "CH4(g)": np.asarray([state.speciesAmount("CH4(g)")]),
        "CO2(g)": np.asarray([state.speciesAmount("CO2(g)")]),
        "H2S(g)": np.asarray([state.speciesAmount("H2S(g)")]),
        "H2O(g)": np.asarray([state.speciesAmount("H2O(g)")]),
        "CH4(liq)": np.asarray([state.speciesAmount("CH4(liq)")]),
        "CO2(liq)": np.asarray([state.speciesAmount("CO2(liq)")]),
        "H2S(liq)": np.asarray([state.speciesAmount("H2S(liq)")]),
        "H2O(liq)": np.asarray([state.speciesAmount("H2O(liq)")]),
    }

    num_regression.check(species_amount)