Skip to content

Commit 49d72b9

Browse files
PythonFZSamTov
andauthored
add mds.units.<unit_system> (#543)
* add `mds.units.<unit_system>` * Update project.py [skip ci] * fix frozen dataclass issue * lint [skip ci] * fix test [skip ci] Co-authored-by: Samuel Tovey <tovey.samuel@gmail.com>
1 parent d163447 commit 49d72b9

File tree

8 files changed

+27
-17
lines changed

8 files changed

+27
-17
lines changed

CI/functional_tests/test_molten_salts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def mdsuite_project(traj_files, tmp_path) -> mds.Project:
8585
name="NaCl",
8686
timestep=0.002,
8787
temperature=1200.0,
88-
units="metal",
88+
units=mds.units.METAL,
8989
simulation_data=na_cl_file,
9090
)
9191
project.add_experiment(

CI/integration_tests/calculators/test_einstein_diffusion_coefficients.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Summary
2525
-------
2626
"""
27+
import dataclasses
2728
import os
2829

2930
import numpy as np
@@ -60,9 +61,7 @@ def test_calculator(tmp_path):
6061
os.chdir(tmp_path)
6162
project = mds.Project()
6263
# introduce nontrivial units to make sure all conversions are correct
63-
units = mdsuite.utils.units.si
64-
units.length = 0.5
65-
units.time = 1.3
64+
units = dataclasses.replace(mdsuite.units.SI, length=0.5, time=1.3)
6665
exp = project.add_experiment(
6766
"test_diff_coeff", timestep=time_step, temperature=4.321, units=units
6867
)

CI/integration_tests/calculators/test_green_kubo_self_diffusion_coefficients.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Summary
2525
-------
2626
"""
27+
import dataclasses
2728
import os
2829

2930
import numpy as np
@@ -71,9 +72,9 @@ def test_calculator(tmp_path):
7172
os.chdir(tmp_path)
7273
project = mds.Project()
7374
# introduce nontrivial units to make sure all conversions are correct
74-
units = mdsuite.utils.units.si
75-
units.length = 0.000007654
76-
units.time = 0.0056789
75+
76+
units = dataclasses.replace(mdsuite.units.SI, length=0.000007654, time=0.0056789)
77+
7778
exp = project.add_experiment(
7879
"test_diff_coeff", timestep=time_step, temperature=kT, units=units
7980
)

CI/unit_tests/database/test_experiment_database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_experiment_simulation_data_nested(tmp_path):
206206

207207
def test_experiment_units(tmp_path):
208208
"""Test that the experiment simulation data is stored correctly in the database"""
209-
from mdsuite.utils.units import Units, si
209+
from mdsuite.utils.units import Units
210210

211211
os.chdir(tmp_path)
212212
custom_units = Units(
@@ -220,10 +220,10 @@ def test_experiment_units(tmp_path):
220220
)
221221

222222
project_1 = mds.Project()
223-
project_1.add_experiment(name="Exp01", units="si")
223+
project_1.add_experiment(name="Exp01", units=mds.units.SI)
224224
project_1.add_experiment(name="Exp02", units=custom_units)
225225

226226
project_2 = mds.Project()
227227

228-
assert project_2.experiments["Exp01"].units == si
228+
assert project_2.experiments["Exp01"].units == mds.units.SI
229229
assert project_2.experiments["Exp02"].units == custom_units

CI/unit_tests/utils/test_units.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import mdsuite as mds
2+
3+
4+
def test_units_loading():
5+
"""Test that the import works"""
6+
assert mds.units.SI.pressure == 1
7+
assert mds.units.METAL.pressure == 100000
8+
assert mds.units.REAL.pressure == 101325.0

mdsuite/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
from importlib import metadata
2929
except ImportError: # for Python<3.8
3030
import importlib_metadata as metadata
31+
3132
import logging
3233
import sys
3334

3435
from mdsuite.experiment import Experiment
3536
from mdsuite.project import Project
36-
from mdsuite.utils import config
37+
from mdsuite.utils import config, units
3738
from mdsuite.utils.molecule import Molecule
3839
from mdsuite.utils.report_computer_characteristics import Report
3940

@@ -43,6 +44,7 @@
4344
Report.__name__,
4445
"config",
4546
Molecule.__name__,
47+
"units",
4648
]
4749
__version__ = metadata.version("mdsuite")
4850

mdsuite/experiment/experiment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def __init__(
190190

191191
if self.units is None:
192192
if units is None:
193-
units = "real"
193+
units = mdsuite.units.REAL
194194
self.units = self.units_to_si(units) # Units used during the simulation.
195195

196196
self.box_array = None # Box vectors.

mdsuite/utils/units.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
golden_ratio = 1.618033988749895 # The golden ratio as taken from scipy
4343

4444

45-
@dataclass()
45+
@dataclass(frozen=True)
4646
class Units:
4747
"""
4848
Dataclass for the units.
@@ -64,7 +64,7 @@ def volume(self) -> float:
6464
return self.length**3
6565

6666

67-
real = Units(
67+
REAL = Units(
6868
time=1e-15,
6969
length=1e-10,
7070
energy=4184 / 6.02214076e23,
@@ -75,7 +75,7 @@ def volume(self) -> float:
7575
)
7676

7777

78-
metal = Units(
78+
METAL = Units(
7979
time=1e-12,
8080
length=1e-10,
8181
energy=1.6022e-19,
@@ -86,7 +86,7 @@ def volume(self) -> float:
8686
)
8787

8888

89-
si = Units(
89+
SI = Units(
9090
time=1,
9191
length=1,
9292
energy=1,
@@ -97,4 +97,4 @@ def volume(self) -> float:
9797
)
9898

9999

100-
units_dict = {"real": real, "metal": metal, "si": si}
100+
units_dict = {"real": REAL, "metal": METAL, "si": SI}

0 commit comments

Comments
 (0)