Skip to content

Commit 73919d3

Browse files
authored
feat: propertyFlash - more robust input handling (#962)
* fix: check that fraction belongs to current component * feat: more robust handling of input components * feat: SystemInterface.hasComponent * chore: release 2.5.20
1 parent 9333f96 commit 73919d3

File tree

11 files changed

+106
-35
lines changed

11 files changed

+106
-35
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<version>${revision}${sha1}${changelist}</version>
1111

1212
<properties>
13-
<revision>2.5.19</revision>
13+
<revision>2.5.20</revision>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1616
<sha1/>

pomJava21.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<version>${revision}${sha1}${changelist}</version>
1111

1212
<properties>
13-
<revision>2.5.19</revision>
13+
<revision>2.5.20</revision>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1616
<sha1/>

pomJava8.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<version>${revision}${sha1}${changelist}-Java8</version>
1111

1212
<properties>
13-
<revision>2.5.19</revision>
13+
<revision>2.5.20</revision>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1616
<sha1 />

src/main/java/neqsim/thermo/component/ComponentGeDuanSun.java

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public double getGamma(PhaseInterface phase, int numberOfComponents, double temp
5858
}
5959
}
6060

61-
6261
/** {@inheritDoc} */
6362
@Override
6463
public double getGamma(PhaseInterface phase, int numberOfComponents, double temperature,

src/main/java/neqsim/thermo/phase/Phase.java

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public Phase clone() {
100100
/**
101101
* <p>
102102
* addcomponent. Increase number of components and add moles to phase.
103+
*
104+
* NB! Does not actually add component to componentarray.
103105
* </p>
104106
*
105107
* @param name Name of component to add.

src/main/java/neqsim/thermo/phase/PhaseInterface.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1904,12 +1904,10 @@ public default void setPhaseTypeName(String phaseTypeName) {
19041904
public double getActivityCoefficientUnSymetric(int k);
19051905

19061906
/**
1907-
* <p>
1908-
* hasComponent.
1909-
* </p>
1907+
* Verify if phase has a component.
19101908
*
1911-
* @param name a {@link String} object
1912-
* @return a boolean
1909+
* @param name Name of component to look for.
1910+
* @return True if component is found.
19131911
*/
19141912
public boolean hasComponent(String name);
19151913

src/main/java/neqsim/thermo/system/SystemInterface.java

+15
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,21 @@ public default int getPhaseNumberOfPhase(String phaseTypeName) {
16131613
*/
16141614
public double getZ();
16151615

1616+
/**
1617+
* Verify if system has a component.
1618+
*
1619+
* @param name Name of component to look for.
1620+
* @return True if component is found.
1621+
*/
1622+
public default boolean hasComponent(String name) {
1623+
for (String fluidComp : getComponentNames()) {
1624+
if (name == fluidComp) {
1625+
return true;
1626+
}
1627+
}
1628+
return false;
1629+
}
1630+
16161631
/** {@inheritDoc} */
16171632
@Override
16181633
public int hashCode();

src/main/java/neqsim/thermodynamicOperations/ThermodynamicOperations.java

+25-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.awt.BorderLayout;
44
import java.awt.Container;
5+
import java.util.Arrays;
56
import java.util.List;
67
import javax.swing.JFrame;
78
import javax.swing.JScrollPane;
@@ -1975,9 +1976,22 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
19751976
String[] calculationError = new String[Spec1.size()];
19761977

19771978
Double[] sum = new Double[Spec1.size()];
1979+
String[] systemComponents = this.system.getComponentNames();
1980+
if (components != null) {
1981+
for (String inputCompName : components) {
1982+
if (!this.system.hasComponent(inputCompName)) {
1983+
for (int t = 0; t < Spec1.size(); t++) {
1984+
calculationError[t] = "Input component list does not match fluid component list.";
1985+
}
1986+
}
1987+
}
1988+
} else {
1989+
components = Arrays.asList(systemComponents);
1990+
}
19781991

19791992
// Verify that sum of fractions equals 1/100, i.e., assume percentages
19801993
boolean hasOnlineFractions = onlineFractions != null;
1994+
19811995
if (hasOnlineFractions) {
19821996
double range = 5;
19831997
for (int t = 0; t < sum.length; t++) {
@@ -2044,24 +2058,18 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
20442058
}
20452059

20462060
if (hasOnlineFractions) {
2047-
/*
2048-
* // New attempt:
2049-
*
2050-
* this.system.setEmptyFluid();
2051-
*
2052-
* // Components in system with no corresponding value in onlineFractions will be zero.
2053-
* for (int componentNumber = 0; componentNumber < onlineFractions .size();
2054-
* componentNumber++) { this.system.addComponent(componentNumber,
2055-
* onlineFractions.get(componentNumber).get(t).doubleValue()); }
2056-
*
2057-
* if (this.system.getTotalNumberOfMoles() < 1e-5) { this.system.setTotalNumberOfMoles(1);
2058-
* }
2059-
*/
2060-
2061-
// Remaining fractions will be set to 0.0
2061+
// Assure that fraction is inserted for the correct component (in case of mismatch of
2062+
// component input and fluid component list)
20622063
double[] fraction = new double[this.system.getNumberOfComponents()];
2063-
for (int comp = 0; comp < onlineFractions.size(); comp++) {
2064-
fraction[comp] = onlineFractions.get(comp).get(t).doubleValue();
2064+
// For all components defined in system
2065+
for (int compIndex = 0; compIndex < fraction.length; compIndex++) {
2066+
// Loop all input component names / fractions
2067+
for (int index = 0; index < components.size(); index++) {
2068+
if (systemComponents[compIndex] == components.get(index)) {
2069+
fraction[compIndex] = onlineFractions.get(index).get(t).doubleValue();
2070+
break;
2071+
}
2072+
}
20652073
}
20662074

20672075
this.system.setMolarComposition(fraction);

src/main/java/neqsim/util/database/NeqSimDataBase.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,15 @@ public static String[] getComponentNames() {
394394
}
395395

396396
/**
397-
* <p>
398-
* hasComponent.
399-
* </p>
397+
* Verify if database has a component.
400398
*
401-
* @param compName a {@link java.lang.String} object
402-
* @return a boolean
399+
* @param name Name of component to look for.
400+
* @return True if component is found.
403401
*/
404-
public static boolean hasComponent(String compName) {
402+
public static boolean hasComponent(String name) {
405403
try (neqsim.util.database.NeqSimDataBase database = new neqsim.util.database.NeqSimDataBase();
406404
java.sql.ResultSet dataSet =
407-
database.getResultSet("select count(*) from comp WHERE NAME='" + compName + "'")) {
405+
database.getResultSet("select count(*) from comp WHERE NAME='" + name + "'")) {
408406
dataSet.next();
409407
int size = dataSet.getInt(1);
410408
if (size == 0) {

src/main/java/neqsim/util/exception/InvalidInputException.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public InvalidInputException(String msg) {
2222
}
2323

2424
/**
25-
* Constructs an <code>InvalidInputException</code> with a default message.
25+
* Constructs an <code>InvalidInputException</code> with a default message like:
26+
*
27+
* Input " + inputName + " was invalid.
2628
*
2729
* @param className Class that exception is raised from
2830
* @param methodName Method that exception is raised from
@@ -33,7 +35,9 @@ public InvalidInputException(String className, String methodName, String inputNa
3335
}
3436

3537
/**
36-
* Constructs an <code>InvalidInputException</code> with the specified detail message.
38+
* Constructs an <code>InvalidInputException</code> with a message like:
39+
*
40+
* "Input " + inputName + " " + msg
3741
*
3842
* @param className Class that exception is raised from
3943
* @param methodName Method that exception is raised from

src/test/java/neqsim/thermodynamicOperations/ThermodynamicOperationsTest.java

+47
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,53 @@ void testpropertyFlashOnlineSingle() {
270270
Assertions.assertEquals(len, s.fluidProperties.length);
271271
}
272272

273+
@Test
274+
void testpropertyFlashOnlineTooFewInputComponents() {
275+
String[] components = {"nitrogen", "oxygen"};
276+
double[] fractions = {0.79, 0.21};
277+
int len = 10;
278+
List<List<Double>> onlineFractions = createDummyRequest(fractions, len);
279+
280+
Double[] pressure = {1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 4.0, 3.5, 3.0, 2.5};
281+
Double[] temperature = {301.0, 301.5, 302.0, 302.5, 303.0, 304.0, 304.0, 303.5, 303.0, 302.5};
282+
283+
SystemInterface fluid = new SystemSrkEos(298, 1.0);
284+
// Add extra component C1
285+
fluid.addComponent("C1");
286+
fluid.addComponents(components);
287+
// Add extra component iC4
288+
fluid.addComponent("iC4");
289+
290+
ThermodynamicOperations ops = new ThermodynamicOperations(fluid);
291+
CalculationResult s = ops.propertyFlash(Arrays.asList(pressure), Arrays.asList(temperature), 1,
292+
Arrays.asList(components), onlineFractions);
293+
Assertions.assertEquals(len, s.fluidProperties.length);
294+
Assertions.assertNull(s.calculationError[0]);
295+
}
296+
297+
@Test
298+
void testPropertyFlashTooManyInputComponents() {
299+
int len = 10;
300+
String[] components_too_many = {"nitrogen", "oxygen", "water"};
301+
double[] fractions_to_many = {0.79, 0.21, 0.01};
302+
303+
Double[] pressure = {1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 4.0, 3.5, 3.0, 2.5};
304+
Double[] temperature = {301.0, 301.5, 302.0, 302.5, 303.0, 304.0, 304.0, 303.5, 303.0, 302.5};
305+
306+
List<List<Double>> onlineFractions_too_many = createDummyRequest(fractions_to_many, len);
307+
SystemInterface fluid = new SystemSrkEos(298, 1.0);
308+
309+
// Add only two components to fluid
310+
String[] components = {"nitrogen", "oxygen"};
311+
fluid.addComponents(components);
312+
ThermodynamicOperations ops = new ThermodynamicOperations(fluid);
313+
CalculationResult s = ops.propertyFlash(Arrays.asList(pressure), Arrays.asList(temperature), 1,
314+
Arrays.asList(components_too_many), onlineFractions_too_many);
315+
Assertions.assertEquals(len, s.fluidProperties.length);
316+
Assertions.assertEquals("Input component list does not match fluid component list.",
317+
s.calculationError[0]);
318+
}
319+
273320
@Disabled
274321
@Test
275322
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)