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

Update distances.py #1275

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions package/MDAnalysis/lib/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,81 @@ def _check_lengths_match(*arrays):
if not all( a.shape == ref for a in arrays):
raise ValueError("Input arrays must all be same shape"
"Got {0}".format([a.shape for a in arrays]))

## NOT COMPLETE - NEED REFINEMENT
def distance(atom1,atom2,box=None):
Copy link
Member

Choose a reason for hiding this comment

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

Please follow PEP8 style, here "leave space after commas".

Copy link
Member

Choose a reason for hiding this comment

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

The call signature should be

distance(a, b, box=None)

where a and b are coordinates (numpy arrays of shape (3,)).

No functions in MDAnalysis.lib will make explicit reference to atoms or other "core data structures". Look at distance_array().

"""
Checks the distance of only a pair of atoms, rather than the array check that works with distance_array

TODO: refinement of the array structure to take advantage of numpy arrays, and generalize the input var

"""
name1 = "bynum" + " " + str(atom1) # concatenate string for first selection
Copy link
Member

Choose a reason for hiding this comment

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

indentation error

name2 = "bynum" + " " + str(atom2) # concatenate string for the second selection

selection1 = u.select_atoms(name1) # select from universe
Copy link
Member

Choose a reason for hiding this comment

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

YOu don't need any selections here. Work with raw coordinates.

selection2 = u.select_atoms(name2) # ditto

selection1_vec = selection1.positions[0] # As there is only one atom, we only need to extract the first row
selection2_vec = selection2.positions[0]

selection1_selection2_array = [selection1_vec, selection2_vec]

# --- For a triclinic box --- #
if box is not None:
box_type = box_check(box) ## Check type of box and store it in box_type variable

if box_type == 'tri_box':
for i in range(len(selection1_selection2_array)):
Copy link
Member

Choose a reason for hiding this comment

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

This looks all a bit complicated. However, implement your tests and then see if your solution works. Once you have one solution it can be improved. But tests are important early on.

if abs(selection1_selection2_array[i][2]) > u.dimensions[5]:
# Z axis minimum image - follows the LAMMPS convention
if selection1_selection2_array[i][2] < 0.0:
selection1_selection2_array[i][2] += u.dimensions[5]
Copy link
Member

Choose a reason for hiding this comment

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

If you're using numpy arrays, use numpy array operations, e.g.

selection1_selection2_array[i, 0:3] += u.dimensions [3:6]

selection1_selection2_array[i][1] += u.dimensions[4]
selection1_selection2_array[i][0] += u.dimensions[3]
else:
selection1_selection2_array[i][2] -= u.dimensions[5]
selection1_selection2_array[i][1] -= u.dimensions[4]
selection1_selection2_array[i][0] -= u.dimensions[3]
# Y axis minimum image
if abs(selection1_selection2_array[i][1]) > u.dimensions[4]:
if selection1_selection2_array[i][1] < 0.0:
selection1_selection2_array[i][1] += u.dimensions[4]
selection1_selection2_array[i][0] += u.dimensions[3]
else:
selection1_selection2_array[i][1] -= u.dimensions[4]
selection1_selection2_array[i][0] -= u.dimensions[3]
# X axis minimum image
if abs(selection1_selection2_array[i][0]) > u.dimensions[3]:
if selection1_selection2_array[i][0] < 0.0:
selection1_selection2_array[i][1] += u.dimensions[3]
else:
selection1_selection2_array[i][1] -= u.dimensions[3]

else: # For non-trclinical boxes
for i in range(len(selection1_selection2_array)):
# X axis
if abs(selection1_selection2_array[i][0]) < -u.dimensions[3]/2:
selection1_selection2_array[i][0] = selection1_selection2_array[i][0] + u.dimensions[3]/2
elif abs(selection1_selection2_array[i][0]) >= u.dimensions[3]/2:
selection1_selection2_array[i][0] = selection1_selection2_array[i][0] - u.dimensions[3]/2

if abs(selection1_selection2_array[i][1]) < -u.dimensions[4]/2:
selection1_selection2_array[i][1] = selection1_selection2_array[i][1] + u.dimensions[4]/2
elif abs(selection1_selection2_array[i][1]) >= u.dimensions[4]/2:
selection1_selection2_array[i][1] = selection1_selection2_array[i][1] - u.dimensions[4]/2

if abs(selection1_selection2_array[i][2]) < -u.dimensions[5]/2:
selection1_selection2_array[i][2] = selection1_selection2_array[i][2] + u.dimensions[5]/2
elif abs(selection1_selection2_array[i][2]) >= u.dimensions[5]/2:
selection1_selection2_array[i][2] = selection1_selection2_array[i][2] - u.dimensions[5]/2


separation = np.power((np.power((selection1_selection2_array[0][0] - selection1_selection2_array[1][0]),2) + np.power((selection1_selection2_array[1][0] - selection1_selection2_array[1][1]),2) + np.power((selection1_selection2_array[2][0] - selection1_selection2_array[2][1]),2)),2)

return separation


Copy link
Member

Choose a reason for hiding this comment

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

Implement tests!!!!


def distance_array(reference, configuration, box=None, result=None, backend="serial"):
"""Calculate all distances between a reference set and another configuration.
Expand Down