Exemplo n.º 1
0
def test_dimensionless_units():
    assert utils.string_to_unit('dimensionless') == unit.dimensionless

    unit_string = utils.unit_to_string(unit.dimensionless)
    unit_value = utils.string_to_unit(unit_string)

    assert unit_value == unit.dimensionless
Exemplo n.º 2
0
def openmm_unit_to_pint(openmm_unit):
    """Converts a `simtk.unit.Unit` to a `pint.Unit`.

    Parameters
    ----------
    openmm_unit: simtk.unit.Unit
        The unit to convert.

    Returns
    -------
    pint.Unit
        The converted unit.
    """
    from openforcefield.utils import unit_to_string

    if openmm_unit is None or isinstance(openmm_unit, UndefinedAttribute):
        return None

    assert isinstance(openmm_unit, simtk_unit.Unit)

    if openmm_unit in unsupported_openmm_units:

        raise ValueError(
            f"Quantities bearing the {openmm_unit} are not "
            f"currently supported by pint."
        )

    openmm_unit_string = unit_to_string(openmm_unit)

    # Handle the case whereby OMM treats daltons as having
    # units of g / mol, whereas SI and pint define them to
    # have units of kg.
    openmm_unit_string = (
        None
        if openmm_unit_string is None
        else openmm_unit_string.replace("dalton", "(gram / mole)")
    )

    try:
        pint_unit = unit(openmm_unit_string).units
    except UndefinedUnitError:

        logger.info(
            f"The {openmm_unit_string} OMM unit string (based on the {openmm_unit} object) "
            f"is undefined in pint"
        )

        raise

    return pint_unit