-
Notifications
You must be signed in to change notification settings - Fork 0
/
dd_modes_1ref.py
166 lines (116 loc) · 4.75 KB
/
dd_modes_1ref.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
import sys
import numpy as np
# from os import listdir
# from os.path import isfile, join
from os.path import basename
from glob import glob
import re
import time
from joblib import Parallel, delayed
import multiprocessing
# t0 = time.time()
# if len(sys.argv) < 4:
# print('\nERROR: missing arguments.')
# print('Please specify the directory where the modes are stored, then the amount of modes you wanna spot, then the number of the atom you are looking for in the file.\n')
# sys.exit()
# print('\n!! The format of the atom to be found is Ex: C182 for carbon and atom number 182 !!\n')
# directory = sys.argv[1]
# max_total = int(sys.argv[2])
# find_atom = str(sys.argv[3])
# all_files = glob(str(directory + '/mode_*.xyz'))
# # all_files = [f for f in listdir(directory) if isfile(join(directory, f))]
# print('Reading files\n')
# lmodes_delta = []
# dmodes= {}
# for file in all_files:
# with open(file) as fp:
# xyz_data = fp.readlines()
# nb_atoms = int(xyz_data[0])
# nb_lines = int(len(xyz_data))
# nb_of_atom_to_find = int(re.findall(r'\d+', find_atom)[0])
# xyz_data.pop(0)
# xyz_data.pop(0)
# ldistances = []
# latom_coord = []
# n = 0
# x_ref = float(xyz_data[nb_of_atom_to_find].split()[1])
# y_ref = float(xyz_data[nb_of_atom_to_find].split()[2])
# z_ref = float(xyz_data[nb_of_atom_to_find].split()[3])
# for data in xyz_data:
# name = '{}{}'.format(data.split()[0], n)
# try:
# x_coord = float(data.split()[1])
# y_coord = float(data.split()[2])
# z_coord = float(data.split()[3])
# atom_coord = [x_coord, y_coord, z_coord]
# distance = np.sqrt(((x_coord - x_ref)**2) + ((y_coord - y_ref)**2) + ((z_coord - z_ref)**2))
# if name == find_atom:
# ldistances.append(distance)
# latom_coord.append(atom_coord)
# n += 1
# except:
# n = 0
# dmodes[basename(file)] = [np.asarray(ldistances), np.asarray(latom_coord)]
# lmodes_delta.append((basename(file), max(ldistances)))
# sorted_lmodes_delta = sorted(lmodes_delta, key=lambda x: x[1], reverse=True)
# for t in sorted_lmodes_delta[:max_total]:
# print('Mode: {}'.format(t[0]), 'Atom: {}'.format(find_atom), 'Value: {:e} A'.format(t[1]))
# t1 = time.time()
# print('Total time (s):\t', t1-t0)
t0 = time.time()
if len(sys.argv) < 4:
print('\nERROR: missing arguments.')
print('Please specify the directory where the modes are stored, then the amount of modes you wanna spot, then the number of the atom you are looking for in the file.\n')
sys.exit()
print('\n!! The format of the atom to be found is Ex: C182 for carbon and atom number 182 !!\n')
directory = sys.argv[1]
max_total = int(sys.argv[2])
find_atom = str(sys.argv[3])
# all_files = [f for f in listdir(directory) if isfile(join(directory, f))]
print('Reading files\n')
all_files = glob(str(directory + '/mode_*.xyz'))
def computing(file, find_atom):
lmodes_delta = []
dmodes= {}
with open(file) as fp:
xyz_data = fp.readlines()
# nb_atoms = int(xyz_data[0])
# nb_lines = int(len(xyz_data))
nb_of_atom_to_find = int(re.findall(r'\d+', find_atom)[0])
xyz_data.pop(0)
xyz_data.pop(0)
ldistances = []
latom_coord = []
n = 0
x_ref = float(xyz_data[nb_of_atom_to_find].split()[1])
y_ref = float(xyz_data[nb_of_atom_to_find].split()[2])
z_ref = float(xyz_data[nb_of_atom_to_find].split()[3])
for data in xyz_data:
name = '{}{}'.format(data.split()[0], n)
try:
x_coord = float(data.split()[1])
y_coord = float(data.split()[2])
z_coord = float(data.split()[3])
atom_coord = [x_coord, y_coord, z_coord]
distance = np.sqrt(((x_coord - x_ref)**2) + ((y_coord - y_ref)**2) + ((z_coord - z_ref)**2))
if name == find_atom:
ldistances.append(distance)
latom_coord.append(atom_coord)
n += 1
except:
n = 0
dmodes[basename(file)] = [np.asarray(ldistances), np.asarray(latom_coord)]
lmodes_delta.append((basename(file), max(ldistances)))
return dmodes, lmodes_delta
num_cores = multiprocessing.cpu_count()
temp = Parallel(n_jobs=num_cores)(delayed(computing)(i, find_atom) for i in all_files)
lmodes_delta = []
dmodes= {}
for i in temp:
dmodes[list(i[0].keys())[0]] = list(i[0].values())[0]
lmodes_delta.extend(i[1])
sorted_lmodes_delta = sorted(lmodes_delta, key=lambda x: x[1], reverse=True)
for t in sorted_lmodes_delta[:max_total]:
print('Mode: {}'.format(t[0]), 'Atom: {}'.format(find_atom), 'Value: {:e} A'.format(t[1]))
t1 = time.time()
print('Total time (s):\t', t1-t0)