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

new implementation rachford rice #996

Merged
merged 5 commits into from
May 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

package neqsim.thermodynamicOperations.flashOps;

import neqsim.thermo.ThermodynamicModelSettings;
import neqsim.thermo.component.ComponentInterface;
import neqsim.thermo.system.SystemInterface;
import neqsim.util.exception.IsNaNException;

/**
* RachfordRice classes.
*
Expand All @@ -23,6 +18,8 @@ public class RachfordRice {
* <p>
* calcBeta. For gas liquid systems.
* </p>
*
* Method based on Michelsen Mollerup, 2001
*
* @return Beta Mole fraction of gas phase
* @throws neqsim.util.exception.IsNaNException if any.
Expand Down Expand Up @@ -152,4 +149,135 @@ public static double calcBeta(double[] K, double[] z) throws neqsim.util.excepti
return nybeta;
}

/**
* <p>
* calcBeta. For gas liquid systems. Method based on Avoiding round-off error in the Rachford–Rice
* equation, Nielsen, Lia, 2023
* </p>
*
* @return Beta Mole fraction of gas phase
* @throws neqsim.util.exception.IsNaNException if any.
* @throws neqsim.util.exception.TooManyIterationsException if any.
*/
public static double calcBeta2(double[] K, double[] z)
throws neqsim.util.exception.IsNaNException,
neqsim.util.exception.TooManyIterationsException {

double tolerance = neqsim.thermo.ThermodynamicModelSettings.phaseFractionMinimumLimit;
double g0 = -1.0;
double g1 = 1.0;

for (int i = 0; i < K.length; i++) {
g0 += z[i] * K[i];
g1 += -z[i] / K[i];
}

if (g0 < 0) {
return tolerance;
}
if (g1 > 0) {
return 1.0 - tolerance;
}

double V = 0.5;
double h = 0.0;

for (int i = 0; i < K.length; i++) {
h += z[i] * (K[i] - 1.0) / (1.0 + V * (K[i] - 1.0));
}
if (h > 0) {
for (int i = 0; i < K.length; i++) {
K[i] = 1.0 / K[i];
}
}

double Kmax = K[0];
double Kmin = K[0];

for (int i = 1; i < K.length; i++) {
if (K[i] < Kmin) {
Kmin = K[i];
} else if (K[i] > Kmax) {
Kmax = K[i];
}
}

double alphaMin = 1.0 / (1.0 - Kmax);
double alphaMax = 1.0 / (1.0 - Kmin);

double alpha = V;

double a = (alpha - alphaMin) / (alphaMax - alpha);
double b = 1.0 / (alpha - alphaMin);

double[] c = new double[K.length];
double[] d = new double[K.length];
for (int i = 0; i < K.length; i++) {
c[i] = 1.0 / (1.0 - K[i]);
d[i] = (alphaMin - c[i]) / (alphaMax - alphaMin);
}

double hb = 0.0;
double amax = alpha;
double bmax = 1e20;
double amin = 0;
double bmin = 1.0 / (alphaMax - alphaMin);
int iter = 0;
int maxIterations = 300;
do {
iter++;
double funk = 0;
double funkder = 0.0;
hb = 0.0;
double hbder = 0.0;
for (int i = 0; i < K.length; i++) {
funk -= z[i] * a * (1.0 + a) / (d[i] + a * (1.0 + d[i]));
funkder -=
z[i] * (a * a + (1.0 + a) * (1.0 + a) * d[i]) / Math.pow(d[i] + a * (1.0 + d[i]), 2.0);
hb += z[i] * b / (1.0 + b * (alphaMin - c[i]));
hbder += z[i] / Math.pow(1.0 + b * (alphaMin - c[i]), 2.0);
}
if (funk > 0) {
amax = a;
} else {
amin = a;
}
if (hb > 0) {
bmax = b;
} else {
bmin = b;
}
a = a - funk / funkder;
if (a > amax || a < amin) {
a = (amax + amin) / 2.0;
}
b = b - hb / hbder;
if (b > bmax || b < bmin) {
b = (bmax + bmin) / 2.0;
}
} while (Math.abs(hb) > 1e-10 && iter < maxIterations);

V = -((1 / b) / a - alphaMax);

if (h > 0) {
V = 1 - V;
}

if (V <= tolerance) {
V = tolerance;
} else if (V >= 1.0 - tolerance) {
V = 1.0 - tolerance;
}

if (iter >= maxIterations) {
throw new neqsim.util.exception.TooManyIterationsException(new RachfordRice(), "calcBeta",
maxIterations);
}
if (Double.isNaN(V)) {
throw new neqsim.util.exception.IsNaNException(new RachfordRice(), "calcBeta", "beta");
}

return V;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void testMain() {
operations.add(multiPhaseMeter);
operations.run();

Assertions.assertEquals(51.3073530232923, multiPhaseMeter.getMeasuredValue("GOR", ""), 1e-12);
Assertions.assertEquals(51.3073530232923, multiPhaseMeter.getMeasuredValue("GOR", ""), 1e-5);
Assertions.assertEquals(3106.770827796345, multiPhaseMeter.getMeasuredValue("GOR_std", ""),
1e-12);
1e-2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void runProcess() throws InterruptedException {

operations.run();

assertEquals(17195.25050, seprator3rdStage.getGasOutStream().getFlowRate("kg/hr"), 0.001);
assertEquals(17195.25050, seprator3rdStage.getGasOutStream().getFlowRate("kg/hr"), 1.1);

assertEquals(seprator3rdStage.getGasOutStream().getFlowRate("kg/hr"),
coolerLP.getOutletStream().getFlowRate("kg/hr"), 1e-4);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/neqsim/thermo/system/SystemPCSAFTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void testInit() {
testSystem.initProperties();
System.out.println("test");
double cp = testSystem.getCp();
assertEquals(208.85116193406583, cp);
assertEquals(208.85116193406583, cp, 0.1);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ public class RachfordRiceTest {
@Test
void testCalcBeta() {

double[] z = new double[] {0.1, 0.9};
double[] K = new double[] {20.0, 0.1};
double[] z = new double[] {0.7, 0.3};
double[] K = new double[] {2.0, 0.01};

try {
Assertions.assertEquals(0.06374269005, RachfordRice.calcBeta(K, z), 1e-6);
Assertions.assertEquals(0.407070707, RachfordRice.calcBeta(K, z), 1e-6);
} catch (Exception e) {
e.printStackTrace();
}

}

@Test
void testCalcBetaMethod2() {

double[] z = new double[] {0.7, 0.3};
double[] K = new double[] {2.0, 0.01};

try {
Assertions.assertEquals(0.407070707, RachfordRice.calcBeta(K, z), 1e-6);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down