def test_equilibrium_solver_approx_overload_3(setup, state_regression): """ An integration test that checks result's reproducibility of the calculation of an equilibrium of a state using EquilibriumSolver::approximate(ChemicalState& state, double T, double P, VectorConstRef be) @param setup a tuple that has some objects from problem setup (system, problem) """ (system, problem) = setup state = ChemicalState(system) solver = EquilibriumSolver(system) T = problem.temperature() P = problem.pressure() b = problem.elementAmounts() solver.approximate(state, T, P, b) exclude = ['pH [-]'] state_regression.check(state, default_tol=dict(atol=1e-5, rtol=1e-14), exclude=exclude)
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)
def _create_chemical_state(chemical_system): # Set the initial amounts of CO2(g) and H2O(g) state = ChemicalState(chemical_system) # Set the initial amounts of CO2(g) and H2O(g) state.setSpeciesAmount('CO2(g)', 1, 'mol') state.setSpeciesAmount('H2O(g)', 1, 'mmol') return state
def test_equilibrium_problem_add_by_chemical_state(partition_with_inert_gaseous_phase, chemical_system): state = ChemicalState(chemical_system) state.setSpeciesAmount("CO2(g)", 10.0) problem = EquilibriumProblem(partition_with_inert_gaseous_phase) problem.add(state) for element in problem.elementAmounts(): assert element == 0.0 assert problem.partition().numInertSpecies() == 2
def test_equilibrium_solver_solve_overload_2(setup, state_regression): """ An integration test that checks result's reproducibility of the calculation of an equilibrium of a state using EquilibriumSolver::solve(ChemicalState& state, const EquilibriumProblem& problem) @param setup a tuple that has some objects from problem setup (system, problem) """ (system, problem) = setup state = ChemicalState(system) solver = EquilibriumSolver(system) solver.solve(state, problem) state_regression.check(state, default_tol=dict(atol=1e-5, rtol=1e-16))
def test_equilibrate_overload_7(setup, state_regression): """ An integration test that checks result's reproducibility of the calculation of a problem using equilibrate(ChemicalState& state, const EquilibriumProblem& problem) and equilibrate(ChemicalState& state, const EquilibriumInverseProblem& problem) @param setup a tuple that has some objects from problem setup (system, problem) """ (system, problem) = setup state = ChemicalState(system) equilibriumResult = equilibrate(state, problem) state_regression.check(state, default_tol=dict(atol=1e-5, rtol=1e-16))
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)
def test_equilibrium_solver_approx_overload_2(setup, num_regression): """ An integration test that checks result's reproducibility of the calculation of an equilibrium of a state using EquilibriumSolver::approximate(ChemicalState& state, const EquilibriumProblem& problem) @param setup a tuple that has some objects from problem setup (system, problem) """ (system, problem) = setup state = ChemicalState(system) solver = EquilibriumSolver(system) solver.approximate(state, problem) stateDict = convert_reaktoro_state_to_dict(state) num_regression.check(stateDict, default_tolerance=dict(atol=1e-5, rtol=1e-16))
def test_CubicEOS_multiple_roots(): """ This problem leads to the following CubicEOS roots PR - Z1 = 1.00027728 Z2 = 0.0001655 Z3 = -0.0011024 since bmix = 1.635e-05 -> Z3 is an invalid root and since Z3 < Z2 < Z1 -> Z2 is an invalid root. Reaktoro should remove Z3, Z2 and proceed instead of removing only Z3 and raising the exception "Logic error: it was expected Z roots of size 3, but got: 2". """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhaseWithElementsOf("H2O Fe(OH)2 Fe(OH)3 NH3") editor.addGaseousPhaseWithElementsOf("NH3") editor.addMineralPhase("Magnetite") system = ChemicalSystem(editor) state = ChemicalState(system) solver = EquilibriumSolver(system) temperature = 298.15 pressure = 1e5 b = [ 3.0, 122.01687012, 1.0, 63.50843506, 0.0, ] result = solver.approximate(state, temperature, pressure, b) assert result.optimum.succeeded # check that it doesn't raise an exception state.properties()
def test_equilibrium_solver_solve_overload_3(setup, num_regression): """ An integration test that checks result's reproducibility of the calculation of an equilibrium of a state using EquilibriumSolver::solve(ChemicalState& state, double T, double P, VectorConstRef be) @param setup a tuple that has some objects from problem setup (system, problem) """ (system, problem) = setup state = ChemicalState(system) solver = EquilibriumSolver(system) solver.solve(state, problem.temperature(), problem.pressure(), problem.elementAmounts()) stateDict = convert_reaktoro_state_to_dict(state) num_regression.check(stateDict, default_tolerance=dict(atol=1e-5, rtol=1e-16))
def test_equilibrate_overload_1(setup, num_regression): """ An integration test that checks result's reproducibility of the calculation of a problem using equilibrate(const EquilibriumProblem& problem) and equilibrate(const EquilibriumInverseProblem& problem) @param setup a tuple that has some objects from problem setup (system, problem) """ (system, problem) = setup equilibriumState = ChemicalState() equilibriumState = equilibrate(problem) stateDict = convert_reaktoro_state_to_dict(equilibriumState) num_regression.check(stateDict, default_tolerance=dict(atol=1e-5, rtol=1e-16))
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)