Skip to content

Commit

Permalink
update gradient flash
Browse files Browse the repository at this point in the history
  • Loading branch information
EvenSol committed Nov 15, 2024
1 parent bcfed40 commit 23d5e2a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public TPgradientFlash(SystemInterface system, double height, double temperature
this.system = system;
this.temperature = temperature;
this.height = height;
Jac = new Matrix(system.getPhase(0).getNumberOfComponents(),
system.getPhase(0).getNumberOfComponents());
fvec = new Matrix(system.getPhase(0).getNumberOfComponents(), 1);
Jac = new Matrix(system.getPhase(0).getNumberOfComponents() + 1,
system.getPhase(0).getNumberOfComponents() + 1);
fvec = new Matrix(system.getPhase(0).getNumberOfComponents() + 1, 1);
}

/**
Expand All @@ -63,7 +63,9 @@ public TPgradientFlash(SystemInterface system, double height, double temperature
* </p>
*/
public void setfvec() {
double sumx = 0.0;
for (int i = 0; i < system.getPhase(0).getNumberOfComponents(); i++) {
sumx += localSystem.getPhases()[0].getComponents()[i].getx();
fvec.set(i, 0,
Math.log(localSystem.getPhases()[0].getComponents()[i].getFugacityCoefficient()
* localSystem.getPhases()[0].getComponents()[i].getx() * localSystem.getPressure())
Expand All @@ -85,6 +87,7 @@ public void setfvec() {
/ neqsim.thermo.ThermodynamicConstantsInterface.R
/ tempSystem.getPhase(0).getTemperature());
}
fvec.set(localSystem.getNumberOfComponents(), 0, sumx - 1.0);
}

/**
Expand Down Expand Up @@ -112,8 +115,27 @@ public void setJac() {
Jac.set(i, j, tempJ);
}
}

int i = system.getPhase(0).getNumberOfComponents();
for (int j = 0; j < system.getPhase(0).getNumberOfComponents(); j++) {
Jac.set(i, j, 1.0);
}

for (i = 0; i < system.getPhase(0).getNumberOfComponents(); i++) {
tempJ = 1.0
/ (localSystem.getPhases()[0].getComponents()[i].getFugacityCoefficient()
* localSystem.getPhases()[0].getComponents()[i].getx() * localSystem.getPressure())
* (localSystem.getPhases()[0].getComponents()[i].getdfugdp()
* localSystem.getPhases()[0].getComponents()[i].getx() * localSystem.getPressure()
+ localSystem.getPhases()[0].getComponents()[i].getFugacityCoefficient()
* localSystem.getPhases()[0].getComponents()[i].getx());
Jac.set(i, system.getPhase(0).getNumberOfComponents(), tempJ);
}
Jac.set(system.getPhase(0).getNumberOfComponents(), system.getPhase(0).getNumberOfComponents(),
0.0);
}


/**
* <p>
* setNewX.
Expand All @@ -122,9 +144,11 @@ public void setJac() {
public void setNewX() {
for (int i = 0; i < system.getPhase(0).getNumberOfComponents(); i++) {
localSystem.getPhase(0).getComponent(i)
.setx(localSystem.getPhase(0).getComponent(i).getx() - 0.5 * dx.get(i, 0));
.setx(localSystem.getPhase(0).getComponent(i).getx() - 0.8 * dx.get(i, 0));
}
localSystem.getPhase(0).normalize();
localSystem.setPressure(
localSystem.getPressure() - 0.8 * dx.get(system.getPhase(0).getNumberOfComponents(), 0));
// localSystem.getPhase(0).normalize();
}

/** {@inheritDoc} */
Expand All @@ -142,14 +166,16 @@ public void run() {

for (int i = 0; i < 20; i++) {
localSystem.setTemperature(localSystem.getTemperature() + deltaT);
for (int o = 0; o < 15; o++) {
int iter = 0;
do {
iter += 1;
localSystem.init(3);
setfvec();
setJac();
dx = Jac.solve(fvec);
dx.print(10, 10);
// dx.print(10, 10);
setNewX();
}
} while (dx.norm2() > 1e-10 && iter < 50);
// localSystem.display();

tempSystem = localSystem.clone();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package neqsim.thermodynamicoperations.flashops;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import neqsim.thermo.system.SystemInterface;
import neqsim.thermo.system.SystemSrkEos;
import neqsim.thermodynamicoperations.ThermodynamicOperations;

public class TPgradientFlashTest {
static Logger logger = LogManager.getLogger(TPgradientFlashTest.class);

@Test
void testRun() {
SystemInterface testSystem = new SystemSrkEos(345, 80.0);

ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem);
testSystem.addComponent("hydrogen", 1.0);
testSystem.addComponent("methane", 8.0);
testSystem.addComponent("propane", 1.0);

testSystem.createDatabase(true);
testSystem.setMixingRule(2);

SystemInterface newSystem = null;
try {
try {
newSystem = testOps.TPgradientFlash(1000, 355).phaseToSystem(0);
} catch (Exception e) {
e.printStackTrace();
}
// newSystem.prettyPrint();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}

assertEquals(0.0987274603, newSystem.getComponent("hydrogen").getx(), 1e-2);
}

}

0 comments on commit 23d5e2a

Please sign in to comment.