From eedf990b9f8c35e6cdf1ca5e69743361e7e654c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85smund=20V=C3=A5ge=20Fannemel?= <34712686+asmfstatoil@users.noreply.github.com> Date: Thu, 21 Mar 2024 15:01:23 +0100 Subject: [PATCH] fix: more robust propertyFlash (#967) --- .../ThermodynamicOperations.java | 7 +- .../ThermodynamicOperationsTest.java | 75 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java b/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java index f0614c6561..357538ccad 100644 --- a/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java +++ b/src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.Logger; import neqsim.api.ioc.CalculationResult; import neqsim.thermo.component.ComponentHydrate; +import neqsim.thermo.component.ComponentInterface; import neqsim.thermo.system.SystemInterface; import neqsim.thermo.system.SystemProperties; import neqsim.thermodynamicOperations.flashOps.CalcIonicComposition; @@ -2031,6 +2032,9 @@ public CalculationResult propertyFlash(List Spec1, List Spec2, i for (int t = 0; t < Spec1.size(); t++) { calculationError[t] = "Sum of fractions must be approximately to 1 or 100, currently (" + String.valueOf(sum[0]) + ")"; + if (sum[0] == 0.0) { + calculationError[t] = calculationError[t] + ". Have you called init(0)?"; + } } } } @@ -2065,7 +2069,8 @@ public CalculationResult propertyFlash(List Spec1, List Spec2, i for (int compIndex = 0; compIndex < fraction.length; compIndex++) { // Loop all input component names / fractions for (int index = 0; index < components.size(); index++) { - if (systemComponents[compIndex] == components.get(index)) { + if (systemComponents[compIndex] == ComponentInterface + .getComponentNameFromAlias(components.get(index))) { fraction[compIndex] = onlineFractions.get(index).get(t).doubleValue(); break; } diff --git a/src/test/java/neqsim/thermodynamicOperations/ThermodynamicOperationsTest.java b/src/test/java/neqsim/thermodynamicOperations/ThermodynamicOperationsTest.java index a3d1a01f6a..2e00d9f227 100644 --- a/src/test/java/neqsim/thermodynamicOperations/ThermodynamicOperationsTest.java +++ b/src/test/java/neqsim/thermodynamicOperations/ThermodynamicOperationsTest.java @@ -53,6 +53,81 @@ void testFlash() { } } + @Test + void testFluidDefined() { + double[] fractions = new double[] {98.0, 2.0}; + List Sp1 = + Arrays.asList(new Double[] {22.1, 23.2, 24.23, 25.98, 25.23, 26.1, 27.3, 28.7, 23.5, 22.7}); + List Sp2 = Arrays.asList( + new Double[] {288.1, 290.1, 295.1, 301.2, 299.3, 310.2, 315.3, 310.0, 305.2, 312.7}); + List components = Arrays.asList(new String[] {"O2", "N2"}); + List> onlineFractions = new ArrayList>(); + + for (double d : fractions) { + ArrayList l = new ArrayList(); + for (int i = 0; i < Sp1.size(); i++) { + l.add(d); + } + onlineFractions.add(l); + } + + SystemInterface fluid_static = new SystemSrkEos(273.15 + 45.0, 22.0); + fluid_static.addComponent("N2", fractions[0]); + fluid_static.addComponent("O2", fractions[1]); + fluid_static.setMixingRule(2); + fluid_static.useVolumeCorrection(true); + fluid_static.setMultiPhaseCheck(true); + // fluid_static.init(0); + + ThermodynamicOperations fluidOps_static = new ThermodynamicOperations(fluid_static); + CalculationResult res_static = fluidOps_static.propertyFlash(Sp1, Sp2, 1, null, null); + + for (String err : res_static.calculationError) { + Assertions.assertEquals(err, + "Sum of fractions must be approximately to 1 or 100, currently (0.0). Have you called init(0)?"); + } + // fluid_static.setTotalNumberOfMoles(1); + fluid_static.init(0); + res_static = fluidOps_static.propertyFlash(Sp1, Sp2, 1, null, null); + for (String err : res_static.calculationError) { + Assertions.assertEquals(err, null); + } + + SystemInterface fluid = new SystemSrkEos(273.15 + 45.0, 22.0); + fluid.addComponent("nitrogen", 0.79); + fluid.addComponent("oxygen", 0.21); + fluid.setMixingRule(2); + fluid.useVolumeCorrection(true); + fluid.setMultiPhaseCheck(true); + + ThermodynamicOperations fluidOps = new ThermodynamicOperations(fluid); + CalculationResult res = fluidOps.propertyFlash(Sp1, Sp2, 1, components, onlineFractions); + Assertions.assertEquals(Sp1.size(), res.calculationError.length); + for (String err : res.calculationError) { + Assertions.assertNull(err); + } + + fluid = new SystemSrkEos(273.15 + 45.0, 22.0); + fluid.addComponent("N2", 0.79); + fluid.addComponent("O2", 0.21); + fluid.setMixingRule(2); + fluid.useVolumeCorrection(true); + fluid.setMultiPhaseCheck(true); + + + fluidOps = new ThermodynamicOperations(fluid); + CalculationResult res2 = fluidOps.propertyFlash(Sp1, Sp2, 1, components, onlineFractions); + Assertions.assertEquals(Sp1.size(), res2.calculationError.length); + for (String err : res2.calculationError) { + Assertions.assertNull(err); + } + + Assertions.assertEquals(res, res2); + // todo: why does below not work? + + // Assertions.assertArrayEquals(res_static.fluidProperties[0], res.fluidProperties[0]); + } + @Test void testNeqSimPython() { SystemInterface thermoSystem = new neqsim.thermo.system.SystemSrkEos(280.0, 10.0);