diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fe454d4..9866eba 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,9 @@ +1.17.0 (2023-05-02) +------------------- + +* Standardize the usage of ``lbmol`` +* Add more valid units for ``mole per mass`` category + 1.16.0 (2023-04-26) ------------------- diff --git a/src/barril/units/_tests/test_legacy_unit.py b/src/barril/units/_tests/test_legacy_unit.py index 6e1bbc8..1079d7a 100644 --- a/src/barril/units/_tests/test_legacy_unit.py +++ b/src/barril/units/_tests/test_legacy_unit.py @@ -102,3 +102,33 @@ class SomeNonExpectedObject: category = unit_database_posc.GetDefaultCategory(SomeNonExpectedObject()) assert category is None + + +def testAddCategoryWithLegacyUnit(unit_database_posc) -> None: + """Try creating a category with legacy unit""" + unit_database_posc.AddCategory( + category="molar-mass", + quantity_type="mass per mol", + valid_units=["g/mol", "kg/mol", "lb/lbmole"], + default_value=100, + min_value=0, + is_min_exclusive=True, + caption="Molar Mass", + ) + valid_units = unit_database_posc.GetValidUnits("molar-mass") + assert sorted(valid_units) == sorted(["g/mol", "kg/mol", "lb/lbmol"]) + + +def testAddCategoryWithLegacyDefaultUnit(unit_database_posc) -> None: + """Try creating a category with legacy default unit""" + unit_database_posc.AddCategory( + category="molar-mass", + quantity_type="mass per mol", + default_unit="lb/lbmole", + default_value=100, + min_value=0, + is_min_exclusive=True, + caption="Molar Mass", + ) + valid_units = unit_database_posc.GetValidUnits("molar-mass") + assert sorted(valid_units) == sorted(["g/mol", "kg/mol", "lb/lbmol"]) diff --git a/src/barril/units/_tests/test_posc_additional_units.py b/src/barril/units/_tests/test_posc_additional_units.py index 86fbc34..fc92fa1 100644 --- a/src/barril/units/_tests/test_posc_additional_units.py +++ b/src/barril/units/_tests/test_posc_additional_units.py @@ -41,8 +41,8 @@ def testDensityGeneration(db) -> None: def testMolarDensity(db) -> None: - db.CheckQuantityTypeUnit("amount of a substance", "gmole/m3") - db.CheckQuantityTypeUnit("concentration of B", "gmole/m3") + db.CheckQuantityTypeUnit("amount of a substance", "gmol/m3") + db.CheckQuantityTypeUnit("concentration of B", "gmol/m3") def testPartsPerMillionByVolumeConcentration(db) -> None: @@ -172,7 +172,7 @@ def testMassPerMol(db) -> None: assert gpermol_to_kgpermol == 1 assert kgpermol_to_gpermol == 1000 - assert db.Convert("mass per mol", "g/mol", "lb/lbmole", 1) == 1 + assert db.Convert("mass per mol", "g/mol", "lb/lbmol", 1) == 1 @pytest.mark.parametrize("category", ["mole per mass", "molality"]) @@ -182,7 +182,9 @@ def testMolPerMass(db, category) -> None: assert molperg_to_molperkg == 1000 assert molperkg_to_molperg == 0.001 - assert db.Convert(category, "mol/g", "lbmole/lb", 1) == 1 + assert db.Convert(category, "mol/g", "lbmol/lb", 1) == 1 + valid_units = db.GetValidUnits(category) + assert valid_units == ["mol/kg", "mol/g", "lbmol/lb"] def testInjectivityFactor(db) -> None: diff --git a/src/barril/units/posc.py b/src/barril/units/posc.py index 81bcc68..d9e7e03 100644 --- a/src/barril/units/posc.py +++ b/src/barril/units/posc.py @@ -1381,7 +1381,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "molar thermodynamic energy", "British thermal units/pound mass mol", - "Btu/lbmole", + "Btu/lbmol", f_base_to_unit, f_unit_to_base, default_category=None, @@ -1401,7 +1401,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "molar heat capacity", "Btus/pound mass mol deg F", - "Btu/lbmole.F", + "Btu/lbmol.F", f_base_to_unit, f_unit_to_base, default_category=None, @@ -3275,7 +3275,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "molar volume", "cubic feet/mole (pound mass)", - "ft3/lbmole", + "ft3/lbmol", f_base_to_unit, f_unit_to_base, default_category=None, @@ -7919,7 +7919,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "molar mass", "moles (pounds mass)", - "lbmole", + "lbmol", f_base_to_unit, f_unit_to_base, default_category=None, @@ -7939,7 +7939,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "concentration of B", "moles (pounds mass)/cubic foot", - "lbmole/ft3", + "lbmol/ft3", f_base_to_unit, f_unit_to_base, default_category=None, @@ -7959,7 +7959,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "concentration of B", "moles (pounds mass)/UK gallon", - "lbmole/galUK", + "lbmol/galUK", f_base_to_unit, f_unit_to_base, default_category=None, @@ -7979,7 +7979,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "concentration of B", "moles (pounds mass)/US gallon", - "lbmole/galUS", + "lbmol/galUS", f_base_to_unit, f_unit_to_base, default_category=None, @@ -7999,7 +7999,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "mole per time", "moles (pounds mass)/hour", - "lbmole/h", + "lbmol/h", f_base_to_unit, f_unit_to_base, default_category=None, @@ -8029,7 +8029,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "mole per time per area", "moles (pounds mass)/hour square foot", - "lbmole/h.ft2", + "lbmol/h.ft2", f_base_to_unit, f_unit_to_base, default_category=None, @@ -8039,7 +8039,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "mole per time", "moles (pounds mass)/second", - "lbmole/s", + "lbmol/s", f_base_to_unit, f_unit_to_base, default_category=None, @@ -8059,7 +8059,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "mole per time per area", "moles (pounds mass)/second square foot", - "lbmole/s.ft2", + "lbmol/s.ft2", f_base_to_unit, f_unit_to_base, default_category=None, @@ -10763,14 +10763,14 @@ def FillUnitDatabaseWithPosc( f_unit_to_base = MakeCustomaryToBase(0.0, 0.001, 1.0, 0.0) f_base_to_unit = MakeBaseToCustomary(0.0, 0.001, 1.0, 0.0) db.AddUnit( - "molar mass", "gram mole", "gmole", f_base_to_unit, f_unit_to_base, default_category=None + "molar mass", "gram mole", "gmol", f_base_to_unit, f_unit_to_base, default_category=None ) f_unit_to_base = MakeCustomaryToBase(0.0, 1, 1.0, 0.0) f_base_to_unit = MakeBaseToCustomary(0.0, 1, 1.0, 0.0) db.AddUnit( "concentration of B", "moles (gram)/cubic metre", - "gmole/m3", + "gmol/m3", f_base_to_unit, f_unit_to_base, default_category=None, @@ -10805,7 +10805,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "mole per time", "moles (pounds mass)/day", - "lbmole/d", + "lbmol/d", f_base_to_unit, f_unit_to_base, default_category=None, @@ -10835,7 +10835,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "concentration of B", "moles (pounds mass)/barrel", - "lbmole/bbl", + "lbmol/bbl", f_base_to_unit, f_unit_to_base, default_category=None, @@ -10875,7 +10875,7 @@ def FillUnitDatabaseWithPosc( db.AddUnit( "dimensionless", "pound mass mol/pound mass mol", - "lbmole/lbmole", + "lbmol/lbmol", f_base_to_unit, f_unit_to_base, default_category="mole per mole", @@ -11149,8 +11149,8 @@ def FillUnitDatabaseWithPosc( f_base_to_unit = MakeBaseToCustomary(0.0, 0.001, 1.0, 0.0) db.AddUnit( "mass per mol", - "lb/lbmole", - "lb/lbmole", + "lb/lbmol", + "lb/lbmol", f_base_to_unit, f_unit_to_base, default_category=None, @@ -11164,8 +11164,8 @@ def FillUnitDatabaseWithPosc( f_base_to_unit = MakeBaseToCustomary(0.0, 1000.0, 1.0, 0.0) db.AddUnit( "mole per mass", - "lbmole/lb", - "lbmole/lb", + "lbmol/lb", + "lbmol/lb", f_base_to_unit, f_unit_to_base, default_category=None, @@ -12353,14 +12353,14 @@ def FillUnitDatabaseWithPosc( "mole per mass", "mole per mass", override=override_categories, - valid_units=["mol/kg"], + valid_units=["mol/kg", "mol/g", "lbmol/lb"], default_unit="mol/kg", ) db.AddCategory( "molality", "mole per mass", override=override_categories, - valid_units=["mol/kg"], + valid_units=["mol/kg", "mol/g", "lbmol/lb"], default_unit="mol/kg", ) db.AddCategory( @@ -12767,7 +12767,7 @@ def FillUnitDatabaseWithPosc( "mole per mole", "dimensionless", override=override_categories, - valid_units=["mol/mol", "kmol/kmol", "lbmole/lbmole"], + valid_units=["mol/mol", "kmol/kmol", "lbmol/lbmol"], ) db.AddCategory( "multiplier", "dimensionless", override=override_categories, valid_units=[""] @@ -13463,7 +13463,7 @@ def FillUnitDatabaseWithPosc( override=override_categories, valid_units=[ "J/mol", - "Btu/lbmole", + "Btu/lbmol", "Btu/mol(lbm)", "kcal/mol", "kcal/mol(g)", @@ -13480,7 +13480,7 @@ def FillUnitDatabaseWithPosc( override=override_categories, valid_units=[ "J/mol", - "Btu/lbmole", + "Btu/lbmol", "Btu/mol(lbm)", "kcal/mol", "kcal/mol(g)", @@ -13497,7 +13497,7 @@ def FillUnitDatabaseWithPosc( override=override_categories, valid_units=[ "J/mol", - "Btu/lbmole", + "Btu/lbmol", "Btu/mol(lbm)", "kcal/mol", "kcal/mol(g)", @@ -13514,7 +13514,7 @@ def FillUnitDatabaseWithPosc( override=override_categories, valid_units=[ "J/mol.K", - "Btu/lbmole.F", + "Btu/lbmol.F", "Btu/mol(lbm).F", "cal/mol.degC", "cal/mol(g).degC", @@ -13528,7 +13528,7 @@ def FillUnitDatabaseWithPosc( override=override_categories, valid_units=[ "J/mol.K", - "Btu/lbmole.F", + "Btu/lbmol.F", "Btu/mol(lbm).F", "cal/mol.degC", "cal/mol(g).degC", @@ -13542,7 +13542,7 @@ def FillUnitDatabaseWithPosc( override=override_categories, valid_units=[ "J/mol.K", - "Btu/lbmole.F", + "Btu/lbmol.F", "Btu/mol(lbm).F", "cal/mol.degC", "cal/mol(g).degC", @@ -14325,7 +14325,7 @@ def FillUnitDatabaseWithPosc( "m3/mol", "dm3/kmol", "dm3/mol(kg)", - "ft3/lbmole", + "ft3/lbmol", "ft3/mol(lbm)", "L/mol", "L/mol(g)", @@ -14472,7 +14472,7 @@ def FillUnitDatabaseWithPosc( "mmol", "mol(g)", "mol(kg)", - "lbmole", + "lbmol", "mol(lbm)", "umol", "kgmol", @@ -14482,7 +14482,7 @@ def FillUnitDatabaseWithPosc( "molar mass", "molar mass", override=override_categories, - valid_units=["lbmole", "gmole"], + valid_units=["lbmol", "gmol"], ) db.AddCategory( "mole per area", "mole per area", override=override_categories, valid_units=["mol/m2"] @@ -14494,8 +14494,8 @@ def FillUnitDatabaseWithPosc( valid_units=[ "mol/m2.s", "mol(lbm)/h.ft2", - "lbmole/h.ft2", - "lbmole/s.ft2", + "lbmol/h.ft2", + "lbmol/s.ft2", "mol(lbm)/s.ft2", ], ) @@ -14507,16 +14507,16 @@ def FillUnitDatabaseWithPosc( "mol/m3", "kmol/m3", "mol(kg)/m3", - "lbmole/ft3", + "lbmol/ft3", "mol(lbm)/ft3", - "lbmole/galUK", + "lbmol/galUK", "mol(lbm)/galUK", - "lbmole/galUS", + "lbmol/galUS", "mol(lbm)/galUS", - "gmole/m3", + "gmol/m3", "mol/L", "mol/cm3", - "lbmole/bbl", + "lbmol/bbl", ], ) db.AddCategory( @@ -14527,16 +14527,16 @@ def FillUnitDatabaseWithPosc( "mol/m3", "kmol/m3", "mol(kg)/m3", - "lbmole/ft3", + "lbmol/ft3", "mol(lbm)/ft3", - "lbmole/galUK", + "lbmol/galUK", "mol(lbm)/galUK", - "lbmole/galUS", + "lbmol/galUS", "mol(lbm)/galUS", - "gmole/m3", + "gmol/m3", "mol/L", "mol/cm3", - "lbmole/bbl", + "lbmol/bbl", ], ) db.AddCategory( @@ -14549,14 +14549,14 @@ def FillUnitDatabaseWithPosc( "mol(kg)/h", "kmol/s", "mol(kg)/s", - "lbmole/h", + "lbmol/h", "mol(lbm)/h", - "lbmole/s", + "lbmol/s", "mol(lbm)/s", "mol/h", "kmol/d", "mol/d", - "lbmole/d", + "lbmol/d", ], ) db.AddCategory( @@ -15620,7 +15620,7 @@ def FillUnitDatabaseWithPosc( "mass per mol", "mass per mol", override=override_categories, - valid_units=["g/mol", "lb/lbmole", "kg/mol"], + valid_units=["g/mol", "lb/lbmol", "kg/mol"], ) db.AddCategory( "viscosity per pressure", diff --git a/src/barril/units/unit_database.py b/src/barril/units/unit_database.py index 8834bbd..3c4125b 100644 --- a/src/barril/units/unit_database.py +++ b/src/barril/units/unit_database.py @@ -46,6 +46,8 @@ ("M(m3)", "MMm3"), ("k(ft3)", "Mcf"), ("Ns/m", "N.s/m"), + ("lbmole", "lbmol"), + ("gmole", "gmol"), ] @@ -478,8 +480,11 @@ def AddCategory( if valid_units is not None: # valid units given: check if all the given units are valid quantity_units = set(self.GetUnits(quantity_type)) - for unit in valid_units: - if unit not in quantity_units: + for i, unit in enumerate(valid_units): + was_unit_fixed, fixed_unit = FixUnitIfIsLegacy(unit) + if was_unit_fixed: + valid_units[i] = fixed_unit + if fixed_unit not in quantity_units: msg = "unit %r is not valid for quantity type %r.\nQuantity units: %r" raise ValueError(msg % (unit, quantity_type, sorted(quantity_units))) @@ -490,6 +495,9 @@ def AddCategory( default_unit = valid_units[0] else: quantity_units = set(self.GetUnits(quantity_type)) + was_unit_fixed, fixed_default_unit = FixUnitIfIsLegacy(default_unit) + if was_unit_fixed: + default_unit = fixed_default_unit if default_unit not in quantity_units: raise ValueError( "unit %r is not valid for default quantity type %r" diff --git a/src/barril/units/unit_rich_text_representations.py b/src/barril/units/unit_rich_text_representations.py index ed30335..17a6eb8 100644 --- a/src/barril/units/unit_rich_text_representations.py +++ b/src/barril/units/unit_rich_text_representations.py @@ -291,8 +291,8 @@ class UnitRichTextRepresentations: "ft2/d": "ft2/d", "mol/m2.s": "mol/m2.s", "mol(lbm)/h.ft2": "mol(lbm)/h.ft2", - "lbmole/h.ft2": "lbmole/h.ft2", - "lbmole/s.ft2": "lbmole/s.ft2", + "lbmol/h.ft2": "lbmol/h.ft2", + "lbmol/s.ft2": "lbmol/s.ft2", "mol(lbm)/s.ft2": "mol(lbm)/s.ft2", "W/m2": "W/m2", "Btu/hr.ft2": "Btu/hr.ft2", @@ -316,7 +316,7 @@ class UnitRichTextRepresentations: "mol/m3": "mol/m3", "kmol/m3": "kmol/m3", "mol(kg)/m3": "mol(kg)/m3", - "lbmole/ft3": "lbmole/ft3", + "lbmol/ft3": "lbmol/ft3", "mol(lbm)/ft3": "mol(lbm)/ft3", "m4/s": "m4/s", "1000m4/d": "1000m4/d", @@ -399,7 +399,7 @@ class UnitRichTextRepresentations: "mol/m3": "mol/m3", "kmol/m3": "kmol/m3", "mol(kg)/m3": "mol(kg)/m3", - "lbmole/ft3": "lbmole/ft3", + "lbmol/ft3": "lbmol/ft3", "mol(lbm)/ft3": "mol(lbm)/ft3", "m3/J": "m3/J", "dm3/kW.h": "dm3/kW.h", @@ -670,7 +670,7 @@ class UnitRichTextRepresentations: "m3/mol": "m3/mol", "dm3/kmol": "dm3/kmol", "dm3/mol(kg)": "dm3/mol(kg)", - "ft3/lbmole": "ft3/lbmole", + "ft3/lbmol": "ft3/lbmol", "ft3/mol(lbm)": "ft3/mol(lbm)", "m3/kmol": "m3/kmol", "m3/mol(kg)": "m3/mol(kg)",