Пример #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
Пример #2
0
def pint_unit_to_openmm(pint_unit):
    """Converts a `pint.Unit` to a `simtk.unit.Unit`.

    Notes
    -----
    Not all pint units are available in OpenMM.

    Parameters
    ----------
    pint_unit: pint.Unit
        The unit to convert.

    Returns
    -------
    simtk.unit.Unit
        The converted unit.
    """
    from openforcefield.utils import string_to_unit

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

    assert isinstance(pint_unit, pint.Unit)

    pint_unit_string = f"{pint_unit:!s}"

    # Handle a unit name change in pint 0.10.*
    pint_unit_string = pint_unit_string.replace("standard_atmosphere",
                                                "atmosphere")

    try:
        # noinspection PyTypeChecker
        openmm_unit = string_to_unit(pint_unit_string)
    except AttributeError:

        logger.info(
            f"The {pint_unit_string} pint unit string (based on the {pint_unit} object) "
            f"could not be understood by `openforcefield.utils.string_to_unit`"
        )

        raise

    return openmm_unit
Пример #3
0
def pint_unit_to_openmm(pint_unit):
    """Converts a `pint.Unit` to a `simtk.unit.Unit`.

    Notes
    -----
    Not all pint units are available in OpenMM.

    Parameters
    ----------
    pint_unit: pint.Unit
        The unit to convert.

    Returns
    -------
    simtk.unit.Unit
        The converted unit.
    """
    from openforcefield.utils import string_to_unit

    if pint_unit is None:
        return None

    assert isinstance(pint_unit, unit.Unit)

    pint_unit_string = str(pint_unit)

    try:
        # noinspection PyTypeChecker
        openmm_unit = string_to_unit(pint_unit_string)
    except AttributeError:

        logging.info(
            f'The {pint_unit_string} pint unit string (based on the {pint_unit} object) '
            f'could not be understood by `openforcefield.utils.string_to_unit`'
        )

        raise

    return openmm_unit