Skip to content

UNITS

Units conversion utilities module.

Available Energy units

["kcal/mol", "kj/mol", "hartree", "ev" "mev", "ryd]

Available Distance units

["ang", "nm", "bohr"]

Available Force units

Combinations between Energy and Distance units

Conversion

Conversion from one unit system to another defined by a name and a callable

Source code in openqdc/utils/units.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class Conversion:
    """
    Conversion from one unit system to another defined by a name and a callable
    """

    def __init__(self, in_unit: str, out_unit: str, func: Callable[[float], float]):
        """

        Parameters:
            in_unit: String defining the units of the current values
            out_unit: String defining the target units
            func: The callable to compute the conversion
        """
        name = "convert_" + in_unit.lower().strip() + "_to_" + out_unit.lower().strip()

        if name in CONVERSION_REGISTRY:
            raise ConversionAlreadyDefined(in_unit, out_unit)
        CONVERSION_REGISTRY[name] = self

        self.name = name
        self.fn = func

    def __call__(self, x):
        return self.fn(x)

__init__(in_unit, out_unit, func)

Parameters:

Name Type Description Default
in_unit str

String defining the units of the current values

required
out_unit str

String defining the target units

required
func Callable[[float], float]

The callable to compute the conversion

required
Source code in openqdc/utils/units.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def __init__(self, in_unit: str, out_unit: str, func: Callable[[float], float]):
    """

    Parameters:
        in_unit: String defining the units of the current values
        out_unit: String defining the target units
        func: The callable to compute the conversion
    """
    name = "convert_" + in_unit.lower().strip() + "_to_" + out_unit.lower().strip()

    if name in CONVERSION_REGISTRY:
        raise ConversionAlreadyDefined(in_unit, out_unit)
    CONVERSION_REGISTRY[name] = self

    self.name = name
    self.fn = func

DistanceTypeConversion

Bases: ConversionEnum, StrEnum

Define the possible distance units for conversion

Source code in openqdc/utils/units.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@unique
class DistanceTypeConversion(ConversionEnum, StrEnum):
    """
    Define the possible distance units for conversion
    """

    ANG = "ang"
    NM = "nm"
    BOHR = "bohr"

    def to(self, distance: "DistanceTypeConversion", fraction: bool = False) -> Callable[[float], float]:
        """
        Get the conversion function to convert the distance to the desired units.

        Parameters:
            distance: distance unit to convert to
            fraction: whether it is distance^1 or distance^-1

        Returns:
            callable to convert the distance to the desired units
        """
        return get_conversion(str(self), str(distance)) if not fraction else get_conversion(str(distance), str(self))

to(distance, fraction=False)

Get the conversion function to convert the distance to the desired units.

Parameters:

Name Type Description Default
distance DistanceTypeConversion

distance unit to convert to

required
fraction bool

whether it is distance^1 or distance^-1

False

Returns:

Type Description
Callable[[float], float]

callable to convert the distance to the desired units

Source code in openqdc/utils/units.py
69
70
71
72
73
74
75
76
77
78
79
80
def to(self, distance: "DistanceTypeConversion", fraction: bool = False) -> Callable[[float], float]:
    """
    Get the conversion function to convert the distance to the desired units.

    Parameters:
        distance: distance unit to convert to
        fraction: whether it is distance^1 or distance^-1

    Returns:
        callable to convert the distance to the desired units
    """
    return get_conversion(str(self), str(distance)) if not fraction else get_conversion(str(distance), str(self))

EnergyTypeConversion

Bases: ConversionEnum, StrEnum

Define the possible energy units for conversion

Source code in openqdc/utils/units.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@unique
class EnergyTypeConversion(ConversionEnum, StrEnum):
    """
    Define the possible energy units for conversion
    """

    KCAL_MOL = "kcal/mol"
    KJ_MOL = "kj/mol"
    HARTREE = "hartree"
    EV = "ev"
    MEV = "mev"
    RYD = "ryd"

    def to(self, energy: "EnergyTypeConversion") -> Callable[[float], float]:
        """
        Get the conversion function to convert the energy to the desired units.

        Parameters:
            energy: energy unit to convert to

        Returns:
            Callable to convert the distance to the desired units
        """
        return get_conversion(str(self), str(energy))

to(energy)

Get the conversion function to convert the energy to the desired units.

Parameters:

Name Type Description Default
energy EnergyTypeConversion

energy unit to convert to

required

Returns:

Type Description
Callable[[float], float]

Callable to convert the distance to the desired units

Source code in openqdc/utils/units.py
46
47
48
49
50
51
52
53
54
55
56
def to(self, energy: "EnergyTypeConversion") -> Callable[[float], float]:
    """
    Get the conversion function to convert the energy to the desired units.

    Parameters:
        energy: energy unit to convert to

    Returns:
        Callable to convert the distance to the desired units
    """
    return get_conversion(str(self), str(energy))

ForceTypeConversion

Bases: ConversionEnum

Define the possible foce units for conversion

Source code in openqdc/utils/units.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
@unique
class ForceTypeConversion(ConversionEnum):
    """
    Define the possible foce units for conversion
    """

    #     Name      = EnergyTypeConversion,         , DistanceTypeConversion
    HARTREE_BOHR = EnergyTypeConversion.HARTREE, DistanceTypeConversion.BOHR
    HARTREE_ANG = EnergyTypeConversion.HARTREE, DistanceTypeConversion.ANG
    HARTREE_NM = EnergyTypeConversion.HARTREE, DistanceTypeConversion.NM
    EV_BOHR = EnergyTypeConversion.EV, DistanceTypeConversion.BOHR
    EV_ANG = EnergyTypeConversion.EV, DistanceTypeConversion.ANG
    EV_NM = EnergyTypeConversion.EV, DistanceTypeConversion.NM
    KCAL_MOL_BOHR = EnergyTypeConversion.KCAL_MOL, DistanceTypeConversion.BOHR
    KCAL_MOL_ANG = EnergyTypeConversion.KCAL_MOL, DistanceTypeConversion.ANG
    KCAL_MOL_NM = EnergyTypeConversion.KCAL_MOL, DistanceTypeConversion.NM
    KJ_MOL_BOHR = EnergyTypeConversion.KJ_MOL, DistanceTypeConversion.BOHR
    KJ_MOL_ANG = EnergyTypeConversion.KJ_MOL, DistanceTypeConversion.ANG
    KJ_MOL_NM = EnergyTypeConversion.KJ_MOL, DistanceTypeConversion.NM
    MEV_BOHR = EnergyTypeConversion.MEV, DistanceTypeConversion.BOHR
    MEV_ANG = EnergyTypeConversion.MEV, DistanceTypeConversion.ANG
    MEV_NM = EnergyTypeConversion.MEV, DistanceTypeConversion.NM
    RYD_BOHR = EnergyTypeConversion.RYD, DistanceTypeConversion.BOHR
    RYD_ANG = EnergyTypeConversion.RYD, DistanceTypeConversion.ANG
    RYD_NM = EnergyTypeConversion.RYD, DistanceTypeConversion.NM

    def __init__(self, energy: EnergyTypeConversion, distance: DistanceTypeConversion):
        self.energy = energy
        self.distance = distance

    def __str__(self):
        return f"{self.energy}/{self.distance}"

    def to(self, energy: EnergyTypeConversion, distance: DistanceTypeConversion) -> Callable[[float], float]:
        """
        Get the conversion function to convert the force to the desired units.

        Parameters:
            energy: energy unit to convert to
            distance: distance unit to convert to

        Returns:
            callable to convert the distance to the desired units
        """
        return lambda x: self.distance.to(distance, fraction=True)(self.energy.to(energy)(x))

to(energy, distance)

Get the conversion function to convert the force to the desired units.

Parameters:

Name Type Description Default
energy EnergyTypeConversion

energy unit to convert to

required
distance DistanceTypeConversion

distance unit to convert to

required

Returns:

Type Description
Callable[[float], float]

callable to convert the distance to the desired units

Source code in openqdc/utils/units.py
116
117
118
119
120
121
122
123
124
125
126
127
def to(self, energy: EnergyTypeConversion, distance: DistanceTypeConversion) -> Callable[[float], float]:
    """
    Get the conversion function to convert the force to the desired units.

    Parameters:
        energy: energy unit to convert to
        distance: distance unit to convert to

    Returns:
        callable to convert the distance to the desired units
    """
    return lambda x: self.distance.to(distance, fraction=True)(self.energy.to(energy)(x))

get_conversion(in_unit, out_unit)

Utility function to get the conversion function between two units.

Parameters:

Name Type Description Default
in_unit

The input unit

required
out_unit

The output unit

required

Returns:

Type Description
Callable[[float], float]

The conversion function

Source code in openqdc/utils/units.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def get_conversion(in_unit: str, out_unit: str) -> Callable[[float], float]:
    """
    Utility function to get the conversion function between two units.

    Parameters:
        in_unit : The input unit
        out_unit : The output unit

    Returns:
        The conversion function
    """
    name = "convert_" + in_unit.lower().strip() + "_to_" + out_unit.lower().strip()
    if in_unit.lower().strip() == out_unit.lower().strip():
        return lambda x: x
    if name not in CONVERSION_REGISTRY:
        raise ConversionNotDefinedError(in_unit, out_unit)
    return CONVERSION_REGISTRY[name]