Skip to content

Commit

Permalink
Merge branch 'cmelab:main' into ml_custom_force
Browse files Browse the repository at this point in the history
  • Loading branch information
marjanalbooyeh authored Aug 30, 2023
2 parents c1ffa99 + a714ac2 commit 288b8ec
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 22 deletions.
60 changes: 38 additions & 22 deletions hoomd_organics/base/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,69 +172,85 @@ def reference_values(self):

@reference_length.setter
def reference_length(self, length):
if isinstance(length, u.array.unyt_quantity):
if (
isinstance(length, u.array.unyt_quantity)
and length.units.dimensions == u.dimensions.length
):
self._reference_values["length"] = length
elif isinstance(length, str) and len(length.split()) == 2:
value, unit = length.split()
if value.isnumeric() and hasattr(u, unit):
self._reference_values["length"] = float(value) * getattr(
u, unit
)
unit = getattr(u, unit)
if unit.dimensions == u.dimensions.length:
self._reference_values["length"] = float(value) * unit
else:
raise ReferenceUnitError(
f"Invalid reference length input.Please provide reference "
f"length (number) and unit (string) or pass length value "
f"as an {str(u.array.unyt_quantity)}."
f"length (number) and length unit (string) or pass length "
f"value as an {str(u.array.unyt_quantity)}."
)
else:
raise ReferenceUnitError(
f"Invalid reference length input.Please provide reference "
f"length (number) and unit (string) or pass length value as "
f"an {str(u.array.unyt_quantity)}."
f"length (number) and length unit (string) or pass length "
f"value as an {str(u.array.unyt_quantity)}."
)

@reference_energy.setter
def reference_energy(self, energy):
if isinstance(energy, u.array.unyt_quantity):
energy_dim = (
(u.dimensions.length**2)
* u.dimensions.mass
/ u.dimensions.time**2
)
if (
isinstance(energy, u.array.unyt_quantity)
and energy.units.dimensions == energy_dim
):
self._reference_values["energy"] = energy
elif isinstance(energy, str) and len(energy.split()) == 2:
value, unit = energy.split()
if value.isnumeric() and hasattr(u, unit):
self._reference_values["energy"] = float(value) * getattr(
u, unit
)
unit = getattr(u, unit)
if unit.dimensions == energy_dim:
self._reference_values["energy"] = float(value) * unit
else:
raise ReferenceUnitError(
f"Invalid reference energy input.Please provide reference "
f"energy (number) and unit (string) or pass energy value "
f"as an {str(u.array.unyt_quantity)}."
f"energy (number) and energy unit (string) or pass energy "
f"value as an {str(u.array.unyt_quantity)}."
)
else:
raise ReferenceUnitError(
f"Invalid reference energy input.Please provide reference "
f"energy (number) and unit (string) or pass energy value as "
f"an {str(u.array.unyt_quantity)}."
f"energy (number) and energy unit (string) or pass energy "
f"value as an {str(u.array.unyt_quantity)}."
)

@reference_mass.setter
def reference_mass(self, mass):
if isinstance(mass, u.array.unyt_quantity):
if (
isinstance(mass, u.array.unyt_quantity)
and mass.units.dimensions == u.dimensions.mass
):
self._reference_values["mass"] = mass
elif isinstance(mass, str) and len(mass.split()) == 2:
value, unit = mass.split()
if value.isnumeric() and hasattr(u, unit):
self._reference_values["mass"] = float(value) * getattr(u, unit)
unit = getattr(u, unit)
if unit.dimensions == u.dimensions.mass:
self._reference_values["mass"] = float(value) * unit
else:
raise ReferenceUnitError(
f"Invalid reference mass input.Please provide reference "
f"mass (number) and unit (string) or pass mass value as "
f"an {str(u.array.unyt_quantity)}."
f"mass (number) and mass unit (string) or pass mass value "
f"as an {str(u.array.unyt_quantity)}."
)
else:
raise ReferenceUnitError(
f"Invalid reference mass input.Please provide reference "
f"mass (number) and unit (string) or pass mass value as an "
f"{str(u.array.unyt_quantity)}."
f"mass (number) and mass unit (string) or pass mass value as "
f"an {str(u.array.unyt_quantity)}."
)

@reference_values.setter
Expand Down
72 changes: 72 additions & 0 deletions hoomd_organics/tests/base/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,30 @@ def test_ref_length_invalid_unit_string(self, polyethylene):
with pytest.raises(ReferenceUnitError):
system.reference_length = "1.0 invalid_unit"

def test_ref_length_invalid_dimension(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
molecules=[polyethylene],
force_field=[OPLS_AA()],
density=1.0,
r_cut=2.5,
auto_scale=False,
)
with pytest.raises(ReferenceUnitError):
system.reference_length = 1.0 * u.g

def test_ref_length_invalid_dimension_string(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
molecules=[polyethylene],
force_field=[OPLS_AA()],
density=1.0,
r_cut=2.5,
auto_scale=False,
)
with pytest.raises(ReferenceUnitError):
system.reference_length = "1.0 g"

def test_set_ref_energy(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
Expand Down Expand Up @@ -379,6 +403,30 @@ def test_ref_energy_invalid_unit_string(self, polyethylene):
with pytest.raises(ReferenceUnitError):
system.reference_energy = "1.0 invalid_unit"

def test_ref_energy_invalid_dimension(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
molecules=[polyethylene],
force_field=[OPLS_AA()],
density=1.0,
r_cut=2.5,
auto_scale=False,
)
with pytest.raises(ReferenceUnitError):
system.reference_energy = 1.0 * u.g

def test_ref_energy_invalid_dimension_string(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
molecules=[polyethylene],
force_field=[OPLS_AA()],
density=1.0,
r_cut=2.5,
auto_scale=False,
)
with pytest.raises(ReferenceUnitError):
system.reference_length = "1.0 m"

def test_set_ref_mass(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
Expand Down Expand Up @@ -440,6 +488,30 @@ def test_ref_mass_invalid_unit_string(self, polyethylene):
with pytest.raises(ReferenceUnitError):
system.reference_mass = "1.0 invalid_unit"

def test_ref_mass_invalid_dimension(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
molecules=[polyethylene],
force_field=[OPLS_AA()],
density=1.0,
r_cut=2.5,
auto_scale=False,
)
with pytest.raises(ReferenceUnitError):
system.reference_energy = 1.0 * u.m

def test_ref_mass_invalid_dimension_string(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system = Pack(
molecules=[polyethylene],
force_field=[OPLS_AA()],
density=1.0,
r_cut=2.5,
auto_scale=False,
)
with pytest.raises(ReferenceUnitError):
system.reference_length = "1.0 m"

def test_lattice_polymer(self, polyethylene):
polyethylene = polyethylene(lengths=2, num_mols=32)
system = Lattice(
Expand Down

0 comments on commit 288b8ec

Please sign in to comment.