Skip to content

Commit

Permalink
add mds.units.<unit_system> (#543)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
PythonFZ and SamTov authored Aug 16, 2022
1 parent d163447 commit 49d72b9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CI/functional_tests/test_molten_salts.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def mdsuite_project(traj_files, tmp_path) -> mds.Project:
name="NaCl",
timestep=0.002,
temperature=1200.0,
units="metal",
units=mds.units.METAL,
simulation_data=na_cl_file,
)
project.add_experiment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Summary
-------
"""
import dataclasses
import os

import numpy as np
Expand Down Expand Up @@ -60,9 +61,7 @@ def test_calculator(tmp_path):
os.chdir(tmp_path)
project = mds.Project()
# introduce nontrivial units to make sure all conversions are correct
units = mdsuite.utils.units.si
units.length = 0.5
units.time = 1.3
units = dataclasses.replace(mdsuite.units.SI, length=0.5, time=1.3)
exp = project.add_experiment(
"test_diff_coeff", timestep=time_step, temperature=4.321, units=units
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Summary
-------
"""
import dataclasses
import os

import numpy as np
Expand Down Expand Up @@ -71,9 +72,9 @@ def test_calculator(tmp_path):
os.chdir(tmp_path)
project = mds.Project()
# introduce nontrivial units to make sure all conversions are correct
units = mdsuite.utils.units.si
units.length = 0.000007654
units.time = 0.0056789

units = dataclasses.replace(mdsuite.units.SI, length=0.000007654, time=0.0056789)

exp = project.add_experiment(
"test_diff_coeff", timestep=time_step, temperature=kT, units=units
)
Expand Down
6 changes: 3 additions & 3 deletions CI/unit_tests/database/test_experiment_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_experiment_simulation_data_nested(tmp_path):

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

os.chdir(tmp_path)
custom_units = Units(
Expand All @@ -220,10 +220,10 @@ def test_experiment_units(tmp_path):
)

project_1 = mds.Project()
project_1.add_experiment(name="Exp01", units="si")
project_1.add_experiment(name="Exp01", units=mds.units.SI)
project_1.add_experiment(name="Exp02", units=custom_units)

project_2 = mds.Project()

assert project_2.experiments["Exp01"].units == si
assert project_2.experiments["Exp01"].units == mds.units.SI
assert project_2.experiments["Exp02"].units == custom_units
8 changes: 8 additions & 0 deletions CI/unit_tests/utils/test_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import mdsuite as mds


def test_units_loading():
"""Test that the import works"""
assert mds.units.SI.pressure == 1
assert mds.units.METAL.pressure == 100000
assert mds.units.REAL.pressure == 101325.0
4 changes: 3 additions & 1 deletion mdsuite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
from importlib import metadata
except ImportError: # for Python<3.8
import importlib_metadata as metadata

import logging
import sys

from mdsuite.experiment import Experiment
from mdsuite.project import Project
from mdsuite.utils import config
from mdsuite.utils import config, units
from mdsuite.utils.molecule import Molecule
from mdsuite.utils.report_computer_characteristics import Report

Expand All @@ -43,6 +44,7 @@
Report.__name__,
"config",
Molecule.__name__,
"units",
]
__version__ = metadata.version("mdsuite")

Expand Down
2 changes: 1 addition & 1 deletion mdsuite/experiment/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def __init__(

if self.units is None:
if units is None:
units = "real"
units = mdsuite.units.REAL
self.units = self.units_to_si(units) # Units used during the simulation.

self.box_array = None # Box vectors.
Expand Down
10 changes: 5 additions & 5 deletions mdsuite/utils/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
golden_ratio = 1.618033988749895 # The golden ratio as taken from scipy


@dataclass()
@dataclass(frozen=True)
class Units:
"""
Dataclass for the units.
Expand All @@ -64,7 +64,7 @@ def volume(self) -> float:
return self.length**3


real = Units(
REAL = Units(
time=1e-15,
length=1e-10,
energy=4184 / 6.02214076e23,
Expand All @@ -75,7 +75,7 @@ def volume(self) -> float:
)


metal = Units(
METAL = Units(
time=1e-12,
length=1e-10,
energy=1.6022e-19,
Expand All @@ -86,7 +86,7 @@ def volume(self) -> float:
)


si = Units(
SI = Units(
time=1,
length=1,
energy=1,
Expand All @@ -97,4 +97,4 @@ def volume(self) -> float:
)


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

0 comments on commit 49d72b9

Please sign in to comment.