Skip to content

Commit 4fb20fc

Browse files
committed
chore: add vector3d
1 parent c89d16f commit 4fb20fc

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

src/py/mat3ra/code/constants.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from math import pi
22

3+
from mat3ra.esse.models.definitions.constants import FundamentalConstants
4+
35

46
class Coefficients:
57
# Same as used in: JS/TS
@@ -13,18 +15,19 @@ class Coefficients:
1315
# and originally taken from https://github.com/hplgit/physical-quantities/blob/master/PhysicalQuantities.py
1416

1517
# Internal, for convenience purposes
16-
_c = 299792458.0 # speed of light, m/s
17-
_mu0 = 4.0e-7 * pi # permeability of vacuum
18-
_eps0 = 1 / _mu0 / _c**2 # permittivity of vacuum
19-
_Grav = 6.67259e-11 # gravitational constant
20-
_hplanck = 6.6260755e-34 # Planck constant, J s
21-
_hbar = _hplanck / (2 * pi) # Planck constant / 2pi, J s
22-
_e = 1.60217733e-19 # elementary charge
23-
_me = 9.1093897e-31 # electron mass
18+
_c = FundamentalConstants.c # speed of light, m/s
19+
_Grav = FundamentalConstants.G # gravitational constant
20+
_hplanck = FundamentalConstants.h # Planck constant, J s
21+
_e = FundamentalConstants.e # elementary charge
22+
_me = FundamentalConstants.me # electron mass
23+
_mu0 = 4.0e-7 * pi # permeability of vacuum, atomic units
24+
2425
_mp = 1.6726231e-27 # proton mass
2526
_Nav = 6.0221367e23 # Avogadro number
2627
_k = 1.380658e-23 # Boltzmann constant, J/K
2728
_amu = 1.6605402e-27 # atomic mass unit, kg
29+
_eps0 = 1 / _mu0 / _c**2 # permittivity of vacuum
30+
_hbar = _hplanck / (2 * pi) # Planck constant / 2pi, J s
2831

2932
# External
3033
BOHR = 4e10 * pi * _eps0 * _hbar**2 / _me / _e**2 # Bohr radius in angstrom

src/py/mat3ra/code/vector.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
from mat3ra.esse.models.core.abstract.point import PointSchema as Vector3DSchema
4+
from mat3ra.utils.mixins import RoundNumericValuesMixin
5+
from pydantic import model_serializer
6+
7+
8+
class Vector3D(Vector3DSchema):
9+
pass
10+
11+
12+
class RoundedVector3D(RoundNumericValuesMixin, Vector3D):
13+
@model_serializer
14+
def to_dict(self, skip_rounding: bool = False) -> List[float]:
15+
rounded_value = self.round_array_or_number(self.root) if not skip_rounding else self.root
16+
return Vector3D(root=rounded_value).model_dump()

tests/py/unit/test_vector.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from mat3ra.code.vector import RoundedVector3D, Vector3D
2+
3+
VECTOR_FLOAT = [1.234567890, 2.345678901, 3.456789012]
4+
VECTOR_FLOAT_ROUNDED_4 = [1.2346, 2.3457, 3.4568]
5+
VECTOR_FLOAT_ROUNDED_3 = [1.235, 2.346, 3.457]
6+
7+
8+
def test_vector_init():
9+
vector = Vector3D(root=VECTOR_FLOAT)
10+
assert vector.model_dump() == VECTOR_FLOAT
11+
12+
13+
def test_vector_init_wrong_type():
14+
try:
15+
_ = Vector3D(root=[1, 2, "3"])
16+
except Exception as e:
17+
assert str(e) == "3 is not of type float"
18+
19+
20+
def test_vector_init_wrong_size():
21+
try:
22+
_ = Vector3D(root=[1, 2])
23+
assert False
24+
except Exception:
25+
assert True
26+
27+
28+
def test_rounded_vector_serialization():
29+
class_reference = RoundedVector3D
30+
class_reference.__round_precision__ = 4
31+
vector = class_reference(root=VECTOR_FLOAT)
32+
assert vector.model_dump() == VECTOR_FLOAT_ROUNDED_4
33+
34+
class_reference = RoundedVector3D
35+
class_reference.__round_precision__ = 3
36+
vector = class_reference(root=VECTOR_FLOAT)
37+
assert vector.model_dump() == VECTOR_FLOAT_ROUNDED_3

0 commit comments

Comments
 (0)