-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create internal module, add doc for utils
- Loading branch information
1 parent
15e0581
commit 6146a4d
Showing
18 changed files
with
126 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ Resources | |
base | ||
modules | ||
library | ||
utils | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Utils | ||
======= | ||
|
||
.. rubric:: Overview | ||
|
||
.. py:currentmodule:: flowermd.utils | ||
.. rubric:: Utils | ||
|
||
.. automodule:: flowermd.utils | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .ff_utils import xml_to_gmso_ff | ||
from .utils import check_return_iterable, validate_ref_value |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import unyt as u | ||
|
||
from flowermd.internal.exceptions import ReferenceUnitError | ||
|
||
"""utils.py | ||
Internal utility methods for flowerMD. | ||
""" | ||
|
||
|
||
def check_return_iterable(obj): | ||
if isinstance(obj, dict): | ||
return [obj] | ||
if isinstance(obj, str): | ||
return [obj] | ||
try: | ||
iter(obj) | ||
return obj | ||
except: # noqa: E722 | ||
return [obj] | ||
|
||
|
||
def validate_ref_value(ref_value, dimension): | ||
"""Validate the reference value and checks the unit dimension. | ||
This function validates the reference value. The reference value can be | ||
provided in three ways: | ||
1. An unyt_quantity instance. | ||
2. A string with the value and unit , for example "1.0 g". | ||
3. A string with the value and unit separated by a "/", for example | ||
"1.0 kcal/mol". | ||
Parameters | ||
---------- | ||
ref_value : unyt_quantity or str; required | ||
The reference value. | ||
dimension : unyt_dimension; required | ||
The dimension of the reference value. | ||
Returns | ||
------- | ||
The validated reference value as an unyt.unyt_quantity instance. | ||
""" | ||
|
||
def _is_valid_dimension(ref_unit): | ||
if ref_unit.dimensions != dimension: | ||
raise ReferenceUnitError( | ||
f"Invalid unit dimension. The reference " | ||
f"value must be in {dimension} " | ||
f"dimension." | ||
) | ||
return True | ||
|
||
def _parse_and_validate_unit(value, unit_str): | ||
if hasattr(u, unit_str): | ||
if unit_str == "amu": | ||
u_unit = u.Unit("amu") | ||
else: | ||
u_unit = getattr(u, unit_str) | ||
if _is_valid_dimension(u_unit): | ||
return float(value) * u_unit | ||
# if the unit contains "/" character, for example "g/mol", check if | ||
# the unit is a valid unit and has the correct dimension. | ||
if len(unit_str.split("/")) == 2: | ||
unit1, unit2 = unit_str.split("/") | ||
if hasattr(u, unit1) and hasattr(u, unit2): | ||
comb_unit = getattr(u, unit1) / getattr(u, unit2) | ||
if _is_valid_dimension(comb_unit): | ||
return float(value) * comb_unit | ||
raise ReferenceUnitError( | ||
f"Invalid reference value. Please provide " | ||
f"a reference value with unit of " | ||
f"{dimension} dimension." | ||
) | ||
|
||
def _is_float(num): | ||
try: | ||
return float(num) | ||
except ValueError: | ||
raise ValueError("The reference value is not a number.") | ||
|
||
# if ref_value is an instance of unyt_quantity, check the dimension. | ||
if isinstance(ref_value, u.unyt_quantity) and _is_valid_dimension( | ||
ref_value.units | ||
): | ||
return ref_value | ||
# if ref_value is a string, check if it is a number and if it is, check if | ||
# the unit exists in unyt and has the correct dimension. | ||
elif isinstance(ref_value, str) and len(ref_value.split()) == 2: | ||
value, unit_str = ref_value.split() | ||
value = _is_float(value) | ||
return _parse_and_validate_unit(value, unit_str) | ||
else: | ||
raise ReferenceUnitError( | ||
f"Invalid reference value. Please provide " | ||
f"a reference value with unit of " | ||
f"{dimension} dimension." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
from .actions import * | ||
from .base_types import HOOMDThermostats | ||
from .ff_utils import xml_to_gmso_ff | ||
from .utils import ( | ||
_calculate_box_length, | ||
check_return_iterable, | ||
get_target_box_mass_density, | ||
get_target_box_number_density, | ||
validate_ref_value, | ||
) | ||
from .utils import get_target_box_mass_density, get_target_box_number_density |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters