Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion between kcal/mol and eV #681

Closed
AllanChain opened this issue Jan 17, 2025 · 5 comments
Closed

Conversion between kcal/mol and eV #681

AllanChain opened this issue Jan 17, 2025 · 5 comments
Labels

Comments

@AllanChain
Copy link

The kilocalorie per mole is a (kcal/mol) is a unit to measure an amount of energy per number of molecules, atoms, or other similar particles. (https://en.wikipedia.org/wiki/Kilocalorie_per_mole). This unit is commonly used in chemistry and biology. It will be convenient to convert it to other energy units like eV, something like http://wild.life.nctu.edu.tw/class/common/energy-unit-conv-table.html.

Currently, it is impossible to do the conversion:

>>> 1 kcal/mol -> eV
error: while type checking
  ┌─ <input:28>:1:1
  │
1 │ 1 kcal/mol -> eV
  │ ^^^^^^^^^^ -- ^^ Energy or Torque
  │ │          │
  │ │          incompatible dimensions in unit conversion
  │ ChemicalPotential or MolarEnthalpyOfVaporization
  │
  =  left hand side: Length² × Mass × Time⁻² × AmountOfSubstance⁻¹    [= ChemicalPotential, MolarEnthalpyOfVaporization]
    right hand side: Length² × Mass × Time⁻²                          [= Energy, Torque]

    Suggested fix: divide the expression on the right hand side by a `AmountOfSubstance` factor
@sharkdp
Copy link
Owner

sharkdp commented Jan 17, 2025

That is possible, but you need to divide by or multiply with N_A accordingly, as the error message suggests:

>>> 1 kcal/mol/N_A -> eV

    = 0.0433641 eV    [Energy or Torque]

>>> 1 eV*N_A -> kcal/mol

    = 23.0605 kcal/mol    [ChemicalPotential or MolarEnthalpyOfVaporization]

@AllanChain
Copy link
Author

Thanks for your reply. This makes sense logically, but it might be difficult or unintuitive for beginners. However, the issue isn’t with numbat itself; rather, it is the longstanding, inconsistent conventions people have been using.

I don't have any idea how to improve this. Maybe some casting rules? Another example would be cm-1, which is widely used as an energy unit.

@suhr
Copy link

suhr commented Feb 7, 2025

Electronvolts are also used for temperature in plasma physics.

@sharkdp
Copy link
Owner

sharkdp commented Feb 16, 2025

I have just pushed two changes to Numbat, one bugfix and one new function quantity_cast, which would allow us to write a generic conversion function like this (in Numbat itself):

fn magic_energy<D: Dim>(q: D) -> Energy =
    if is_zero(q)
      then 0
    else if is_dimensionless(q / K)  # q is a temperature: E = k_B T
      then k_B quantity_cast(q, K)
    else if is_dimensionless(q / m)  # q is a wavelength: E = ℎ c / λ
      then  c / quantity_cast(q, m)
    else if is_dimensionless(q / g)  # q is a mass: E = m c²
      then quantity_cast(q, g) * c²
    else if is_dimensionless(q / (cal/mol))  # q is energy per mole: E = q / N_A
      then quantity_cast(q, cal/mol) / N_A
    else error("magic_energy: Cannot convert quantity of unit '{unit_name(q)}' to an energy")

This could then be used like:

>>> 1e5 K -> magic_energy -> eV

    = 8.61733 eV    [Energy or Torque]

>>> 500 nm -> magic_energy -> eV

    = 2.47968 eV    [Energy or Torque]

>>> electron_mass -> magic_energy -> keV

    = 510.999 keV    [Energy or Torque]

>>> 1 kcal/mol -> magic_energy -> meV

    = 43.3641 meV    [Energy or Torque]

>>> 1 s -> magic_energy
error: runtime error
 = User error: magic_energy: Cannot convert quantity of unit 's' to an energy

We could provide similar "magic" conversion functions for other target dimensions.

@sharkdp
Copy link
Owner

sharkdp commented Feb 16, 2025

Let's actually close this in favor of an older ticket #481

@sharkdp sharkdp closed this as completed Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants