Spin-Chemistry-Labs / radicalpy

An intuitive open-source framework specific to radical pair spin dynamics.
https://radicalpy.readthedocs.io/
MIT License
8 stars 1 forks source link

Use `pint` #66

Open vatai opened 1 year ago

vatai commented 1 year ago

https://pint.readthedocs.io/en/stable/

vatai commented 1 year ago

Candidate for Constant class:

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))
from types import SimpleNamespace

from radicalpy import Q_, ureg
from radicalpy.data import CONSTANTS_JSON, SPIN_DATA, gamma_mT

with open(CONSTANTS_JSON) as f:
    CONSTANTS_DATA = json.load(f)

class Constant(Q_):
    def __new__(cls, *args):
        # By default, `pint.Quantity` aka `Q_` has `len(args) == 2`,
        # i.e. value and unit, but a `Constant` we want to init with
        # the single `dict` which contains all of that + other details
        # which we want to save
        if len(args) == 1:
            data = args[0]
            value = data.pop("value")
            unit = ureg(data.pop("unit"))
            obj = super().__new__(cls, value, unit)
            obj.details = SimpleNamespace(**data)
        else:
            obj = super().__new__(cls, *args)
        return obj

hbar_data = CONSTANTS_DATA["hbar"]
c_data = CONSTANTS_DATA["c"]
print(f"{hbar_data=}")
# hbar_value = hbar_data.pop("value")
# const = Constant(hbar_value, hbar_data)
hbar = Constant(hbar_data)
print(f"{type(hbar)=}")
print(f"{hbar=}")
print(f"{100.0 * hbar=}")
print(f"{100.0 * ureg('J s') + hbar=}")
print(f"{hbar.details=}")

c = Constant(c_data)
print(f"{type(c)=}")
print(f"{c=}")
print(f"{c.details=}")
print(f"{hbar * c=}")
prd = hbar * c
print(f"{prd=}")
print(f"{type(prd)=}")
# print(f"{prd.details=}") # THIS BREAKS! (which is good)
vatai commented 1 year ago

Note to future self: Consider moving isotropic and spin_quanum_number (aka multiplicity2spin) from data.py back to utils.py assuming the potential cyclic dependency between utils.py and data.py is resolved by eliminating the need data.constants in utils.py.

vatai commented 1 year ago

Plan:

vatai commented 1 year ago

Some units:

TODO: see if pint can convert from mT to MHz (and to ang_freq)