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 shift_coord for gro writer #730

Merged
merged 19 commits into from
Sep 7, 2023
Merged

Conversation

daico007
Copy link
Member

@daico007 daico007 commented Jun 5, 2023

I bumped into some odd behavior when writing out gro file, i.e., realizing the coordinates are slightly shifted. I thought it's a bug but turn out to be a feature, i.e., a check to move all sites to ensure their coords are all positive. This PR add the option to preserve the original coordinate for the gro writer.

@codecov
Copy link

codecov bot commented Jun 5, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.02% ⚠️

Comparison is base (d584ffa) 92.07% compared to head (5525350) 92.06%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #730      +/-   ##
==========================================
- Coverage   92.07%   92.06%   -0.02%     
==========================================
  Files          67       67              
  Lines        6893     6892       -1     
==========================================
- Hits         6347     6345       -2     
- Misses        546      547       +1     
Files Changed Coverage Δ
gmso/formats/gro.py 97.19% <100.00%> (-0.03%) ⬇️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@CalCraven CalCraven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one extra test, otherwise it's good to go.

gmso/formats/gro.py Outdated Show resolved Hide resolved
gmso/formats/gro.py Outdated Show resolved Hide resolved
@@ -131,7 +134,8 @@ def write_gro(top, filename, precision=3):

"""
pos_array = np.ndarray.copy(top.positions)
pos_array = _validate_positions(pos_array)
if shift_coord:
pos_array = _validate_positions(pos_array)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was glancing at this function and feel like it should be rewritten to use vectorized operations instead of this a for loop.

def _validate_positions(pos_array):
    """Modify coordinates, as necessary, to fit limitations of the GRO format."""
    if np.min(pos_array) < 0:
        warnings.warn(
            "Topology contains some negative positions. Translating "
            "in order to ensure all coordinates are non-negative."
        )
    min_xyz = np.min(x, axis=1) # smallest position of each coordinate
    min_xyz0 =  np.where(min_xyz<0, min_xyz, 0) #shift by 0 or minimum value of the positions in that coordinate
    pos_array -= min_xyz0
    return pos_array

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below comment. I think it's worthwhile to make these changes to this function as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh shoot, I somehow miss these, I will add these changes in

@@ -48,6 +48,10 @@ def test_write_gro(self):
top = from_parmed(pmd.load_file(get_fn("ethane.gro"), structure=True))
top.save("out.gro")

def test_write_gro_with_shift_coord(self):
top = from_parmed(pmd.load_file(get_fn("ethane.gro"), structure=True))
top.save("out.gro", shift_coord=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_top = Topology("out.gro")
assert np.all(list(map(lambda x: x.positions>=0, read_top.sites)))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how these comments got resolved, @daico007 did you address them somehow?

Copy link
Contributor

@CalCraven CalCraven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@daico007 daico007 merged commit 0011e15 into mosdef-hub:main Sep 7, 2023
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants