-
Notifications
You must be signed in to change notification settings - Fork 0
/
neb_snapshots.py
194 lines (167 loc) · 6.94 KB
/
neb_snapshots.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
import os
import sys
import shutil
import numpy as np
import matplotlib.pyplot as plt
import time
def read_spline_file(fname):
with open(fname) as input_data:
listi = []
images = []
spline = []
for i,line in enumerate(input_data):
line = line.strip()
if line == '\n' or not line:
listi.append('E')
else:
line=line.split()
listi.append(line)
if line[0] == 'Interp.:':
spline.append(i)
if line[0] == 'Images:':
images.append(i)
return listi, images, spline
def read_images(start_point, listi):
arclength = []
#energy = []
energy2 = []
index = start_point
while True:
if listi[index][0].strip().upper() == 'INTERP.:' or listi[index][0].strip().upper() == 'E':
break
arclength.append( float(listi[index][1]) )
#energy.append( float(listi[index][2]) )
energy2.append( float(listi[index][2])*627.509 )
index += 1
return arclength, energy2
def read_spline(start_point, listi):
arclength = []
#energy = []
energy2 = []
index = start_point
while True:
if listi[index][0].strip().upper() == 'ITERATION:' or listi[index][0].strip().upper() == 'E':
break
arclength.append( float(listi[index][1]) )
#energy.append( float(listi[index][2]) )
energy2.append( float(listi[index][2])*627.509 )
index += 1
if index == len(listi):
break
return arclength, energy2
if __name__ == "__main__":
"""
Script to generate quick 'energy-profile' trajectory from an ORCA NEB run
using python 3, numpy and matplotlib.
Usage: python neb_snapshots.py basename.interp start_at_iter<int> end_at_iter<int> full<bool>
(in the given order)
Original Authors: Vilhjalmur Asgeirsson, Benedikt Orri Birgirsson (UI, 2018)
email for bugs and requests: via9@hi.is, bob9@hi.is
"""
# ============================================
# Print header
# ============================================
print('==========================================')
print(' Optimization Profile: ORCA-NEB')
print('==========================================')
print('Modified by FX: 05.05.2023')
# ============================================
# set default values for input arguments
# ============================================
fname = 'orca.interp'
start_from = 0
end_at = -1
# ============================================
# get input arguments
# ============================================
# Notice that the ordering of the input arguments matter!
for i in range(1, len(sys.argv)):
if i == 1:
fname=sys.argv[i]
if not os.path.isfile(fname):
raise RuntimeError("Can not find file: %s", fname)
elif i == 2:
try:
start_from = int(sys.argv[i])
except:
raise TypeError("Invalid type for the second argument. Expecting int")
elif i == 3:
try:
end_at = int(sys.argv[i])
except:
raise TypeError("Invalid type for the third argument. Expecting int")
else:
raise RuntimeError("Too many input arguments. Usage: python neb_snapshots.py basename.interp start_at<int> end_at<int>")
print('=> looking at iteration %i to %i' % (start_from, end_at))
# - - - - - - - - - - - - - - - - - - - - - - -
# Let the plotting begin...
# - - - - - - - - - - - - - - - - - - - - - - -
# ==========================================================
# We read .interp file only once into 'listi'
# and the starting points of each 'images' and 'interp'
# sections in the file.
# =========================================================
listi, start_images, start_spline = read_spline_file(fname)
no_of_iters = len(start_spline)
if end_at == -1:
end_at = no_of_iters
# ==========================================================
# Make some checks...
# =========================================================
if len(start_images) != len(start_spline):
raise RuntimeError("Corrupt spline file!")
if start_from > no_of_iters or end_at > no_of_iters or start_from > end_at:
raise RuntimeError("The number of iterations in the .interp file is incorrect")
# ==========================================================
# Create dir. neb_frames (you can comment out this section)
# ==========================================================
path = os.getcwd()
working_dir = path+'/neb_frames'
if os.path.isdir(working_dir):
print('Directory %s found!' % working_dir)
print(' => Existing files are overwritten!')
else:
os.mkdir(working_dir)
print('Working dir: %s' % working_dir)
os.chdir(working_dir)
one_iter = False
if no_of_iters == 1:
if 'final' in fname.lower():
print('*** Note that %s contains only the last iteration of a NEB/CI-NEB run ***' % fname)
else:
print('%s contains only one iteration? ***' % fname)
one_iter = True
# ==========================================================
# Read and plot the spline and images of the .interp file
# ==========================================================
for i in range(start_from, end_at):
# read spline and images from list: listi
arcS, Eimg = read_images(start_images[i]+1, listi)
arcS2, Eimg2 = read_spline(start_spline[i]+1, listi)
if i == start_from:
# initial frame is black
plt.plot(arcS2, Eimg2, '-k')
plt.plot(arcS, Eimg, '.k', markersize=6.0)
elif i == end_at-1:
# final frame is red
plt.plot(arcS2, Eimg2, '-', color=[0.6, 0.0, 0.0],linewidth=1.5)
plt.plot(arcS, Eimg, '.',color=[0.6, 0.0, 0.0], markersize=8.0)
else:
# interm. frames are gray
plt.plot(arcS2, Eimg2, '-',color=[0.4, 0.4, 0.4])
plt.plot(arcS, Eimg, '.',color=[0.4, 0.4, 0.4], markersize=4.0)
# save whole profile
plt.xlabel("Displacement [Bohr]", fontsize=15)
plt.ylabel("Energy [kcal/mol]", fontsize=15)
plt.title( "Iter.: %i to %i" % (start_from, end_at-1) )
plt.savefig('neb_optimization.png')
# Make last iter.
plt.clf()
plt.plot(arcS2, Eimg2, '-',color=[0.6, 0.0, 0.0], linewidth=1.5)
plt.plot(arcS, Eimg, '.',color=[0.6, 0.0, 0.0], markersize=8.0)
plt.xlabel("Displacement [Bohr]",fontsize=15)
plt.ylabel("Energy [kcal/mol]", fontsize=15)
plt.savefig('neb_lastiter.png')
print('==========================================')
print('Execution terminated (see /neb_frames).')
print('==========================================')