Skip to content

Commit 9443010

Browse files
authored
Add more units (#983)
* add psi and psig units * add common unit specification
1 parent 3e034e0 commit 9443010

File tree

9 files changed

+165
-23
lines changed

9 files changed

+165
-23
lines changed

src/main/java/neqsim/Units.java

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package neqsim;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* <p>
7+
* Units class.
8+
* </p>
9+
*
10+
* @author even
11+
* @version $Id: $Id
12+
*/
13+
public class Units {
14+
/**
15+
* Unit class nested within Units.
16+
*/
17+
public class Unit {
18+
public String symbol;
19+
public String symbolName;
20+
21+
public Unit(String symbol, String symbolName) {
22+
this.symbol = symbol;
23+
this.symbolName = symbolName;
24+
}
25+
}
26+
27+
public static HashMap<String, Unit> activeUnits = new HashMap<>();
28+
public static HashMap<String, Unit> defaultUnits = new HashMap<>();
29+
private static HashMap<String, Unit> siUnits = new HashMap<>();
30+
private static HashMap<String, Unit> fieldUnits = new HashMap<>();
31+
32+
public Units() {
33+
if (activeUnits.size() == 0) {
34+
activeUnits.put("temperature", new Unit("C", "Celsius"));
35+
activeUnits.put("pressure", new Unit("bara", "bar absolute"));
36+
activeUnits.put("enthalpy", new Unit("J/kg", "Joule per kg"));
37+
activeUnits.put("entropy", new Unit("J/kgK", "Joule per kg and Kelvin"));
38+
activeUnits.put("density", new Unit("kg/m3", "kg per cubic meter"));
39+
40+
siUnits.putAll(activeUnits); // Makes a copy of activeUnits
41+
siUnits.put("temperature", new Unit("K", "Kelvin"));
42+
siUnits.put("pressure", new Unit("Pa", "Pascal"));
43+
siUnits.put("enthalpy", new Unit("J/mol", "Joule per mole"));
44+
siUnits.put("density", new Unit("mol/m3", "mol per cubic meter"));
45+
46+
fieldUnits.putAll(activeUnits); // Makes a copy of activeUnits
47+
fieldUnits.put("temperature", new Unit("F", "Fahrenheit"));
48+
fieldUnits.put("pressure", new Unit("psia", "pounds per square inch absolute"));
49+
fieldUnits.put("enthalpy", new Unit("Btu/lbmol", "Btu per lbmol"));
50+
fieldUnits.put("density", new Unit("lb/ft3", "pound per cubic foot"));
51+
52+
defaultUnits.putAll(activeUnits); // Makes a copy of activeUnits
53+
}
54+
}
55+
56+
public static void activateSIUnits() {
57+
activeUnits = new HashMap<>(siUnits);
58+
}
59+
60+
public static void activateFieldUnits() {
61+
activeUnits = new HashMap<>(fieldUnits);
62+
}
63+
64+
public static void activateDefaultUnits() {
65+
activeUnits = new HashMap<>(defaultUnits); // Reassign with a copy
66+
}
67+
68+
public static String getSymbol(String name) {
69+
return activeUnits.get(name).symbol;
70+
}
71+
72+
public static String getSymbolName(String name) {
73+
return activeUnits.get(name).symbolName;
74+
}
75+
76+
public void setUnit(String name, String symbol, String symbolName) {
77+
Unit unit = activeUnits.get(name);
78+
if (unit != null) {
79+
unit.symbol = symbol;
80+
unit.symbolName = symbolName;
81+
}
82+
}
83+
}

src/main/java/neqsim/thermo/phase/Phase.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
import java.util.ArrayList;
1010
import org.apache.logging.log4j.LogManager;
11-
import org.apache.logging.log4j.Logger;
1211
import neqsim.physicalProperties.PhysicalPropertyHandler;
1312
import neqsim.thermo.ThermodynamicConstantsInterface;
1413
import neqsim.thermo.ThermodynamicModelSettings;
1514
import neqsim.thermo.component.ComponentInterface;
1615
import neqsim.thermo.system.SystemInterface;
1716
import neqsim.util.exception.InvalidInputException;
17+
import org.apache.logging.log4j.Logger;
1818

1919
/**
2020
* Phase class.
@@ -296,6 +296,14 @@ public double getTemperature() {
296296
return temperature;
297297
}
298298

299+
/** {@inheritDoc} */
300+
@Override
301+
public double getTemperature(String unit) {
302+
neqsim.util.unit.TemperatureUnit tempConversion =
303+
new neqsim.util.unit.TemperatureUnit(getTemperature(), "K");
304+
return tempConversion.getValue(unit);
305+
}
306+
299307
/** {@inheritDoc} */
300308
@Override
301309
public double getPressure() {
@@ -1065,6 +1073,12 @@ public double getEnthalpy(String unit) {
10651073
case "kJ/kg":
10661074
conversionFactor = 1.0 / getNumberOfMolesInPhase() / getMolarMass() / 1000.0;
10671075
break;
1076+
case "Btu/lbmol":
1077+
conversionFactor = 1.0 / getNumberOfMolesInPhase() * 0.429923;
1078+
break;
1079+
case "Btu":
1080+
conversionFactor = 0.00094781712;
1081+
break;
10681082
default:
10691083
throw new RuntimeException("unit not supported " + unit);
10701084
}

src/main/java/neqsim/thermo/phase/PhaseInterface.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,14 @@ public default void addMolesChemReac(int component, double dn) {
13601360
*/
13611361
public double getTemperature();
13621362

1363+
/**
1364+
* method to return temperature in a specified unit.
1365+
*
1366+
* @param unit Supported units are K, C, R
1367+
* @return temperature in specified unit
1368+
*/
1369+
public double getTemperature(String unit);
1370+
13631371
/**
13641372
* Get pressure of phase.
13651373
*
@@ -1370,7 +1378,7 @@ public default void addMolesChemReac(int component, double dn) {
13701378
/**
13711379
* Get pressure of phase in a specified unit.
13721380
*
1373-
* @param unit Supported units are bara, barg, Pa and MPa
1381+
* @param unit Supported units are bara, barg, Pa, MPa, psi, psia, psig
13741382
* @return pressure in specified unit
13751383
*/
13761384
public double getPressure(String unit);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ public default int getPhaseNumberOfPhase(String phaseTypeName) {
13941394
/**
13951395
* method to return pressure in a specified unit.
13961396
*
1397-
* @param unit Supported units are bara, barg, Pa and MPa
1397+
* @param unit Supported units are bara, barg, Pa, MPa, psi, psia, psig
13981398
* @return pressure in specified unit
13991399
*/
14001400
public double getPressure(String unit);

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

+24-18
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ public void createDatabase(boolean reset) {
15891589
@Override
15901590
public String[][] createTable(String name) {
15911591
initProperties();
1592-
1592+
neqsim.Units units = new neqsim.Units();
15931593
java.text.DecimalFormat nf = new java.text.DecimalFormat();
15941594

15951595
java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
@@ -1645,9 +1645,11 @@ public String[][] createTable(String name) {
16451645

16461646
buf = new StringBuffer();
16471647
table[getPhases()[0].getNumberOfComponents() + 2][0] = "Density";
1648-
table[getPhases()[0].getNumberOfComponents() + 2][i + 2] =
1649-
nf.format(getPhase(i).getPhysicalProperties().getDensity(), buf, test).toString();
1650-
table[getPhases()[0].getNumberOfComponents() + 2][6] = "[kg/m^3]";
1648+
table[getPhases()[0].getNumberOfComponents() + 2][i + 2] = nf
1649+
.format(getPhase(i).getDensity(neqsim.Units.activeUnits.get("density").symbol), buf, test)
1650+
.toString();
1651+
table[getPhases()[0].getNumberOfComponents() + 2][6] =
1652+
neqsim.Units.activeUnits.get("density").symbol;
16511653

16521654
// Double.longValue(system.getPhase(i).getBeta());
16531655
buf = new StringBuffer();
@@ -1692,19 +1694,17 @@ public String[][] createTable(String name) {
16921694

16931695
buf = new StringBuffer();
16941696
table[getPhases()[0].getNumberOfComponents() + 9][0] = "Enthalpy";
1695-
table[getPhases()[0].getNumberOfComponents() + 9][i + 2] = nf.format(
1696-
(getPhase(i).getEnthalpy()
1697-
/ (getPhase(i).getNumberOfMolesInPhase() * getPhase(i).getMolarMass() * 1000)),
1698-
buf, test).toString();
1699-
table[getPhases()[0].getNumberOfComponents() + 9][6] = "[kJ/kg]";
1697+
table[getPhases()[0].getNumberOfComponents() + 9][i + 2] =
1698+
nf.format((getPhase(i).getEnthalpy(neqsim.Units.getSymbol("enthalpy"))), buf, test)
1699+
.toString();
1700+
table[getPhases()[0].getNumberOfComponents() + 9][6] = neqsim.Units.getSymbol("enthalpy");
17001701

17011702
buf = new StringBuffer();
17021703
table[getPhases()[0].getNumberOfComponents() + 10][0] = "Entropy";
1703-
table[getPhases()[0].getNumberOfComponents() + 10][i + 2] = nf.format(
1704-
(getPhase(i).getEntropy()
1705-
/ (getPhase(i).getNumberOfMolesInPhase() * getPhase(i).getMolarMass() * 1000)),
1706-
buf, test).toString();
1707-
table[getPhases()[0].getNumberOfComponents() + 10][6] = "[kJ/kg*K]";
1704+
table[getPhases()[0].getNumberOfComponents() + 10][i + 2] =
1705+
nf.format((getPhase(i).getEntropy(neqsim.Units.getSymbol("entropy"))), buf, test)
1706+
.toString();
1707+
table[getPhases()[0].getNumberOfComponents() + 10][6] = neqsim.Units.getSymbol("entropy");
17081708

17091709
buf = new StringBuffer();
17101710
table[getPhases()[0].getNumberOfComponents() + 11][0] = "JT coefficient";
@@ -1760,14 +1760,14 @@ public String[][] createTable(String name) {
17601760
buf = new StringBuffer();
17611761
table[getPhases()[0].getNumberOfComponents() + 19][0] = "Pressure";
17621762
table[getPhases()[0].getNumberOfComponents() + 19][i + 2] =
1763-
Double.toString(getPhase(i).getPressure());
1764-
table[getPhases()[0].getNumberOfComponents() + 19][6] = "[bar]";
1763+
Double.toString(getPhase(i).getPressure(neqsim.Units.getSymbol("pressure")));
1764+
table[getPhases()[0].getNumberOfComponents() + 19][6] = neqsim.Units.getSymbol("pressure");
17651765

17661766
buf = new StringBuffer();
17671767
table[getPhases()[0].getNumberOfComponents() + 20][0] = "Temperature";
17681768
table[getPhases()[0].getNumberOfComponents() + 20][i + 2] =
1769-
Double.toString(getPhase(i).getTemperature());
1770-
table[getPhases()[0].getNumberOfComponents() + 20][6] = "[K]";
1769+
Double.toString(getPhase(i).getTemperature(neqsim.Units.getSymbol("temperature")));
1770+
table[getPhases()[0].getNumberOfComponents() + 20][6] = neqsim.Units.getSymbol("temperature");
17711771
Double.toString(getPhase(i).getTemperature());
17721772

17731773
buf = new StringBuffer();
@@ -2156,6 +2156,9 @@ public double getEnthalpy(String unit) {
21562156
case "J":
21572157
conversionFactor = 1.0;
21582158
break;
2159+
case "Btu":
2160+
conversionFactor = 0.00094781712;
2161+
break;
21592162
case "kJ/kmol":
21602163
case "J/mol":
21612164
conversionFactor = 1.0 / getTotalNumberOfMoles();
@@ -2166,6 +2169,9 @@ public double getEnthalpy(String unit) {
21662169
case "kJ/kg":
21672170
conversionFactor = 1.0 / getTotalNumberOfMoles() / getMolarMass() / 1000.0;
21682171
break;
2172+
case "Btu/lbmol":
2173+
conversionFactor = 1.0 / getTotalNumberOfMoles() * 0.429923;
2174+
break;
21692175
default:
21702176
throw new RuntimeException("unit not supported " + unit);
21712177
}

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public double getConversionFactor(String name) {
4747
conversionFactor = 1.0;
4848
break;
4949
case "psi":
50-
conversionFactor = 0.06894757;
50+
conversionFactor = 0.0689475729317831;
51+
break;
52+
case "psia":
53+
conversionFactor = 0.0689475729317831;
54+
break;
55+
case "psig":
56+
conversionFactor = 0.0689475729317831;
5157
break;
5258
case "Pa":
5359
conversionFactor = 1.0e-5;
@@ -82,6 +88,9 @@ public double getValue(String tounit) {
8288
if (tounit.equals("barg")) {
8389
return (getConversionFactor(inunit) / getConversionFactor("bara")) * invalue
8490
- ThermodynamicConstantsInterface.referencePressure;
91+
} else if (tounit.equals("psig")) {
92+
return (getConversionFactor(inunit) / getConversionFactor("bara")) * invalue * 14.503773773
93+
- 14.7;
8594
} else if (inunit.equals("barg")) {
8695
return (getConversionFactor(inunit) / getConversionFactor("bara")) * invalue
8796
+ ThermodynamicConstantsInterface.referencePressure;

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

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public double getConversionFactor(String name) {
4040
case "R":
4141
conversionFactor = 5.0 / 9.0;
4242
break;
43+
case "F":
44+
conversionFactor = 5.0 / 9.0;
45+
break;
4346
}
4447
return conversionFactor;
4548
}
@@ -57,6 +60,10 @@ public double getValue(String tounit) {
5760
if (tounit.equals("C")) {
5861
return getConversionFactor(inunit) / getConversionFactor("K") * invalue - 273.15;
5962
}
63+
if (tounit.equals("F")) {
64+
return (getConversionFactor(inunit) / getConversionFactor("K") * invalue - 273.15) * 1.8
65+
+ 32.0;
66+
}
6067
return getConversionFactor(inunit) / getConversionFactor(tounit) * invalue;
6168
}
6269
}

src/test/java/neqsim/thermo/system/SystemPrEosTest.java src/test/java/neqsim/thermo/system/SystemPrEoSTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.junit.jupiter.api.Test;
88
import neqsim.thermodynamicOperations.ThermodynamicOperations;
99

10-
class SystemPrEoSTest extends neqsim.NeqSimTest {
10+
public class SystemPrEoSTest extends neqsim.NeqSimTest {
1111
static neqsim.thermo.system.SystemInterface testSystem = null;
1212
static neqsim.thermo.ThermodynamicModelTest testModel = null;
1313

@@ -52,6 +52,14 @@ public void testMolarVolume() {
5252
testSystem.initProperties();
5353
assertEquals(testSystem.getMolarVolume("m3/mol"),
5454
testSystem.getMolarMass("kg/mol") / testSystem.getDensity("kg/m3"));
55+
56+
testSystem.prettyPrint();
57+
58+
neqsim.Units.activateFieldUnits();
59+
testSystem.prettyPrint();
60+
61+
neqsim.Units.activateDefaultUnits();
62+
testSystem.prettyPrint();
5563
}
5664

5765
/**

src/test/java/neqsim/util/unit/PressureUnitTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public void testSetPressure() {
2929
assertEquals(101325.0, fluid.getPressure("Pa"), 1e-4);
3030
assertEquals(101.3250, fluid.getPressure("kPa"), 1e-4);
3131
assertEquals(1.0, fluid.getPressure("atm"), 1e-4);
32+
assertEquals(14.6959488, fluid.getPressure("psi"), 1e-4);
33+
assertEquals(14.6959488, fluid.getPressure("psia"), 1e-4);
34+
assertEquals(-0.0040512245077, fluid.getPressure("psig"), 1e-4);
3235

3336
fluid.setPressure(11.0, "bara");
3437
testOps.TPflash();
@@ -39,6 +42,10 @@ public void testSetPressure() {
3942
assertEquals(11.0e5, fluid.getPressure("Pa"), 1e-4);
4043
assertEquals(11e2, fluid.getPressure("kPa"), 1e-4);
4144
assertEquals(10.856155933, fluid.getPressure("atm"), 1e-4);
45+
assertEquals(159.54151180, fluid.getPressure("psi"), 1e-4);
46+
assertEquals(159.54151180, fluid.getPressure("psia"), 1e-4);
47+
assertEquals(144.841511503000, fluid.getPressure("psig"), 1e-4);
48+
4249
}
4350
}
4451

0 commit comments

Comments
 (0)