Skip to content

Commit

Permalink
feat: Phase.hasComponent check against normalized name (#965)
Browse files Browse the repository at this point in the history
feat: Phase.hasComponent check against normalized name
feat: new function Phase.getComponentNames
fix: check that compnumber is valid and not in use (previously possible to overwrite component in array)
docfix
  • Loading branch information
asmfstatoil authored Mar 21, 2024
1 parent 45c4abc commit 3ec2eb5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ public default double getLogFugacityCoefficient() {

/**
* <p>
* getName.
* Getter for property <code>componentName</code>, i.e., normalized component name.
* </p>
*
* @return a {@link java.lang.String} object
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/neqsim/thermo/phase/Phase.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Phase clone() {

/**
* <p>
* addcomponent. Increase number of components and add moles to phase.
* Increase number of components and add moles to phase.
*
* NB! Does not actually add component to componentarray.
* </p>
Expand Down Expand Up @@ -1768,10 +1768,37 @@ public ComponentInterface getComponent(String name) {

/** {@inheritDoc} */
@Override
public boolean hasComponent(String name) {
public String[] getComponentNames() {
ArrayList<String> components = new ArrayList<String>();

for (int j = 0; j < componentArray.length; j++) {
if (componentArray[j] != null) {
components.add(componentArray[j].getComponentName());
}
}

String[] componentList = new String[components.size()];
for (int j = 0; j < numberOfComponents; j++) {
componentList[j] = components.get(j);
}
return componentList;
}

/** {@inheritDoc} */
@Override
public boolean hasComponent(String name, boolean normalized) {
for (int i = 0; i < numberOfComponents; i++) {
if (componentArray[i].getName().equals(name)) {
return true;
if (componentArray != null) {
if (normalized) {
if (componentArray[i].getComponentName()
.equals(ComponentInterface.getComponentNameFromAlias(name))) {
return true;
}
} else {
if (componentArray[i].getName().equals(name)) {
return true;
}
}
}
}
return false;
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/neqsim/thermo/phase/PhaseInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public interface PhaseInterface extends ThermodynamicConstantsInterface, Cloneable {
/**
* <p>
* addcomponent.
* Add component to component array and update moles variables.
* </p>
*
* @param name Name of component.
Expand Down Expand Up @@ -329,13 +329,21 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int initType

/**
* <p>
* getcomponentArray.
* Get component array of Phase.
* </p>
*
* @return an array of {@link neqsim.thermo.component.ComponentInterface} objects
*/
public ComponentInterface[] getcomponentArray();


/**
* Get normalized names of components in phase.
*
* @return Array of names of components in phase.
*/
public String[] getComponentNames();

/**
* <p>
* getMass.
Expand Down Expand Up @@ -1903,13 +1911,24 @@ public default void setPhaseTypeName(String phaseTypeName) {
*/
public double getActivityCoefficientUnSymetric(int k);

/**
* Verify if phase has a component.
*
* @param name Name of component to look for. NB! Converts name to normalized name.
* @return True if component is found.
*/
public default boolean hasComponent(String name) {
return hasComponent(name, true);
}

/**
* Verify if phase has a component.
*
* @param name Name of component to look for.
* @param normalized Set true to convert input name to normalized component name.
* @return True if component is found.
*/
public boolean hasComponent(String name);
public boolean hasComponent(String name, boolean normalized);

/**
* <p>
Expand Down
35 changes: 21 additions & 14 deletions src/main/java/neqsim/thermo/system/SystemInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,9 @@ public default void display() {
public String[] getCompIDs();

/**
* Get names of all components in System.
* Get normalized names of all components in System.
*
* @return an array of {@link java.lang.String} objects
* @return Array of names of components in System.
*/
public String[] getCompNames();

Expand Down Expand Up @@ -675,13 +675,14 @@ public default ComponentInterface getComponent(String name) {
}

/**
* <p>
* Getter for property <code>componentNames</code>.
* </p>
* Get normalized names of components in System.
*
* @return Component names in system.
* @return Array of names of components in system.
*/
public String[] getComponentNames();
public default String[] getComponentNames() {
return getPhase(0).getComponentNames();
}


/**
* <p>
Expand Down Expand Up @@ -1616,16 +1617,22 @@ public default int getPhaseNumberOfPhase(String phaseTypeName) {
/**
* Verify if system has a component.
*
* @param name Name of component to look for.
* @param name Name of component to look for. NB! Converts name to normalized name.
* @return True if component is found.
*/
public default boolean hasComponent(String name) {
for (String fluidComp : getComponentNames()) {
if (name == fluidComp) {
return true;
}
}
return false;
return hasComponent(name, true);
}

/**
* Verify if system has a component.
*
* @param name Name of component to look for.
* @param normalized Set true to convert input name to normalized component name.
* @return True if component is found.
*/
public default boolean hasComponent(String name, boolean normalized) {
return getPhase(0).hasComponent(name, normalized);
}

/** {@inheritDoc} */
Expand Down
24 changes: 8 additions & 16 deletions src/main/java/neqsim/thermo/system/SystemThermo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
* This is the base class of the System classes.
*/
public abstract class SystemThermo implements SystemInterface {
/** Logger object for class. */
static Logger logger = LogManager.getLogger(SystemThermo.class);
// Class variables
private static final int MAX_PHASES = 6;

/** Serialization version UID. */
private static final long serialVersionUID = 1000;

protected int a;
Expand Down Expand Up @@ -945,7 +947,7 @@ public void addTBPfraction(String componentName, double numberOfMoles, double mo
getPhase(i).getComponent(componentName).setIsTBPfraction(true);
getPhase(i).getComponent(componentName).setParachorParameter(
characterization.getTBPModel().calcParachorParameter(molarMass, density)); // 59.3+2.34*molarMass*1000.0);
// //0.5003*thermo.ThermodynamicConstantsInterface.R*TC/PC*(0.25969-racketZ));
// 0.5003*thermo.ThermodynamicConstantsInterface.R*TC/PC*(0.25969-racketZ));
getPhase(i).getComponent(componentName).setCriticalViscosity(
characterization.getTBPModel().calcCriticalViscosity(molarMass * 1000.0, density)); // 7.94830*Math.sqrt(1e3*molarMass)*Math.pow(PC,2.0/3.0)/Math.pow(TC,
// 1.0/6.0)*1e-7);
Expand Down Expand Up @@ -1934,21 +1936,6 @@ public String[] getCompNames() {
return names;
}

/** {@inheritDoc} */
@Override
public String[] getComponentNames() {
ArrayList<String> components = new ArrayList<String>();

for (int j = 0; j < numberOfComponents; j++) {
components.add(phaseArray[0].getComponents()[j].getName());
}
String[] componentList = new String[components.size()];
for (int j = 0; j < numberOfComponents; j++) {
componentList[j] = components.get(j);
}
return componentList;
}

/** {@inheritDoc} */
@Override
public String getComponentNameTag() {
Expand Down Expand Up @@ -3038,6 +3025,11 @@ public neqsim.standards.StandardInterface getStandard(String standardName) {
return standard;
}

/**
* Get sum of phase <code>beta</code> values.
*
* @return Sum of <code>beta</code> beta values
*/
public final double getSumBeta() {
double sum = 0;
for (int k = 0; k < numberOfPhases; k++) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/neqsim/thermo/phase/PhaseSrkEosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ void setUp() {
@Test
void testAddcomponent() {
Assertions.assertEquals(0, p.getNumberOfComponents());
Assertions.assertFalse(p.hasComponent("ethane"));

p.addComponent("ethane", 0, 0, 0);
Assertions.assertEquals(1, p.getNumberOfComponents());

Assertions.assertTrue(p.hasComponent("ethane"));

// Expects True because C2 normalized -> ethane
Assertions.assertTrue(p.hasComponent("C2"));
// Expects True because was created as ethane
Assertions.assertTrue(p.hasComponent("ethane", false));
// expects False because C2 is not equal to ethane
Assertions.assertFalse(p.hasComponent("C2", false));

try {
p.addComponent("methane", 0, 0, 0);
} catch (Exception e) {
Expand All @@ -27,6 +38,9 @@ void testAddcomponent() {

p.addComponent("methane", 0, 0, 1);
Assertions.assertEquals(2, p.getNumberOfComponents());
String[] d = p.getComponentNames();
Assertions.assertTrue(d[0].equals("ethane"));
Assertions.assertTrue(d[1].equals("methane"));
}

@Test
Expand Down

0 comments on commit 3ec2eb5

Please sign in to comment.