firecode 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- firecode/TEST_NOTEBOOK.ipynb +3940 -0
- firecode/__init__.py +0 -0
- firecode/__main__.py +118 -0
- firecode/_gaussian.py +97 -0
- firecode/algebra.py +405 -0
- firecode/ase_manipulations.py +879 -0
- firecode/atropisomer_module.py +516 -0
- firecode/automep.py +130 -0
- firecode/calculators/__init__.py +29 -0
- firecode/calculators/_gaussian.py +98 -0
- firecode/calculators/_mopac.py +242 -0
- firecode/calculators/_openbabel.py +154 -0
- firecode/calculators/_orca.py +129 -0
- firecode/calculators/_xtb.py +786 -0
- firecode/concurrent_test.py +119 -0
- firecode/embedder.py +2590 -0
- firecode/embedder_options.py +577 -0
- firecode/embeds.py +881 -0
- firecode/errors.py +65 -0
- firecode/graph_manipulations.py +333 -0
- firecode/hypermolecule_class.py +364 -0
- firecode/mep_relaxer.py +199 -0
- firecode/modify_settings.py +186 -0
- firecode/mprof.py +65 -0
- firecode/multiembed.py +148 -0
- firecode/nci.py +186 -0
- firecode/numba_functions.py +260 -0
- firecode/operators.py +776 -0
- firecode/optimization_methods.py +609 -0
- firecode/parameters.py +84 -0
- firecode/pka.py +275 -0
- firecode/profiler.py +17 -0
- firecode/pruning.py +421 -0
- firecode/pt.py +32 -0
- firecode/quotes.json +6651 -0
- firecode/quotes.py +9 -0
- firecode/reactive_atoms_classes.py +666 -0
- firecode/references.py +11 -0
- firecode/rmsd.py +74 -0
- firecode/settings.py +75 -0
- firecode/solvents.py +126 -0
- firecode/tests/C2F2H4.xyz +10 -0
- firecode/tests/C2H4.xyz +8 -0
- firecode/tests/CH3Cl.xyz +7 -0
- firecode/tests/HCOOH.xyz +7 -0
- firecode/tests/HCOOOH.xyz +8 -0
- firecode/tests/chelotropic.txt +3 -0
- firecode/tests/cyclical.txt +3 -0
- firecode/tests/dihedral.txt +2 -0
- firecode/tests/string.txt +3 -0
- firecode/tests/trimolecular.txt +9 -0
- firecode/tests.py +151 -0
- firecode/torsion_module.py +1035 -0
- firecode/utils.py +541 -0
- firecode-1.0.0.dist-info/LICENSE +165 -0
- firecode-1.0.0.dist-info/METADATA +321 -0
- firecode-1.0.0.dist-info/RECORD +59 -0
- firecode-1.0.0.dist-info/WHEEL +5 -0
- firecode-1.0.0.dist-info/top_level.txt +1 -0
firecode/utils.py
ADDED
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
'''
|
|
3
|
+
FIRECODE: Filtering Refiner and Embedder for Conformationally Dense Ensembles
|
|
4
|
+
Copyright (C) 2021-2024 Nicolò Tampellini
|
|
5
|
+
|
|
6
|
+
SPDX-License-Identifier: LGPL-3.0-or-later
|
|
7
|
+
|
|
8
|
+
This program is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
This program is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with this program. If not, see
|
|
20
|
+
https://www.gnu.org/licenses/lgpl-3.0.en.html#license-text.
|
|
21
|
+
|
|
22
|
+
'''
|
|
23
|
+
|
|
24
|
+
import os
|
|
25
|
+
import sys
|
|
26
|
+
import time
|
|
27
|
+
from _tkinter import TclError
|
|
28
|
+
from shutil import rmtree
|
|
29
|
+
from subprocess import DEVNULL, STDOUT, CalledProcessError, check_call, run
|
|
30
|
+
|
|
31
|
+
import numpy as np
|
|
32
|
+
from cclib.io import ccread
|
|
33
|
+
from numpy.linalg import LinAlgError
|
|
34
|
+
|
|
35
|
+
from firecode.algebra import get_alignment_matrix, norm_of, rot_mat_from_pointer
|
|
36
|
+
from firecode.errors import TriangleError
|
|
37
|
+
from firecode.graph_manipulations import graphize
|
|
38
|
+
from firecode.pt import pt
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class suppress_stdout_stderr(object):
|
|
42
|
+
'''
|
|
43
|
+
A context manager for doing a "deep suppression" of stdout and stderr in
|
|
44
|
+
Python, i.e. will suppress all print, even if the print originates in a
|
|
45
|
+
compiled C/Fortran sub-function.
|
|
46
|
+
This will not suppress raised exceptions, since exceptions are printed
|
|
47
|
+
to stderr just before a script exits, and after the context manager has
|
|
48
|
+
exited (at least, I think that is why it lets exceptions through).
|
|
49
|
+
|
|
50
|
+
'''
|
|
51
|
+
def __init__(self):
|
|
52
|
+
# Open a pair of null files
|
|
53
|
+
self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]
|
|
54
|
+
# Save the actual stdout (1) and stderr (2) file descriptors.
|
|
55
|
+
self.save_fds = [os.dup(1), os.dup(2)]
|
|
56
|
+
|
|
57
|
+
def __enter__(self):
|
|
58
|
+
# Assign the null pointers to stdout and stderr.
|
|
59
|
+
os.dup2(self.null_fds[0],1)
|
|
60
|
+
os.dup2(self.null_fds[1],2)
|
|
61
|
+
|
|
62
|
+
def __exit__(self, *_):
|
|
63
|
+
# Re-assign the real stdout/stderr back to (1) and (2)
|
|
64
|
+
os.dup2(self.save_fds[0],1)
|
|
65
|
+
os.dup2(self.save_fds[1],2)
|
|
66
|
+
# Close all file descriptors
|
|
67
|
+
for fd in self.null_fds + self.save_fds:
|
|
68
|
+
os.close(fd)
|
|
69
|
+
|
|
70
|
+
class HiddenPrints:
|
|
71
|
+
def __enter__(self):
|
|
72
|
+
self._original_stdout = sys.stdout
|
|
73
|
+
sys.stdout = open(os.devnull, 'w')
|
|
74
|
+
|
|
75
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
76
|
+
sys.stdout.close()
|
|
77
|
+
sys.stdout = self._original_stdout
|
|
78
|
+
|
|
79
|
+
def clean_directory(to_remove=None):
|
|
80
|
+
|
|
81
|
+
if to_remove:
|
|
82
|
+
for name in to_remove:
|
|
83
|
+
try:
|
|
84
|
+
os.remove(name)
|
|
85
|
+
except IsADirectoryError:
|
|
86
|
+
rmtree(os.path.join(os.getcwd(), name))
|
|
87
|
+
except FileNotFoundError:
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
for f in os.listdir():
|
|
91
|
+
if f.split('.')[0] == 'temp':
|
|
92
|
+
try:
|
|
93
|
+
os.remove(f)
|
|
94
|
+
except IsADirectoryError:
|
|
95
|
+
rmtree(os.path.join(os.getcwd(), f))
|
|
96
|
+
except FileNotFoundError:
|
|
97
|
+
pass
|
|
98
|
+
elif f.startswith('temp_'):
|
|
99
|
+
try:
|
|
100
|
+
os.remove(f)
|
|
101
|
+
except IsADirectoryError:
|
|
102
|
+
rmtree(os.path.join(os.getcwd(), f))
|
|
103
|
+
except FileNotFoundError:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
def run_command(command:str, p=False):
|
|
107
|
+
if p:
|
|
108
|
+
print("Command: {}".format(command))
|
|
109
|
+
result = run(command.split(), shell=False, capture_output=True)
|
|
110
|
+
if result.stderr:
|
|
111
|
+
raise CalledProcessError(
|
|
112
|
+
returncode = result.returncode,
|
|
113
|
+
cmd = result.args,
|
|
114
|
+
stderr = result.stderr
|
|
115
|
+
)
|
|
116
|
+
if p and result.stdout:
|
|
117
|
+
print("Command Result: {}".format(result.stdout.decode('utf-8')))
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
def align_structures(structures:np.array, indices=None, **kwargs):
|
|
121
|
+
'''
|
|
122
|
+
Aligns molecules of a structure array (shape is (n_structures, n_atoms, 3))
|
|
123
|
+
to the first one, based on the indices. If not provided, all atoms are used
|
|
124
|
+
to get the best alignment. Return is the aligned array.
|
|
125
|
+
|
|
126
|
+
'''
|
|
127
|
+
|
|
128
|
+
reference = structures[0]
|
|
129
|
+
targets = structures[1:]
|
|
130
|
+
if isinstance(indices, (list, tuple)):
|
|
131
|
+
indices = np.array(indices)
|
|
132
|
+
|
|
133
|
+
indices = slice(0,len(reference)) if (indices is None or len(indices) == 0) else indices.ravel()
|
|
134
|
+
|
|
135
|
+
reference -= np.mean(reference[indices], axis=0)
|
|
136
|
+
for t, _ in enumerate(targets):
|
|
137
|
+
targets[t] -= np.mean(targets[t,indices], axis=0)
|
|
138
|
+
|
|
139
|
+
output = np.zeros(structures.shape)
|
|
140
|
+
output[0] = reference
|
|
141
|
+
|
|
142
|
+
for t, target in enumerate(targets):
|
|
143
|
+
|
|
144
|
+
try:
|
|
145
|
+
matrix = get_alignment_matrix(reference[indices], target[indices])
|
|
146
|
+
|
|
147
|
+
except LinAlgError:
|
|
148
|
+
# it is actually possible for the kabsch alg not to converge
|
|
149
|
+
matrix = np.eye(3)
|
|
150
|
+
|
|
151
|
+
# output[t+1] = np.array([matrix @ vector for vector in target])
|
|
152
|
+
output[t+1] = (matrix @ target.T).T
|
|
153
|
+
|
|
154
|
+
return output
|
|
155
|
+
|
|
156
|
+
def write_xyz(coords:np.array, atomnos:np.array, output, title='temp'):
|
|
157
|
+
'''
|
|
158
|
+
output is of _io.TextIOWrapper type
|
|
159
|
+
|
|
160
|
+
'''
|
|
161
|
+
assert atomnos.shape[0] == coords.shape[0]
|
|
162
|
+
assert coords.shape[1] == 3
|
|
163
|
+
string = ''
|
|
164
|
+
string += str(len(coords))
|
|
165
|
+
string += f'\n{title}\n'
|
|
166
|
+
for i, atom in enumerate(coords):
|
|
167
|
+
string += '%s % .6f % .6f % .6f\n' % (pt[atomnos[i]].symbol, atom[0], atom[1], atom[2])
|
|
168
|
+
output.write(string)
|
|
169
|
+
|
|
170
|
+
def read_xyz(filename):
|
|
171
|
+
'''
|
|
172
|
+
Wrapper for ccread. Raises an error if unsuccessful.
|
|
173
|
+
|
|
174
|
+
'''
|
|
175
|
+
mol = ccread(filename)
|
|
176
|
+
assert mol is not None, f'Reading molecule {filename} failed - check its integrity.'
|
|
177
|
+
return mol
|
|
178
|
+
|
|
179
|
+
def time_to_string(total_time: float, verbose=False, digits=1):
|
|
180
|
+
'''
|
|
181
|
+
Converts totaltime (float) to a timestring
|
|
182
|
+
with hours, minutes and seconds.
|
|
183
|
+
'''
|
|
184
|
+
timestring = ''
|
|
185
|
+
|
|
186
|
+
names = ('days', 'hours', 'minutes', 'seconds') if verbose else ('d', 'h', 'm', 's')
|
|
187
|
+
|
|
188
|
+
if total_time > 24*3600:
|
|
189
|
+
d = total_time // (24*3600)
|
|
190
|
+
timestring += f'{int(d)} {names[0]} '
|
|
191
|
+
total_time %= (24*3600)
|
|
192
|
+
|
|
193
|
+
if total_time > 3600:
|
|
194
|
+
h = total_time // 3600
|
|
195
|
+
timestring += f'{int(h)} {names[1]} '
|
|
196
|
+
total_time %= 3600
|
|
197
|
+
|
|
198
|
+
if total_time > 60:
|
|
199
|
+
m = total_time // 60
|
|
200
|
+
timestring += f'{int(m)} {names[2]} '
|
|
201
|
+
total_time %= 60
|
|
202
|
+
|
|
203
|
+
timestring += f'{round(total_time, digits):{2+digits}} {names[3]}'
|
|
204
|
+
|
|
205
|
+
return timestring
|
|
206
|
+
|
|
207
|
+
def pretty_num(n):
|
|
208
|
+
if n < 1e3:
|
|
209
|
+
return str(n)
|
|
210
|
+
if n < 1e6:
|
|
211
|
+
return str(round(n/1e3, 2)) + ' k'
|
|
212
|
+
return str(round(n/1e6, 2)) + ' M'
|
|
213
|
+
|
|
214
|
+
def loadbar(iteration, total, prefix='', suffix='', decimals=1, length=50, fill='#'):
|
|
215
|
+
percent = ('{0:.' + str(decimals) + 'f}').format(100 * (iteration/float(total)))
|
|
216
|
+
filledLength = int(length * iteration // total)
|
|
217
|
+
bar = fill * filledLength + '-' * (length - filledLength)
|
|
218
|
+
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end='\r')
|
|
219
|
+
if iteration == total:
|
|
220
|
+
print()
|
|
221
|
+
|
|
222
|
+
def cartesian_product(*arrays):
|
|
223
|
+
return np.stack(np.meshgrid(*arrays), -1).reshape(-1, len(arrays))
|
|
224
|
+
|
|
225
|
+
def rotation_matrix_from_vectors(vec1, vec2):
|
|
226
|
+
"""
|
|
227
|
+
Find the rotation matrix that aligns vec1 to vec2
|
|
228
|
+
:param vec1: A 3d "source" vector
|
|
229
|
+
:param vec2: A 3d "destination" vector
|
|
230
|
+
:return mat: A transform matrix (3x3) which when applied to vec1, aligns it with vec2.
|
|
231
|
+
|
|
232
|
+
"""
|
|
233
|
+
assert vec1.shape == (3,)
|
|
234
|
+
assert vec2.shape == (3,)
|
|
235
|
+
|
|
236
|
+
a, b = (vec1 / norm_of(vec1)).reshape(3), (vec2 / norm_of(vec2)).reshape(3)
|
|
237
|
+
v = np.cross(a, b)
|
|
238
|
+
if norm_of(v) != 0:
|
|
239
|
+
c = np.dot(a, b)
|
|
240
|
+
s = norm_of(v)
|
|
241
|
+
kmat = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]])
|
|
242
|
+
rotation_matrix = np.eye(3) + kmat + kmat.dot(kmat) * ((1 - c) / (s ** 2))
|
|
243
|
+
return rotation_matrix
|
|
244
|
+
|
|
245
|
+
# if the cross product is zero, then vecs must be parallel or perpendicular
|
|
246
|
+
if norm_of(a + b) == 0:
|
|
247
|
+
pointer = np.array([0,0,1])
|
|
248
|
+
return rot_mat_from_pointer(pointer, 180)
|
|
249
|
+
|
|
250
|
+
return np.eye(3)
|
|
251
|
+
|
|
252
|
+
def polygonize(lengths):
|
|
253
|
+
'''
|
|
254
|
+
Returns coordinates for the polygon vertices used in cyclical TS construction,
|
|
255
|
+
as a list of vector couples specifying starting and ending point of each pivot
|
|
256
|
+
vector. For bimolecular TSs, returns vertices for the centered superposition of
|
|
257
|
+
two segments. For trimolecular TSs, returns triangle vertices.
|
|
258
|
+
|
|
259
|
+
:params vertices: list of floats, used as polygon side lenghts.
|
|
260
|
+
:return vertices_out: list of vectors couples (start, end)
|
|
261
|
+
'''
|
|
262
|
+
assert len(lengths) in (2,3)
|
|
263
|
+
|
|
264
|
+
arr = np.zeros((len(lengths),2,3))
|
|
265
|
+
|
|
266
|
+
if len(lengths) == 2:
|
|
267
|
+
|
|
268
|
+
arr[0,0] = np.array([-lengths[0]/2,0,0])
|
|
269
|
+
arr[0,1] = np.array([+lengths[0]/2,0,0])
|
|
270
|
+
arr[1,0] = np.array([-lengths[1]/2,0,0])
|
|
271
|
+
arr[1,1] = np.array([+lengths[1]/2,0,0])
|
|
272
|
+
|
|
273
|
+
vertices_out = np.vstack(([arr],[arr]))
|
|
274
|
+
vertices_out[1,1] *= -1
|
|
275
|
+
|
|
276
|
+
else:
|
|
277
|
+
|
|
278
|
+
if not all([lengths[i] < lengths[i-1] + lengths[i-2] for i in (0,1,2)]):
|
|
279
|
+
raise TriangleError(f'Impossible to build a triangle with sides {lengths}')
|
|
280
|
+
# check that we can build a triangle with the specified vectors
|
|
281
|
+
|
|
282
|
+
arr[0,1] = np.array([lengths[0],0,0])
|
|
283
|
+
arr[1,0] = np.array([lengths[0],0,0])
|
|
284
|
+
|
|
285
|
+
a = np.power(lengths[0], 2)
|
|
286
|
+
b = np.power(lengths[1], 2)
|
|
287
|
+
c = np.power(lengths[2], 2)
|
|
288
|
+
x = (a-b+c)/(2*a**0.5)
|
|
289
|
+
y = (c-x**2)**0.5
|
|
290
|
+
|
|
291
|
+
arr[1,1] = np.array([x,y,0])
|
|
292
|
+
arr[2,0] = np.array([x,y,0])
|
|
293
|
+
|
|
294
|
+
vertices_out = np.vstack(([arr],[arr],[arr],[arr],
|
|
295
|
+
[arr],[arr],[arr],[arr]))
|
|
296
|
+
|
|
297
|
+
swaps = [(1,2),(2,1),(3,1),(3,2),(4,0),(5,0),(5,1),(6,0),(6,2),(7,0),(7,1),(7,2)]
|
|
298
|
+
|
|
299
|
+
for t,v in swaps:
|
|
300
|
+
# triangle, vector couples to be swapped
|
|
301
|
+
vertices_out[t,v][[0,1]] = vertices_out[t,v][[1,0]]
|
|
302
|
+
|
|
303
|
+
return vertices_out
|
|
304
|
+
|
|
305
|
+
def ase_view(mol):
|
|
306
|
+
'''
|
|
307
|
+
Display an Hypermolecule instance from the ASE GUI
|
|
308
|
+
'''
|
|
309
|
+
from ase import Atoms
|
|
310
|
+
from ase.gui.gui import GUI
|
|
311
|
+
from ase.gui.images import Images
|
|
312
|
+
|
|
313
|
+
if hasattr(mol, 'reactive_atoms_classes_dict'):
|
|
314
|
+
images = []
|
|
315
|
+
|
|
316
|
+
for c, coords in enumerate(mol.atomcoords):
|
|
317
|
+
centers = np.vstack([atom.center for atom in mol.reactive_atoms_classes_dict[c].values()])
|
|
318
|
+
atomnos = np.concatenate((mol.atomnos, [0 for _ in centers]))
|
|
319
|
+
totalcoords = np.concatenate((coords, centers))
|
|
320
|
+
images.append(Atoms(atomnos, positions=totalcoords))
|
|
321
|
+
|
|
322
|
+
else:
|
|
323
|
+
images = [Atoms(mol.atomnos, positions=coords) for coords in mol.atomcoords]
|
|
324
|
+
|
|
325
|
+
try:
|
|
326
|
+
GUI(images=Images(images), show_bonds=True).run()
|
|
327
|
+
except TclError:
|
|
328
|
+
print('--> GUI not available from command line interface. Skipping it.')
|
|
329
|
+
|
|
330
|
+
double_bonds_thresholds_dict = {
|
|
331
|
+
'CC':1.4,
|
|
332
|
+
'CN':1.3,
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
def get_double_bonds_indices(coords, atomnos):
|
|
336
|
+
'''
|
|
337
|
+
Returns a list containing 2-elements tuples
|
|
338
|
+
of indices involved in any double bond
|
|
339
|
+
'''
|
|
340
|
+
mask = (atomnos != 1)
|
|
341
|
+
numbering = np.arange(len(coords))[mask]
|
|
342
|
+
coords = coords[mask]
|
|
343
|
+
atomnos = atomnos[mask]
|
|
344
|
+
output = []
|
|
345
|
+
|
|
346
|
+
for i1, _ in enumerate(coords):
|
|
347
|
+
for i2 in range(i1+1, len(coords)):
|
|
348
|
+
dist = norm_of(coords[i1] - coords[i2])
|
|
349
|
+
tag = ''.join(sorted([pt[atomnos[i1]].symbol,
|
|
350
|
+
pt[atomnos[i2]].symbol]))
|
|
351
|
+
|
|
352
|
+
threshold = double_bonds_thresholds_dict.get(tag)
|
|
353
|
+
if threshold is not None and dist < threshold:
|
|
354
|
+
output.append((numbering[i1], numbering[i2]))
|
|
355
|
+
|
|
356
|
+
return output
|
|
357
|
+
|
|
358
|
+
def get_scan_peak_index(energies, max_thr=50, min_thr=0.1):
|
|
359
|
+
'''
|
|
360
|
+
Returns the index of the energies iterable that
|
|
361
|
+
corresponds to the most prominent peak.
|
|
362
|
+
'''
|
|
363
|
+
_l = len(energies)
|
|
364
|
+
peaks = [i for i in range(_l) if (
|
|
365
|
+
|
|
366
|
+
energies[i-1] < energies[i] >= energies[(i+1)%_l] and
|
|
367
|
+
max_thr > energies[i] > min_thr
|
|
368
|
+
# discard peaks that are too small or too big
|
|
369
|
+
)]
|
|
370
|
+
|
|
371
|
+
if not peaks:
|
|
372
|
+
return energies.index(max(energies))
|
|
373
|
+
# if no peaks are present, return the highest
|
|
374
|
+
|
|
375
|
+
if len(peaks) == 1:
|
|
376
|
+
return peaks[0]
|
|
377
|
+
# if one is present, return that
|
|
378
|
+
|
|
379
|
+
peaks_nrg = [energies[i] for i in peaks]
|
|
380
|
+
return energies.index(max(peaks_nrg))
|
|
381
|
+
# if more than one, return the highest
|
|
382
|
+
|
|
383
|
+
def molecule_check(old_coords, new_coords, atomnos, max_newbonds=0):
|
|
384
|
+
'''
|
|
385
|
+
Checks if two molecules have the same bonds between the same atomic indices
|
|
386
|
+
'''
|
|
387
|
+
old_bonds = {(a, b) for a, b in list(graphize(old_coords, atomnos).edges) if a != b}
|
|
388
|
+
new_bonds = {(a, b) for a, b in list(graphize(new_coords, atomnos).edges) if a != b}
|
|
389
|
+
|
|
390
|
+
delta_bonds = (old_bonds | new_bonds) - (old_bonds & new_bonds)
|
|
391
|
+
|
|
392
|
+
if len(delta_bonds) > max_newbonds:
|
|
393
|
+
return False
|
|
394
|
+
|
|
395
|
+
return True
|
|
396
|
+
|
|
397
|
+
def scramble_check(TS_structure, TS_atomnos, excluded_atoms, mols_graphs, max_newbonds=0, logfunction=None, title=None) -> bool:
|
|
398
|
+
'''
|
|
399
|
+
Check if a multimolecular arrangement has scrambled during some optimization
|
|
400
|
+
steps. If more than a given number of bonds changed (formed or broke) the
|
|
401
|
+
structure is considered scrambled, and the method returns False.
|
|
402
|
+
'''
|
|
403
|
+
assert len(TS_structure) == sum([len(graph.nodes) for graph in mols_graphs])
|
|
404
|
+
|
|
405
|
+
bonds = set()
|
|
406
|
+
for i, graph in enumerate(mols_graphs):
|
|
407
|
+
|
|
408
|
+
pos = sum([len(other_graph.nodes) for j, other_graph in enumerate(mols_graphs) if j < i])
|
|
409
|
+
|
|
410
|
+
for bond in [tuple(sorted((a+pos, b+pos))) for a, b in list(graph.edges) if a != b]:
|
|
411
|
+
bonds.add(bond)
|
|
412
|
+
# creating bond set containing all bonds present in the desired transition state
|
|
413
|
+
|
|
414
|
+
new_bonds = {tuple(sorted((a, b))) for a, b in list(graphize(TS_structure, TS_atomnos).edges) if a != b}
|
|
415
|
+
delta_bonds = (bonds | new_bonds) - (bonds & new_bonds)
|
|
416
|
+
# delta_bonds -= {tuple(sorted(pair)) for pair in constrained_indices}
|
|
417
|
+
|
|
418
|
+
for bond in delta_bonds.copy():
|
|
419
|
+
for a in excluded_atoms:
|
|
420
|
+
if a in bond:
|
|
421
|
+
delta_bonds -= {bond}
|
|
422
|
+
# removing bonds involving constrained atoms: they are not counted as scrambled bonds
|
|
423
|
+
|
|
424
|
+
if len(delta_bonds) > max_newbonds:
|
|
425
|
+
if logfunction is not None:
|
|
426
|
+
logfunction(f"{title}, scramble_check - found {len(delta_bonds)} extra bonds: {delta_bonds}")
|
|
427
|
+
return False
|
|
428
|
+
|
|
429
|
+
return True
|
|
430
|
+
|
|
431
|
+
def rotate_dihedral(coords, dihedral, angle, mask=None, indices_to_be_moved=None):
|
|
432
|
+
'''
|
|
433
|
+
Rotate a molecule around a given bond.
|
|
434
|
+
Atoms that will move are the ones
|
|
435
|
+
specified by mask or indices_to_be_moved.
|
|
436
|
+
If both are None, only the first index of
|
|
437
|
+
the dihedral iterable is moved.
|
|
438
|
+
|
|
439
|
+
angle: angle, in degrees
|
|
440
|
+
'''
|
|
441
|
+
|
|
442
|
+
i1, i2, i3 ,_ = dihedral
|
|
443
|
+
|
|
444
|
+
if indices_to_be_moved is not None:
|
|
445
|
+
mask = np.array([i in indices_to_be_moved for i, _ in enumerate(coords)])
|
|
446
|
+
|
|
447
|
+
if mask is None:
|
|
448
|
+
mask = i1
|
|
449
|
+
|
|
450
|
+
axis = coords[i2] - coords[i3]
|
|
451
|
+
mat = rot_mat_from_pointer(axis, angle)
|
|
452
|
+
center = coords[i3]
|
|
453
|
+
|
|
454
|
+
coords[mask] = (mat @ (coords[mask] - center).T).T + center
|
|
455
|
+
|
|
456
|
+
return coords
|
|
457
|
+
|
|
458
|
+
def flatten(array, typefunc=float):
|
|
459
|
+
out = []
|
|
460
|
+
def rec(_l):
|
|
461
|
+
for e in _l:
|
|
462
|
+
if type(e) in [list, tuple, np.ndarray]:
|
|
463
|
+
rec(e)
|
|
464
|
+
else:
|
|
465
|
+
out.append(typefunc(e))
|
|
466
|
+
rec(array)
|
|
467
|
+
return out
|
|
468
|
+
|
|
469
|
+
def auto_newline(string, max_line_len=50, padding=2):
|
|
470
|
+
string = str(string)
|
|
471
|
+
|
|
472
|
+
out = [' '*padding]
|
|
473
|
+
line_len = 0
|
|
474
|
+
for word in string.split():
|
|
475
|
+
out.append(word)
|
|
476
|
+
line_len += len(word) + 1
|
|
477
|
+
|
|
478
|
+
if line_len >= max_line_len:
|
|
479
|
+
out.append('\n'+' '*padding)
|
|
480
|
+
line_len = 0
|
|
481
|
+
|
|
482
|
+
return ' '.join(out)
|
|
483
|
+
|
|
484
|
+
def smi_to_3d(smi, new_filename):
|
|
485
|
+
with open("temp_smi.txt", "w") as f:
|
|
486
|
+
f.write(smi)
|
|
487
|
+
|
|
488
|
+
check_call(f'obabel -i smi temp_smi.txt -o xyz -O {new_filename}.xyz -h --gen3d'.split(), stdout=DEVNULL, stderr=STDOUT)
|
|
489
|
+
# data = read_xyz(f"{new_filename}.xyz")
|
|
490
|
+
clean_directory(["temp_smi.txt"])
|
|
491
|
+
|
|
492
|
+
return new_filename + ".xyz"
|
|
493
|
+
|
|
494
|
+
def timing_wrapper(function, *args, payload=None, **kwargs):
|
|
495
|
+
'''
|
|
496
|
+
Generic function wrapper that appends the
|
|
497
|
+
execution time at the end of return.
|
|
498
|
+
If payload is not None, appends it at the end
|
|
499
|
+
of the function return, before the elapsed time.
|
|
500
|
+
|
|
501
|
+
'''
|
|
502
|
+
start_time = time.perf_counter()
|
|
503
|
+
func_return = function(*args, **kwargs)
|
|
504
|
+
elapsed = time.perf_counter() - start_time
|
|
505
|
+
|
|
506
|
+
if payload is None:
|
|
507
|
+
return func_return, elapsed
|
|
508
|
+
|
|
509
|
+
return func_return, payload, elapsed
|
|
510
|
+
|
|
511
|
+
def _saturation_check(atomnos, charge=0):
|
|
512
|
+
|
|
513
|
+
transition_metals = [
|
|
514
|
+
"Sc", "Ti", "V", "Cr", "Mn", "Fe",
|
|
515
|
+
"Co", "Ni", "Cu", "Zn", "Y", "Zr",
|
|
516
|
+
"Nb", "Mo", "Tc", "Ru", "Rh", "Pd",
|
|
517
|
+
"Ag", "Cd", "La", "Ce", "Pr", "Nd",
|
|
518
|
+
"Pm", "Sm", "Eu", "Gd", "Tb", "Dy",
|
|
519
|
+
"Ho", "Er", "Tm", "Yb", "Lu", "Hf",
|
|
520
|
+
"Ta", "W", "Re", "Os", "Ir", "Pt",
|
|
521
|
+
"Au", "Hg", "Th", "Pa", "U", "Np",
|
|
522
|
+
"Pu", "Am",
|
|
523
|
+
]
|
|
524
|
+
|
|
525
|
+
# if we have any transition metal, it's hard to tell
|
|
526
|
+
# if the structure looks ok: in this case we assume it is.
|
|
527
|
+
organometallic = any([pt[a].symbol in transition_metals for a in atomnos])
|
|
528
|
+
|
|
529
|
+
odd_valent = [ #1 valent
|
|
530
|
+
"H", "Li", "Na", "K", "Rb", "Cs",
|
|
531
|
+
"F", "Cl", "Br", "I", "At",
|
|
532
|
+
|
|
533
|
+
# 3/5 valent
|
|
534
|
+
"N", "P", "As", "Sb", "Bi",
|
|
535
|
+
"B", "Al", "Ga", "In", "Tl",
|
|
536
|
+
]
|
|
537
|
+
|
|
538
|
+
n_odd_valent = sum([1 for a in atomnos if pt[a].symbol in odd_valent])
|
|
539
|
+
looks_ok = ((n_odd_valent + charge) / 2) % 1 < 0.001 if not organometallic else True
|
|
540
|
+
|
|
541
|
+
return looks_ok
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|