From 0e70072e3f2fb26c2ef664c248791bb0c7b5cc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85smund=20V=C3=A5ge=20Fannemel?= <34712686+asmfstatoil@users.noreply.github.com> Date: Tue, 30 May 2023 09:41:58 +0200 Subject: [PATCH] refact: getPhase (use PhaseType enum) --- .../neqsim/thermo/system/SystemInterface.java | 22 +++++-- .../neqsim/thermo/system/SystemThermo.java | 58 ++++++++++++------- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/main/java/neqsim/thermo/system/SystemInterface.java b/src/main/java/neqsim/thermo/system/SystemInterface.java index 1523cd5a2..2a1bc5316 100644 --- a/src/main/java/neqsim/thermo/system/SystemInterface.java +++ b/src/main/java/neqsim/thermo/system/SystemInterface.java @@ -2411,13 +2411,27 @@ public default double getNumberOfMoles() { /** *

- * getPhaseNumberOfPhase. + * Get phase number of phase of specific type. *

* - * @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); + + /** + *

+ * Get phase number of phase of specific type. * + *

+ * + * @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)); + } /** *

diff --git a/src/main/java/neqsim/thermo/system/SystemThermo.java b/src/main/java/neqsim/thermo/system/SystemThermo.java index 63c58a1b1..5ea9987a6 100644 --- a/src/main/java/neqsim/thermo/system/SystemThermo.java +++ b/src/main/java/neqsim/thermo/system/SystemThermo.java @@ -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 componentNames = new ArrayList(); + // todo: replace numberOfComponents with length of componentNames. + protected int numberOfComponents = 0; + // protected ArrayList resultArray1 = new ArrayList(); protected String[] CapeOpenProperties11 = {"molecularWeight", "speedOfSound", "jouleThomsonCoefficient", "internalEnergy", "internalEnergy.Dtemperature", "gibbsEnergy", @@ -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; @@ -2319,9 +2326,13 @@ 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]]; } @@ -2329,12 +2340,17 @@ public final PhaseInterface getPhase(int 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} */ @@ -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; } }