Skip to content

Commit

Permalink
refact: systemthermo.phaseArray as PhaseType enum, updated setPhaseTy…
Browse files Browse the repository at this point in the history
…pe methods (#704)

* refact: introduce setPhaseType using PhaseType enum
* refact: change system phasetype array to enum
* refact: java.lang.String -> String
* refact: not deprecate set and get phasetype as str
  • Loading branch information
asmfstatoil committed May 23, 2023
1 parent f18eeb0 commit 55a5ac8
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 82 deletions.
4 changes: 0 additions & 4 deletions src/main/java/neqsim/thermo/phase/PhaseInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -1945,9 +1945,7 @@ public default void setPhaseType(int phaseType) {
* </p>
*
* @return a {@link String} object
* @deprecated Replace with {@link getType}
*/
@Deprecated
public default String getPhaseTypeName() {
return getType().getDesc();
}
Expand All @@ -1958,9 +1956,7 @@ public default String getPhaseTypeName() {
* </p>
*
* @param phaseTypeName a {@link String} object
* @deprecated Replace with {@link setType}
*/
@Deprecated
public default void setPhaseTypeName(String phaseTypeName) {
setType(PhaseType.byDesc(phaseTypeName));
}
Expand Down
69 changes: 44 additions & 25 deletions src/main/java/neqsim/thermo/system/SystemInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ public static SystemInterface addFluids(SystemInterface addFluid1, SystemInterfa
* getInterfacialTension.
* </p>
*
* @param phase1 a int
* @param phase2 a int
* @param phase1 phase number of phase1
* @param phase2 phase number of phase2
* @param unit a {@link java.lang.String} object
* @return a double
* @return interfacial tension with specified unit
*/
public double getInterfacialTension(int phase1, int phase2, String unit);

Expand Down Expand Up @@ -1052,7 +1052,7 @@ public default boolean hasSolidPhase() {
*
* @return a {@link java.lang.String} object
*/
public java.lang.String getMixingRuleName();
public String getMixingRuleName();

/**
* <p>
Expand All @@ -1061,7 +1061,7 @@ public default boolean hasSolidPhase() {
*
* @return a {@link java.lang.String} object
*/
public java.lang.String getModelName();
public String getModelName();

/**
* <p>
Expand Down Expand Up @@ -1402,7 +1402,7 @@ public default boolean hasSolidPhase() {
*
* @return a {@link java.lang.String} object
*/
public java.lang.String getFluidInfo();
public String getFluidInfo();

/**
* <p>
Expand All @@ -1411,7 +1411,7 @@ public default boolean hasSolidPhase() {
*
* @param info a {@link java.lang.String} object
*/
public void setFluidInfo(java.lang.String info);
public void setFluidInfo(String info);

/**
* <p>
Expand Down Expand Up @@ -1891,32 +1891,51 @@ public double calcBeta()
// public double getdfugdt(int i, int j);

/**
* <p>
* method to set the phase type of a given phase.
* </p>
* Change the phase type of a given phase.
*
* @param phaseToChange a int
* @param newPhaseType a int
* @param phaseToChange the phase number of the phase to set phase type
* @param newPhaseType the phasetype number to set
* @deprecated Replaced by {@link setPhaseType}
*/
public void setPhaseType(int phaseToChange, int newPhaseType);
@Deprecated
public default void setPhaseType(int phaseToChange, int newPhaseType) {
setPhaseType(phaseToChange, PhaseType.byValue(newPhaseType));
}

/**
* <p>
* setPhaseType.
* </p>
* Change the phase type of a given phase.
*
* @param phases a {@link java.lang.String} object
* @param newPhaseType a int
* @param phaseToChange the phase number of the phase to set phase type
* @param phaseTypeName the phase type name, see PhaseTypes
*/
public void setPhaseType(String phases, int newPhaseType);
public default void setPhaseType(int phaseToChange, String phaseTypeName) {
setPhaseType(phaseToChange, PhaseType.byDesc(phaseTypeName));
}

/**
* method to set the phase type of a given phase.
* Change the phase type of a given phase.
*
* @param phaseToChange the phase number of the phase to set phase type
* @param phaseTypeName the phase type name (valid names are gas or liquid)
* @param pt PhaseType to set
*/
public void setPhaseType(int phaseToChange, PhaseType pt);

/**
* Set phase type of all phases.
*
* @param phases Set to "all" to set all phases, else nothing happens.
* @param newPhaseType the phasetype number to set
* @deprecated Replaced by {@link setAllPhaseType}
*/
@Deprecated
public void setPhaseType(String phases, int newPhaseType);

/**
* Set phase type of all phases.
*
* @param pt PhaseType to set phases as.
*/
public void setPhaseType(int phaseToChange, String phaseTypeName);
public void setAllPhaseType(PhaseType pt);

/**
* <p>
Expand Down Expand Up @@ -2198,7 +2217,7 @@ public default ComponentInterface getComponent(int i) {
*
* @return a {@link java.lang.String} object
*/
public java.lang.String getFluidName();
public String getFluidName();

/**
* <p>
Expand All @@ -2207,7 +2226,7 @@ public default ComponentInterface getComponent(int i) {
*
* @param fluidName a {@link java.lang.String} object
*/
public void setFluidName(java.lang.String fluidName);
public void setFluidName(String fluidName);

/**
* <p>
Expand Down Expand Up @@ -2514,7 +2533,7 @@ public void setImplementedCompositionDeriativesofFugacity(
*
* @param name Component name to add
*/
public void addToComponentNames(java.lang.String name);
public void addToComponentNames(String name);

/**
* <p>
Expand Down
99 changes: 53 additions & 46 deletions src/main/java/neqsim/thermo/system/SystemThermo.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ abstract class SystemThermo implements SystemInterface {
public int maxNumberOfPhases = 2;
protected int attractiveTermNumber = 0;

// phasetype to be enum
protected int[] phaseType = {1, 0, 0, 0, 0, 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.
protected int[] phaseIndex = {0, 1, 2, 3, 4, 5};

// All phases of System. Flashes reorders phaseArray by density.
protected PhaseInterface[] phaseArray;
protected PhaseInterface[] phaseArray = new PhaseInterface[MAX_PHASES];

protected ChemicalReactionOperations chemicalReactionOperations = null;
private int mixingRule = 1;
Expand All @@ -136,7 +136,6 @@ abstract class SystemThermo implements SystemInterface {
* </p>
*/
public SystemThermo() {
phaseArray = new PhaseInterface[MAX_PHASES];
characterization = new Characterise(this);
interfaceProp = new InterfaceProperties(this);
}
Expand Down Expand Up @@ -168,12 +167,14 @@ public SystemThermo(double T, double P) {
"SystemThermo", "P", "is negative");
throw new RuntimeException(ex);
}
beta[0] = 1.0;
beta[1] = 1.0;
beta[2] = 1.0;
beta[3] = 1.0;
beta[4] = 1.0;
beta[5] = 1.0;

reInitPhaseType();
phaseType[4] = phaseType[3];
phaseType[5] = phaseType[3];

for (int i = 0; i < MAX_PHASES; i++) {
beta[i] = 1.0;
}
}

/** {@inheritDoc} */
Expand All @@ -186,8 +187,8 @@ public int getNumberOfComponents() {
@Override
public void clearAll() {
setTotalNumberOfMoles(0);
phaseType[0] = 1;
phaseType[1] = 0;
phaseType[0] = PhaseType.byValue(1);
phaseType[1] = PhaseType.byValue(0);
numberOfComponents = 0;
setNumberOfPhases(2);
beta[0] = 1.0;
Expand Down Expand Up @@ -588,7 +589,7 @@ public SystemInterface phaseToSystem(PhaseInterface newPhase) {
setTotalNumberOfMoles(newPhase.getNumberOfMolesInPhase());
this.init(0);
setNumberOfPhases(1);
setPhaseType(0, newPhase.getPhaseType());
setPhaseType(0, newPhase.getType());
initBeta();
init_x_y();
this.init(1);
Expand Down Expand Up @@ -629,7 +630,7 @@ public SystemInterface phaseToSystem(int phaseNumber) {

newSystem.init(0);
newSystem.setNumberOfPhases(1);
newSystem.setPhaseType(0, getPhase(phaseNumber).getPhaseType()); // phaseType[phaseNumber]);
newSystem.setPhaseType(0, getPhase(phaseNumber).getType()); // phaseType[phaseNumber]);
newSystem.init(1);
return newSystem;
}
Expand Down Expand Up @@ -798,7 +799,7 @@ public void addTBPfraction(String componentName, double numberOfMoles, double mo
refSystem.addComponent("default", 1.0, 273.15, 50.0, 0.1);
refSystem.init(0);
refSystem.setNumberOfPhases(1);
refSystem.setPhaseType(0, "liquid");
refSystem.setPhaseType(0, PhaseType.LIQUID);
molarMass = 1000 * molarMass;
TC = characterization.getTBPModel().calcTC(molarMass, density);
PC = characterization.getTBPModel().calcPC(molarMass, density);
Expand Down Expand Up @@ -944,7 +945,7 @@ public void addTBPfraction(String componentName, double numberOfMoles, double mo
refSystem.addComponent("default", 1.0, 273.15, 50.0, 0.1);
refSystem.init(0);
refSystem.setNumberOfPhases(1);
refSystem.setPhaseType(0, "liquid");
refSystem.setPhaseType(0, PhaseType.LIQUID);
molarMass = 1000 * molarMass;
// characterization.getTBPModel().calcTC(molarMass, density);
TC = criticalTemperature;
Expand Down Expand Up @@ -1813,11 +1814,11 @@ public void initAnalytic(int type) {
// TODO: should actually clear all entries in arrays?
setNumberOfPhases(getMaxNumberOfPhases());
for (int i = 0; i < numberOfPhases; i++) {
phaseType[i] = 0;
phaseType[i] = PhaseType.byValue(0);
beta[i] = 1.0;
phaseIndex[i] = i;
}
phaseType[0] = 1;
phaseType[0] = PhaseType.byValue(1);
for (int i = 0; i < numberOfPhases; i++) {
if (isPhase(i)) {
getPhase(i).init(getTotalNumberOfMoles(), numberOfComponents, type,
Expand Down Expand Up @@ -2895,39 +2896,44 @@ public void setTemperature(double newTemperature, String unit) {

/** {@inheritDoc} */
@Override
public void setPhaseType(int phaseToChange, int newPhaseType) {
// System.out.println("new phase type: cha " + newPhaseType);
public void setPhaseType(int phaseToChange, PhaseType pt) {
// System.out.println("new phase type: cha " + pt);
if (allowPhaseShift) {
phaseType[phaseIndex[phaseToChange]] = newPhaseType;
phaseType[phaseIndex[phaseToChange]] = pt;
}
}

/** {@inheritDoc} */
@Override
public void setPhaseType(int phaseToChange, String phaseTypeName) {
// System.out.println("new phase type: cha " + newPhaseType);
int newPhaseType = 1;
if (allowPhaseShift) {
if (phaseTypeName.equals("gas")) {
newPhaseType = 1;
} else if (StateOfMatter.isLiquid(PhaseType.byDesc(phaseTypeName))) {
newPhaseType = 0;
} else {
newPhaseType = 0;
}
phaseType[phaseIndex[phaseToChange]] = newPhaseType;
int newPhaseType = 0;
if (phaseTypeName.equals("gas")) {
newPhaseType = 1;
} else if (StateOfMatter.isLiquid(PhaseType.byDesc(phaseTypeName))) {
newPhaseType = 0;
} else {
newPhaseType = 0;
}

setPhaseType(phaseToChange, PhaseType.byValue(newPhaseType));
}

/** {@inheritDoc} */
@Override
public void setPhaseType(String phases, int newPhaseType) {
// System.out.println("new phase type: cha " + newPhaseType);
if (phases.equals("all")) {
setAllPhaseType(PhaseType.byValue(newPhaseType));
}
}

/** {@inheritDoc} */
@Override
public void setAllPhaseType(PhaseType pt) {
if (allowPhaseShift) {
if (phases.equals("all")) {
for (int i = 0; i < getMaxNumberOfPhases(); i++) {
phaseType[i] = newPhaseType;
}
for (int i = 0; i < getMaxNumberOfPhases(); i++) {
setPhaseType(i, pt);
}
}
}
Expand All @@ -2936,10 +2942,10 @@ public void setPhaseType(String phases, int newPhaseType) {
@Override
public void invertPhaseTypes() {
for (int i = 0; i < getMaxNumberOfPhases(); i++) {
if (phaseType[i] == 0) {
phaseType[i] = 1;
if (phaseType[i] == PhaseType.byValue(0)) {
phaseType[i] = PhaseType.byValue(1);
} else {
phaseType[i] = 0;
phaseType[i] = PhaseType.byValue(0);
}
}
}
Expand All @@ -2957,10 +2963,11 @@ public void setPhase(PhaseInterface phase, int numb) {
/** {@inheritDoc} */
@Override
public void reInitPhaseType() {
phaseType[0] = 1;
phaseType[1] = 0;
phaseType[2] = 0;
phaseType[3] = 0;
phaseType[0] = PhaseType.byValue(1);
phaseType[1] = PhaseType.byValue(0);
phaseType[2] = PhaseType.byValue(0);
phaseType[3] = PhaseType.byValue(0);
// TODO: why stop at 3 and not iterate through MAX_PHASES elements?
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -3927,13 +3934,13 @@ public void setFluidInfo(String info) {

/** {@inheritDoc} */
@Override
public java.lang.String getFluidName() {
public String getFluidName() {
return fluidName;
}

/** {@inheritDoc} */
@Override
public void setFluidName(java.lang.String fluidName) {
public void setFluidName(String fluidName) {
this.fluidName = fluidName;
}

Expand Down Expand Up @@ -3972,7 +3979,7 @@ public void calcKIJ(boolean ok) {

/** {@inheritDoc} */
@Override
public java.lang.String getModelName() {
public String getModelName() {
return modelName;
}

Expand All @@ -3981,7 +3988,7 @@ public java.lang.String getModelName() {
*
* @param modelName New value of property modelName.
*/
public void setModelName(java.lang.String modelName) {
public void setModelName(String modelName) {
this.modelName = modelName;
}

Expand Down
Loading

0 comments on commit 55a5ac8

Please sign in to comment.