Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system and utils unit tests #34

Merged
merged 15 commits into from
Aug 30, 2023
Merged
86 changes: 62 additions & 24 deletions hoomd_organics/base/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,48 +171,86 @@ def reference_values(self):
return self._reference_values

@reference_length.setter
def reference_length(self, length, unit=None):
if isinstance(length, u.array.unyt_quantity):
self._reference_values["length"] = length
elif isinstance(unit, str) and (
isinstance(length, float) or isinstance(length, int)
def reference_length(self, length):
if (
isinstance(length, u.array.unyt_quantity)
and length.units.dimensions == u.dimensions.length
):
self._reference_values["length"] = length * getattr(u, unit)
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):
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 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, unit=None):
if isinstance(energy, u.array.unyt_quantity):
self._reference_values["energy"] = energy
elif isinstance(unit, str) and (
isinstance(energy, float) or isinstance(energy, int)
def reference_energy(self, energy):
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 * getattr(u, unit)
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):
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 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, unit=None):
if isinstance(mass, u.array.unyt_quantity):
self._reference_values["mass"] = mass
elif isinstance(unit, str) and (
isinstance(mass, float) or isinstance(mass, int)
def reference_mass(self, mass):
if (
isinstance(mass, u.array.unyt_quantity)
and mass.units.dimensions == u.dimensions.mass
):
self._reference_values["mass"] = mass * getattr(u, unit)
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):
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 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
Loading
Loading