Skip to content

Instantly share code, notes, and snippets.

@janash
Created February 16, 2022 01:43
Show Gist options
  • Save janash/e614d3f861f70a7115a741f2861fb960 to your computer and use it in GitHub Desktop.
Save janash/e614d3f861f70a7115a741f2861fb960 to your computer and use it in GitHub Desktop.
import numpy
import os
def calculate_distance(coords1, coords2):
x_distance = coords1[0] - coords2[0]
y_distance = coords1[1] - coords2[1]
z_distance = coords1[2] - coords2[2]
distance = numpy.sqrt(x_distance**2 + y_distance**2 + z_distance**2)
return distance
def bond_check(bond_length, min_length=0, max_length=1.5):
"""
Checks a bond distance to see if it meets the criteria for a bond based on a minimum and maximum length.
The default minimum is 0 angstroms. The default maximum is 1.5 angtroms.
"""
if bond_length > min_length and bond_length < max_length:
return True
else:
return False
def open_xyz(filename):
"""
This function parses an xyz file and seperates the symbols and the coordinates.
"""
xyz_file = numpy.genfromtxt(fname=filename, skip_header=2, dtype='unicode')
symbols = xyz_file[:,0]
coords = xyz_file[:,1:]
coords = coords.astype(float)
return symbols, coords
file_location = os.path.join('data', 'water.xyz')
symbols, coordinates = open_xyz(file_location)
num_atoms = len(symbols)
for num1 in range(0, num_atoms):
for num2 in range(0, num_atoms):
if num1<num2:
distance = calculate_distance(coordinates[num1], coordinates[num2])
if bond_check(distance) is True:
print(F'{symbols[num1]} to {symbols[num2]} : {distance:.3f}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment