From 11dd1a0ba6984dba9477d1e448ae913a9309f6cb Mon Sep 17 00:00:00 2001 From: Even Solbraa <41290109+EvenSol@users.noreply.github.com> Date: Sat, 4 May 2024 08:28:52 +0200 Subject: [PATCH] improved speed of water saturator (#999) --- .../flashOps/SaturateWithWater.java | 58 +++++++----- .../flashOps/SaturateWithWaterTest.java | 92 +++++++++++++++++++ 2 files changed, 125 insertions(+), 25 deletions(-) create mode 100644 src/test/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWaterTest.java diff --git a/src/main/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWater.java b/src/main/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWater.java index d90e84886b..f75d3fa9c1 100644 --- a/src/main/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWater.java +++ b/src/main/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWater.java @@ -37,17 +37,20 @@ public SaturateWithWater(SystemInterface system) { @Override public void run() { if (!system.getPhase(0).hasComponent("water")) { - system.addComponent("water", system.getTotalNumberOfMoles()); - system.createDatabase(true); + system.addComponent("water", system.getTotalNumberOfMoles() / 100.0); system.setMixingRule(system.getMixingRule()); - if (system.doMultiPhaseCheck()) { - system.setMultiPhaseCheck(true); - } - system.init(0); } - double dn = 1.0; - int i = 0; + boolean changedMultiPhase = false; + if (system.doMultiPhaseCheck() == false) { + system.setMultiPhaseCheck(true); + changedMultiPhase = true; + } + + + if (system.getComponent("water").getNumberOfmoles() < system.getTotalNumberOfMoles() / 2.0) { + system.addComponent("water", system.getTotalNumberOfMoles()); + } this.tpFlash = new TPflash(system); tpFlash.run(); boolean hasAq = false; @@ -56,32 +59,37 @@ public void run() { } double lastdn = 0.0; if (system.hasPhaseType(PhaseType.AQUEOUS)) { - lastdn = system.getPhaseOfType("aqueous").getComponent("water").getNumberOfMolesInPhase(); + lastdn = system.getPhase(PhaseType.AQUEOUS).getNumberOfMolesInPhase(); } else { - lastdn = system.getPhase(0).getNumberOfMolesInPhase() / 100.0; + lastdn = system.getPhase(0).getNumberOfMolesInPhase(); } - + double dn = 1.0; + int i = 0; do { i++; - - if (!hasAq) { - system.addComponent("water", lastdn * 0.5); - lastdn *= 0.8; + if (system.getNumberOfPhases() == 1 && hasAq) { + lastdn = -system.getComponent("water").getNumberOfmoles() * 0.1; + } else if (!hasAq) { + lastdn = Math.abs(lastdn) * 1.05; } else { - lastdn = system.getPhaseOfType("aqueous").getComponent("water").getNumberOfMolesInPhase(); - dn = lastdn / system.getNumberOfMoles(); - system.addComponent("water", -lastdn); + lastdn = + -system.getPhaseOfType("aqueous").getComponent("water").getNumberOfMolesInPhase() * 0.9; } + dn = lastdn / system.getNumberOfMoles(); + system.addComponent("water", lastdn); tpFlash.run(); - // system.display(); - hasAq = system.hasPhaseType("aqueous"); - } while ((i < 50 && Math.abs(dn) > 1e-7) || !hasAq && i <= 50); + hasAq = system.hasPhaseType(PhaseType.AQUEOUS); + } while (Math.abs(dn) > 1e-7 && i <= 50); if (i == 50) { logger.error("could not find solution - in water saturate : dn " + dn); } - // logger.info("i " + i + " dn " + dn); - system.removePhase(system.getNumberOfPhases() - 1); - tpFlash.run(); + if (system.hasPhaseType(PhaseType.AQUEOUS)) { + system.removePhase(system.getNumberOfPhases() - 1); + tpFlash.run(); + } + if (changedMultiPhase) { + system.setMultiPhaseCheck(false); + } } /** @@ -116,7 +124,7 @@ public static void main(String[] args) { // testSystem.display(); // testSystem.addComponent("water", 1); // testOps.saturateWithWater(); - testSystem.display(); + // testSystem.display(); // testOps.TPflash(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); diff --git a/src/test/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWaterTest.java b/src/test/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWaterTest.java new file mode 100644 index 0000000000..69ad8bfea9 --- /dev/null +++ b/src/test/java/neqsim/thermodynamicOperations/flashOps/SaturateWithWaterTest.java @@ -0,0 +1,92 @@ +package neqsim.thermodynamicOperations.flashOps; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; +import neqsim.thermo.system.SystemInterface; +import neqsim.thermo.system.SystemPrEos; +import neqsim.thermo.system.SystemSrkCPAstatoil; +import neqsim.thermodynamicOperations.ThermodynamicOperations; + +public class SaturateWithWaterTest { + static Logger logger = LogManager.getLogger(SaturateWithWaterTest.class); + + @Test + void testRun() { + SystemInterface testSystem = new SystemSrkCPAstatoil(273.15 + 70.0, 150.0); + + testSystem.addComponent("methane", 75.0); + testSystem.addComponent("ethane", 7.5); + testSystem.addComponent("propane", 4.0); + testSystem.addComponent("n-butane", 1.0); + testSystem.addComponent("i-butane", 0.6); + testSystem.addComponent("n-hexane", 0.3); + testSystem.addPlusFraction("C6", 1.3, 100.3 / 1000.0, 0.8232); + testSystem.setMixingRule(10); + testSystem.setMultiPhaseCheck(true); + + ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem); + try { + testOps.saturateWithWater(); + } catch (Exception ex) { + logger.error(ex.getMessage(), ex); + } + assertEquals(0.0029033655101811814, testSystem.getComponent("water").getz(), 1e-5); + } + + + @Test + void testRun2() { + SystemInterface testSystem = new SystemSrkCPAstatoil(273.15 + 70.0, 150.0); + + testSystem.addComponent("methane", 75.0); + testSystem.addComponent("ethane", 7.5); + testSystem.addComponent("propane", 4.0); + testSystem.setMixingRule(10); + + ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem); + try { + testOps.saturateWithWater(); + } catch (Exception ex) { + logger.error(ex.getMessage(), ex); + } + assertEquals(0.002891748277007660, testSystem.getComponent("water").getz(), 1e-5); + } + + @Test + void testRun3() { + SystemInterface testSystem = new SystemSrkCPAstatoil(273.15 + 20.0, 150.0); + + testSystem.addComponent("methane", 2.0); + testSystem.addComponent("n-heptane", 75.0); + testSystem.setMixingRule(10); + testSystem.setMultiPhaseCheck(true); + + ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem); + try { + testOps.saturateWithWater(); + } catch (Exception ex) { + logger.error(ex.getMessage(), ex); + } + assertEquals(2.8465024974919816E-4, testSystem.getComponent("water").getz(), 1e-5); + } + + @Test + void testRun4() { + SystemInterface testSystem = new SystemPrEos(273.15 + 20.0, 150.0); + + testSystem.addComponent("methane", 2.0); + testSystem.addComponent("n-heptane", 75.0); + testSystem.setMixingRule("classic"); + testSystem.setMultiPhaseCheck(true); + + ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem); + try { + testOps.saturateWithWater(); + } catch (Exception ex) { + logger.error(ex.getMessage(), ex); + } + assertEquals(2.4301370485671443E-4, testSystem.getComponent("water").getz(), 1e-5); + } +}