forked from sannabenter/JEDI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjedi.py
More file actions
182 lines (138 loc) · 6.12 KB
/
jedi.py
File metadata and controls
182 lines (138 loc) · 6.12 KB
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
# Main routine of the JEDI analysis.
import numpy as np
import os
import argparse
import random # needed for quotes
# Initializing variables for optional arguments
directory_flag = False # Only if the user specifies the name of a directory, this is set to true
vmd_flag = True # Generate tcl scripts for generating color-coded structures in VMD by default
man_strain = None # Set an optional maximum strain for VMD to None by default
modus = None
# Check if any optional positional arguments are given
parser = argparse.ArgumentParser()
parser.add_argument('--d', help = "Please specify a directory after the --d flag.")
parser.add_argument('--v', nargs = '+', help = "Either specify a maximum strain and a modus or call with 'n' if you wish no vmd analysis.")
args = parser.parse_args()
# turn flags to variables and set to True or False
if args.d:
directory_flag = True
directory = args.d
else:
directory = None
if args.v:
if args.v == ['n']:
vmd_flag = False
else:
man_strain = args.v[0]
modus = args.v[1]
if __name__ == '__main__':
print("\n Preparing the JEDI analysis...", "\n")
from jedi_files import energies_file # import energies_file marker (True if E_geoms.txt exists)
from jedi_directory import *
import jedi_rims
# run kill_atoms.py, to generate a list of which atoms should be ignored in JEDI; continuing jedi.py if no atoms should be deleted.
from jedi_kill_atoms import RIM_type
# import list of atoms, that should be ignored in JEDI-Analysis and updated x0, xF
# run b_mat.py, to calculate the B-Matrix.
import jedi_b
# run delta_q.py and calculate q0, qF and delta_q
from jedi_delta_q import delta_q
##### Read in multiple files generated from imported scripts: #####
# Transposed B-Matrix from b_mat.txt (generated from b_mat.py)
# Hessian in cartesian coordinates from H_Cart.txt or H_Cart_manip.txt (in case atoms are deleted in kill_atoms.txt) (generated from hessian.py)
# Read the B-Matrix
B = np.loadtxt('b_mat.txt')
B_transp = np.transpose(B)
if os.path.exists("H_Cart_manip.txt"):
with open("H_Cart_manip.txt", "r") as H_Cart_file:
H_cart = np.loadtxt('H_Cart_manip.txt')
elif os.path.exists("H_Cart.txt"):
with open("H_Cart.txt", "r") as H_Cart_file:
H_cart = np.loadtxt('H_Cart.txt')
H_Cart_file.close()
if energies_file == True:
with open('E_geoms.txt', 'r') as energies_file:
all_E_geometries = np.loadtxt(energies_file)
E_geometries = all_E_geometries[0]
energies_file.close()
###########################
## Matrix Calculations ##
###########################
# Calculate the number of RIMs (= number of rows in the B-Matrix), equivalent to number of redundant internal coordinates
NRIMs = int(len(RIM_type))
# Calculate the pseudoinverse of the B-Matrix and its transposed (take care of diatomic molecules specifically)
if B.ndim == 1:
B_plus = B_transp/2
B_transp_plus = B/2
else:
B_plus = np.linalg.pinv(B, 0.0001)
B_transp_plus = np.linalg.pinv( np.transpose(B),0.0001 )
# Calculate the P-Matrix (eq. 4 in Helgaker's paper)
P = np.dot(B, B_plus)
#############################################
# JEDI analysis #
#############################################
# Calculate the Hessian in RIMs (take care to get the correct multiplication for a diatomic molecule
if B.ndim == 1:
H_q = B_transp_plus.dot( H_cart ).dot( B_plus )
else:
H_q = P.dot( B_transp_plus ).dot( H_cart ).dot( B_plus ).dot( P )
# Calculate the total energies in RIMs and its deviation from E_geometries
E_RIMs_total = 0.5 * np.transpose( delta_q ).dot( H_q ).dot( delta_q )
proc_geom_RIMs = 100 * ( E_RIMs_total - E_geometries ) / E_geometries
# Get the energy stored in every RIM (take care to get the right multiplication for a diatomic molecule)
E_RIMs = []
if B.ndim == 1:
E_current = 0.5 * delta_q[0] * H_q * delta_q[0]
E_RIMs.append(E_current)
else:
for i in range(NRIMs):
E_current = 0
for j in range(NRIMs):
E_current += 0.5 * delta_q[i] * H_q[i,j] * delta_q[j]
E_RIMs.append(E_current)
# Get the percentage of the energy stored in every RIM
proc_E_RIMs = []
for i in range(NRIMs):
proc_E_RIMs.append( 100 * E_RIMs[i] / E_RIMs_total )
#############################################
# Output section #
#############################################
# Header
print("\n \n")
print(" ************************************************")
print(" * JEDI ANALYSIS *")
print(" * Judgement of Energy DIstribution *")
print(" ************************************************\n")
# Comparison of total energies
print(" Strain Energy (h) Deviation (%)")
print(" Geometries " + "%.8f" % E_geometries + " -" )
print('%5s%16.8f%14.2f' % (" Red. Int. Modes", E_RIMs_total, proc_geom_RIMs))
# JEDI analysis
print("\n RIM No. RIM type delta_q (a.u.) Percentage Energy (h)")
for i in range(NRIMs):
print('%6i%7s%-18s%17.7f%9.1f%17.7f' % (i+1, " ", RIM_type[i], delta_q[i], proc_E_RIMs[i], E_RIMs[i]))
# Write the energies to a file
f = open('energies.txt', 'w')
f.write('{} {}\n'.format(E_geometries, E_RIMs_total))
f.close()
# Write the parts of the energy stored in the RIMs to a file
f = open('E_RIMs.txt', 'w')
for i in E_RIMs:
f.write("%s\n" % i)
f.close()
# Write the percentages of the energy stored in the RIMs to a file
f = open('E_RIMs_perc.txt', 'w')
for i in proc_E_RIMs:
f.write("%s\n" % i)
f.close()
if vmd_flag == True:
pass
import vmd_gen
# Fairwell messages
print("\n")
print("\nJEDI terminated successfully.\n")
quotes = open('jedi_quotes-library.txt').read().splitlines()
quote = random.choice(quotes)
print(quote)
print("\n")