def equilibrium_problem_with_h2o_feoh2_feoh3_nh3_magnetite(): ''' Build a problem with H2O, Fe(OH)2, Fe(OH)3, NH3 and Magnetite ''' database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhase("H2O Fe(OH)2 Fe(OH)3 NH3") editor.addGaseousPhase("NH3(g)") editor.addMineralPhase("Magnetite") system = ChemicalSystem(editor) problem = EquilibriumProblem(system) problem.add("H2O", 1, "kg") problem.add("Fe(OH)2", 1, "mol") problem.add("Fe(OH)3", 2, "mol") problem.add("NH3", 1, "mol") return (system, problem)
def equilibrium_inverse_with_h2o_nacl_caco3_co2_calcite_fixed_phase_volume(): ''' Build a problem with H2O, NaCl, CaCO3, CO2 and Calcite with fixed values of phase volume ''' editor = ChemicalEditor() editor.addAqueousPhase("H2O NaCl CaCO3") editor.addGaseousPhase(["H2O(g)", "CO2(g)"]) editor.addMineralPhase("Calcite") system = ChemicalSystem(editor) problem = EquilibriumInverseProblem(system) problem.add("H2O", 1, "kg") problem.add("NaCl", 0.1, "mol") problem.fixPhaseVolume("Gaseous", 0.2, "m3", "CO2") problem.fixPhaseVolume("Aqueous", 0.3, "m3", "1 kg H2O; 0.1 mol NaCl") problem.fixPhaseVolume("Calcite", 0.5, "m3", "CaCO3") return (system, problem)
def equilibrium_inverse_with_h_o_na_cl_ca_mg_c_defined_ph(): ''' Build a problem with H, Na, Cl, Ca, Mg, C with defined pH ''' database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhase("H O Na Cl Ca Mg C") editor.addGaseousPhase("H O C") system = ChemicalSystem(editor) problem = EquilibriumInverseProblem(system) problem.add("H2O", 1, "kg") problem.add("NaCl", 0.1, "mol") problem.add("CaCl2", 2, "mmol") problem.add("MgCl2", 4, "mmol") problem.pH(4.0, "CO2") return (system, problem)
def equilibrium_inverse_with_h2o_nacl_caco3_co2_fixed_mass_amount_and_alkalinity(): """ Build a problem with H2O, NaCl, CaCO3, CO2 and Calcite with fixed values of Species Mass, Amount and alkalinity """ editor = ChemicalEditor() editor.addAqueousPhaseWithElementsOf("H2O NaCl CaCO3") editor.addGaseousPhase(["H2O(g)", "CO2(g)"]) editor.addMineralPhase("Calcite") system = ChemicalSystem(editor) problem = EquilibriumInverseProblem(system) problem.add("H2O", 1, "kg") problem.add("NaCl", 0.1, "mol") problem.fixSpeciesMass("Calcite", 100, "g") problem.fixSpeciesAmount("CO2(g)", 1.0, "mol") problem.alkalinity(25.0, "meq/L", "Cl") return (system, problem)
def test_add_phases_wrong_use(): """Test the wrong usage of addAqueousPhase, addGaseousPhase and addMineralPhase.""" editor = ChemicalEditor() with pytest.raises(RuntimeError): editor.addAqueousPhase("H2O(l) C Ca") with pytest.raises(RuntimeError): editor.addAqueousPhase(["H2O C Ca"]) with pytest.raises(RuntimeError): editor.addGaseousPhase("CO2(g) H") with pytest.raises(RuntimeError): editor.addGaseousPhase(["CO2"]) with pytest.raises(RuntimeError): editor.addMineralPhase("Siderita C") with pytest.raises(RuntimeError): editor.addMineralPhase(["CaCO3"])
def test_equilibrium_CH4_liq_gas(temperature, pressure, num_regression): db = Database("supcrt98.xml") editor = ChemicalEditor(db) eos_params = CubicEOSParams() eos_params.phase_identification_method = PhaseIdentificationMethod.GibbsEnergyAndEquationOfStateMethod editor.addGaseousPhase(["CH4(g)"]).setChemicalModelPengRobinson(eos_params) editor.addLiquidPhase(["CH4(liq)" ]).setChemicalModelPengRobinson(eos_params) system = ChemicalSystem(editor) problem = EquilibriumProblem(system) problem.setTemperature(temperature, "K") problem.setPressure(pressure, "Pa") problem.add("CH4(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 = { "CH4(g)": np.asarray([state.speciesAmount("CH4(g)")]), "CH4(liq)": np.asarray([state.speciesAmount("CH4(liq)")]), } num_regression.check(species_amounts)
def equilibrium_inverse_with_h2o_nacl_caco3_calcilte_and_fixed_mass(): """ Build a problem with H2O, NaCL, CaCO3, CO2, Calcite with fixed species mass and amount """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhase("H2O NaCl CaCO3") editor.addGaseousPhase(["H2O(g)", "CO2(g)"]) editor.addMineralPhase("Calcite") system = ChemicalSystem(editor) problem = EquilibriumInverseProblem(system) problem.add("H2O", 1, "kg") problem.add("NaCl", 0.1, "mol") problem.fixSpeciesMass("Calcite", 100, "g") problem.fixSpeciesAmount("CO2(g)", 1.0, "mol") return (system, problem)
def equilibrium_problem_with_h2o_co2_nacl_halite_60C_300bar(): """ Build a problem with 1 kg of H2O, 100 g of CO2 and 0.1 mol of NaCl at 60 °C and 300 bar """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhase("H2O NaCl CO2") editor.addGaseousPhase(["H2O(g)", "CO2(g)"]) editor.addMineralPhase("Halite") system = ChemicalSystem(editor) problem = EquilibriumProblem(system) problem.add("H2O", 1, "kg") problem.add("CO2", 100, "g") problem.add("NaCl", 0.1, "mol") problem.setTemperature(60, "celsius") problem.setPressure(300, "bar") return (system, problem)
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 equilibrium_inverse_with_h_o_na_cl_ca_mg_c_fixed_amount_and_activity(): """ Build a problem with H, Na, Cl, Ca, Mg, C with fixed species amount, activity and defined pH """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhase("H O Na Cl Ca Mg C") editor.addGaseousPhase("H O C") system = ChemicalSystem(editor) problem = EquilibriumInverseProblem(system) problem.add("H2O", 1, "kg") problem.add("NaCl", 0.1, "mol") problem.add("CaCl2", 2, "mmol") problem.add("MgCl2", 4, "mmol") problem.pH(3.0, "HCl") problem.fixSpeciesAmount("CO2(g)", 1.0, "mol") problem.fixSpeciesActivity("O2(g)", 0.20) return (system, problem)
def equilibrium_problem_with_h2o_co2_nacl_halite_dissolved_60C_300bar(): """ Build a problem with H2O, H+, Na+, Cl-, HCO3-, CO2(aq), CO3-- and Halite at 60 °C and 300 bar """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhase( ["CO(aq)", "CO2(aq)", "CO3--", "Cl-", "ClO-", "ClO2-", "ClO3-", "ClO4-", "H+", "H2(aq)", "H2O(l)", "H2O2(aq)", "HCO3-", "HCl(aq)", "HClO(aq)", "HClO2(aq)", "HO2-", "Na+", "NaCl(aq)", "NaOH(aq)", "O2(aq)", "OH-"] ).setActivityModelDrummondCO2() editor.addGaseousPhase(["H2O(g)", "CO2(g)"]).setChemicalModelSpycherPruessEnnis() editor.addMineralPhase("Halite") system = ChemicalSystem(editor) problem = EquilibriumProblem(system) problem.add("H2O", 1, "kg") problem.add("CO2", 100, "g") problem.add("NaCl", 1, "mol") problem.setTemperature(60, "celsius") problem.setPressure(300, "bar") return (system, problem)
def test_different_results(state_regression): from reaktoro import ChemicalEditor, ChemicalState, ChemicalSystem, Database, EquilibriumProblem, EquilibriumSolver, Partition database = Database('supcrt07.xml') editor = ChemicalEditor(database) aqueous_elements = ["C", "Ca", "Cl", "Fe", "H", "Na", "O", "S", "Ba", "Sr"] aqueous_phase = editor.addAqueousPhaseWithElements(aqueous_elements) assert aqueous_phase.name() == 'Aqueous' mineral_species = [ "Anhydrite", "Barite", "Calcite", "Celestite", "Siderite", "Pyrrhotite" ] for mineral in mineral_species: editor.addMineralPhase(mineral) gaseous_species = ["CO2(g)", "H2S(g)", "CH4(g)"] editor.addGaseousPhase(gaseous_species) chemical_system = ChemicalSystem(editor) element_index = { e.name(): index for index, e in enumerate(chemical_system.elements()) } species_index = { s.name(): index for index, s in enumerate(chemical_system.species()) } phase_index = { p.name(): index for index, p in enumerate(chemical_system.phases()) } reaktoro_case = get_reaktoro_case() equilibrium_problem = EquilibriumProblem(chemical_system) equilibrium_problem.setTemperature(reaktoro_case.temperature_in_K) equilibrium_problem.setPressure(reaktoro_case.pressure_in_Pa) partition = Partition(chemical_system) partition.setInertPhases([phase_index['Gaseous']]) equilibrium_problem.setPartition(partition) chemical_state = ChemicalState(chemical_system) for name, index, molar_amount in reaktoro_case.species_amounts: assert index == species_index[name] chemical_state.setSpeciesAmount(index, molar_amount) equilibrium_problem.addState(chemical_state) solver = EquilibriumSolver(chemical_system) solver.setPartition(partition) result = solver.solve(chemical_state, equilibrium_problem) assert result.optimum.succeeded state_regression.check(chemical_state, default_tol=dict(atol=1e-5, rtol=1e-14))
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)
def chemical_editor(): editor = ChemicalEditor() editor.addAqueousPhase("H2O(l) H+ OH- HCO3- CO2(aq) CO3--".split()) editor.addGaseousPhase("H2O(g) CO2(g)".split()) editor.addMineralPhase("Graphite") return editor