Skip to content

Instantly share code, notes, and snippets.

@singularitti
Last active September 22, 2022 03:53
Show Gist options
  • Save singularitti/c48b086b9235b5034442a7a12f7bee52 to your computer and use it in GitHub Desktop.
Save singularitti/c48b086b9235b5034442a7a12f7bee52 to your computer and use it in GitHub Desktop.
Unit conversion in research #Python #research #physics
#!/usr/bin/env python3
from scipy.constants import (Avogadro, angstrom, electron_volt,
physical_constants)
BOHR = physical_constants['Bohr radius'][0]
EH_EV = physical_constants['Hartree energy in eV'][0]
RY_EV = physical_constants['Rydberg constant times hc in eV'][0]
EH_J = physical_constants['hartree-joule relationship'][0]
EH_HZ = physical_constants['hartree-hertz relationship'][0]
EH_K = physical_constants['hartree-kelvin relationship'][0]
EH_M_INVERSE = physical_constants['hartree-inverse meter relationship'][0]
EV_M_INVERSE = physical_constants['electron volt-inverse meter relationship'][0]
EV_K = physical_constants['electron volt-kelvin relationship'][0]
RY_J = physical_constants['Rydberg constant times hc in J'][0]
# ===================== Functions =====================
def j_to_ev(value):
"""
Convert the *value* in unit joule to electronvolt.
:param value: The value to be converted.
:return: The converted value.
"""
return value / electron_volt
def ev_to_j(value):
"""
Convert the *value* in unit electronvolt to joule.
:param value: The value to be converted.
:return: The converted value.
"""
return value * electron_volt
def gpa_to_megabar(value):
"""
Convert the *value* in unit gigapascal to megabar.
:param value: The value to be converted.
:return: The converted value.
"""
return value * 0.01
def megabar_to_gpa(value):
"""
Convert the *value* in unit megabar to gigapascal.
:param value: The value to be converted.
:return: The converted value.
"""
return value / 0.01
def b3_to_a3(value):
"""
Convert the *value* in unit cubic bohr radius to what in cubic angstrom.
:param value: The value to be converted.
:return: The converted value.
"""
return value * (BOHR / angstrom) ** 3
def a3_to_b3(value):
"""
Convert the *value* in unit cubic angstrom to what in cubic bohr radius.
:param value: The value to be converted.
:return: The converted value.
"""
return value * (angstrom / BOHR) ** 3
def eh_to_ev(value):
"""
Convert the *value* in unit hartree to electronvolt.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_EV
def ev_to_eh(value):
"""
Convert the *value* in unit electronvolt to hartree.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EH_EV
def ry_to_ev(value):
"""
Convert the *value* in unit rydberg to electronvolt.
:param value: The value to be converted.
:return: The converted value.
"""
return value * RY_EV
def ev_to_ry(value):
"""
Convert the *value* in unit electronvolt to rydberg.
:param value: The value to be converted.
:return: The converted value.
"""
return value / RY_EV
def j_to_eh(value):
"""
Convert the *value* in unit joule to hartree.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EH_J
def eh_to_j(value):
"""
Convert the *value* in unit hartree to joule.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_J
def eh_to_hz(value):
"""
Convert the *value* in unit hartree to hertz.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_HZ
def hz_to_eh(value):
"""
Convert the *value* in unit hertz to hartree.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EH_HZ
def eh_to_k(value):
"""
Convert the *value* in unit hartree to kelvin.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_K
def k_to_eh(value):
"""
Convert the *value* in unit kelvin to hartree.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EH_K
def eh_to_m_inverse(value):
"""
Convert the *value* in unit hartree to :math:`\\text{m}^{-1}`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_M_INVERSE
def m_inverse_to_eh(value):
"""
Convert the *value* in unit :math:`\\text{m}^{-1}` to hartree.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EH_M_INVERSE
def eh_to_cm_inverse(value):
"""
Convert the *value* in unit hartree to :math:`\\text{cm}^{-1}`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_M_INVERSE / 100
def cm_inverse_to_eh(value):
"""
Convert the *value* in unit :math:`\\text{cm}^{-1}` to hartree.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EH_M_INVERSE * 100
def ev_to_m_inverse(value):
"""
Convert the *value* in unit electronvolt to :math:`\\text{m}^{-1}`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EV_M_INVERSE
def m_inverse_to_ev(value):
"""
Convert the *value* in unit :math:`\\text{m}^{-1}` to electronvolt.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EV_M_INVERSE
def ev_to_cm_inverse(value):
"""
Convert the *value* in unit electronvolt to :math:`\\text{cm}^{-1}`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EV_M_INVERSE / 100
def cm_inverse_to_ev(value):
"""
Convert the *value* in unit :math:`\\text{cm}^{-1}` to electronvolt.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EV_M_INVERSE * 100
def ev_to_k(value):
"""
Convert the *value* in unit electronvolt to kelvin.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EV_K
def k_to_ev(value):
"""
Convert the *value* in unit kelvin to electronvolt.
:param value: The value to be converted.
:return: The converted value.
"""
return value / EV_K
def ry_to_j(value):
"""
Convert the *value* in unit rydberg to joule.
:param value: The value to be converted.
:return: The converted value.
"""
return value * RY_J
def j_to_ry(value):
"""
Convert the *value* in unit joule to rydberg.
:param value: The value to be converted.
:return: The converted value.
"""
return value / RY_J
def gpa_to_ev_a3(value):
"""
Convert the *value* in unit gigapascal to :math:`\\frac{ \\text{electronvolt} }{ \\text{angstrom}^3 }`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * 1e9 / electron_volt * angstrom ** 3
def ev_a3_to_gpa(value):
"""
Convert the *value* in unit :math:`\\frac{ \\text{electronvolt} }{ \\text{angstrom}^3 }` to gigapascal.
:param value: The value to be converted.
:return: The converted value.
"""
return value / 1e9 * electron_volt / angstrom ** 3
def gpa_to_ev_b3(value):
"""
Convert the *value* in unit gigapascal to :math:`\\frac{ \\text{electronvolt} }{ \\text{bohr radius}^3 }`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * 1e9 / electron_volt * BOHR ** 3
def ev_b3_to_gpa(value):
"""
Convert the *value* in unit :math:`\\frac{ \\text{electronvolt} }{ \\text{bohr radius}^3 }` to gigapascal.
:param value: The value to be converted.
:return: The converted value.
"""
return value / 1e9 * electron_volt / BOHR ** 3
def gpa_to_ry_b3(value):
"""
Convert the *value* in unit gigapascal to :math:`\\frac{ \\text{rydberg} }{ \\text{bohr radius}^3 }`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * 1e9 / RY_J * BOHR ** 3
def ry_b3_to_gpa(value):
"""
Convert the *value* in unit :math:`\\frac{ \\text{rydberg} }{ \\text{bohr radius}^3 }` to gigapascal.
:param value: The value to be converted.
:return: The converted value.
"""
return value / 1e9 * RY_J / BOHR ** 3
def gpa_to_ha_b3(value):
"""
Convert the *value* in unit gigapascal to :math:`\\frac{ \\text{hartree} }{ \\text{bohr radius}^3 }`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * 1e9 / RY_J * BOHR ** 3 / 2
def ha_b3_to_gpa(value):
"""
Convert the *value* in unit :math:`\\frac{ \\text{hartree} }{ \\text{bohr radius}^3 }` to gigapascal.
:param value: The value to be converted.
:return: The converted value.
"""
return 2 * value / 1e9 * RY_J / BOHR ** 3
def ry_b_to_ev_a(value):
"""
The atomic force.
Convert the *value* in unit :math:`\\frac{ \\text{rydberg} }{ \\text{bohr radius} }` to
:math:`\\frac{ \\text{electronvolt} }{ \\text{angstrom} }`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * RY_EV / (BOHR / angstrom)
def ha_b_to_ev_a(value):
"""
The atomic force.
Convert the *value* in unit :math:`\\frac{ \\text{hartree} }{ \\text{bohr radius} }` to
:math:`\\frac{ \\text{electronvolt} }{ \\text{angstrom} }`.
:param value: The value to be converted.
:return: The converted value.
"""
return value * EH_EV / (BOHR / angstrom)
def ry_to_kj_mol(value):
"""
Convert the *value* is in unit Rydberg, the converted value is in unit
:math:`\\frac{ \\text{k J} }{ \\text{mol} }`.
:param value: The value to be converted.
:return: the converted value.
"""
return value * Avogadro * RY_J / 1000
def ry_to_j_mol(value):
"""
Convert the *value* is in unit Rydberg, the converted value is in unit
:math:`\\frac{ \\text{k J} }{ \\text{mol} }`.
:param value: The value to be converted.
:return: the converted value.
"""
return value * Avogadro * RY_J
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment