Skip to content

Commit

Permalink
chore: add constants + cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
timurbazhirov committed Apr 16, 2024
1 parent 42b17b1 commit 7ed7a3d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repos:
- id: isort
- id: mypy
- id: check-yaml
exclude: ^tests/js
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: local
Expand Down
4 changes: 2 additions & 2 deletions src/py/mat3ra/code/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional
from typing import Any, Dict

from mat3ra.utils import object as object_utils

Expand Down Expand Up @@ -26,7 +26,7 @@ def set_prop(self, name: str, value: Any) -> None:
def unset_prop(self, name: str) -> None:
del self._json[name]

def set_props(self, json: Dict[str, Any] = {}) -> "BaseUnderscoreJsonPropsHandler":
def set_props(self, json: Dict[str, Any] = {}) -> Any:
for key, value in json.items():
self.set_prop(key, value)
return self
34 changes: 34 additions & 0 deletions src/py/mat3ra/code/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
from math import pi


class Coefficients:
# Same as used in: JS/TS
EV_TO_RY = 0.0734986176
BOHR_TO_ANGSTROM = 0.52917721092
ANGSTROM_TO_BOHR = 1 / 0.52917721092
EV_A_TO_RY_BOHR = 1 / 25.71104309541616

# The below is migrated from:
# https://github.com/Exabyte-io/express/blob/22614e549cdc3b0c344718b72ee2000383d77922/express/parsers/settings.py
# and originally taken from https://github.com/hplgit/physical-quantities/blob/master/PhysicalQuantities.py

# Internal, for convenience purposes
_c = 299792458.0 # speed of light, m/s
_mu0 = 4.0e-7 * pi # permeability of vacuum
_eps0 = 1 / _mu0 / _c**2 # permittivity of vacuum
_Grav = 6.67259e-11 # gravitational constant
_hplanck = 6.6260755e-34 # Planck constant, J s
_hbar = _hplanck / (2 * pi) # Planck constant / 2pi, J s
_e = 1.60217733e-19 # elementary charge
_me = 9.1093897e-31 # electron mass
_mp = 1.6726231e-27 # proton mass
_Nav = 6.0221367e23 # Avogadro number
_k = 1.380658e-23 # Boltzmann constant, J/K
_amu = 1.6605402e-27 # atomic mass unit, kg

# External
BOHR = 4e10 * pi * _eps0 * _hbar**2 / _me / _e**2 # Bohr radius in angstrom
eV = 1.0
HARTREE = _me * _e**3 / 16 / pi**2 / _eps0**2 / _hbar**2 # in eV
RYDBERG = 0.5 * HARTREE # in eV
Ry = RYDBERG
Ha = HARTREE
kJ = 1000.0 / _e
kcal = 4.184 * kJ
cm_inv_to_ev = 0.00012398 # cm^-1 to eV
ry_bohr_to_eV_A = 25.71104309541616 # or RYDBERG / BOHR


class Tolerance:
# in crystal coordinates
Expand Down
7 changes: 6 additions & 1 deletion src/py/mat3ra/code/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ def to_json(self, exclude: List[str] = []) -> Dict[str, Any]:
return self.clean(object_utils.clone_deep(object_utils.omit(self._json, exclude)))

def clone(self, extra_context: Dict[str, Any] = {}) -> Any:
return self.__class__.__init__({**self.to_json(), **extra_context})
config = self.to_json()
config.update(extra_context)
# To avoid:
# Argument 1 to "__init__" of "BaseUnderscoreJsonPropsHandler" has incompatible type "Dict[str, Any]";
# expected "BaseUnderscoreJsonPropsHandler"
return self.__class__.__init__(config) # type: ignore[arg-type]

@staticmethod
def validate_data(data: Dict[str, Any], clean: bool = False):
Expand Down

0 comments on commit 7ed7a3d

Please sign in to comment.