def test_bips_setup_without_derivatives(): """ This test takes into account the case where the BIPs are constants. In such a case, define matrices for kT and kTT are not necessary since all entries would be zero. Thus, only k should be defined. """ def bips_function(T): k_00 = k_11 = k_22 = 0.0 k_01 = k_10 = 0.01 k_02 = k_20 = 0.50 k_12 = k_21 = 0.40 k = [ [k_00, k_01, k_02], [k_10, k_11, k_12], [k_20, k_21, k_22] ] bips_values = BinaryInteractionParams(k) return bips_values T_dummy = 2.0 bips_expected = bips_function(T_dummy) cubic_eos_params = CubicEOSParams(binary_interaction_values=bips_function) bips_calculated = cubic_eos_params.binary_interaction_values(T_dummy) assert_array_equal(bips_calculated.k, bips_expected.k) assert len(bips_calculated.kT) == 0 assert len(bips_calculated.kTT) == 0
def test_bips_calculation_function(): """ Test the wrapper for the function that calculates the BIPs for mixtures modeled by a Cubic EOS. In this particular case, the BIPs depend on the temperature. """ def bips_function(T): k_00 = k_11 = k_22 = 0.0 * T k_01 = k_10 = 0.01 * T k_02 = k_20 = 0.50 * T k_12 = k_21 = 0.40 * T k = [ [k_00, k_01, k_02], [k_10, k_11, k_12], [k_20, k_21, k_22] ] kT_00 = kT_11 = kT_22 = 0.0 kT_01 = kT_10 = 0.01 kT_02 = kT_20 = 0.50 kT_12 = kT_21 = 0.40 kT = [ [kT_00, kT_01, kT_02], [kT_10, kT_11, kT_12], [kT_20, kT_21, kT_22] ] kTT_00 = kTT_11 = kTT_22 = 0.0 kTT_01 = kTT_10 = 0.0 kTT_02 = kTT_20 = 0.0 kTT_12 = kTT_21 = 0.0 kTT = [ [kTT_00, kTT_01, kTT_02], [kTT_10, kTT_11, kTT_12], [kTT_20, kTT_21, kTT_22] ] bips_values = BinaryInteractionParams(k, kT, kTT) return bips_values T_dummy = 2.0 bips_expected = bips_function(T_dummy) cubic_eos_params = CubicEOSParams(binary_interaction_values=bips_function) bips_calculated = cubic_eos_params.binary_interaction_values(T_dummy) assert_array_equal(bips_calculated.k, bips_expected.k) assert_array_equal(bips_calculated.kT, bips_expected.kT) assert_array_equal(bips_calculated.kTT, bips_expected.kTT)
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 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 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)