Skip to content

Commit

Permalink
Impovements and bug fixes for enum PhaseType (#692)
Browse files Browse the repository at this point in the history
* waterDegasserBugs

* style and assert@

* update from master (#690)

* 684 addfluid is not adding pc if not existing (#685)

* added test for addFLuids

* updated test

* updated test with new pseudo component

* solution for bug

* refact: simplified if-else nesting

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>

* dont do log of zeros (#688)

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>

* modeidief test

* log off

* rebase on master (#693)

* 684 addfluid is not adding pc if not existing (#685)

* added test for addFLuids

* updated test

* updated test with new pseudo component

* solution for bug

* refact: simplified if-else nesting

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>

* dont do log of zeros (#688)

* AddfluidHighTemp (#689)

* added test for addFLuids

* updated test

* updated test with new pseudo component

* solution for bug

* add test TPflas high

* added gitlense extension

* update

* update

* update test

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>

* added pretty print for table

* added prettyPrint + various fixes to PhaseType use

* copy phasetype object in clone method

* removed phasetpye setter not needed?

* disable test that throws run tim exception

* fixed doc

* fixed doc

* added BWRS parameter table

* exception handling BWRS

* updated glycol rigtest

* disable pH test

* fixed bug

* enabled test ph test

* added serialization test back again

---------

Co-authored-by: Sviatoslav Eroshkin <109044598+Sviatose@users.noreply.github.com>
Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>
  • Loading branch information
3 people authored May 21, 2023
1 parent c31240c commit 135d3c7
Show file tree
Hide file tree
Showing 17 changed files with 382 additions and 43 deletions.
5 changes: 1 addition & 4 deletions src/main/java/neqsim/thermo/component/ComponentBWRS.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public ComponentBWRS(String component_name, double moles, double molesInPhase, i
dataSet.next();
dataSet.getClob("name");
} catch (Exception ex) {
dataSet.close();
dataSet = database
.getResultSet(("SELECT * FROM mbwr32param WHERE name='" + component_name + "'"));
dataSet.next();
logger.error(ex.getMessage(), ex);
}

for (int i = 0; i < 32; i++) {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/neqsim/thermo/phase/Phase.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ abstract class Phase implements PhaseInterface {
double pressure = 0;

protected PhaseInterface[] refPhase = null;

protected int phaseType = 0;
protected String phaseTypeName = "gas";
protected PhaseType pt = PhaseType.GAS;


Expand All @@ -83,6 +80,7 @@ public Phase clone() {
logger.error("Cloning failed.", ex);
}

clonedPhase.pt = PhaseType.byValue(pt.getValue());
clonedPhase.componentArray = this.componentArray.clone();
for (int i = 0; i < numberOfComponents; i++) {
clonedPhase.componentArray[i] = this.componentArray[i].clone();
Expand Down Expand Up @@ -410,7 +408,7 @@ public neqsim.physicalProperties.physicalPropertySystem.PhysicalPropertiesInterf
/** {@inheritDoc} */
@Override
public void init() {
init(numberOfMolesInPhase / beta, numberOfComponents, initType, phaseType, beta);
init(numberOfMolesInPhase / beta, numberOfComponents, initType, pt.getValue(), beta);
}

/** {@inheritDoc} */
Expand All @@ -424,8 +422,8 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int type, in

this.beta = beta;
numberOfMolesInPhase = beta * totalNumberOfMoles;
if (this.phaseType != phase) {
this.phaseType = phase;
if (this.pt.getValue() != phase) {
this.pt = PhaseType.byValue(phase);
// setPhysicalProperties(physicalPropertyType);
}
this.setInitType(type);
Expand Down Expand Up @@ -1958,20 +1956,18 @@ public final PhaseType getType() {
@Override
public final void setType(PhaseType pt) {
this.pt = pt;
this.phaseType = pt.getValue();
this.phaseTypeName = pt.getDesc();
}

/** {@inheritDoc} */
@Override
public final int getPhaseType() {
return phaseType;
return pt.getValue();
}

/** {@inheritDoc} */
@Override
public String getPhaseTypeName() {
return phaseTypeName;
return pt.getDesc();
}

/** {@inheritDoc} */
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/neqsim/thermo/phase/PhaseBWRSEos.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,15 @@ public double getdFdN() {
getComponent(0).addMoles(dn);
numberOfMolesInPhase += dn;

init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
double fold = getF();
numberOfMolesInPhase -= 2 * dn;
getComponent(0).addMoles(-2 * dn);
init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
double fnew = getF();
numberOfMolesInPhase += dn;
getComponent(0).addMoles(dn);
init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
// System.out.println("F " + getFpol()*1e3+ " "+ getFexp()*1e3 + " super " +
// super.getF() + " phasetype " +getType());
return (fold - fnew) / (2 * dn);
Expand All @@ -449,13 +449,13 @@ public double getdFdN() {
public double dFdT() {
// double dv = temperature/1000.0;
// temperature = temperature + dv;
// init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
// init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
// double fold = getF();
// temperature = temperature - 2*dv;
// init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
// init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
// double fnew = getF();
// temperature = temperature + dv;
// init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
// init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
// System.out.println("dFdT " + ((fold-fnew)/(2*dv)) + " super " +
// (getFpoldT()+getFexpdT())*1e3+ " phasetype " +getType());
return (getFpoldT() + getFexpdT()) * 1e3; // (fold-fnew)/(2*dv);
Expand All @@ -470,13 +470,13 @@ public double dFdT() {
public double dFdTdT() {
double dv = temperature / 1000.0;
temperature += dv;
init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
double fold = dFdT();
init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
final double fold = dFdT();
temperature -= 2 * dv;
init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
double fnew = dFdT();
temperature += dv;
init(numberOfMolesInPhase, numberOfComponents, 3, phaseType, 1.0);
init(numberOfMolesInPhase, numberOfComponents, 3, pt.getValue(), 1.0);
return (fold - fnew) / (2 * dv);
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/neqsim/thermo/phase/PhaseDesmukhMather.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int initType
double beta) {
super.init(totalNumberOfMoles, numberOfComponents, initType, phase, beta);
if (initType != 0) {
phaseTypeName = phase == 0 ? "liquid" : "gas";
pt = PhaseType.byValue(phase);
// phaseTypeName = phase == 0 ? "liquid" : "gas";
}
setMolarVolume(0.980e-3 * getMolarMass() * 1e5);
Z = pressure * getMolarVolume() / (R * temperature);
Expand Down Expand Up @@ -77,8 +78,8 @@ && getComponents()[k].getComponentName().equals("MDEA")) {

// if
// (dataSet.getString("comp1").trim().equals(getComponents()[l].getComponentName())) {
// templ = k;
// tempk = l;
// templ = k;
// tempk = l;
// }
this.aij[k][l] = Double.parseDouble(dataSet.getString("aijDesMath"));
this.bij[k][l] = Double.parseDouble(dataSet.getString("bijDesMath"));
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/neqsim/thermo/phase/PhaseEos.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public void displayInteractionCoefficients(String intType) {
@Override
public void init(double totalNumberOfMoles, int numberOfComponents, int type, int phase,
double beta) {
if (phase > 1) {
phase = 0;
}
if (!mixingRuleDefined) {
setMixingRule(1);
}
Expand All @@ -95,7 +98,6 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int type, in
}

if (type != 0) {
phaseTypeName = phase == 0 ? "liquid" : "gas";
try {
if (calcMolarVolume) {
molarVolume = molarVolume(pressure, temperature,
Expand Down Expand Up @@ -127,7 +129,6 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int type, in
loc_ATT = calcATT(this, temperature, pressure, numberOfComponents);
}

// logger.info("V/b" + (getVolume()/getB()) + " Z " + getZ());
double sumHydrocarbons = 0.0;
double sumAqueous = 0.0;
for (int i = 0; i < numberOfComponents; i++) {
Expand All @@ -144,8 +145,7 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int type, in
} else if (sumHydrocarbons > sumAqueous) {
setType(PhaseType.OIL);
} else {
// setType(PhaseType.AQUEOUS); // todo: this breaks tests
phaseTypeName = "aqueous";
setType(PhaseType.AQUEOUS);
}

// if ((hasComponent("water") && getVolume() / getB() < 1.75 &&
Expand Down Expand Up @@ -1265,4 +1265,4 @@ public double dFdNdT(int i) {
return ((ComponentEosInterface) getComponent(i)).dFdNdT(this, this.getNumberOfComponents(),
temperature, pressure);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/neqsim/thermo/phase/PhaseGE.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void init(double temperature, double pressure, double totalNumberOfMoles,
if (sumHydrocarbons > sumAqueous) {
setType(PhaseType.OIL);
} else {
phaseTypeName = "aqueous";
pt = PhaseType.byName("aqueous");
}
}

Expand All @@ -106,7 +106,7 @@ public void init(double totalNumberOfMoles, int numberOfComponents, int initType
if (sumHydrocarbons > sumAqueous) {
setType(PhaseType.OIL);
} else {
phaseTypeName = "aqueous";
pt = PhaseType.byName("aqueous");
}

// calc liquid density
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/neqsim/thermo/phase/PhaseGEUnifac.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void checkGroups() {
/** {@inheritDoc} */
@Override
public double getExcessGibbsEnergy() {
return getExcessGibbsEnergy(this, numberOfComponents, temperature, pressure, phaseType);
return getExcessGibbsEnergy(this, numberOfComponents, temperature, pressure, pt.getValue());
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public double calcXLR() {
* @return a double
*/
public double calcGammaLRdV() {
if (phaseType == 1) {
if (pt.getValue() == 1) {
return 0.0;
}
// return 0.0; // problem ved ren komponent
Expand Down Expand Up @@ -797,7 +797,7 @@ public double dFLRdTdT() {
*/
public double dFLRdV() {
return (FLRV() + dFdAlphaLR() * alphaLRdV) * 1e-5; // + FLRGammaLR()*gammLRdV +
// 0*FLRXLR()*XLRdGammaLR()*gammLRdV)*1e-5;
// 0*FLRXLR()*XLRdGammaLR()*gammLRdV)*1e-5;
}

/**
Expand Down Expand Up @@ -1424,7 +1424,8 @@ public double FBornD() {
* @return a double
*/
public double FBornTT() {
return 2.0 * (avagadroNumber * electronCharge * electronCharge
return 2.0
* (avagadroNumber * electronCharge * electronCharge
/ (4.0 * pi * vacumPermittivity * R * temperature * temperature * temperature))
* (1.0 / getSolventDiElectricConstant() - 1.0) * bornX;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public double calcXLR() {
* @return a double
*/
public double calcGammaLRdV() {
if (phaseType == 1) {
if (pt.getValue() == 1) {
return 0.0;
}
// return 0.0; // problem ved ren komponent
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/neqsim/thermo/phase/PhasePrCPA.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public boolean solveX() {
double err = .0;
int iter = 0;
try {
molarVolume(pressure, temperature, getA(), getB(), phaseType);
molarVolume(pressure, temperature, getA(), getB(), pt.getValue());
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/neqsim/thermo/phase/PhaseType.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package neqsim.thermo.phase;

import java.io.Serializable;
import neqsim.util.exception.InvalidInputException;

/**
* Types of phases.
*/
public enum PhaseType {
public enum PhaseType implements Serializable{
LIQUID("liquid", 0), GAS("gas", 1), OIL("oil", 2), AQUEOUS("aqueous", 3), HYDRATE("hydrate",
4), WAX("wax", 5), SOLID("solid", 6), SOLIDCOMPLEX("solidComplex", 7);

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/neqsim/thermo/system/SystemInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,14 @@ public default void initThermoProperties() {
*/
public void display(String name);

/**
* Prints the fluid in a visually appealing way.
*
*/
public default void prettyPrint() {
neqsim.thermo.util.readwrite.TablePrinter.printTable(createTable(getFluidName()));
}

/**
* <p>
* addFluid.
Expand Down Expand Up @@ -2018,7 +2026,6 @@ public double calcBeta()
*/
public String[] getComponentNames();


/**
* <p>
* Get component by name.
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/neqsim/thermo/util/readwrite/TablePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package neqsim.thermo.util.readwrite;

import java.io.Serializable;

/**
* A utility class for pretty printing a 2D string table.
*/
public class TablePrinter implements Serializable{

/**
* Prints a 2D string table in a formatted and visually appealing way.
*
* @param table The 2D string table to be printed.
*/
public static void printTable(String[][] table) {
if (table == null || table.length == 0 || table[0].length == 0) {
System.out.println("Table is empty.");
return;
}

int[] columnWidths = getColumnWidths(table);

printHorizontalLine(columnWidths);

for (String[] row : table) {
printRow(row, columnWidths);
printHorizontalLine(columnWidths);
}
}

/**
* Calculates the maximum width of each column in the table.
*
* @param table The 2D string table.
* @return An array containing the maximum width of each column.
*/
private static int[] getColumnWidths(String[][] table) {
int columns = table[0].length;
int[] columnWidths = new int[columns];

for (String[] row : table) {
for (int i = 0; i < columns; i++) {
int width = row[i].length();
if (width > columnWidths[i]) {
columnWidths[i] = width;
}
}
}

return columnWidths;
}

/**
* Prints a horizontal line separator based on the column widths.
*
* @param columnWidths An array containing the maximum width of each column.
*/
private static void printHorizontalLine(int[] columnWidths) {
System.out.print("+");
for (int width : columnWidths) {
for (int i = 0; i < width + 2; i++) {
System.out.print("-");
}
System.out.print("+");
}
System.out.println();
}

/**
* Prints a row of the table with appropriate padding based on column widths.
*
* @param row The row of data to be printed.
* @param columnWidths An array containing the maximum width of each column.
*/
private static void printRow(String[] row, int[] columnWidths) {
System.out.print("|");
for (int i = 0; i < row.length; i++) {
String cell = row[i];
int padding = columnWidths[i] - cell.length();
System.out.print(" " + cell);
for (int j = 0; j < padding; j++) {
System.out.print(" ");
}
System.out.print(" |");
}
System.out.println();
}
}
2 changes: 2 additions & 0 deletions src/main/resources/data/MBWR32param.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,"methane",0.000009898,0.021996082,-0.532278800,20.216579620,-2234.398926000,0.000010679,0.000145792,-0.926581666,291.536473200,0.000000231,0.000138721,0.004780467,0.000011761,-0.000198209,-0.025128877,0.000009748,-0.000000120,0.000041283,-0.000000721,508.173825500,-91989.031920000,-2.732264677,74990.243510000,0.001114060,1.083955159,-0.000044909,-1.380337847,-0.000000023,0.000037616,-2.375166954e-10,-0.000000012,0.000000676,10.150000000
2,"ethane",-0.018439486,1.051016206,-16.057820303,848.440275620,-42738.409106000,0.000765652,-0.483607241,85.195473835,-16607.434721000,-0.000037521,0.028616309,-2.868528597,0.000119069,-0.008531571,3.836506384,0.000024986,0.000005797,-0.007164832,0.000125778,22240.102466000,-1480051.23280000,50.498054887,1642883.75992000,0.213253871,37.791273422,-0.000011857,-31.630780767,-0.000004100,0.001487004,0.000000003,-0.000002167,0.000024000,10.150000000
Loading

0 comments on commit 135d3c7

Please sign in to comment.