Skip to content

Commit 0622128

Browse files
committed
update
1 parent 214dbfb commit 0622128

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

src/main/java/neqsim/thermodynamicoperations/BaseOperation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public double[] get(String name) {
3535
return new double[3];
3636
}
3737

38+
/** {@inheritDoc} */
39+
@Override
40+
public double[] get(String name, double[] defaultValue) {
41+
double[] result = get(name);
42+
return result != null ? result : defaultValue;
43+
}
44+
3845
/** {@inheritDoc} */
3946
@Override
4047
public String[][] getResultTable() {

src/main/java/neqsim/thermodynamicoperations/OperationInterface.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ public interface OperationInterface extends Runnable, java.io.Serializable {
7272
*/
7373
public double[] get(String name);
7474

75+
/**
76+
* Returns the named result array, or the supplied default if the key is not found or the result
77+
* is {@code null}. This overload makes Python/JPype usage natural:
78+
* {@code pe_data.get("dewT", emptyArray)} mirrors the Python dict {@code .get(key, default)}
79+
* idiom.
80+
*
81+
* @param name the result key (e.g. "dewT", "bubP")
82+
* @param defaultValue the array to return when the key is absent or maps to {@code null}
83+
* @return the result array, or {@code defaultValue} if not found
84+
*/
85+
default double[] get(String name, double[] defaultValue) {
86+
double[] result = get(name);
87+
return result != null ? result : defaultValue;
88+
}
89+
7590
/**
7691
* <p>
7792
* getJFreeChart.

src/main/java/neqsim/thermodynamicoperations/ThermodynamicOperations.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,19 @@ public double[] get(String name) {
23602360
return getOperation().get(name);
23612361
}
23622362

2363+
/**
2364+
* Returns the named result array, or the supplied default if the key is not found or the result
2365+
* is {@code null}. Convenience for Python/JPype consumers where
2366+
* {@code ops.get("dewT", emptyArray)} mirrors the Python dict {@code .get(key, default)} idiom.
2367+
*
2368+
* @param name the result key (e.g. "dewT", "bubP")
2369+
* @param defaultValue the array to return when the key is absent or maps to {@code null}
2370+
* @return the result array, or {@code defaultValue} if not found
2371+
*/
2372+
public double[] get(String name, double[] defaultValue) {
2373+
return getOperation().get(name, defaultValue);
2374+
}
2375+
23632376
/**
23642377
* <p>
23652378
* Getter for the field <code>operation</code>.

src/test/java/neqsim/thermodynamicoperations/phaseenvelopeops/multicomponentenvelopeops/PTPhaseEnvelopeMichelsenTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package neqsim.thermodynamicoperations.phaseenvelopeops.multicomponentenvelopeops;
22

33
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
45
import static org.junit.jupiter.api.Assertions.assertEquals;
56
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -583,4 +584,54 @@ void testQualityLinesBubbleFirst() {
583584
assertTrue(qT25.length > 3, "Quality line 0.25 should have > 3 points");
584585
assertTrue(qT75.length > 3, "Quality line 0.75 should have > 3 points");
585586
}
587+
588+
/**
589+
* Test that the get(String, double[]) overload returns results when the key exists and returns
590+
* the default array when the key is absent or maps to null. This enables the Python/JPype idiom
591+
* {@code pe_data.get("dewT", [])} that previously threw a JPype overload resolution error.
592+
*/
593+
@Test
594+
void testGetWithDefault() {
595+
neqsim.thermo.system.SystemInterface testSystem =
596+
new neqsim.thermo.system.SystemSrkEos(298.0, 50.0);
597+
testSystem.addComponent("nitrogen", 0.01);
598+
testSystem.addComponent("CO2", 0.01);
599+
testSystem.addComponent("methane", 0.98);
600+
testSystem.setMixingRule("classic");
601+
602+
ThermodynamicOperations testOps = new ThermodynamicOperations(testSystem);
603+
testOps.TPflash();
604+
testSystem.initProperties();
605+
testOps.calcPTphaseEnvelope();
606+
607+
double[] emptyDefault = new double[0];
608+
609+
// Existing key should return actual data, not the default
610+
double[] dewT = testOps.get("dewT", emptyDefault);
611+
assertNotNull(dewT, "dewT with default should not be null");
612+
assertTrue(dewT.length > 0, "dewT should have data points");
613+
assertTrue(dewT != emptyDefault, "dewT should be the actual array, not the default");
614+
615+
double[] dewP = testOps.get("dewP", emptyDefault);
616+
assertNotNull(dewP, "dewP with default should not be null");
617+
assertTrue(dewP.length > 0, "dewP should have data points");
618+
619+
// Absent key should return the default
620+
double[] unknown = testOps.get("nonExistentKey", emptyDefault);
621+
assertArrayEquals(emptyDefault, unknown, "Absent key should return the default array");
622+
623+
// Key that maps to null (e.g. dewT2) should return the default
624+
double[] dewT2 = testOps.get("dewT2", emptyDefault);
625+
assertArrayEquals(emptyDefault, dewT2, "dewT2 (null) should return the default array");
626+
627+
// Also test via the OperationInterface directly
628+
PTPhaseEnvelopeMichelsen env = (PTPhaseEnvelopeMichelsen) testOps.getOperation();
629+
double[] bubP = env.get("bubP", emptyDefault);
630+
assertNotNull(bubP, "bubP via operation with default should not be null");
631+
assertTrue(bubP.length > 0, "bubP should have data points");
632+
633+
double[] missing = env.get("noSuchKey", emptyDefault);
634+
assertArrayEquals(emptyDefault, missing,
635+
"Absent key via operation should return the default array");
636+
}
586637
}

0 commit comments

Comments
 (0)