Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: getPhase (use PhaseType enum) #719

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/main/java/neqsim/thermo/system/SystemInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -2411,13 +2411,27 @@ public default double getNumberOfMoles() {

/**
* <p>
* getPhaseNumberOfPhase.
* Get phase number of phase of specific type.
* </p>
*
* @param phaseTypeName a {@link java.lang.String} object
* @return a int
* @param pt Phase type to look for.
* @return Phase number
*/
public int getPhaseNumberOfPhase(PhaseType pt);

/**
* <p>
* Get phase number of phase of specific type. *
* </p>
*
* @param phaseTypeName Name of phase type to look for
* @return Phase number
* @deprecated Replaced by getPhaseNumberOfPhase
*/
public int getPhaseNumberOfPhase(String phaseTypeName);
@Deprecated
public default int getPhaseNumberOfPhase(String phaseTypeName) {
return getPhaseNumberOfPhase(PhaseType.byDesc(phaseTypeName));
}

/**
* <p>
Expand Down
58 changes: 37 additions & 21 deletions src/main/java/neqsim/thermo/system/SystemThermo.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ abstract class SystemThermo implements SystemInterface {
// or
public String componentNameTag = "";
protected neqsim.thermo.characterization.WaxCharacterise waxCharacterisation = null;
protected double[] beta = new double[MAX_PHASES];
protected int a;

private ArrayList<String> componentNames = new ArrayList<String>();
// todo: replace numberOfComponents with length of componentNames.
protected int numberOfComponents = 0;

// protected ArrayList<String> resultArray1 = new ArrayList<String>();
protected String[] CapeOpenProperties11 = {"molecularWeight", "speedOfSound",
"jouleThomsonCoefficient", "internalEnergy", "internalEnergy.Dtemperature", "gibbsEnergy",
Expand All @@ -100,20 +102,25 @@ abstract class SystemThermo implements SystemInterface {
"density.Dpressure", "density.Dmoles", "volume", "volume.Dpressure", "volume.Dtemperature",
"molecularWeight.Dtemperature", "molecularWeight.Dpressure", "molecularWeight.Dmoles",
"compressibilityFactor"};
protected int numberOfComponents = 0;
protected int numberOfPhases = 2;
public int maxNumberOfPhases = 2;
protected int attractiveTermNumber = 0;

// PhaseType of phases belonging to system.
protected PhaseType[] phaseType = new PhaseType[MAX_PHASES];

// Index refers to position in phaseArray. First value of phaseIndex is the phase which is created
// first and the last is the phase created last.
/** Number of phases in use/existing. */
protected int numberOfPhases = 2;
/** Maximum allowed number of phases . */
public int maxNumberOfPhases = 2;
/**
* Array of indexes to phaseArray keeping track of the creation order of the phases where 0 is the
* first created phase and the lowest number is the phase created last.
*/
protected int[] phaseIndex = {0, 1, 2, 3, 4, 5};

// All phases of System. Flashes reorders phaseArray by density.
/**
* Array containing all phases of System. NB! Phases are reorered according to density, use
* phaseIndex to keep track of the creation order.
*/
protected PhaseInterface[] phaseArray = new PhaseInterface[MAX_PHASES];
// PhaseType of phases belonging to system.
protected PhaseType[] phaseType = new PhaseType[MAX_PHASES];
protected double[] beta = new double[MAX_PHASES];

protected ChemicalReactionOperations chemicalReactionOperations = null;
private int mixingRule = 1;
Expand Down Expand Up @@ -2319,22 +2326,31 @@ public boolean isPhase(int i) {
/** {@inheritDoc} */
@Override
public final PhaseInterface getPhase(int i) {
if (i >= getNumberOfPhases() && phaseArray[phaseIndex[i]] == null) {
throw new RuntimeException("Can not return phase number " + i
+ ". Current number of phases are " + getNumberOfPhases());
if (i < 0) {
throw new RuntimeException(new neqsim.util.exception.InvalidInputException(this, "getPhase",
"i", i + " is not valid, must be in the range 0-" + this.getNumberOfPhases()));
} else if (i >= getNumberOfPhases() && phaseArray[phaseIndex[i]] == null) {
throw new RuntimeException(new neqsim.util.exception.InvalidInputException(
this.getClass() + ":getPhase - Can not return phase number " + i
+ ". Current number of phases are " + getNumberOfPhases()));
}
return phaseArray[phaseIndex[i]];
}

/** {@inheritDoc} */
@Override
public PhaseInterface getPhase(String phaseTypeName) {
for (int i = 0; i < numberOfPhases; i++) {
if (getPhase(i).getPhaseTypeName().equals(phaseTypeName)) {
return getPhase(i);
}
PhaseType pt = PhaseType.byDesc(phaseTypeName);
if (!this.hasPhaseType(pt)) {
throw new RuntimeException("Phase with phase type " + pt + " not found.");
}

int phaseNum = getPhaseNumberOfPhase(pt);
if (phaseNum >= 0) {
return getPhase(phaseNum);
}
throw new RuntimeException();

return null;
}

/** {@inheritDoc} */
Expand All @@ -2350,9 +2366,9 @@ public PhaseInterface getPhaseOfType(String phaseTypeName) {

/** {@inheritDoc} */
@Override
public int getPhaseNumberOfPhase(String phaseTypeName) {
public int getPhaseNumberOfPhase(PhaseType pt) {
for (int i = 0; i < numberOfPhases; i++) {
if (getPhase(i).getPhaseTypeName().equals(phaseTypeName)) {
if (getPhase(i).getType() == pt) {
return i;
}
}
Expand Down