From 66a0e0f0e8c795631c3a77b7aacbdd649702cefa Mon Sep 17 00:00:00 2001 From: Umesh Timalsina Date: Tue, 29 Jun 2021 16:12:08 -0500 Subject: [PATCH 1/5] Initial Commit: Wrap Molbox box code in gmso box --- gmso/core/box.py | 62 +++++++++++++++++++++++++++++++++------ gmso/tests/test_box.py | 6 ++-- gmso/tests/test_lammps.py | 14 +++------ 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/gmso/core/box.py b/gmso/core/box.py index b2e9f836c..e781b9acc 100644 --- a/gmso/core/box.py +++ b/gmso/core/box.py @@ -3,6 +3,7 @@ import numpy as np import unyt as u +from molbox import Box as MolBox from unyt.array import allclose_units @@ -117,30 +118,73 @@ class Box(object): """ - def __init__(self, lengths, angles=None): + def __init__(self, lengths, angles=None, precision=None): """Construct a `Box` based on lengths and angles.""" - self._lengths = _validate_lengths(lengths) - self._angles = _validate_angles(angles) + lengths = _validate_lengths(lengths) + angles = _validate_angles(angles) + precision = int(precision or 6) + self._box = MolBox( + lengths=lengths.value, angles=angles.value, precision=precision + ) @property def lengths(self): """Return edge lengths of the box.""" - return self._lengths + return self._box.lengths * u.nm @property def angles(self): """Return angles of the box.""" - return self._angles + return self._box.angles * u.degree @lengths.setter def lengths(self, lengths): """Set the lengths of the box.""" - self._lengths = _validate_lengths(lengths) + lengths = _validate_lengths(lengths).value + self._set_internal_box_vectors(lengths=lengths) @angles.setter def angles(self, angles): """Set the angles of the box.""" - self._angles = _validate_angles(angles) + angles = _validate_angles(angles).value + self._set_internal_box_vectors(angles=angles) + + @property + def precision(self): + """Amount of decimals to represent floating point values.""" + return self._box._precision + + @precision.setter + def precision(self, precision): + """Decimal point precision(default=16).""" + self._box.precision = precision + + def _set_internal_box_vectors(self, lengths=None, angles=None): + from molbox.box import _lengths_angles_to_vectors + + if angles is None: + angles = self.angles.value + if lengths is None: + lengths = self.lengths.value + + self._box._vectors = _lengths_angles_to_vectors( + lengths, angles, self._box.precision + ) + + ( + Lx, + Ly, + Lz, + xy, + xz, + yz, + ) = self._box._from_vecs_to_lengths_tilt_factors() + self._box._Lx = Lx + self._box._Ly = Ly + self._box._Lz = Lz + self._box._xy = xy + self._box._xz = xz + self._box._yz = yz def _unit_vectors_from_angles(self): """Return unit vectors describing prism from angles.""" @@ -173,7 +217,7 @@ def _unit_vectors_from_angles(self): def get_vectors(self): """Return the vectors of the box.""" - return (self._lengths * self.get_unit_vectors().T).T + return (self.lengths * self.get_unit_vectors().T).T def get_unit_vectors(self): """Return the normalized vectors of the box.""" @@ -182,7 +226,7 @@ def get_unit_vectors(self): def __repr__(self): """Return formatted representation of the box.""" return "Box(a={}, b={}, c={}, alpha={}, beta={}, gamma={})".format( - *self._lengths, *self._angles + *self.lengths, *self.angles ) def __eq__(self, other): diff --git a/gmso/tests/test_box.py b/gmso/tests/test_box.py index 3939167ff..61f57b07e 100644 --- a/gmso/tests/test_box.py +++ b/gmso/tests/test_box.py @@ -3,6 +3,7 @@ import numpy as np import pytest import unyt as u +from molbox.box import BoxError from unyt.testing import assert_allclose_units from gmso.core.box import Box @@ -25,7 +26,7 @@ def test_bad_lengths(self, lengths, angles): Box(lengths=lengths, angles=angles) def test_build_2D_Box(self): - with pytest.warns(UserWarning): + with pytest.raises(BoxError): Box(lengths=u.nm * [4, 4, 0]) def test_dtype(self, box): @@ -47,7 +48,8 @@ def test_angles_setter(self, lengths, angles): box = Box(lengths=lengths, angles=u.degree * np.ones(3)) angles *= u.degree box.angles = angles - assert (box.angles == angles).all() + print(angles, box.angles) + assert u.allclose_units(angles, box.angles, rtol=10 ** (-box.precision)) @pytest.mark.parametrize("lengths", [[3, 3, 3], [4, 4, 4], [4, 6, 4]]) def test_setters_with_lists(self, lengths): diff --git a/gmso/tests/test_lammps.py b/gmso/tests/test_lammps.py index 79acf7b54..9d8b33863 100644 --- a/gmso/tests/test_lammps.py +++ b/gmso/tests/test_lammps.py @@ -89,15 +89,9 @@ def test_read_lammps_triclinic(self, typed_ar_system): write_lammpsdata(typed_ar_system, filename="data.triclinic") read = read_lammpsdata("data.triclinic") - assert_allclose_units( - read.box.lengths, - u.unyt_array([1, 1, 1], u.nm), - rtol=1e-5, - atol=1e-8, + assert u.allclose_units( + read.box.lengths, u.unyt_array([1.0, 1.0, 1.0], u.nm) ) - assert_allclose_units( - read.box.angles, - u.unyt_array([60, 90, 120], u.degree), - rtol=1e-5, - atol=1e-8, + assert u.allclose_units( + read.box.angles, u.unyt_array([60, 90, 120], u.degree) ) From c0007f0ffe51dcfaabd2d372ea2b41bd9a63af3d Mon Sep 17 00:00:00 2001 From: Umesh Timalsina Date: Tue, 29 Jun 2021 17:24:47 -0500 Subject: [PATCH 2/5] WIP- Unit fixes to lammps --- environment-dev.yml | 2 ++ gmso/core/box.py | 3 ++- gmso/formats/lammpsdata.py | 14 ++++++-------- gmso/tests/test_lammps.py | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/environment-dev.yml b/environment-dev.yml index 34738007b..373d6be00 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -22,3 +22,5 @@ dependencies: - ipywidgets - ele >= 0.2.0 - pre-commit + - pip: + - git+https://github.com/mosdef-hub/molbox.git#egg=molbox diff --git a/gmso/core/box.py b/gmso/core/box.py index e781b9acc..4b20cbe90 100644 --- a/gmso/core/box.py +++ b/gmso/core/box.py @@ -23,7 +23,6 @@ def _validate_lengths(lengths): lengths *= input_unit lengths.convert_to_units(u.nm) - if np.any( np.less( lengths, @@ -120,8 +119,10 @@ class Box(object): def __init__(self, lengths, angles=None, precision=None): """Construct a `Box` based on lengths and angles.""" + print(lengths, angles) lengths = _validate_lengths(lengths) angles = _validate_angles(angles) + print(lengths, angles) precision = int(precision or 6) self._box = MolBox( lengths=lengths.value, angles=angles.value, precision=precision diff --git a/gmso/formats/lammpsdata.py b/gmso/formats/lammpsdata.py index b1e05107a..2fcbbff00 100644 --- a/gmso/formats/lammpsdata.py +++ b/gmso/formats/lammpsdata.py @@ -100,20 +100,18 @@ def write_lammpsdata(topology, filename, atom_style="full"): atol=1e-8, ): warnings.warn("Orthorhombic box detected") - box.lengths.convert_to_units(u.angstrom) + lengths = box.lengths.to(u.angstrom) for i, dim in enumerate(["x", "y", "z"]): data.write( "{0:.6f} {1:.6f} {2}lo {2}hi\n".format( - 0, box.lengths.value[i], dim + 0, lengths.value[i], dim ) ) else: warnings.warn("Non-orthorhombic box detected") - box.lengths.convert_to_units(u.angstrom) - box.angles.convert_to_units(u.radian) - vectors = box.get_vectors() - a, b, c = box.lengths - alpha, beta, gamma = box.angles + vectors = box.get_vectors().to(u.dimensionless * u.angstrom) + a, b, c = box.lengths.to(u.angstrom) + alpha, beta, gamma = box.angles.to(u.radian) lx = a xy = b * np.cos(gamma) @@ -372,7 +370,7 @@ def read_lammpsdata( # Validate 'unit_style' if unit_style not in ["real"]: - raiseValueError( + raise ValueError( 'Unit Style "{}" is invalid or is not currently supported'.format( unit_style ) diff --git a/gmso/tests/test_lammps.py b/gmso/tests/test_lammps.py index 9d8b33863..147e70361 100644 --- a/gmso/tests/test_lammps.py +++ b/gmso/tests/test_lammps.py @@ -89,7 +89,7 @@ def test_read_lammps_triclinic(self, typed_ar_system): write_lammpsdata(typed_ar_system, filename="data.triclinic") read = read_lammpsdata("data.triclinic") - assert u.allclose_units( + assert_allclose_units( read.box.lengths, u.unyt_array([1.0, 1.0, 1.0], u.nm) ) assert u.allclose_units( From aea28d4317bb3ef70579da617881401b04e04f4f Mon Sep 17 00:00:00 2001 From: Umesh Timalsina Date: Thu, 1 Jul 2021 13:31:08 -0500 Subject: [PATCH 3/5] WIP: Remove print --- gmso/core/box.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gmso/core/box.py b/gmso/core/box.py index 4b20cbe90..1f027c782 100644 --- a/gmso/core/box.py +++ b/gmso/core/box.py @@ -119,7 +119,6 @@ class Box(object): def __init__(self, lengths, angles=None, precision=None): """Construct a `Box` based on lengths and angles.""" - print(lengths, angles) lengths = _validate_lengths(lengths) angles = _validate_angles(angles) print(lengths, angles) From 167728823cf9abd73dbcc6a93017d365ea8a50c0 Mon Sep 17 00:00:00 2001 From: Umesh Timalsina Date: Wed, 7 Jul 2021 11:57:40 -0500 Subject: [PATCH 4/5] WIP- Use molbox from conda --- environment-dev.yml | 3 +-- environment.yml | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/environment-dev.yml b/environment-dev.yml index 373d6be00..985423339 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -22,5 +22,4 @@ dependencies: - ipywidgets - ele >= 0.2.0 - pre-commit - - pip: - - git+https://github.com/mosdef-hub/molbox.git#egg=molbox + - molbox diff --git a/environment.yml b/environment.yml index bba54d5d1..031ce1473 100644 --- a/environment.yml +++ b/environment.yml @@ -10,3 +10,4 @@ dependencies: - pydantic - networkx - ele >= 0.2.0 + - molbox From 31bc80860da8a15d0e1cbde43901060d40adbe58 Mon Sep 17 00:00:00 2001 From: Co Quach Date: Tue, 31 Aug 2021 14:06:54 -0500 Subject: [PATCH 5/5] Fix syntax bug --- gmso/core/box.py | 4 ++-- gmso/tests/test_lammps.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gmso/core/box.py b/gmso/core/box.py index 9deeed833..3dfc65a45 100644 --- a/gmso/core/box.py +++ b/gmso/core/box.py @@ -227,8 +227,8 @@ def json_dict(self): from gmso.abc.serialization_utils import unyt_to_dict return { - "lengths": unyt_to_dict(self._lengths), - "angles": unyt_to_dict(self._angles), + "lengths": unyt_to_dict(self.lengths), + "angles": unyt_to_dict(self.angles), } def __repr__(self): diff --git a/gmso/tests/test_lammps.py b/gmso/tests/test_lammps.py index 147e70361..322b7daaa 100644 --- a/gmso/tests/test_lammps.py +++ b/gmso/tests/test_lammps.py @@ -92,6 +92,6 @@ def test_read_lammps_triclinic(self, typed_ar_system): assert_allclose_units( read.box.lengths, u.unyt_array([1.0, 1.0, 1.0], u.nm) ) - assert u.allclose_units( - read.box.angles, u.unyt_array([60, 90, 120], u.degree) + assert_allclose_units( + read.box.angles, u.unyt_array([60, 90, 120], u.degree), rtol=1e-5 )