Skip to content

Commit

Permalink
fix possible to read kij from database whn using __ in component name (
Browse files Browse the repository at this point in the history
…#1184)

* fix possible to read kij from database whn using __ in component name

* added test

* update

* update

* update
  • Loading branch information
EvenSol authored Nov 19, 2024
1 parent 2fed0aa commit a87a457
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 54 deletions.
92 changes: 50 additions & 42 deletions src/main/java/neqsim/thermo/mixingrule/EosMixingRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,8 @@ public double calcAi(int compNumb, PhaseInterface phase, double temperature, dou
// pressure, numbcomp) * R * temperature * alpha_mix;
A = getB() * R * temperature * compArray[compNumb].getAder() + phase.getNumberOfMolesInPhase()
* compArray[compNumb].getBi() * R * temperature * alpha_mix;
// calcBi(compNumb, phase, temperature, pressure, numbcomp) * R * temperature * alpha_mix;
// calcBi(compNumb, phase, temperature, pressure, numbcomp) * R * temperature *
// alpha_mix;

// System.out.println("Ai: " + A);
return A;
Expand Down Expand Up @@ -2130,9 +2131,10 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
wijCalcOrFitted = new int[phase.getNumberOfComponents()][phase.getNumberOfComponents()];
try (neqsim.util.database.NeqSimDataBase database = new neqsim.util.database.NeqSimDataBase()) {
for (int k = 0; k < phase.getNumberOfComponents(); k++) {
String component_name = phase.getComponents()[k].getComponentName();
String component_name = phase.getComponent(k).getComponentName();

for (int l = k; l < phase.getNumberOfComponents(); l++) {
String component_name2 = phase.getComponents()[l].getComponentName();
if (k == l) {
classicOrHV[k][l] = "Classic";
classicOrWS[k][l] = "Classic";
Expand All @@ -2141,26 +2143,32 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
} else {
java.sql.ResultSet dataSet = null;
try {
int underscoreIndex = component_name.indexOf("__"); // double underscore
if (underscoreIndex != -1) {
component_name = component_name.substring(0, underscoreIndex);
}
int underscoreIndex2 = component_name2.indexOf("__");
if (underscoreIndex2 != -1) {
component_name2 = component_name2.substring(0, underscoreIndex2);
}
if (phase.getComponent(k).isIsTBPfraction()
|| phase.getComponent(l).isIsTBPfraction()) {
throw new Exception("no interaction coefficient for TBP fractions");
}
int templ = l;
int tempk = k;

if (NeqSimDataBase.createTemporaryTables()) {
dataSet = database.getResultSet("SELECT * FROM intertemp WHERE (comp1='"
+ component_name + "' AND comp2='" + phase.getComponents()[l].getComponentName()
+ "') OR (comp1='" + phase.getComponents()[l].getComponentName()
+ "' AND comp2='" + component_name + "')");
+ component_name + "' AND comp2='" + component_name2 + "') OR (comp1='"
+ component_name2 + "' AND comp2='" + component_name + "')");
} else {
dataSet = database.getResultSet("SELECT * FROM inter WHERE (comp1='"
+ component_name + "' AND comp2='" + phase.getComponents()[l].getComponentName()
+ "') OR (comp1='" + phase.getComponents()[l].getComponentName()
+ "' AND comp2='" + component_name + "')");
+ component_name + "' AND comp2='" + component_name2 + "') OR (comp1='"
+ component_name2 + "' AND comp2='" + component_name + "')");
}
dataSet.next();
if (dataSet.getString("comp1").trim()
.equals(phase.getComponents()[l].getComponentName())) {
if (dataSet.getString("comp1").trim().equals(component_name2)) {
templ = k;
tempk = l;
}
Expand Down Expand Up @@ -2277,22 +2285,22 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
nEOSkij);
// System.out.println("intparam not defined .... CALCULATING intparam
// between "
// +phase.getComponents()[l].getComponentName() + " and " +
// phase.getComponents()[k].getComponentName()+ " to " +
// +component_name2 + " and " +
// component_name+ " to " +
// intparam[k][l]);
} else if ((phase.getComponents()[k].getComponentName().equals("CO2")
} else if ((component_name.equals("CO2")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("CO2")
|| (component_name2.equals("CO2")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.1;
} else if ((phase.getComponents()[k].getComponentName().equals("nitrogen")
} else if ((component_name.equals("nitrogen")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("nitrogen")
|| (component_name2.equals("nitrogen")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.08;
} else if ((phase.getComponents()[k].getComponentName().equals("water")
} else if ((component_name.equals("water")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("water")
|| (component_name2.equals("water")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.2;

Expand All @@ -2312,9 +2320,9 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {

intparamT[k][l] = 0.0;
}
} else if ((phase.getComponents()[k].getComponentName().equals("MEG")
} else if ((component_name.equals("MEG")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("MEG")
|| (component_name2.equals("MEG")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.2;
if (phase.getClass().getName().equals("neqsim.thermo.phase.PhaseSrkCPA")
Expand All @@ -2331,9 +2339,9 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
// intparam[k][l] = 0.01;
intparamT[k][l] = 0.0;
}
} else if ((phase.getComponents()[k].getComponentName().equals("ethanol")
} else if ((component_name.equals("ethanol")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("ethanol")
|| (component_name2.equals("ethanol")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.0;
if (phase.getClass().getName().equals("neqsim.thermo.phase.PhaseSrkCPA")
Expand All @@ -2347,9 +2355,9 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
intparam[k][l] = -0.1;
}
}
} else if ((phase.getComponents()[k].getComponentName().equals("methanol")
} else if ((component_name.equals("methanol")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("methanol")
|| (component_name2.equals("methanol")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.0;
if (phase.getClass().getName().equals("neqsim.thermo.phase.PhaseSrkCPA")
Expand All @@ -2363,9 +2371,9 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
intparam[k][l] = -0.2;
}
}
} else if ((phase.getComponents()[k].getComponentName().equals("TEG")
} else if ((component_name.equals("TEG")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("TEG")
|| (component_name2.equals("TEG")
&& phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.12;
if (phase.getClass().getName().equals("neqsim.thermo.phase.PhaseSrkCPA")
Expand All @@ -2375,27 +2383,25 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
intparam[k][l] = 0.12;
intparamT[k][l] = 0.0;
}
} else if ((phase.getComponents()[k].getComponentName().equals("S8")
&& phase.getComponents()[l].isIsTBPfraction())
|| (phase.getComponents()[l].getComponentName().equals("S8")
&& phase.getComponents()[k].isIsTBPfraction())) {
} else if ((component_name.equals("S8") && phase.getComponents()[l].isIsTBPfraction())
|| (component_name2.equals("S8") && phase.getComponents()[k].isIsTBPfraction())) {
intparam[k][l] = 0.05;
} else {
// if((phase.getComponents()[l].getComponentName().equals("CO2") ||
// phase.getComponents()[k].getComponentName().equals("CO2")) && k!=l)
// if((component_name2.equals("CO2") ||
// component_name.equals("CO2")) && k!=l)
// intparam[k][l] = 0.1;
// else if((phase.getComponents()[l].getComponentName().equals("H2S") ||
// phase.getComponents()[k].getComponentName().equals("H2S")) && k!=l)
// else if((component_name2.equals("H2S") ||
// component_name.equals("H2S")) && k!=l)
// intparam[k][l] = 0.2;
// else if((phase.getComponents()[l].getComponentName().equals("water")
// else if((component_name2.equals("water")
// ||
// phase.getComponents()[k].getComponentName().equals("water")) && k!=l)
// component_name.equals("water")) && k!=l)
// intparam[k][l] = 0.5;
// else intparam[k][l] = 0.0;
// System.out.println("intparam not defined .... setting intparam
// between " +
// phase.getComponents()[l].getComponentName() + " and " +
// phase.getComponents()[k].getComponentName() + " to " +
// component_name2 + " and " +
// component_name + " to " +
// intparam[k][l]);
}

Expand All @@ -2406,8 +2412,8 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
intparamji[k][l] = intparam[k][l];
intparamji[l][k] = intparam[k][l];
// System.out.println("kij set to " + intparam[l][k] + " " +
// phase.getComponents()[l].getComponentName() + " " +
// phase.getComponents()[k].getComponentName());
// component_name2 + " " +
// component_name);

classicOrHV[k][l] = "Classic";
classicOrHV[l][k] = classicOrHV[k][l];
Expand Down Expand Up @@ -2458,7 +2464,8 @@ public EosMixingRulesInterface getMixingRule(int i, PhaseInterface phase) {
mixingRuleName = "classic-CPA_T";
return new ClassicSRKT2();
} else if (i == 10) {
// return new ElectrolyteMixRule(phase, HValpha, HVgij, HVgii, classicOrHV,wij);}
// return new ElectrolyteMixRule(phase, HValpha, HVgij, HVgii,
// classicOrHV,wij);}
org.ejml.simple.SimpleMatrix mat1 = new org.ejml.simple.SimpleMatrix(intparamij);
org.ejml.simple.SimpleMatrix mat2 = new org.ejml.simple.SimpleMatrix(intparamji);
org.ejml.simple.SimpleMatrix mat3 = new org.ejml.simple.SimpleMatrix(intparamT);
Expand Down Expand Up @@ -2517,7 +2524,8 @@ public EosMixingRulesInterface resetMixingRule(int i, PhaseInterface phase) {
mixingRuleName = "classic-CPA_T";
return new ClassicSRKT2();
} else if (i == 10) {
// return new ElectrolyteMixRule(phase, HValpha, HVgij, HVgii, classicOrHV,wij);}
// return new ElectrolyteMixRule(phase, HValpha, HVgij, HVgii,
// classicOrHV,wij);}
org.ejml.simple.SimpleMatrix mat1 = new org.ejml.simple.SimpleMatrix(intparamij);
org.ejml.simple.SimpleMatrix mat2 = new org.ejml.simple.SimpleMatrix(intparamji);
org.ejml.simple.SimpleMatrix mat3 = new org.ejml.simple.SimpleMatrix(intparamT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void runCurveTest() {
Stream stream_1 = new Stream("Stream1", testFluid);
stream_1.run();

stream_1.getFluid().prettyPrint();
// stream_1.getFluid().prettyPrint();

Compressor comp1 = new Compressor("compressor 1", stream_1);
comp1.setCompressorChartType("interpolate and extrapolate");
Expand Down Expand Up @@ -253,17 +253,28 @@ public void runCurveTest() {
comp1.getAntiSurge().setSurgeControlFactor(1.0);
comp1.run();

org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("speed " + comp1.getSpeed());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("out pres " + comp1.getOutletPressure());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("out temp " + (comp1.getOutTemperature() - 273.15));
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("feed flow " + (comp1.getInletStream().getFlowRate("m3/hr")));
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("polytropic head " + comp1.getPolytropicFluidHead());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("polytropic efficiency " + comp1.getPolytropicEfficiency());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("dist to surge " + comp1.getDistanceToSurge());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("surge flow rate margin " + comp1.getSurgeFlowRateMargin());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("surge flow rate " + comp1.getSurgeFlowRate());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("duty " + comp1.getPower("MW"));
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class).debug("surge " + comp1.isSurge());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("speed " + comp1.getSpeed());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("out pres " + comp1.getOutletPressure());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("out temp " + (comp1.getOutTemperature() - 273.15));
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("feed flow " + (comp1.getInletStream().getFlowRate("m3/hr")));
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("polytropic head " + comp1.getPolytropicFluidHead());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("polytropic efficiency " + comp1.getPolytropicEfficiency());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("dist to surge " + comp1.getDistanceToSurge());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("surge flow rate margin " + comp1.getSurgeFlowRateMargin());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("surge flow rate " + comp1.getSurgeFlowRate());
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("duty " + comp1.getPower("MW"));
org.apache.logging.log4j.LogManager.getLogger(CompressorChartTest.class)
.debug("surge " + comp1.isSurge());
Assertions.assertTrue(comp1.isSurge() == false);
Assertions.assertEquals(158.7732888, comp1.getOutletPressure(), 1e-3);
}
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/neqsim/thermo/mixingrule/EosMixingRulesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package neqsim.thermo.mixingrule;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import neqsim.thermo.phase.PhaseEos;

/**
* Test class for verifying the behavior of EOS (Equation of State) mixing rules in the NeqSim
* library.
*/
/**
* This class contains unit tests for verifying the behavior of different mixing rules
* in the neqsim thermodynamic system.
*/
public class EosMixingRulesTest {
@Test
void testSetMixingRuleName() {

neqsim.thermo.system.SystemPrEos testSystem = new neqsim.thermo.system.SystemPrEos(298.0, 10.0);
testSystem.addComponent("nitrogen", 0.01);
testSystem.addComponent("CO2", 0.01);
testSystem.changeComponentName(testSystem.getComponent(0).getName(),
(testSystem.getComponent(0).getName() + "__well1"));
testSystem.changeComponentName(testSystem.getComponent(1).getName(),
(testSystem.getComponent(1).getName() + "__well1"));



testSystem.addComponent("nitrogen", 0.01);
testSystem.addComponent("CO2", 0.01);
testSystem.changeComponentName(testSystem.getComponent(2).getName(),
(testSystem.getComponent(2).getName() + "__well2"));
testSystem.changeComponentName(testSystem.getComponent(3).getName(),
(testSystem.getComponent(3).getName() + "__well2"));


testSystem.setMixingRule("classic");

double kij =
((PhaseEos) testSystem.getPhase(0)).getMixingRule().getBinaryInteractionParameter(0, 1);
double kij2 =
((PhaseEos) testSystem.getPhase(0)).getMixingRule().getBinaryInteractionParameter(3, 0);

// Print kij
assertEquals(-0.019997, kij, 1e-5);
assertTrue(kij == kij2);

}

@Test
void testMEGOil() {

neqsim.thermo.system.SystemSrkCPAstatoil testSystem =
new neqsim.thermo.system.SystemSrkCPAstatoil(298.0, 10.0);
testSystem.addTBPfraction("C8", 0.01, 90.9 / 1000.0, 0.9);
testSystem.addComponent("ethanol", 0.01);

testSystem.setMixingRule("classic");

double kij =
((PhaseEos) testSystem.getPhase(0)).getMixingRule().getBinaryInteractionParameter(0, 1);

// Print kij
assertEquals(-0.05, kij, 1e-5);

}

@Test
void testHCoilInter() {

neqsim.thermo.system.SystemPrEos testSystem = new neqsim.thermo.system.SystemPrEos(298.0, 10.0);
testSystem.addTBPfraction("C8", 0.01, 90.9 / 1000.0, 0.9);
testSystem.addComponent("CO2", 0.01);
testSystem.changeComponentName(testSystem.getComponent(0).getName(),
(testSystem.getComponent(0).getName() + "__well1"));
testSystem.changeComponentName(testSystem.getComponent(1).getName(),
(testSystem.getComponent(1).getName() + "__well1"));


testSystem.addTBPfraction("C8", 0.01, 90.9 / 1000.0, 0.9);
testSystem.addComponent("CO2", 0.01);
testSystem.changeComponentName(testSystem.getComponent(2).getName(),
(testSystem.getComponent(2).getName() + "__well2"));
testSystem.changeComponentName(testSystem.getComponent(3).getName(),
(testSystem.getComponent(3).getName() + "__well2"));

testSystem.setMixingRule("classic");

double kij =
((PhaseEos) testSystem.getPhase(0)).getMixingRule().getBinaryInteractionParameter(0, 1);
double kij2 =
((PhaseEos) testSystem.getPhase(0)).getMixingRule().getBinaryInteractionParameter(3, 0);

// Print kij
assertEquals(0.1, kij, 1e-5);
assertTrue(kij == kij2);
}
}

0 comments on commit a87a457

Please sign in to comment.