Skip to content

Commit

Permalink
Merge pull request #59 from molssi-seamm/dev
Browse files Browse the repository at this point in the history
Added temperature - energy conversions.
  • Loading branch information
seamm authored Jul 15, 2024
2 parents 7c29f30 + 17b086e commit e458224
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 107 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
=======
History
=======
2024.7.15 -- Added temperature-energy conversions
* Added conversions from temeprature units, like K, to energy, like kJ/mol and vice
versa.
* Used more of the Pint contexts, which should allow wavenumbers to frequency, etc.

2024.6.27 -- Added support for URI's
* Now recognize URI's in the form local:path/to/file
* An optional URI handler can be passed in to resolve such URI's.
Expand Down
216 changes: 109 additions & 107 deletions seamm_util/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,113 +141,115 @@
Q_ = ureg.Quantity
units_class = ureg("1 km").__class__

_d = pint.Context("chemistry")

factor = ureg.mol / ureg.avogadro_number

_d.add_transformation("", "[substance]", lambda ureg, x: x * factor)
_d.add_transformation("[substance]", "", lambda ureg, x: x / factor)

_d.add_transformation("1 / [substance]", "", lambda ureg, x: x * factor)
_d.add_transformation("", "1 / [substance]", lambda ureg, x: x / factor)

_d.add_transformation("[mass] / [substance]", "[mass]", lambda ureg, x: x * factor)
_d.add_transformation("[mass]", "[mass] / [substance]", lambda ureg, x: x / factor)

# kJ/mol/Å --> eV/Å
_d.add_transformation(
"[length] * [mass] / [substance] / [time] ** 2",
"[length] * [mass] / [time] ** 2",
lambda ureg, x: x * factor,
)
_d.add_transformation(
"[length] * [mass] / [time] ** 2",
"[length] * [mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol --> eV
_d.add_transformation(
"[length] ** 2 * [mass] / [substance] / [time] ** 2",
"[length] ** 2 * [mass] / [time] ** 2",
lambda ureg, x: x * factor,
)
_d.add_transformation(
"[length] ** 2 * [mass] / [time] ** 2",
"[length] ** 2 * [mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/K --> eV/K
_d.add_transformation(
"[length] ** 2 * [mass] / [substance] / [temperature] / [time] ** 2",
"[length] ** 2 * [mass] / [temperature] / [time] ** 2",
lambda ureg, x: x * factor,
)
_d.add_transformation(
"[length] ** 2 * [mass] / [temperature] / [time] ** 2",
"[length] ** 2 * [mass] / [substance] / [temperature] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/Å^2 --> eV/Å^2
_d.add_transformation(
"[mass] / [substance] / [time] ** 2",
"[mass] / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV/Å^2 --> kJ/mol/Å^2
_d.add_transformation(
"[mass] / [time] ** 2",
"[mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/Å^3 --> eV/Å^3
_d.add_transformation(
"[mass] / [length] / [substance] / [time] ** 2",
"[mass] / [length] / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV/Å^3 --> kJ/mol/Å^3
_d.add_transformation(
"[mass] / [length] / [time] ** 2",
"[mass] / [length] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/Å^4 --> eV/Å^4
_d.add_transformation(
"[mass] / [length] ** 2 / [substance] / [time] ** 2",
"[mass] / [length] ** 2 / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV/Å^4 --> kJ/mol/Å^4
_d.add_transformation(
"[mass] / [length] ** 2 / [time] ** 2",
"[mass] / [length] ** 2 / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol*Å^6 --> eV*Å^6
_d.add_transformation(
"[length] ** 8 * [mass] / [substance] / [time] ** 2",
"[length] ** 8 * [mass] / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV*Å^6 --> kJ/mol*Å^6
_d.add_transformation(
"[length] ** 8 * [mass] / [time] ** 2",
"[length] ** 8 * [mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

ureg.add_context(_d)
ureg.enable_contexts("chemistry")
if True:
_d = pint.Context("chemistry")

factor = ureg.mol / ureg.avogadro_number

_d.add_transformation("", "[substance]", lambda ureg, x: x * factor)
_d.add_transformation("[substance]", "", lambda ureg, x: x / factor)

_d.add_transformation("1 / [substance]", "", lambda ureg, x: x * factor)
_d.add_transformation("", "1 / [substance]", lambda ureg, x: x / factor)

_d.add_transformation("[mass] / [substance]", "[mass]", lambda ureg, x: x * factor)
_d.add_transformation("[mass]", "[mass] / [substance]", lambda ureg, x: x / factor)

# kJ/mol/Å --> eV/Å
_d.add_transformation(
"[length] * [mass] / [substance] / [time] ** 2",
"[length] * [mass] / [time] ** 2",
lambda ureg, x: x * factor,
)
_d.add_transformation(
"[length] * [mass] / [time] ** 2",
"[length] * [mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol --> eV
_d.add_transformation(
"[length] ** 2 * [mass] / [substance] / [time] ** 2",
"[length] ** 2 * [mass] / [time] ** 2",
lambda ureg, x: x * factor,
)
_d.add_transformation(
"[length] ** 2 * [mass] / [time] ** 2",
"[length] ** 2 * [mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/K --> eV/K
_d.add_transformation(
"[length] ** 2 * [mass] / [substance] / [temperature] / [time] ** 2",
"[length] ** 2 * [mass] / [temperature] / [time] ** 2",
lambda ureg, x: x * factor,
)
_d.add_transformation(
"[length] ** 2 * [mass] / [temperature] / [time] ** 2",
"[length] ** 2 * [mass] / [substance] / [temperature] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/Å^2 --> eV/Å^2
_d.add_transformation(
"[mass] / [substance] / [time] ** 2",
"[mass] / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV/Å^2 --> kJ/mol/Å^2
_d.add_transformation(
"[mass] / [time] ** 2",
"[mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/Å^3 --> eV/Å^3
_d.add_transformation(
"[mass] / [length] / [substance] / [time] ** 2",
"[mass] / [length] / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV/Å^3 --> kJ/mol/Å^3
_d.add_transformation(
"[mass] / [length] / [time] ** 2",
"[mass] / [length] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol/Å^4 --> eV/Å^4
_d.add_transformation(
"[mass] / [length] ** 2 / [substance] / [time] ** 2",
"[mass] / [length] ** 2 / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV/Å^4 --> kJ/mol/Å^4
_d.add_transformation(
"[mass] / [length] ** 2 / [time] ** 2",
"[mass] / [length] ** 2 / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

# kJ/mol*Å^6 --> eV*Å^6
_d.add_transformation(
"[length] ** 8 * [mass] / [substance] / [time] ** 2",
"[length] ** 8 * [mass] / [time] ** 2",
lambda ureg, x: x * factor,
)

# eV*Å^6 --> kJ/mol*Å^6
_d.add_transformation(
"[length] ** 8 * [mass] / [time] ** 2",
"[length] ** 8 * [mass] / [substance] / [time] ** 2",
lambda ureg, x: x / factor,
)

ureg.add_context(_d)

ureg.enable_contexts("spectroscopy", "boltzmann", "energy", "chemistry")


def default_units(units_or_dimensions):
Expand Down

0 comments on commit e458224

Please sign in to comment.