Repository: yt-project/unyt
Pull request #155 (2020-06-29 15:11:27)

add more alternative spellings for solar units
Fixes https://github.com/yt-project/yt/issues/2704.

Added a test too.

neutrinoceros: great !
While we're at it, I was also considering adding `"m_sun"` as well as `"tsun"` for consistency with the different solar units. What do you think ?
ngoldbaum: Can you check that those don’t already work? The logic that generates names is more complicated than what’s in these lists.
neutrinoceros: Just did, I confirm that none of them currently work.

Patch:
diff --git a/unyt/_unit_lookup_table.py b/unyt/_unit_lookup_table.py
index b71c9bb..abaaa79 100644
--- a/unyt/_unit_lookup_table.py
+++ b/unyt/_unit_lookup_table.py
@@ -351,7 +351,14 @@ physical_constants = OrderedDict(
         ("hbar", (0.5 * planck_mks / np.pi, "J*s", ["reduced_planck_constant"])),
         ("σ", (5.670373e-8, "W/m**2/K**4", ["stefan_boltzmann_constant"])),
         ("Tcmb", (2.726, "K", ["CMB_temperature"])),
-        ("Msun", (mass_sun_kg, "kg", ["msun", "solar_mass", "mass_sun"])),
+        (
+            "Msun",
+            (
+                mass_sun_kg,
+                "kg",
+                ["msun", "m_sun", "m_Sun", "M_sun", "M_Sun", "solar_mass", "mass_sun"],
+            ),
+        ),
         ("Mjup", (mass_jupiter_kg, "kg", ["mjup", "jupiter_mass", "mass_jupiter"])),
         ("mercury_mass", (mass_mercury_kg, "kg", ["mass_mercury"])),
         ("venus_mass", (mass_venus_kg, "kg", ["mass_venus"])),
@@ -440,11 +447,23 @@ default_unit_name_alternatives = OrderedDict(
         ("day", ("d",)),
         ("yr", ("year",)),
         # Solar units
-        ("Msun", ("solar_mass", "solMass")),
-        ("Rsun", ("rsun", "r_sun", "solar_radius", "solRadius")),
-        ("Lsun", ("lsun", "l_sun", "solar_luminosity", "solLuminosity")),
-        ("Tsun", ("t_sun", "solar_temperature", "solTemperature")),
-        ("Zsun", ("z_sun", "solar_metallicity", "solMetallicity")),
+        (
+            "Msun",
+            ("msun", "m_sun", "M_sun", "m_Sun", "solar_mass", "solMass", "mass_sun"),
+        ),
+        ("Rsun", ("rsun", "r_sun", "R_sun", "r_Sun", "solar_radius", "solRadius")),
+        (
+            "Lsun",
+            ("lsun", "l_sun", "L_sun", "l_Sun", "solar_luminosity", "solLuminosity"),
+        ),
+        (
+            "Tsun",
+            ("t_sun", "tsun", "T_sun", "t_Sun", "solar_temperature", "solTemperature"),
+        ),
+        (
+            "Zsun",
+            ("z_sun", "zsun", "Z_sun", "z_Sun", "solar_metallicity", "solMetallicity"),
+        ),
         ("Mjup", ("m_jup", "jupiter_mass")),
         ("Mearth", ("m_earth", "earth_mass")),
         ("Rjup", ("r_jup", "jupiter_radius")),


Test patch:
diff --git a/unyt/tests/test_units.py b/unyt/tests/test_units.py
index e9d5ee9..4cf89f1 100644
--- a/unyt/tests/test_units.py
+++ b/unyt/tests/test_units.py
@@ -758,6 +758,23 @@ def test_name_alternatives():
         assert hasattr(unyt.unit_symbols, name)
 
 
+def test_solar_unit_name_alternatives():
+    import unyt
+    from unyt import Unit
+
+    # check that m_sun, m_Sun, M_sun, M_Sun, msun, and Msun all work
+    for lower_name_prefix in "mrltz":
+        base_name = lower_name_prefix + "sun"
+        for name_prefix in [lower_name_prefix, lower_name_prefix.upper()]:
+            alternative_names = [name_prefix + suf for suf in ["sun", "_sun", "_Sun"]]
+            for name in alternative_names:
+                assert Unit(name) == Unit(base_name)
+                assert hasattr(unyt, name)
+                # only solar mass units are in physical constants
+                if lower_name_prefix == "m":
+                    assert hasattr(unyt.physical_constants, name)
+
+
 def test_attosecond():
     from unyt import Unit, attosecond, second