Esempio n. 1
0
def test_old_registry_multiple_load():
    # See Issue #157 for details
    reg1 = UnitRegistry()
    reg1.add("code_length", 1.0, length)
    reg1.add("code_mass", 1.0, mass)
    reg1.add("code_time", 1.0, time)
    reg1.add("code_temperature", 1.0, temperature)
    UnitSystem(
        reg1.unit_system_id,
        "code_length",
        "code_mass",
        "code_time",
        "code_temperature",
        registry=reg1,
    )

    cm = Unit("code_mass", registry=reg1)
    cl = Unit("code_length", registry=reg1)

    (cm / cl).latex_representation()

    with open(OLD_JSON_PATH) as f:
        json_data = f.read()

    reg2 = UnitRegistry.from_json(json_data)
    UnitSystem(
        reg2.unit_system_id,
        "code_length",
        "code_mass",
        "code_time",
        "code_temperature",
        registry=reg2,
    )
Esempio n. 2
0
def test_bad_unit_system():
    with pytest.raises(IllDefinedUnitSystem):
        UnitSystem("atomic", "nm", "fs", "nK", "rad")
    with pytest.raises(IllDefinedUnitSystem):
        UnitSystem("atomic", "nm", "fs", "nK", "rad", registry=UnitRegistry())
    with pytest.raises(IllDefinedUnitSystem):
        UnitSystem("atomic", us.nm, us.fs, us.nK, us.rad)
    with pytest.raises(IllDefinedUnitSystem):
        UnitSystem("atomic", us.nm, us.fs, us.nK, us.rad, registry=UnitRegistry())
Esempio n. 3
0
def test_code_unit():
    from unyt import UnitRegistry

    ureg = UnitRegistry()
    ureg.add("code_length", 10.0, length)
    ureg.add("code_magnetic_field", 2.0, magnetic_field_mks)
    u = Unit("code_length", registry=ureg)
    assert u.is_code_unit is True
    assert u.get_base_equivalent() == Unit("m")
    u = Unit("cm")
    assert u.is_code_unit is False

    u = Unit("code_magnetic_field", registry=ureg)
    assert u.get_base_equivalent("mks") == Unit("T")
    with pytest.raises(UnitsNotReducible):
        assert u.get_base_equivalent("cgs")

    # see issue #60
    u = Unit("s/m")
    assert u.get_mks_equivalent() == Unit("s/m")
    assert u.get_mks_equivalent() != Unit("ohm")
    assert u.get_cgs_equivalent() == Unit("s/cm")

    u = Unit("kC")
    assert u.get_cgs_equivalent() == Unit("kesu")
    assert u.get_cgs_equivalent().get_mks_equivalent() == u

    UnitSystem(ureg.unit_system_id, "code_length", "kg", "s", registry=ureg)

    u = Unit("cm", registry=ureg)
    ue = u.get_base_equivalent("code")

    assert str(ue) == "code_length"
    assert ue.base_value == 10
    assert ue.dimensions is length

    class FakeDataset(object):
        unit_registry = ureg

    ds = FakeDataset()

    UnitSystem(ds, "code_length", "kg", "s", registry=ureg)

    u = Unit("cm", registry=ureg)
    ue = u.get_base_equivalent(ds)

    assert str(ue) == "code_length"
    assert ue.base_value == 10
    assert ue.dimensions is length

    with pytest.raises(UnitParseError):
        Unit("code_length")
Esempio n. 4
0
def test_code_unit():
    from unyt import UnitRegistry

    ureg = UnitRegistry()
    ureg.add('code_length', 10., length)
    ureg.add('code_magnetic_field', 2.0, magnetic_field_mks)
    u = Unit('code_length', registry=ureg)
    assert u.is_code_unit is True
    assert u.get_base_equivalent() == Unit('m')
    u = Unit('cm')
    assert u.is_code_unit is False

    u = Unit('code_magnetic_field', registry=ureg)
    assert u.get_base_equivalent('mks') == Unit('T')
    assert u.get_base_equivalent('cgs') == Unit('gauss')

    UnitSystem(ureg.unit_system_id, 'code_length', 'kg', 's', registry=ureg)

    u = Unit('cm', registry=ureg)
    ue = u.get_base_equivalent('code')

    assert str(ue) == 'code_length'
    assert ue.base_value == 10
    assert ue.dimensions is length

    class FakeDataset(object):
        unit_registry = ureg

    ds = FakeDataset()

    UnitSystem(ds, 'code_length', 'kg', 's', registry=ureg)

    u = Unit('cm', registry=ureg)
    ue = u.get_base_equivalent(ds)

    assert str(ue) == 'code_length'
    assert ue.base_value == 10
    assert ue.dimensions is length

    with pytest.raises(UnitParseError):
        Unit('code_length')
Esempio n. 5
0
def test_unit_systems():
    goofy_unit_system = UnitSystem(
        "goofy", "ly", "lbm", "hr", temperature_unit="R",
        angle_unit="arcsec", current_mks_unit="mA",
        luminous_intensity_unit="cd")
    assert goofy_unit_system["temperature"] == Unit("R")
    assert goofy_unit_system[dimensions.solid_angle] == Unit("arcsec**2")
    assert goofy_unit_system["energy"] == Unit("lbm*ly**2/hr**2")
    goofy_unit_system["energy"] = "eV"
    assert goofy_unit_system["energy"] == Unit("eV")
    assert goofy_unit_system["magnetic_field_mks"] == Unit("lbm/(hr**2*mA)")
    assert "goofy" in unit_system_registry
Esempio n. 6
0
def test_bad_unit_system():
    with pytest.raises(IllDefinedUnitSystem):
        UnitSystem('atomic', 'nm', 'fs', 'nK', 'rad')
Esempio n. 7
0
def test_create_unit_system_from_unit_objects():
    s = UnitSystem("test_units", us.Mpc, us.Msun, us.s)
    assert s["length"] == us.Mpc
    assert s["mass"] == us.Msun
    assert s["time"] == us.s