Skip to content

Commit

Permalink
feat: propertyFlash - more robust input handling (#962)
Browse files Browse the repository at this point in the history
* fix: check that fraction belongs to current component
* feat: more robust handling of input components
* feat: SystemInterface.hasComponent
* chore: release 2.5.20
  • Loading branch information
asmfstatoil committed Mar 19, 2024
1 parent 9333f96 commit 73919d3
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<version>${revision}${sha1}${changelist}</version>

<properties>
<revision>2.5.19</revision>
<revision>2.5.20</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<sha1/>
Expand Down
2 changes: 1 addition & 1 deletion pomJava21.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<version>${revision}${sha1}${changelist}</version>

<properties>
<revision>2.5.19</revision>
<revision>2.5.20</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<sha1/>
Expand Down
2 changes: 1 addition & 1 deletion pomJava8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<version>${revision}${sha1}${changelist}-Java8</version>

<properties>
<revision>2.5.19</revision>
<revision>2.5.20</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<sha1 />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public double getGamma(PhaseInterface phase, int numberOfComponents, double temp
}
}


/** {@inheritDoc} */
@Override
public double getGamma(PhaseInterface phase, int numberOfComponents, double temperature,
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/neqsim/thermo/phase/Phase.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public Phase clone() {
/**
* <p>
* addcomponent. Increase number of components and add moles to phase.
*
* NB! Does not actually add component to componentarray.
* </p>
*
* @param name Name of component to add.
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/neqsim/thermo/phase/PhaseInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -1904,12 +1904,10 @@ public default void setPhaseTypeName(String phaseTypeName) {
public double getActivityCoefficientUnSymetric(int k);

/**
* <p>
* hasComponent.
* </p>
* Verify if phase has a component.
*
* @param name a {@link String} object
* @return a boolean
* @param name Name of component to look for.
* @return True if component is found.
*/
public boolean hasComponent(String name);

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/neqsim/thermo/system/SystemInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,21 @@ public default int getPhaseNumberOfPhase(String phaseTypeName) {
*/
public double getZ();

/**
* Verify if system has a component.
*
* @param name Name of component to look for.
* @return True if component is found.
*/
public default boolean hasComponent(String name) {
for (String fluidComp : getComponentNames()) {
if (name == fluidComp) {
return true;
}
}
return false;
}

/** {@inheritDoc} */
@Override
public int hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
Expand Down Expand Up @@ -1975,9 +1976,22 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
String[] calculationError = new String[Spec1.size()];

Double[] sum = new Double[Spec1.size()];
String[] systemComponents = this.system.getComponentNames();
if (components != null) {
for (String inputCompName : components) {
if (!this.system.hasComponent(inputCompName)) {
for (int t = 0; t < Spec1.size(); t++) {
calculationError[t] = "Input component list does not match fluid component list.";
}
}
}
} else {
components = Arrays.asList(systemComponents);
}

// Verify that sum of fractions equals 1/100, i.e., assume percentages
boolean hasOnlineFractions = onlineFractions != null;

if (hasOnlineFractions) {
double range = 5;
for (int t = 0; t < sum.length; t++) {
Expand Down Expand Up @@ -2044,24 +2058,18 @@ public CalculationResult propertyFlash(List<Double> Spec1, List<Double> Spec2, i
}

if (hasOnlineFractions) {
/*
* // New attempt:
*
* this.system.setEmptyFluid();
*
* // Components in system with no corresponding value in onlineFractions will be zero.
* for (int componentNumber = 0; componentNumber < onlineFractions .size();
* componentNumber++) { this.system.addComponent(componentNumber,
* onlineFractions.get(componentNumber).get(t).doubleValue()); }
*
* if (this.system.getTotalNumberOfMoles() < 1e-5) { this.system.setTotalNumberOfMoles(1);
* }
*/

// Remaining fractions will be set to 0.0
// Assure that fraction is inserted for the correct component (in case of mismatch of
// component input and fluid component list)
double[] fraction = new double[this.system.getNumberOfComponents()];
for (int comp = 0; comp < onlineFractions.size(); comp++) {
fraction[comp] = onlineFractions.get(comp).get(t).doubleValue();
// For all components defined in system
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)) {
fraction[compIndex] = onlineFractions.get(index).get(t).doubleValue();
break;
}
}
}

this.system.setMolarComposition(fraction);
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/neqsim/util/database/NeqSimDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,15 @@ public static String[] getComponentNames() {
}

/**
* <p>
* hasComponent.
* </p>
* Verify if database has a component.
*
* @param compName a {@link java.lang.String} object
* @return a boolean
* @param name Name of component to look for.
* @return True if component is found.
*/
public static boolean hasComponent(String compName) {
public static boolean hasComponent(String name) {
try (neqsim.util.database.NeqSimDataBase database = new neqsim.util.database.NeqSimDataBase();
java.sql.ResultSet dataSet =
database.getResultSet("select count(*) from comp WHERE NAME='" + compName + "'")) {
database.getResultSet("select count(*) from comp WHERE NAME='" + name + "'")) {
dataSet.next();
int size = dataSet.getInt(1);
if (size == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public InvalidInputException(String msg) {
}

/**
* Constructs an <code>InvalidInputException</code> with a default message.
* Constructs an <code>InvalidInputException</code> with a default message like:
*
* Input " + inputName + " was invalid.
*
* @param className Class that exception is raised from
* @param methodName Method that exception is raised from
Expand All @@ -33,7 +35,9 @@ public InvalidInputException(String className, String methodName, String inputNa
}

/**
* Constructs an <code>InvalidInputException</code> with the specified detail message.
* Constructs an <code>InvalidInputException</code> with a message like:
*
* "Input " + inputName + " " + msg
*
* @param className Class that exception is raised from
* @param methodName Method that exception is raised from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,53 @@ void testpropertyFlashOnlineSingle() {
Assertions.assertEquals(len, s.fluidProperties.length);
}

@Test
void testpropertyFlashOnlineTooFewInputComponents() {
String[] components = {"nitrogen", "oxygen"};
double[] fractions = {0.79, 0.21};
int len = 10;
List<List<Double>> onlineFractions = createDummyRequest(fractions, len);

Double[] pressure = {1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 4.0, 3.5, 3.0, 2.5};
Double[] temperature = {301.0, 301.5, 302.0, 302.5, 303.0, 304.0, 304.0, 303.5, 303.0, 302.5};

SystemInterface fluid = new SystemSrkEos(298, 1.0);
// Add extra component C1
fluid.addComponent("C1");
fluid.addComponents(components);
// Add extra component iC4
fluid.addComponent("iC4");

ThermodynamicOperations ops = new ThermodynamicOperations(fluid);
CalculationResult s = ops.propertyFlash(Arrays.asList(pressure), Arrays.asList(temperature), 1,
Arrays.asList(components), onlineFractions);
Assertions.assertEquals(len, s.fluidProperties.length);
Assertions.assertNull(s.calculationError[0]);
}

@Test
void testPropertyFlashTooManyInputComponents() {
int len = 10;
String[] components_too_many = {"nitrogen", "oxygen", "water"};
double[] fractions_to_many = {0.79, 0.21, 0.01};

Double[] pressure = {1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 4.0, 3.5, 3.0, 2.5};
Double[] temperature = {301.0, 301.5, 302.0, 302.5, 303.0, 304.0, 304.0, 303.5, 303.0, 302.5};

List<List<Double>> onlineFractions_too_many = createDummyRequest(fractions_to_many, len);
SystemInterface fluid = new SystemSrkEos(298, 1.0);

// Add only two components to fluid
String[] components = {"nitrogen", "oxygen"};
fluid.addComponents(components);
ThermodynamicOperations ops = new ThermodynamicOperations(fluid);
CalculationResult s = ops.propertyFlash(Arrays.asList(pressure), Arrays.asList(temperature), 1,
Arrays.asList(components_too_many), onlineFractions_too_many);
Assertions.assertEquals(len, s.fluidProperties.length);
Assertions.assertEquals("Input component list does not match fluid component list.",
s.calculationError[0]);
}

@Disabled
@Test
@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 73919d3

Please sign in to comment.