From fc1c65592d55229d04d455e39ad707f6aac4fbd0 Mon Sep 17 00:00:00 2001 From: Even Solbraa <41290109+EvenSol@users.noreply.github.com> Date: Sat, 25 Nov 2023 15:33:16 +0000 Subject: [PATCH 1/4] bug fix --- .../distillation/DistillationColumn.java | 17 ++++++++--------- .../ThermodynamicOperations.java | 9 +++------ .../flashOps/TPmultiflash.java | 7 ++++++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/neqsim/processSimulation/processEquipment/distillation/DistillationColumn.java b/src/main/java/neqsim/processSimulation/processEquipment/distillation/DistillationColumn.java index 478fd5a19..feecfc5f2 100644 --- a/src/main/java/neqsim/processSimulation/processEquipment/distillation/DistillationColumn.java +++ b/src/main/java/neqsim/processSimulation/processEquipment/distillation/DistillationColumn.java @@ -370,14 +370,13 @@ public void run(UUID id) { trays.get(i + 1).getLiquidOutStream()); ((SimpleTray) trays.get(i)).run(id); } - for (int i = 1; i <= numberOfTrays - 1; i++) { - int replaceStream = trays.get(i).getNumberOfInputStreams() - 2; - if (i == (numberOfTrays - 1)) { - replaceStream = trays.get(i).getNumberOfInputStreams() - 1; - } - ((Mixer) trays.get(i)).replaceStream(replaceStream, trays.get(i - 1).getGasOutStream()); - ((SimpleTray) trays.get(i)).run(id); - } + /* + * for (int i = 1; i <= numberOfTrays - 1; i++) { int replaceStream = + * trays.get(i).getNumberOfInputStreams() - 2; if (i == (numberOfTrays - 1)) { replaceStream + * = trays.get(i).getNumberOfInputStreams() - 1; } ((Mixer) + * trays.get(i)).replaceStream(replaceStream, trays.get(i - 1).getGasOutStream()); + * ((SimpleTray) trays.get(i)).run(id); } + */ for (int i = 0; i < numberOfTrays; i++) { err += Math.abs( oldtemps[i] - ((MixerInterface) trays.get(i)).getThermoSystem().getTemperature()); @@ -385,7 +384,7 @@ public void run(UUID id) { logger.info("error iter " + err + " iteration " + iter); // System.out.println("error iter " + err + " iteration " + iter); // massBalanceCheck(); - } while (err > 1e-4 && err < errOld && iter < maxNumberOfIterations); // && + } while (err > 1e-4 && err < errOld && iter < maxNumberOfIterations); // && // !massBalanceCheck()); // massBalanceCheck(); // componentMassBalanceCheck("water"); diff --git a/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java b/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java index 891a95a37..18c08aa98 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; @@ -146,7 +143,7 @@ public void TPVflash(double volumeSpec, String unit) { public void TPflash() { double flowRate = system.getTotalNumberOfMoles(); double minimumFlowRate = 1e-50; - if (flowRate < 1e-3) { + if (flowRate < 1e-5) { system.setTotalNumberOfMoles(1.0); system.init(1); } @@ -157,13 +154,13 @@ public void TPflash() { } else { run(); } - if (flowRate < 1e-3) { + if (flowRate < 1e-5) { if (flowRate < minimumFlowRate) { system.setTotalNumberOfMoles(minimumFlowRate); } 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 7d18355b6..e5ad949bb 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) { 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,10 @@ public void stabilityAnalysis2() { } } } while ((Math.abs(err) > 1e-9 || err > errOld) && iter < 200); + if (iter > 198) { + System.out.println("too many iterations....." + err); + new Exception("to many iterations "); + } // logger.info("err: " + err + " ITER " + iter); double xTrivialCheck0 = 0.0; double xTrivialCheck1 = 0.0; From 1ce41c5c408ec888fccd427ddc5d5bbbfae049c5 Mon Sep 17 00:00:00 2001 From: Even Solbraa <41290109+EvenSol@users.noreply.github.com> Date: Sat, 25 Nov 2023 17:02:40 +0000 Subject: [PATCH 2/4] update flash --- .../neqsim/thermodynamicOperations/flashOps/TPmultiflash.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java index e5ad949bb..62986616b 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) { + if (iter % 7 == 0 && useaccsubst) { double vec1 = 0.0; double vec2 = 0.0; From dad7d8188f3482c78e1cf22a19e67cc4489ae4b0 Mon Sep 17 00:00:00 2001 From: Even Solbraa <41290109+EvenSol@users.noreply.github.com> Date: Sat, 25 Nov 2023 20:44:32 +0000 Subject: [PATCH 3/4] imporve flash --- .../flashOps/TPmultiflash.java | 11 ++++- .../flashOps/TPFlashTest.java | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java index 62986616b..f910ff183 100644 --- a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java +++ b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java @@ -842,7 +842,8 @@ public void stabilityAnalysis2() { } } while ((Math.abs(err) > 1e-9 || err > errOld) && iter < 200); if (iter > 198) { - System.out.println("too many iterations....." + err); + 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); @@ -932,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()) { @@ -973,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 6fff85139..7a7017765 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); + } + } + } + + + } } From 7a68815a85cb7e9f4cdaa6069faaf3272dc3c6d1 Mon Sep 17 00:00:00 2001 From: Even Solbraa <41290109+EvenSol@users.noreply.github.com> Date: Sun, 26 Nov 2023 09:19:23 +0000 Subject: [PATCH 4/4] improve flash --- .../flashOps/TPmultiflash.java | 14 ++++++++++- .../flashOps/TPFlashTest.java | 23 ++++--------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java index f910ff183..4357ad22d 100644 --- a/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java +++ b/src/main/java/neqsim/thermodynamicOperations/flashOps/TPmultiflash.java @@ -28,6 +28,7 @@ public class TPmultiflash extends TPflash { static Logger logger = LogManager.getLogger(TPmultiflash.class); // SystemInterface clonedSystem; + boolean startFromBottom = true; boolean multiPhaseTest = false; double[][] dQdbeta; double[][] Qmatrix; @@ -340,7 +341,15 @@ public void stabilityAnalysis() { } } - for (int j = system.getPhase(0).getNumberOfComponents() - 1; j >= 0; j--) { + int start = system.getPhase(0).getNumberOfComponents() - 1; + int end = 0; + int val = -1; + if (!startFromBottom) { + start = 0; + val = 1; + end = system.getPhase(0).getNumberOfComponents() - 1; + } + for (int j = start; j != end; j = j + val) { if (minimumGibbsEnergySystem.getPhase(0).getComponent(j).getx() < 1e-100 || (minimumGibbsEnergySystem.getPhase(0).getComponent(j).getIonicCharge() != 0) || (minimumGibbsEnergySystem.getPhase(0).getComponent(j).isHydrocarbon() @@ -372,6 +381,7 @@ public void stabilityAnalysis() { double errOld = 1.0e100; boolean useaccsubst = true; do { + clonedSystem.get(0).init(1); errOld = err; iter++; err = 0; @@ -1028,6 +1038,8 @@ public void run() { */ if (hasRemovedPhase && !secondTime) { secondTime = true; + startFromBottom = false; + stabilityAnalysis(); run(); } /* diff --git a/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java b/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java index 7a7017765..5594bf558 100644 --- a/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java +++ b/src/test/java/neqsim/thermodynamicOperations/flashOps/TPFlashTest.java @@ -1,7 +1,6 @@ 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; @@ -143,7 +142,7 @@ void testRun6() { @Test void testTPflash1() { - testSystem = new neqsim.thermo.system.SystemSrkEos(273.15 + 177.0, 316.0); + testSystem = new neqsim.thermo.system.SystemSrkEos(273.15 + 290, 400.0); testSystem.addComponent("water", 65.93229747922976); testSystem.addComponent("NaCl", 0.784426208131475); testSystem.addComponent("nitrogen", 0.578509157534656); @@ -166,23 +165,11 @@ void testTPflash1() { testSystem.setMixingRule("classic"); testSystem.setMultiPhaseCheck(true); testOps = new ThermodynamicOperations(testSystem); + testOps.TPflash(); + testSystem.prettyPrint(); + + } - 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); - } - } - } - } }