-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyseminario.py
executable file
·223 lines (179 loc) · 7.12 KB
/
pyseminario.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A python3 script computing bond and valence angle force constants using
the Seminario (projected Hessian) method.
@authors: Szymon Szrajer, Zuzanna Wojdyla, Tomasz Borowski
Last update: 13.01.2022
Last update: 18.05.2023
"""
import sys
from pyseminario_aux import fchk_read_n_atoms, fchk_read_atoms, fchk_read_hessian
from pyseminario_aux import Bond, Angle, triple_to_2_bond_labels, read_section_from_input, print_help
### ---------------------------------------------------------------------- ###
### Seting the file names ###
sys_argv_len = len(sys.argv)
if sys_argv_len > 1:
inp_file_name = sys.argv[1]
else:
inp_file_name = None
if inp_file_name == None:
print("pySeminario input file not found \n")
sys.exit(1)
### if -h - write help and exit ###
if inp_file_name == "-h":
print_help()
sys.exit(1)
### ---------------------------------------------------------------------- ###
### Setting files and for which bonds and angles k will be calculated ###
input_f = open(inp_file_name, 'r')
chk_file_names = read_section_from_input(input_f, '%FILES', '%END')
bonds_lines = read_section_from_input(input_f, '%BONDS', '%END')
angles_lines = read_section_from_input(input_f, '%ANGLES', '%END')
input_f.close()
bonds_2_calc = [] # a list of 2-element tuples with atom numbers in ascending order
for i in range(len(bonds_lines)):
str_list = bonds_lines[i].split()
num_list = []
for item in str_list:
num_list.append(eval(item))
num_list.sort()
bonds_2_calc.append(tuple(num_list))
angles_2_calc = []
for i in range(len(angles_lines)):
str_list = angles_lines[i].split()
num_list = []
for item in str_list:
num_list.append(eval(item))
a = num_list[0]
b = num_list[1]
c = num_list[2]
if a < c:
ordered_num_list = [a, b, c]
else:
ordered_num_list = [c, b, a]
angles_2_calc.append(tuple(ordered_num_list))
atoms_2_calc = [] # list of intigers - atom numbers (1-based)
for item in bonds_2_calc + angles_2_calc:
for num in item:
atoms_2_calc.append(num)
atoms_2_calc = list(set(atoms_2_calc))
atoms_2_calc.sort()
##############################################################################
# Main loop through the fchk files
results = [] # a list of dictionaries, one per fchk file
for i in range(len(chk_file_names)):
fchk_file = chk_file_names[i][:-1] # remove '\n'
fchk_file = './' + fchk_file # assumes the files are in the current dir
res_dict = {}
res_dict['file name'] = chk_file_names[i][:-1]
### ---------------------------------------------------------------------- ###
### Reading from fchk file ###
fchk = open(fchk_file, 'r')
N_atoms= 0 # int, number of atoms in the system
atom_list = [] # a list of atom objects
hessians_3x3 = {} # a dic with 3x3 hessians with (at_1, at_2) as keys
N_atoms = fchk_read_n_atoms(fchk)
atom_list = fchk_read_atoms(fchk,atoms_2_calc)
at_pairs_list = bonds_2_calc.copy()
for triple in angles_2_calc:
ab_label, bc_label = triple_to_2_bond_labels(triple)
for label in [ab_label, bc_label]:
if label not in at_pairs_list:
at_pairs_list.append(label)
for pair in at_pairs_list:
hessians_3x3[pair] = fchk_read_hessian(fchk,pair)
fchk.close()
### ---------------------------------------------------------------------- ###
### arranging data into lists of Bond and Angle objects ###
bond_list = [] # a list of bond objects (needed for bond and angle k calculations)
angle_list =[] # a list of angle objects
at_nr_2_seq = {} # a dict with atom number as key and seq number (0-based) in atom_list as value
for i in range(len(atom_list)):
at = atom_list[i]
at_nr_2_seq[ at.get_number() ] = i
for pair in at_pairs_list:
at1 = atom_list[ at_nr_2_seq[ pair[0] ] ]
at2 = atom_list[ at_nr_2_seq[ pair[1] ] ]
two_atoms = [at1, at2]
hess = hessians_3x3[ pair ]
bond = Bond(two_atoms, hess)
bond.set_label()
bond_list.append( bond )
for triple in angles_2_calc:
ab_label, bc_label = triple_to_2_bond_labels(triple)
ab_bond = None
bc_bond = None
for bond in bond_list:
bond_lab = bond.get_label()
if bond_lab == ab_label:
ab_bond = bond
elif bond_lab == bc_label:
bc_bond = bond
two_bonds = [ab_bond, bc_bond]
angle = Angle( two_bonds )
angle_list.append( angle )
### ---------------------------------------------------------------------- ###
### calculations of force constants ###
# preparatory calculations for bonds and angles
for bond in bond_list:
bond.set_bond_length()
bond.set_u_ab()
bond.set_eigs()
for angle in angle_list:
angle.set_label()
angle.set_deg_value()
angle.set_u_n()
angle.set_u_pa()
angle.set_u_pc()
angle.set_k()
# actual calculations of k-values and storing the results
for bond in bond_list:
if bond.get_label() in bonds_2_calc:
bond.set_k()
label = bond.get_label()
length = bond.get_bond_length()
k = bond.get_k()
res_dict[label] = [length, k]
for angle in angle_list:
angle.set_k()
label = angle.get_label()
ang = angle.get_deg_value()
k = angle.get_k()
res_dict[label] = [ang, k]
results.append(res_dict)
### ---------------------------------------------------------------------- ###
### outputing the results ###
results_label_wise = {}
# aranging the results label-wise
for label in bonds_2_calc:
res_list = []
for item in results:
f_name = item['file name']
r = item[label][0]
k = item[label][1]
res_list.append( [f_name, r, k] )
results_label_wise[label] = res_list
for label in angles_2_calc:
res_list = []
for item in results:
f_name = item['file name']
ang = item[label][0]
k = item[label][1]
res_list.append( [f_name, ang, k] )
results_label_wise[label] = res_list
# printing the results
for label in bonds_2_calc:
print('-----------------------------------------------')
print("Bond " + str(label))
print("File name " + "r[A] " + "k[kcal/mol*A^2]")
res_list = results_label_wise[label]
for res in res_list:
print(res[0] +'\t'+ '{:7.3f}'.format(res[1]) +'\t'+ '{:7.3f}'.format(res[2]))
for label in angles_2_calc:
print('-----------------------------------------------')
print("Angle " + str(label))
print("File name " + "angle[deg] " + "k[kcal/mol*rad^2]")
res_list = results_label_wise[label]
for res in res_list:
print(res[0] +'\t'+ '{:7.3f}'.format(res[1]) +'\t'+ '{:7.3f}'.format(res[2]))