Skip to content

Commit

Permalink
fix minor bug (#1196)
Browse files Browse the repository at this point in the history
* fix minor bug

* update

* update

* update logging

* update

* update tests

* update

* ConstantVolumeDepletionTest

* update

* update

* update

* update

* update

* update

* update flash

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* updete tezt

* debuger log off

* update model

* remove print
  • Loading branch information
EvenSol authored Dec 3, 2024
1 parent ade0fd5 commit 8c1796a
Show file tree
Hide file tree
Showing 22 changed files with 511 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ public void run(UUID id) {
for (int j = 0; j < inletStream.getThermoSystem().getPhase(0).getNumberOfComponents(); j++) {
int index = inletStream.getThermoSystem().getPhase(0).getComponent(j).getComponentNumber();
double moles = inletStream.getThermoSystem().getPhase(0).getComponent(j).getNumberOfmoles();
double change =
(moles * splitFactor[i] - moles > 0) ? moles : moles * splitFactor[i] - moles;
double change = (moles * splitFactor[i] - moles > 0) ? 0.0 : moles * splitFactor[i] - moles;
splitStream[i].getThermoSystem().addComponent(index, change);
}
ThermodynamicOperations thermoOps =
Expand Down
75 changes: 41 additions & 34 deletions src/main/java/neqsim/thermodynamicoperations/flashops/Flash.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class Flash extends BaseOperation {
int i = 0;
int j = 0;
int iterations = 0;
int maxNumberOfIterations = 100;
int maxNumberOfIterations = 50;
double gibbsEnergy = 0;
double gibbsEnergyOld = 0;
double Kold = 0;
Expand All @@ -46,6 +46,7 @@ public abstract class Flash extends BaseOperation {
double[] oldDeltalnK;
double[] deltalnK;
double[] tm;
double tmLimit = -1e-8;
int lowestGibbsEnergyPhase = 0;
SysNewtonRhapsonTPflash secondOrderSolver;
/** Set true to do solid phase check and calculations */
Expand All @@ -72,6 +73,8 @@ public int findLowestGibbsEnergyPhase() {
} else {
lowestGibbsEnergyPhase = 1;
}
logger.info("Lowest Gibbs energy phase determined: Phase {}", lowestGibbsEnergyPhase);

findLowestGibbsPhaseIsChecked = true;
}
return lowestGibbsEnergyPhase;
Expand Down Expand Up @@ -100,37 +103,34 @@ public void stabilityAnalysis() throws neqsim.util.exception.IsNaNException,
double[] d = new double[system.getPhases()[0].getNumberOfComponents()];
double[][] x = new double[2][system.getPhases()[0].getNumberOfComponents()];
double[] error = new double[2];
tm = new double[system.getPhase(0).getNumberOfComponents()];
tm = new double[2];
double[] alpha = null;
Matrix f = new Matrix(system.getPhases()[0].getNumberOfComponents(), 1);
Matrix df = null;
int maxiterations = 50;
int maxiterations = 100;
double fNorm = 1.0e10;
double fNormOld = 0.0;
for (int i = 0; i < system.getPhases()[0].getNumberOfComponents(); i++) {
d[i] = minGibsPhaseLogZ[i] + minGibsLogFugCoef[i];
}

SystemInterface clonedSystem = minimumGibbsEnergySystem;
clonedSystem.setTotalNumberOfMoles(1.0);

double[] sumw = new double[2];
sumw[1] = 0.0;
sumw[0] = 0.0;
for (int i = 0; i < clonedSystem.getPhase(0).getNumberOfComponents(); i++) {
sumw[1] += clonedSystem.getPhase(0).getComponent(i).getz()
/ clonedSystem.getPhase(0).getComponent(i).getK();
sumw[0] += clonedSystem.getPhase(0).getComponent(i).getK()
* clonedSystem.getPhase(0).getComponent(i).getz();
if (clonedSystem.getPhase(0).getComponent(i).getz() > 0) {
sumw[0] += clonedSystem.getPhase(0).getComponent(i).getK()
* clonedSystem.getPhase(0).getComponent(i).getz();
}
}

// System.out.println("sumw0 " + sumw[0]);
// System.out.println("sumw1 " + sumw[1]);

int start = 0;
int end = 1; // clonedSystem.getNumberOfPhases()-1;
int end = 1;
int mult = 1;
// if (sumw[1] > sumw[0]) {

if (lowestGibbsEnergyPhase == 0) {
start = end;
end = 0;
Expand Down Expand Up @@ -175,8 +175,8 @@ public void stabilityAnalysis() throws neqsim.util.exception.IsNaNException,
+ clonedSystem.getPhase(j).getComponent(i).getLogFugacityCoefficient() - d[i]));
}
fNorm = f.norm2();
if (fNorm > fNormOld && iterations > 3 || (iterations + 1) % 7 != 0) {
break;
if (fNorm > fNormOld && iterations > 3 && (iterations - 1) % 7 != 0) {
// break;
}
if (iterations % 7 == 0 && fNorm < fNormOld && !secondOrderStabilityAnalysis) {
double vec1 = 0.0;
Expand Down Expand Up @@ -241,7 +241,7 @@ public void stabilityAnalysis() throws neqsim.util.exception.IsNaNException,
// logger.info("err newton " + error[j]);
}

logger.info("norm f " + f.norm1());
// logger.info("norm f " + f.norm1());
// clonedSystem.display();
sumw[j] = 0.0;
for (int i = 0; i < clonedSystem.getPhases()[0].getNumberOfComponents(); i++) {
Expand All @@ -252,25 +252,27 @@ public void stabilityAnalysis() throws neqsim.util.exception.IsNaNException,
deltalogWi[i] = logWi[i] - oldlogw[i];
clonedSystem.getPhase(j).getComponent(i).setx(Wi[j][i] / sumw[j]);
}
// logger.info("fnorm " + f.norm1() + " err " + error[j] + " iterations " +
// iterations + " phase " + j);
} while ((f.norm1() > 1e-6 && iterations < maxiterations) || (iterations % 7) == 0
|| iterations < 3);
// logger.info("fnorm " + f.norm1() + " err " + error[j] + " iterations " + iterations
// + " phase " + j);
} while ((f.norm1() > 1e-6 && iterations < maxiterations || error[j] > 1e-6)
|| (iterations % 7) == 0 || iterations < 3);
// (error[j]<oldErr && oldErr<oldOldErr) &&
// logger.info("err " + error[j]);
// logger.info("iterations " + iterations);
// logger.info("f.norm1() " + f.norm1());
if (iterations >= maxiterations) {
logger.error("err staability check " + error[j]);
// throw new util.exception.TooManyIterationsException();
// logger.error("err staability check " + error[j]);
throw new neqsim.util.exception.TooManyIterationsException("too many iterations", null,
maxiterations);
}

tm[j] = 1.0;
for (int i = 0; i < clonedSystem.getPhases()[0].getNumberOfComponents(); i++) {
tm[j] -= Wi[j][i];
x[j][i] = clonedSystem.getPhase(j).getComponent(i).getx();
}
if (tm[j] < -1e-4 && error[j] < 1e-6) {

if (tm[j] < tmLimit && error[j] < 1e-6) {
break;
} else {
tm[j] = 1.0;
Expand All @@ -280,25 +282,30 @@ public void stabilityAnalysis() throws neqsim.util.exception.IsNaNException,
// check for trivial solution
double diffx = 0.0;
for (int i = 0; i < clonedSystem.getPhase(0).getNumberOfComponents(); i++) {
diffx += Math.abs(clonedSystem.getPhase(0).getComponent(i).getx()
- clonedSystem.getPhase(1).getComponent(i).getx());
diffx += Math.abs(clonedSystem.getPhase(j).getComponent(i).getx()
- minimumGibbsEnergySystem.getPhase(0).getComponent(i).getx());
}
if (diffx < 1e-10) {
tm[0] = 0.0;
tm[1] = 0.0;
}

if (((tm[0] < -1e-4) || (tm[1] < -1e-4)) && !(Double.isNaN(tm[0]) || (Double.isNaN(tm[1])))) {
if (((tm[0] < tmLimit) || (tm[1] < tmLimit))
&& !(Double.isNaN(tm[0]) || (Double.isNaN(tm[1])))) {
for (int i = 0; i < clonedSystem.getPhases()[0].getNumberOfComponents(); i++) {
if (system.getPhases()[1].getComponent(i).getx() < 1e-100) {
if (system.getPhase(0).getComponent(i).getx() < 1e-100) {
continue;
}
if (tm[0] < -1e-4) {
system.getPhases()[1].getComponent(i).setK((Wi[0][i] / sumw[0]) / (Wi[1][i] / sumw[1]));
system.getPhases()[0].getComponent(i).setK((Wi[0][i] / sumw[0]) / (Wi[1][i] / sumw[1]));
} else if (tm[1] < -1e-4) {
system.getPhases()[1].getComponent(i).setK((Wi[0][i] / sumw[0]) / (Wi[1][i] / sumw[1]));
system.getPhases()[0].getComponent(i).setK((Wi[0][i] / sumw[0]) / (Wi[1][i] / sumw[1]));
if (tm[0] < tmLimit) {
system.getPhases()[1].getComponent(i).setK(clonedSystem.getPhase(0).getComponent(i).getx()
/ clonedSystem.getPhase(1).getComponent(i).getx());
system.getPhases()[0].getComponent(i).setK(clonedSystem.getPhase(0).getComponent(i).getx()
/ clonedSystem.getPhase(1).getComponent(i).getx());
} else if (tm[1] < tmLimit) {
system.getPhases()[1].getComponent(i).setK(clonedSystem.getPhase(0).getComponent(i).getx()
/ clonedSystem.getPhase(1).getComponent(i).getx());
system.getPhases()[0].getComponent(i).setK(clonedSystem.getPhase(0).getComponent(i).getx()
/ clonedSystem.getPhase(1).getComponent(i).getx());
} else {
logger.info("error in stability anlysis");
system.init(0);
Expand Down Expand Up @@ -332,7 +339,7 @@ public boolean stabilityCheck() {
logger.error("error ", ex);
}
}
if (!(tm[0] < -1e-4) && !(tm[1] < -1e-4) || system.getPhase(0).getNumberOfComponents() == 1) {
if (tm[0] > tmLimit && tm[1] > tmLimit || system.getPhase(0).getNumberOfComponents() == 1) {
stable = true;
system.init(0);
// logger.info("system is stable");
Expand All @@ -351,7 +358,7 @@ public boolean stabilityCheck() {
} else {
RachfordRice rachfordRice = new RachfordRice();
try {
system.setBeta(rachfordRice.calcBeta(system.getKvector(), system.getzvector()));
system.setBeta(rachfordRice.calcBeta(system.getKvector(), minimumGibbsEnergySystem.getzvector()));
} catch (Exception ex) {
if (!Double.isNaN(rachfordRice.getBeta()[0])) {
system.setBeta(rachfordRice.getBeta()[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package neqsim.thermodynamicoperations.flashops;

import java.io.Serializable;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import neqsim.thermo.component.ComponentInterface;
import neqsim.thermo.system.SystemInterface;

Expand All @@ -16,9 +19,10 @@
* @author Even Solbraa
*/
public class RachfordRice implements Serializable {
static Logger logger = LogManager.getLogger(PHsolidFlash.class);
private static final long serialVersionUID = 1000;
private double[] beta = new double[2];
private static String method = "Michelsen2001"; // alternative use Nielsen2023
private static String method = "Michelsen2001"; // alternative use Nielsen2023 or Michelsen2001

/**
* <p>
Expand Down Expand Up @@ -192,6 +196,8 @@ public double calcBetaMichelsen2001(double[] K, double[] z)
beta[1] = 1.0 - nybeta;

if (iterations >= maxIterations) {
logger.debug("K " + Arrays.toString(K));
logger.debug("z " + Arrays.toString(z));
throw new neqsim.util.exception.TooManyIterationsException(new RachfordRice(),
"calcBetaMichelsen2001", maxIterations);
}
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/neqsim/thermodynamicoperations/flashops/TPflash.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,15 @@ public void run() {
system.init(0);
system.init(1);

if (system.getPhase(0).getGibbsEnergy() < system.getPhase(1).getGibbsEnergy()) {
minimumGibbsEnergy = system.getPhase(0).getGibbsEnergy();
if ((system.getPhase(0).getGibbsEnergy()
* (1.0 - Math.signum(system.getPhase(0).getGibbsEnergy()) * 1e-8)) < system.getPhase(1)
.getGibbsEnergy()) {
minGibbsPhase = 0;
} else {
minGibbsPhase = 1;
minimumGibbsEnergy = system.getPhase(1).getGibbsEnergy();
}
// logger.debug("minimum gibbs phase " + minGibbsPhase);
minimumGibbsEnergy = system.getPhase(minGibbsPhase).getGibbsEnergy();

if (system.getPhase(0).getNumberOfComponents() == 1 || system.getMaxNumberOfPhases() == 1) {
system.setNumberOfPhases(1);
Expand Down Expand Up @@ -466,19 +469,23 @@ public void run() {
operation.run();
} else {
// Checks if gas or oil is the most stable phase
if (system.getPhase(0).getType() == PhaseType.GAS) {
gasgib = system.getPhase(0).getGibbsEnergy();
system.setPhaseType(0, PhaseType.byValue(0));
system.init(1, 0);
liqgib = system.getPhase(0).getGibbsEnergy();
} else {
liqgib = system.getPhase(0).getGibbsEnergy();
system.setPhaseType(0, PhaseType.byValue(1));
system.init(1, 0);
gasgib = system.getPhase(0).getGibbsEnergy();
}
try {
if (system.getPhase(0).getType() == PhaseType.GAS) {
gasgib = system.getPhase(0).getGibbsEnergy();
system.setPhaseType(0, PhaseType.byValue(0));

if (gasgib * (1.0 - Math.signum(gasgib) * 1e-8) < liqgib) {
system.init(1, 0);
liqgib = system.getPhase(0).getGibbsEnergy();
} else {
liqgib = system.getPhase(0).getGibbsEnergy();
system.setPhaseType(0, PhaseType.byValue(1));
system.init(1, 0);
gasgib = system.getPhase(0).getGibbsEnergy();
}
if (gasgib * (1.0 - Math.signum(gasgib) * 1e-8) < liqgib) {
system.setPhaseType(0, PhaseType.byValue(1));
}
} catch (Exception e) {
system.setPhaseType(0, PhaseType.byValue(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import neqsim.process.equipment.distillation.Condenser;
import neqsim.process.equipment.distillation.DistillationColumn;
import neqsim.process.equipment.stream.Stream;
import neqsim.process.equipment.stream.StreamInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import neqsim.process.equipment.pipeline.PipeBeggsAndBrills;
import neqsim.process.equipment.stream.Stream;
import neqsim.thermo.ThermodynamicConstantsInterface;
import neqsim.thermodynamicoperations.ThermodynamicOperations;
Expand Down Expand Up @@ -318,8 +317,7 @@ public void testPipeLineBeggsAndBrills4() {
ThermodynamicOperations testOps2 = new ThermodynamicOperations(testSystem3);
testOps2.TPflash();
testSystem3.initPhysicalProperties();

Assertions.assertEquals(testSystem3.hasPhaseType("oil"), true);
Assertions.assertEquals(testSystem3.hasPhaseType("gas"), true);

Stream stream_3 = new Stream("Stream1", testSystem3);
stream_3.setFlowRate(massFlowRate, "kg/hr");
Expand All @@ -343,7 +341,7 @@ public void testPipeLineBeggsAndBrills4() {

Assertions.assertEquals(testSystem3.hasPhaseType("gas"), true);

Assertions.assertEquals(temperatureOut3, -11.04463, 1);
Assertions.assertEquals(temperatureOut3, -8.81009355441591, 1);
Assertions.assertEquals(pressureOut3, 18.3429, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.junit.jupiter.api.Test;
import neqsim.process.equipment.pipeline.PipeBeggsAndBrills;
import neqsim.process.equipment.reservoir.SimpleReservoir;
import neqsim.process.equipment.reservoir.WellFlow;
import neqsim.process.equipment.stream.StreamInterface;
import neqsim.process.equipment.util.Adjuster;
import neqsim.process.equipment.valve.ThrottlingValve;
Expand Down Expand Up @@ -49,12 +47,13 @@ void testRun() {
void testRunTransientRes2() {
neqsim.thermo.system.SystemInterface fluid1 =
new neqsim.thermo.system.SystemPrEos(298.15, 38.0);

fluid1.addComponent("water", 3.599);
fluid1.addComponent("nitrogen", 0.599);
fluid1.addComponent("CO2", 0.51);
fluid1.addComponent("methane", 99.8);
fluid1.setMixingRule(2);
fluid1.setMultiPhaseCheck(true);
fluid1.setMultiPhaseCheck(false);

double producxtionIndex = 10.000100751427403E-3;

Expand Down Expand Up @@ -87,8 +86,7 @@ void testRunTransientRes2() {
compressor.setCompressionRatio(2.0);

neqsim.process.equipment.heatexchanger.Cooler intercooler =
new neqsim.process.equipment.heatexchanger.Cooler("cooler",
compressor.getOutletStream());
new neqsim.process.equipment.heatexchanger.Cooler("cooler", compressor.getOutletStream());
intercooler.setOutTemperature(25.0, "C");

neqsim.process.equipment.compressor.Compressor compressor2 =
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/neqsim/process/equipment/util/AdjusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void testRun() {
stream1.setTemperature(75.0, "C");

neqsim.process.equipment.pipeline.PipeBeggsAndBrills flowline1 =
new neqsim.process.equipment.pipeline.PipeBeggsAndBrills("flowline",
stream1);
new neqsim.process.equipment.pipeline.PipeBeggsAndBrills("flowline", stream1);
flowline1.setDiameter(0.25);
flowline1.setPipeWallRoughness(15e-6);
flowline1.setLength(1200);
Expand Down Expand Up @@ -61,8 +60,7 @@ void testRun2() {
stream1.setTemperature(75.0, "C");

neqsim.process.equipment.pipeline.PipeBeggsAndBrills flowline1 =
new neqsim.process.equipment.pipeline.PipeBeggsAndBrills("flowline",
stream1);
new neqsim.process.equipment.pipeline.PipeBeggsAndBrills("flowline", stream1);
flowline1.setDiameter(0.25);
flowline1.setPipeWallRoughness(15e-6);
flowline1.setLength(1200);
Expand All @@ -86,6 +84,6 @@ void testRun2() {

assertEquals(flowline1.getOutletStream().getPressure(), 120, 1e-3);
assertEquals(flowline1.getOutletStream().getFlowRate("MSm3/day"), 4.0, 1e-3);
assertEquals(flowline1.getInletStream().getPressure(), 200, 0.1);
assertEquals(flowline1.getInletStream().getPressure(), 199.976882003305, 0.1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void testRun() {

assertEquals(0.0012319218375683974,
streamSaturator.getOutletStream().getFluid().getPhase(0).getComponent("water").getx(),
1e-16);
1e-8);
}

@Test
Expand Down
Loading

0 comments on commit 8c1796a

Please sign in to comment.