-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npa_util.py
executable file
·56 lines (48 loc) · 1.67 KB
/
npa_util.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys, argparse
parser = argparse.ArgumentParser(description='Extracts NPA charges from TURBOMOLE log files.')
parser.add_argument('LogFile', metavar='tm.log', help='The TURBOMOLE log file')
parser.add_argument('ATOMS', metavar='ATOMS', help='The list of atoms, e.g. 1,2,4,9')
parser.add_argument('-a', '--add', dest='Sum', action='store_true', help='adds the charges of the given atoms')
parser.add_argument('-s', '--spin', dest='Spin', action='store_true', help='uses NPA spin density')
args = parser.parse_args()
# check for file
if not os.path.isfile(args.LogFile):
sys.exit("Error! File '%s' not found." % ( args.LogFile ))
# load log file
try:
with open(args.LogFile) as f:
rawlines = f.read().splitlines()
except:
sys.exit("Error! Cannot read file '%s'." % ( args.LogFile ))
try:
line_to_find = "atomic populations from spin density:" if args.Spin else "atomic populations from total density:"
idx = rawlines.index(line_to_find) + 1
except:
sys.exit("Error! The TURBOMOLE log file does not contain a natural population analysis.")
# generate atom list
atoms = []
try:
for atom in str(args.ATOMS).split(','):
atoms.append(int(atom))
except:
sys.exit("Error! Cannot read the given atom list.")
# read the lines of interest
lines = []
while idx < len(rawlines):
idx += 1
if rawlines[idx].strip() == '':
break
lines.append(rawlines[idx])
# run mode
if args.Sum:
# print sum charge
charge = 0.0
for atom in atoms:
charge += float(lines[atom-1].split()[2])
print(charge)
else:
# print charges
for atom in atoms:
print(lines[atom-1])