Skip to content

Commit

Permalink
improved speed of water saturator (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvenSol authored May 4, 2024
1 parent 286b1a4 commit 11dd1a0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ public SaturateWithWater(SystemInterface system) {
@Override
public void run() {
if (!system.getPhase(0).hasComponent("water")) {
system.addComponent("water", system.getTotalNumberOfMoles());
system.createDatabase(true);
system.addComponent("water", system.getTotalNumberOfMoles() / 100.0);
system.setMixingRule(system.getMixingRule());
if (system.doMultiPhaseCheck()) {
system.setMultiPhaseCheck(true);
}
system.init(0);
}
double dn = 1.0;
int i = 0;

boolean changedMultiPhase = false;
if (system.doMultiPhaseCheck() == false) {
system.setMultiPhaseCheck(true);
changedMultiPhase = true;
}


if (system.getComponent("water").getNumberOfmoles() < system.getTotalNumberOfMoles() / 2.0) {
system.addComponent("water", system.getTotalNumberOfMoles());
}
this.tpFlash = new TPflash(system);
tpFlash.run();
boolean hasAq = false;
Expand All @@ -56,32 +59,37 @@ public void run() {
}
double lastdn = 0.0;
if (system.hasPhaseType(PhaseType.AQUEOUS)) {
lastdn = system.getPhaseOfType("aqueous").getComponent("water").getNumberOfMolesInPhase();
lastdn = system.getPhase(PhaseType.AQUEOUS).getNumberOfMolesInPhase();
} else {
lastdn = system.getPhase(0).getNumberOfMolesInPhase() / 100.0;
lastdn = system.getPhase(0).getNumberOfMolesInPhase();
}

double dn = 1.0;
int i = 0;
do {
i++;

if (!hasAq) {
system.addComponent("water", lastdn * 0.5);
lastdn *= 0.8;
if (system.getNumberOfPhases() == 1 && hasAq) {
lastdn = -system.getComponent("water").getNumberOfmoles() * 0.1;
} else if (!hasAq) {
lastdn = Math.abs(lastdn) * 1.05;
} else {
lastdn = system.getPhaseOfType("aqueous").getComponent("water").getNumberOfMolesInPhase();
dn = lastdn / system.getNumberOfMoles();
system.addComponent("water", -lastdn);
lastdn =
-system.getPhaseOfType("aqueous").getComponent("water").getNumberOfMolesInPhase() * 0.9;
}
dn = lastdn / system.getNumberOfMoles();
system.addComponent("water", lastdn);
tpFlash.run();
// system.display();
hasAq = system.hasPhaseType("aqueous");
} while ((i < 50 && Math.abs(dn) > 1e-7) || !hasAq && i <= 50);
hasAq = system.hasPhaseType(PhaseType.AQUEOUS);
} while (Math.abs(dn) > 1e-7 && i <= 50);
if (i == 50) {
logger.error("could not find solution - in water saturate : dn " + dn);
}
// logger.info("i " + i + " dn " + dn);
system.removePhase(system.getNumberOfPhases() - 1);
tpFlash.run();
if (system.hasPhaseType(PhaseType.AQUEOUS)) {
system.removePhase(system.getNumberOfPhases() - 1);
tpFlash.run();
}
if (changedMultiPhase) {
system.setMultiPhaseCheck(false);
}
}

/**
Expand Down Expand Up @@ -116,7 +124,7 @@ public static void main(String[] args) {
// testSystem.display();
// testSystem.addComponent("water", 1);
// testOps.saturateWithWater();
testSystem.display();
// testSystem.display();
// testOps.TPflash();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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.SystemPrEos;
import neqsim.thermo.system.SystemSrkCPAstatoil;
import neqsim.thermodynamicOperations.ThermodynamicOperations;

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

@Test
void testRun() {
SystemInterface testSystem = new SystemSrkCPAstatoil(273.15 + 70.0, 150.0);

testSystem.addComponent("methane", 75.0);
testSystem.addComponent("ethane", 7.5);
testSystem.addComponent("propane", 4.0);
testSystem.addComponent("n-butane", 1.0);
testSystem.addComponent("i-butane", 0.6);
testSystem.addComponent("n-hexane", 0.3);
testSystem.addPlusFraction("C6", 1.3, 100.3 / 1000.0, 0.8232);
testSystem.setMixingRule(10);
testSystem.setMultiPhaseCheck(true);

ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem);
try {
testOps.saturateWithWater();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
assertEquals(0.0029033655101811814, testSystem.getComponent("water").getz(), 1e-5);
}


@Test
void testRun2() {
SystemInterface testSystem = new SystemSrkCPAstatoil(273.15 + 70.0, 150.0);

testSystem.addComponent("methane", 75.0);
testSystem.addComponent("ethane", 7.5);
testSystem.addComponent("propane", 4.0);
testSystem.setMixingRule(10);

ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem);
try {
testOps.saturateWithWater();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
assertEquals(0.002891748277007660, testSystem.getComponent("water").getz(), 1e-5);
}

@Test
void testRun3() {
SystemInterface testSystem = new SystemSrkCPAstatoil(273.15 + 20.0, 150.0);

testSystem.addComponent("methane", 2.0);
testSystem.addComponent("n-heptane", 75.0);
testSystem.setMixingRule(10);
testSystem.setMultiPhaseCheck(true);

ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem);
try {
testOps.saturateWithWater();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
assertEquals(2.8465024974919816E-4, testSystem.getComponent("water").getz(), 1e-5);
}

@Test
void testRun4() {
SystemInterface testSystem = new SystemPrEos(273.15 + 20.0, 150.0);

testSystem.addComponent("methane", 2.0);
testSystem.addComponent("n-heptane", 75.0);
testSystem.setMixingRule("classic");
testSystem.setMultiPhaseCheck(true);

ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem);
try {
testOps.saturateWithWater();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
assertEquals(2.4301370485671443E-4, testSystem.getComponent("water").getz(), 1e-5);
}
}

0 comments on commit 11dd1a0

Please sign in to comment.