From 3910d39326daf7dcc9db01611b50ee2c903b6c05 Mon Sep 17 00:00:00 2001 From: Even Solbraa <41290109+EvenSol@users.noreply.github.com> Date: Sat, 25 Nov 2023 21:47:19 +0100 Subject: [PATCH] improve TPflash speed (#860) * bug fix * update flash * imporve flash --- .../ThermodynamicOperations.java | 5 +- .../flashOps/TPmultiflash.java | 16 ++++++- .../flashOps/TPFlashTest.java | 46 +++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java b/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java index 274a118766..bdd1c095f9 100644 --- a/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java +++ b/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java @@ -3,14 +3,11 @@ import java.awt.BorderLayout; import java.awt.Container; import java.util.List; - import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; - import neqsim.api.ioc.CalculationResult; import neqsim.thermo.component.ComponentHydrate; import neqsim.thermo.system.SystemInterface; @@ -164,7 +161,7 @@ public void TPflash() { } else { system.setTotalNumberOfMoles(flowRate); } - system.init(2); + system.init(1); } } diff --git a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java index 7d18355b6e..f910ff1837 100644 --- a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java +++ b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java @@ -377,7 +377,7 @@ public void stabilityAnalysis() { err = 0; if (iter <= 150 || !system.isImplementedCompositionDeriativesofFugacity()) { - if (iter % 7 == 0 && iter < 150 && useaccsubst) { + if (iter % 7 == 0 && useaccsubst) { double vec1 = 0.0; double vec2 = 0.0; @@ -420,6 +420,7 @@ public void stabilityAnalysis() { deltalogWi[i] = logWi[i] - oldlogw[i]; err += Math.abs(logWi[i] - oldlogw[i]); Wi[j][i] = Math.exp(logWi[i]); + useaccsubst = true; } if (iter > 2 && err > errOld) { useaccsubst = false; @@ -840,6 +841,11 @@ public void stabilityAnalysis2() { } } } while ((Math.abs(err) > 1e-9 || err > errOld) && iter < 200); + if (iter > 198) { + System.out.println("too many iterations....." + err + " temperature " + + system.getTemperature("C") + " C " + system.getPressure("bara") + " bara"); + new Exception("to many iterations "); + } // logger.info("err: " + err + " ITER " + iter); double xTrivialCheck0 = 0.0; double xTrivialCheck1 = 0.0; @@ -927,6 +933,7 @@ public void run() { double oldDiff = 1.0e10; double chemdev = 0; int iterOut = 0; + double maxerr = 1e-12; do { iterOut++; if (system.isChemicalSystem()) { @@ -968,11 +975,16 @@ public void run() { // diff = Math.abs((system.getBeta(system.getNumberOfPhases() - 1) - oldBeta) / // oldBeta); // logger.info("diff multiphase " + diff); - } while (diff > 1e-12 && !removePhase && (diff < oldDiff || iterations < 50) + if (iterations % 50 == 0) { + maxerr *= 100.0; + } + } while (diff > maxerr && !removePhase && (diff < oldDiff || iterations < 50) && iterations < 200); // this.solveBeta(true); if (iterations >= 199) { logger.error("error in multiphase flash..did not solve in 200 iterations"); + logger.error("diff " + diff + " temperaure " + system.getTemperature("C") + " pressure " + + system.getPressure("bara")); diff = this.solveBeta(); } } while ((Math.abs(chemdev) > 1e-10 && iterOut < 100) diff --git a/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java b/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java index 6fff851397..7a70177656 100644 --- a/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java +++ b/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java @@ -1,6 +1,7 @@ package neqsim.thermodynamicOperations.flashOps; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import neqsim.thermodynamicOperations.ThermodynamicOperations; @@ -139,4 +140,49 @@ void testRun6() { assertEquals(0.27697023508525664, testSystem5.getBeta(), 1e-6); assertEquals(3, testSystem5.getNumberOfPhases()); } + + @Test + void testTPflash1() { + testSystem = new neqsim.thermo.system.SystemSrkEos(273.15 + 177.0, 316.0); + testSystem.addComponent("water", 65.93229747922976); + testSystem.addComponent("NaCl", 0.784426208131475); + testSystem.addComponent("nitrogen", 0.578509157534656); + testSystem.addComponent("methane", 22.584113183429718); + testSystem.addComponent("ethane", 3.43870686718215); + testSystem.addComponent("propane", 0.26487350163523365); + testSystem.addComponent("i-butane", 0.04039429848533373); + testSystem.addComponent("n-butane", 0.1543856425679738); + testSystem.addComponent("i-pentane", 0.04039429848533373); + testSystem.addComponent("n-pentane", 0.1543856425679738); + testSystem.addTBPfraction("C6", 0.568724470114871, 84.93298402237961 / 1000.0, + 666.591171644071 / 1000.0); + testSystem.addTBPfraction("C7", 0.9478147516962493, 90.01311937418495 / 1000.0, + 746.9101810251765 / 1000.0); + testSystem.addTBPfraction("C8", 0.974840433764089, 102.34691375809437 / 1000.0, + 776.2927119017166 / 1000.0); + testSystem.addTBPfraction("C9", 0.5505907716430188, 116.06055719132209 / 1000.0, + 791.2983315058531 / 1000.0); + testSystem.addTBPfraction("C10", 1.9704404325720026, 221.831957 / 1000.0, 842.802708 / 1000.0); + testSystem.setMixingRule("classic"); + testSystem.setMultiPhaseCheck(true); + testOps = new ThermodynamicOperations(testSystem); + + for (int i = 0; i < 50; i++) { + testSystem.setTemperature(i, "C"); + for (int j = 1; j < 50; j++) { + testSystem.setPressure(j, "bara"); + try { + testOps.TPflash(); + testSystem.initProperties(); + } catch (Exception e) { + System.out.println("temperature " + testSystem.getTemperature("C") + " pressure " + + testSystem.getPressure("bara")); + e.printStackTrace(); + assertTrue(false); + } + } + } + + + } }