forked from berquist/personal_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orca_generate_plot_strings.py
executable file
·193 lines (159 loc) · 6.16 KB
/
orca_generate_plot_strings.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
#!/usr/bin/env python
'''Generate an ORCA input file specifically for plotting cube files.
Usage:
orca_generate_plot_strings.py [options] [--canon=canon_list] [--uno=uno_list]
If canon_list or uno_list consists of two numbers, all cubes in that range
(inclusive) will be generated.
Options:
--prefix=PREFIX Append a prefix to all generated files.
--eldens Generate the electron density.
--spindens Generate the spin density.
--beta Orbital file contains separate beta spins/orbitals.
--dim=DIM Number of points in each dimension. [default: 40]
--cclib=OUTFILE Use cclib to determine occupied/virtual MO ranges from output file. Generate 2*NOcc cubes.
--cclib_all If `--cclib`, plot all possible MOs.
--max=MAX Don't generate any cube files after MAX orbital.
--print_args Print the parsed argument block.
Examples:
orca_generate_plot_strings.py --spindens --beta --uno=0,4,5,6,7 --prefix='example' --canon=1,2,10
outputs:
%plots
format gaussian_cube
dim1 40
dim2 40
dim3 40
spindens("example.density.spin.cube");
uno("example.uno.0.cube", 0);
uno("example.uno.4.cube", 4);
uno("example.uno.5.cube", 5);
uno("example.uno.6.cube", 6);
uno("example.uno.7.cube", 7);
mo("example.mo.01a.cube", 1, 0);
mo("example.mo.01b.cube", 1, 1);
mo("example.mo.02a.cube", 2, 0);
mo("example.mo.02b.cube", 2, 1);
mo("example.mo.10a.cube", 10, 0);
mo("example.mo.10b.cube", 10, 1);
end
'''
from __future__ import print_function
from vmd_templates import pad_left_zeros_l
def mo_string(prefix, mo_num, op_num):
'''Create the string for generating a cube of a molecular orbital.'''
if op_num == 0:
spin = 'a'
elif op_num == 1:
spin = 'b'
else:
return
template = 'mo("{prefix}mo.{mo_num}{spin}.cube", {mo_num}, {op_num});'
return template.format(prefix=prefix,
mo_num=mo_num,
op_num=op_num,
spin=spin)
def uno_string(prefix, uno_num):
'''Create the string for generating a cube of a UHF natural orbital.'''
template = 'uno("{prefix}uno.{uno_num}.cube", {uno_num});'
return template.format(prefix=prefix,
uno_num=uno_num)
def eldens_string(prefix):
'''Create the string for generating a cube of the electron density.'''
template = 'eldens("{prefix}density.el.cube");'
return template.format(prefix=prefix)
def spindens_string(prefix):
'''Create the string for generating a cube of the spin density.'''
template = 'spindens("{prefix}density.spin.cube");'
return template.format(prefix=prefix)
def arg_to_list(arg):
'''Convert the given argument to a list of a single element if it's an atom,
'''
if isinstance(arg, str):
newarg = eval(arg)
if isinstance(newarg, int):
newarg = [newarg]
return newarg
elif isinstance(arg, list):
return arg
else:
# we might be trouble if this ever gets reached...
return list(arg)
def generate_block(args):
'''Create the %plots block based upon command-line arguments passed in
through the args dictionary.
'''
# Handle the file prefix first.
if args['--prefix'] is None:
prefix = ''
else:
prefix = args['--prefix'] + '.'
# The "block" will be formed by generating bunch of strings,
# appending them to this list, then calling list.join once
# everything's done.
block_parts = ['%plots']
block_parts.append(' format gaussian_cube')
block_parts.append(' dim1 {}'.format(args['--dim']))
block_parts.append(' dim2 {}'.format(args['--dim']))
block_parts.append(' dim3 {}'.format(args['--dim']))
# If we desire to use cclib and automate some things...
if args['--cclib']:
# pylint: disable=E1101
from cclib.io import ccopen
job = ccopen(args['--cclib'])
data = job.parse()
plot_range = data.homos[0] * 2
if args['--cclib_all']:
plot_range = data.nmo
# cclib-discovered values take precedence
if args['--canon']:
args['--canon'] = list(range(plot_range))
if args['--uno']:
args['--uno'] = list(range(plot_range))
if args['--eldens']:
block_parts.append(' ' + eldens_string(prefix))
if args['--spindens']:
block_parts.append(' ' + spindens_string(prefix))
# Limit the number of orbitals we're going to generate.
if args['--max']:
maxorb = int(args['--max'])
if args['--canon']:
args['--canon'] = [i for i in args['--canon'] if i <= maxorb]
if args['--uno']:
args['--uno'] = [i for i in args['--uno'] if i <= maxorb]
# Plot the UNOs first due to an 'operator' bug in ORCA.
if args['--uno']:
# We're always either a string or a list.
if isinstance(args['--uno'], str):
splitstr = args['--uno'].split(',')
else:
splitstr = args['--uno']
if len(splitstr) == 2:
args['--uno'] = pad_left_zeros_l(range(int(splitstr[0]), int(splitstr[1]) + 1))
else:
args['--uno'] = pad_left_zeros_l(arg_to_list(args['--uno']))
for uno_num in args['--uno']:
block_parts.append(' ' + uno_string(prefix, uno_num))
if args['--canon']:
# We're always either a string or a list.
if isinstance(args['--canon'], str):
splitstr = args['--canon'].split(',')
else:
splitstr = args['--canon']
if len(splitstr) == 2:
args['--canon'] = pad_left_zeros_l(range(int(splitstr[0]), int(splitstr[1]) + 1))
else:
args['--canon'] = pad_left_zeros_l(arg_to_list(args['--canon']))
for mo_num in args['--canon']:
block_parts.append(' ' + mo_string(prefix, mo_num, 0))
if args['--beta']:
block_parts.append(' ' + mo_string(prefix, mo_num, 1))
block_parts.append(' end')
block = '\n'.join(block_parts)
return block
if __name__ == '__main__':
from docopt import docopt
# pylint: disable=C0103
args = docopt(__doc__)
if args['--print_args']:
print(args)
block = generate_block(args)
print(block)