Skip to content

Commit a56409d

Browse files
authored
added test for temperatures (#984)
1 parent 9443010 commit a56409d

File tree

4 files changed

+135
-28
lines changed

4 files changed

+135
-28
lines changed

src/main/java/neqsim/thermo/system/SystemThermo.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -5021,11 +5021,20 @@ public final void setTemperature(double newTemperature, int phase) {
50215021
public void setTemperature(double newTemperature, String unit) {
50225022
for (int i = 0; i < getMaxNumberOfPhases(); i++) {
50235023
if (unit.equals("K")) {
5024+
// Direct setting as Kelvin
50245025
getPhases()[i].setTemperature(newTemperature);
50255026
} else if (unit.equals("C")) {
5027+
// Convert Celsius to Kelvin
50265028
getPhases()[i].setTemperature(newTemperature + 273.15);
5029+
} else if (unit.equals("F")) {
5030+
// Convert Fahrenheit to Kelvin
5031+
getPhases()[i].setTemperature((newTemperature - 32) * 5.0 / 9.0 + 273.15);
5032+
} else if (unit.equals("R")) {
5033+
// Convert Rankine to Kelvin
5034+
getPhases()[i].setTemperature(newTemperature * 5.0 / 9.0);
50275035
} else {
5028-
throw new RuntimeException("unit not supported " + unit);
5036+
// Exception for unsupported units
5037+
throw new RuntimeException("Unit not supported: " + unit);
50295038
}
50305039
}
50315040
}

src/main/java/neqsim/util/unit/TemperatureUnit.java

+70-27
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,89 @@ public TemperatureUnit(double value, String name) {
2424
}
2525

2626
/**
27-
* <p>
28-
* getConversionFactor.
29-
* </p>
27+
* Get conversion factor for temperature unit conversions to Kelvin. Note: This is primarily for
28+
* understanding scale, not for direct conversions including offsets.
3029
*
31-
* @param name a {@link java.lang.String} object
32-
* @return a double
30+
* @param name a {@link java.lang.String} object representing the temperature unit
31+
* @return a double representing the conversion factor relative to Kelvin
3332
*/
3433
public double getConversionFactor(String name) {
35-
double conversionFactor = 1.0;
3634
switch (name) {
3735
case "K":
38-
conversionFactor = 1.0;
39-
break;
40-
case "R":
41-
conversionFactor = 5.0 / 9.0;
42-
break;
36+
return 1.0;
37+
case "C":
38+
return 1.0; // Same scale as Kelvin
4339
case "F":
44-
conversionFactor = 5.0 / 9.0;
45-
break;
40+
return 5.0 / 9.0; // Scale factor for Fahrenheit to Kelvin
41+
case "R":
42+
return 5.0 / 9.0; // Scale factor for Rankine to Kelvin
43+
default:
44+
throw new IllegalArgumentException("Unknown unit: " + name);
4645
}
47-
return conversionFactor;
4846
}
4947

50-
/** {@inheritDoc} */
48+
/**
49+
* Convert a value from one temperature unit to another.
50+
*
51+
* @param value the temperature value to convert
52+
* @param fromUnit the unit of the input temperature
53+
* @param toUnit the unit to convert the temperature to
54+
* @return the converted temperature value
55+
*/
5156
@Override
52-
public double getValue(double val, String fromunit, String tounit) {
53-
invalue = val;
54-
return getConversionFactor(fromunit) / getConversionFactor(tounit) * invalue;
57+
public double getValue(double value, String fromUnit, String toUnit) {
58+
if (fromUnit.equals(toUnit)) {
59+
return value;
60+
}
61+
62+
// Convert input to Kelvin first
63+
double tempInKelvin = value;
64+
if (fromUnit.equals("C")) {
65+
tempInKelvin += 273.15;
66+
} else if (fromUnit.equals("F")) {
67+
tempInKelvin = (value - 32) * 5.0 / 9.0 + 273.15;
68+
} else if (fromUnit.equals("R")) {
69+
tempInKelvin = value * 5.0 / 9.0;
70+
}
71+
72+
// Convert from Kelvin to target unit
73+
if (toUnit.equals("K")) {
74+
return tempInKelvin;
75+
} else if (toUnit.equals("C")) {
76+
return tempInKelvin - 273.15;
77+
} else if (toUnit.equals("F")) {
78+
return (tempInKelvin - 273.15) * 9.0 / 5.0 + 32;
79+
} else if (toUnit.equals("R")) {
80+
return tempInKelvin * 9.0 / 5.0;
81+
}
82+
83+
throw new IllegalArgumentException("Unsupported unit: " + toUnit);
5584
}
5685

57-
/** {@inheritDoc} */
86+
/**
87+
* Convert a given temperature value from Kelvin to a specified unit.
88+
*
89+
* @param toUnit the target unit to convert the temperature to ("C", "F", "R")
90+
* @return the converted temperature value in the target unit
91+
*/
5892
@Override
59-
public double getValue(String tounit) {
60-
if (tounit.equals("C")) {
61-
return getConversionFactor(inunit) / getConversionFactor("K") * invalue - 273.15;
62-
}
63-
if (tounit.equals("F")) {
64-
return (getConversionFactor(inunit) / getConversionFactor("K") * invalue - 273.15) * 1.8
65-
+ 32.0;
93+
public double getValue(String toUnit) {
94+
switch (toUnit) {
95+
case "K":
96+
// Convert from Kelvin to Kelvin
97+
return invalue;
98+
case "C":
99+
// Convert from Kelvin to Celsius
100+
return invalue - 273.15;
101+
case "F":
102+
// Convert from Kelvin to Fahrenheit
103+
return invalue * 9.0/5.0 - 459.67;
104+
case "R":
105+
// Convert from Kelvin to Rankine
106+
return invalue * 9.0 / 5.0;
107+
default:
108+
// Handle unsupported units
109+
throw new IllegalArgumentException("Unsupported conversion unit: " + toUnit);
66110
}
67-
return getConversionFactor(inunit) / getConversionFactor(tounit) * invalue;
68111
}
69112
}

src/test/java/neqsim/processSimulation/processSystem/MLA_bug_test.java

+1
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,6 @@ public void runProcessTEG() throws InterruptedException {
348348
} catch (Exception ex) {
349349
logger.error("Something failed");
350350
}
351+
System.out.println("water in gas "+ dehydratedGas.getFluid().getComponent("water").getx());
351352
}
352353
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package neqsim.util.unit;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
import neqsim.thermo.ThermodynamicConstantsInterface;
6+
import neqsim.thermodynamicOperations.ThermodynamicOperations;
7+
8+
class TemperatureUnitTest extends neqsim.NeqSimTest {
9+
/**
10+
* <p>
11+
* testSetPressure
12+
* </p>
13+
*/
14+
@Test
15+
public void testSetTemperature() {
16+
neqsim.thermo.system.SystemPrEos fluid = new neqsim.thermo.system.SystemPrEos(298.0, 10.0);
17+
fluid.addComponent("nitrogen", 10.0);
18+
fluid.addComponent("nC10", 10.0);
19+
fluid.setPressure(0.0, "barg");
20+
21+
ThermodynamicOperations testOps = new ThermodynamicOperations(fluid);
22+
testOps.TPflash();
23+
fluid.initProperties();
24+
25+
assertEquals(ThermodynamicConstantsInterface.referencePressure, fluid.getPressure("bara"),
26+
1e-4);
27+
assertEquals(24.850000000000, fluid.getTemperature("C"), 1e-4);
28+
assertEquals(76.7300000, fluid.getTemperature("F"), 1e-4);
29+
assertEquals(536.4, fluid.getTemperature("R"), 1e-4);
30+
31+
fluid.setTemperature(11.0, "F");
32+
testOps.TPflash();
33+
34+
assertEquals(470.67, fluid.getTemperature("R"), 1e-4);
35+
assertEquals(-11.6666666666, fluid.getTemperature("C"), 1e-4);
36+
assertEquals(261.483333333, fluid.getTemperature("K"), 1e-4);
37+
38+
fluid.setTemperature(527.67, "R");
39+
testOps.TPflash();
40+
41+
assertEquals(68.0, fluid.getTemperature("F"), 1e-4);
42+
assertEquals(20.0, fluid.getTemperature("C"), 1e-4);
43+
assertEquals(293.15, fluid.getTemperature("K"), 1e-4);
44+
45+
fluid.setTemperature(25.25, "C");
46+
testOps.TPflash();
47+
48+
assertEquals(77.4499999999, fluid.getTemperature("F"), 1e-4);
49+
assertEquals(537.12, fluid.getTemperature("R"), 1e-4);
50+
assertEquals(298.4, fluid.getTemperature("K"), 1e-4);
51+
52+
}
53+
}
54+

0 commit comments

Comments
 (0)