-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriting functions.py
258 lines (169 loc) · 6.91 KB
/
Writing functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
# coding: utf-8
# ## Writing Functions
# In[23]:
##def function_name(parameters):
## ** function body code **
## return value_to_return
# In[24]:
import numpy
import os
file_location = os.path.join('data', 'water.xyz')
xyz_file = numpy.genfromtxt(fname=file_location, skip_header=2, dtype='unicode')
symbols = xyz_file[:, 0]
coordinates = xyz_file[:, 1:]
coordinates = coordinates.astype(numpy.float)
num_atoms = len(symbols)
for num1 in range(0, num_atoms):
for num2 in range(0, num_atoms):
if num1 < num2:
x_distance = coordinates[num1, 0] - coordinates[num2, 0]
y_distance = coordinates[num1, 1] - coordinates[num2, 1]
z_distance = coordinates[num1, 2] - coordinates[num2, 2]
bond_length_12 = numpy.sqrt(x_distance ** 2 + y_distance ** 2 + z_distance ** 2)
if bond_length_12 > 0 and bond_length_12 <= 1.5:
print(F'{symbols[num1]} to {symbols[num2]} : {bond_length_12:.3f}')
# In[25]:
def calculate_distance(atom1_coord, atom2_coord):
x_distance = atom1_coord[0] - atom2_coord[0]
y_distance = atom1_coord[1] - atom2_coord[1]
z_distance = atom1_coord[2] - atom2_coord[2]
bond_length_12 = numpy.sqrt(x_distance ** 2 + y_distance ** 2 + z_distance ** 2)
return bond_length_12
# In[26]:
for num1 in range(0, num_atoms):
for num2 in range(0, num_atoms):
if num1 < num2:
bond_length_12 = calculate_distance(coordinates[num1], coordinates[num2])
if bond_length_12 > 0 and bond_length_12 <= 1.5:
print(F'{symbols[num1]} to {symbols[num2]} : {bond_length_12:.3f}')
# In[27]:
def bond_check(atom_distance):
if atom_distance > 0 and atom_distance <= 1.5:
return True
else:
return False
# In[28]:
# Modify the bond_check function to take a minimum length and a maximum length as user input.
def bond_check(atom_distance, minimum_length, maximum_length):
if atom_distance > minimum_length and atom_distance <= maximum_length:
return True
else:
return False
# In[29]:
help(numpy.genfromtxt)
# In[30]:
def calculate_distance(atom1_coord, atom2_coord):
"""Calculate the distance between two three-dimensional points."""
x_distance = atom1_coord[0] - atom2_coord[0]
y_distance = atom1_coord[1] - atom2_coord[1]
z_distance = atom1_coord[2] - atom2_coord[2]
bond_length_12 = numpy.sqrt(x_distance**2+y_distance**2+z_distance**2)
return bond_length_12
# In[31]:
def bond_check(atom_distance, minimum_length=0, maximum_length=1.5):
"""
Check if a distance is a bond based on a minimum and maximum bond length.
"""
if atom_distance > minimum_length and atom_distance <= maximum_length:
return True
else:
return False
# In[32]:
print(bond_check(1.5))
print(bond_check(1.6))
# In[33]:
print(bond_check(1.6, maximum_length=1.6))
# In[34]:
num_atoms = len(symbols)
for num1 in range(0, num_atoms):
for num2 in range(0, num_atoms):
if num1 < num2:
bond_length_12 = calculate_distance(coordinates[num1], coordinates[num2])
if bond_check(bond_length_12) is True:
print(F'{symbols[num1]} to {symbols[num2]} : {bond_length_12:.3f}')
# In[35]:
## We might also want to write a function that opens and processes our xyz file for us. Write a function called open_xyz which takes an xyz file as a parameter and returns the symbols and coordinates.
##Hint: You can return two values from a function by typing return variable1, variable2.
def open_xyz(filename):
xyz_file = numpy.genfromtxt(fname=filename, skip_header=2, dtype='unicode')
symbols = xyz_file[:, 0]
coord = (xyz_file[:, 1:])
coord = coord.astype(numpy.float)
return symbols, coord
# In[36]:
import numpy
import os
file_location = os.path.join('data', 'water.xyz')
symbols, coord = 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:
bond_length_12 = calculate_distance(coord[num1], coord[num2])
if bond_check(bond_length_12) is True:
print(F'{symbols[num1]} to {symbols[num2]} : {bond_length_12:.3f}')
# In[37]:
def print_bonds(atom_symbols, atom_coordinates):
num_atoms = len(atom_symbols)
for num1 in range(0, num_atoms):
for num2 in range(0, num_atoms):
if num1 < num2:
bond_length_12 = calculate_distance(atom_coordinates[num1], atom_coordinates[num2])
if bond_check(bond_length_12) is True:
print(F'{atom_symbols[num1]} to {atom_symbols[num2]} : {bond_length_12:.3f}')
# In[38]:
import numpy
import os
def calculate_distance(atom1_coord, atom2_coord):
"""
Calculate the distance between two three-dimensional points.
"""
x_distance = atom1_coord[0] - atom2_coord[0]
y_distance = atom1_coord[1] - atom2_coord[1]
z_distance = atom1_coord[2] - atom2_coord[2]
bond_length_12 = numpy.sqrt(x_distance ** 2 + y_distance ** 2 + z_distance ** 2)
return bond_length_12
def bond_check(atom_distance, minimum_length=0, maximum_length=1.5):
"""Check if a distance is a bond based on a minimum and maximum bond length"""
if atom_distance > minimum_length and atom_distance <= maximum_length:
return True
else:
return False
def open_xyz(filename):
"""
Open and read an xyz file. Returns tuple of symbols and coordinates.
"""
xyz_file = numpy.genfromtxt(fname=filename, skip_header=2, dtype='unicode')
symbols = xyz_file[:,0]
coord = (xyz_file[:,1:])
coord = coord.astype(numpy.float)
return symbols, coord
def print_bonds(atom_symbols, atom_coordinates):
"""
Prints atom symbols and bond length for a set of atoms.
"""
num_atoms = len(atom_symbols)
for num1 in range(0, num_atoms):
for num2 in range(0, num_atoms):
if num1 < num2:
bond_length_12 = calculate_distance(atom_coordinates[num1], atom_coordinates[num2])
if bond_check(bond_length_12) is True:
print(F'{atom_symbols[num1]} to {atom_symbols[num2]} : {bond_length_12:.3f}')
# In[39]:
water_file_location = os.path.join('data', 'water.xyz')
water_symbols, water_coords = open_xyz(water_file_location)
benzene_file_location = os.path.join('data', 'benzene.xyz')
benzene_symbols, benzene_coords = open_xyz(benzene_file_location)
print(F'Printing bonds for water.')
print_bonds(water_symbols, water_coords)
print(F'Printing bonds for benzene.')
print_bonds(benzene_symbols, benzene_coords)
# In[40]:
## In earlier lessons, we used glob to process multiple files. How could you use glob to print bonds for all the xyz files?
import glob
xyz_files = glob.glob("data/*.xyz")
for xyz_file in xyz_files:
atom_symbols, atom_coords = open_xyz(xyz_file)
print("Printing bonds for ", xyz_file)
print_bonds(atom_symbols, atom_coords)