def test_add_phases_with_elements_wrong_use(): """Test the wrong usage of addAqueousPhaseWithElements.""" editor = ChemicalEditor() with pytest.raises(RuntimeError): editor.addAqueousPhaseWithElements("H2O(l) C Ca") with pytest.raises(RuntimeError): editor.addAqueousPhaseWithElements(["H2O C Ca"])
def test_equilibrium_path(table_regression, tmpdir): """ An integration test that checks result's reproducibility of the calculation of an equilibrium path between two states """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhaseWithElements("H O C Na Cl") system = ChemicalSystem(editor) problem1 = EquilibriumProblem(system) problem1.add("H2O", 1, "kg") problem1.add("CO2", 0.5, "mol") problem1.add("HCl", 1, "mol") problem2 = EquilibriumProblem(system) problem2.add("H2O", 1, "kg") problem2.add("CO2", 0.5, "mol") problem2.add("NaOH", 2, "mol") state1 = equilibrate(problem1) state2 = equilibrate(problem2) path = EquilibriumPath(system) output = path.output() output.precision(16) output.filename(tmpdir.dirname + "/equilibriumPathResult.txt") # Define which outputs will be written and checked output.add("t") output.add("pH") output.add("speciesMolality(HCO3-)") output.add("speciesMolality(CO2(aq))") output.add("speciesMolality(CO3--)") path.solve(state1, state2) pathTable = pd.read_csv( tmpdir.dirname + "/equilibriumPathResult.txt", index_col=None, delim_whitespace=True, ) table_regression.check(pathTable, default_tol=dict(atol=1e-5, rtol=1e-14))
def test_add_phases_with_elements_right_use(): """Test the normal use of addAqueousPhaseWithElements, addGaseousPhaseWithElements and addMineralPhaseWithElements.""" editor1 = ChemicalEditor() editor1.addAqueousPhaseWithElements("H C O Ca") editor1.addGaseousPhaseWithElements("H C O") editor1.addMineralPhaseWithElements("Ca C O") editor2 = ChemicalEditor() editor2.addAqueousPhaseWithElements(["H", "C", "O", "Ca"]) editor2.addGaseousPhaseWithElements(["H", "C", "O"]) editor2.addMineralPhaseWithElements(["Ca", "C", "O"]) system1 = ChemicalSystem(editor1) system2 = ChemicalSystem(editor2) _check_equivalent_chemical_systems(system1, system2)
def equilibrium_inverse_with_h_o_na_cl_ca_c_calcite_ph_and_fixed_amounts(): """ Build a problem with H, O, Na, Cl, Ca, C and Calcite with defined pH and fixed species amount """ database = Database("supcrt98.xml") editor = ChemicalEditor(database) editor.addAqueousPhaseWithElements("H O Na Cl Ca C") editor.addMineralPhase("Calcite") system = ChemicalSystem(editor) problem = EquilibriumInverseProblem(system) problem.add("H2O", 1, "kg") problem.add("NaCl", 0.1, "mol") problem.pH(8.0, "HCl", "NaOH") problem.fixSpeciesAmount("Calcite", 1, "mol") 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.addAqueousPhaseWithElements("H O Na Cl Ca Mg C") editor.addGaseousPhaseWithElements("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_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.addAqueousPhaseWithElements("H O Na Cl Ca Mg C") editor.addGaseousPhaseWithElements("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 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))