Debian Codegraph @

cclib/parser/mopacparser.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2017, the cclib development team
4 #
5 # This file is part of cclib (http://cclib.github.io) and is distributed under
6 # the terms of the BSD 3-Clause License.
7
8 """Parser for MOPAC output files"""
9
10 # Based on parser in RMG-Py by Greg Magoon
11 # https://github.com/ReactionMechanismGenerator/RMG-Py/blob/master/external/cclib/parser/mopacparser.py
12 # Also parts from Ben Albrecht
13 # https://github.com/ben-albrecht/cclib/blob/master/cclib/parser/mopacparser.py
14 # Merged and modernized by Geoff Hutchison
15
16 from __future__ import print_function
17
18 import re
19 import math
20
21 import numpy
22
23 from cclib.parser import data
24 from cclib.parser import logfileparser
25 from cclib.parser import utils
26
27
28 def symbol2int(symbol):
29 t = utils.PeriodicTable()
30 return t.number[symbol]
31
32 class MOPAC(logfileparser.Logfile):
33 """A MOPAC20XX output file."""
34
35 def __init__(self, *args, **kwargs):
36
37 # Call the __init__ method of the superclass
38 super(MOPAC, self).__init__(logname="MOPAC", *args, **kwargs)
39
40 def __str__(self):
41 """Return a string representation of the object."""
42 return "MOPAC log file %s" % (self.filename)
43
44 def __repr__(self):
45 """Return a representation of the object."""
46 return 'MOPAC("%s")' % (self.filename)
47
48 def normalisesym(self, label):
49 """MOPAC does not require normalizing symmetry labels."""
50 return label
51
52 def before_parsing(self):
53 #TODO
54
55 # Defaults
56 charge = 0
57 self.set_attribute('charge', charge)
58 mult = 1
59 self.set_attribute('mult', mult)
60
61 # Keep track of whether or not we're performing an
62 # (un)restricted calculation.
63 self.unrestricted = False
64 self.is_rohf = False
65
66 # Keep track of 1SCF vs. gopt since gopt is default
67 self.onescf = False
68 self.geomdone = False
69
70 # Compile the dashes-and-or-spaces-only regex.
71 self.re_dashes_and_spaces = re.compile('^[\s-]+$')
72
73 self.star = ' * '
74 self.stars = ' *******************************************************************************'
75
76 self.spinstate = {'SINGLET': 1,
77 'DOUBLET': 2,
78 'TRIPLET': 3,
79 'QUARTET': 4,
80 'QUINTET': 5,
81 'SEXTET': 6,
82 'HEPTET': 7,
83 'OCTET': 8,
84 'NONET': 9}
85
86 def extract(self, inputfile, line):
87 """Extract information from the file object inputfile."""
88
89 if "(Version:" in line:
90 # Part of the full version can be extracted from here, but is
91 # missing information about the bitness.
92 package_version = line[line.find("MOPAC") + 5:line.find("(")]
93 package_version = package_version[:4]
94 if "BETA" in line:
95 package_version = package_version + " BETA"
96 self.metadata["package_version"] = package_version
97
98 # Don't use the full package version until we know its field
99 # yet.
100 if "For non-commercial use only" in line:
101 tokens = line.split()
102 tokens = tokens[8:]
103 assert len(tokens) == 2
104 package_version_full = tokens[0]
105 if tokens[1] != "**":
106 package_version_full = '-'.join(tokens)[:-2]
107
108 # Extract the atomic numbers and coordinates from the optimized geometry
109 # note that cartesian coordinates section occurs multiple times in the file, and we want to end up using the last instance
110 # also, note that the section labeled cartesian coordinates doesn't have as many decimal places as the one used here
111 # Example 1 (not used):
112 # CARTESIAN COORDINATES
113 #
114 # NO. ATOM X Y Z
115 #
116 # 1 O 4.7928 -0.8461 0.3641
117 # 2 O 5.8977 -0.3171 0.0092
118 # ...
119 # Example 2 (used):
120 # ATOM CHEMICAL X Y Z
121 # NUMBER SYMBOL (ANGSTROMS) (ANGSTROMS) (ANGSTROMS)
122 #
123 # 1 O 4.79280259 * -0.84610232 * 0.36409474 *
124 # 2 O 5.89768035 * -0.31706418 * 0.00917035 *
125 # ... etc.
126 if line.split() == ["NUMBER", "SYMBOL", "(ANGSTROMS)", "(ANGSTROMS)", "(ANGSTROMS)"]:
127
128 self.updateprogress(inputfile, "Attributes", self.cupdate)
129
130 self.inputcoords = []
131 self.inputatoms = []
132
133 blankline = inputfile.next()
134
135 atomcoords = []
136 line = inputfile.next()
137 while len(line.split()) > 6:
138 # MOPAC Version 14.019L 64BITS suddenly appends this block with
139 # "CARTESIAN COORDINATES" block with no blank line.
140 tokens = line.split()
141 self.inputatoms.append(symbol2int(tokens[1]))
142 xc = float(tokens[2])
143 yc = float(tokens[4])
144 zc = float(tokens[6])
145 atomcoords.append([xc, yc, zc])
146 line = inputfile.next()
147
148 self.inputcoords.append(atomcoords)
149
150 if not hasattr(self, "natom"):
151 self.atomnos = numpy.array(self.inputatoms, 'i')
152 self.natom = len(self.atomnos)
153
154 if 'CHARGE ON SYSTEM =' in line:
155 charge = int(line.split()[5])
156 self.set_attribute('charge', charge)
157
158 if 'SPIN STATE DEFINED' in line:
159 # find the multiplicity from the line token (SINGLET, DOUBLET, TRIPLET, etc)
160 mult = self.spinstate[line.split()[1]]
161 self.set_attribute('mult', mult)
162
163 # Read energy (in kcal/mol, converted to eV)
164 #
165 # FINAL HEAT OF FORMATION = -333.88606 KCAL = -1396.97927 KJ
166 if 'FINAL HEAT OF FORMATION =' in line:
167 if not hasattr(self, "scfenergies"):
168 self.scfenergies = []
169 self.scfenergies.append(utils.convertor(self.float(line.split()[5]), "kcal/mol", "eV"))
170
171 # Molecular mass parsing (units will be amu)
172 #
173 # MOLECULAR WEIGHT == 130.1890
174 if line[0:35] == ' MOLECULAR WEIGHT =':
175 self.molmass = self.float(line.split()[3])
176
177 #rotational constants
178 #Example:
179 # ROTATIONAL CONSTANTS IN CM(-1)
180 #
181 # A = 0.01757641 B = 0.00739763 C = 0.00712013
182 # could also read in moment of inertia, but this should just differ by a constant: rot cons= h/(8*Pi^2*I)
183 # note that the last occurence of this in the thermochemistry section has reduced precision,
184 # so we will want to use the 2nd to last instance
185 if line[0:40] == ' ROTATIONAL CONSTANTS IN CM(-1)':
186 blankline = inputfile.next()
187 rotinfo = inputfile.next()
188 if not hasattr(self, "rotcons"):
189 self.rotcons = []
190 broken = rotinfo.split()
191 # leave the rotational constants in Hz
192 a = float(broken[2])
193 b = float(broken[5])
194 c = float(broken[8])
195 self.rotcons.append([a, b, c])
196
197 # Start of the IR/Raman frequency section.
198 # Example:
199 # VIBRATION 1 1A ATOM PAIR ENERGY CONTRIBUTION RADIAL
200 # FREQ. 15.08 C 12 -- C 16 +7.9% (999.0%) 0.0%
201 # T-DIPOLE 0.2028 C 16 -- H 34 +5.8% (999.0%) 28.0%
202 # TRAVEL 0.0240 C 16 -- H 32 +5.6% (999.0%) 35.0%
203 # RED. MASS 1.7712 O 1 -- O 4 +5.2% (999.0%) 0.4%
204 # EFF. MASS7752.8338
205 #
206 # VIBRATION 2 2A ATOM PAIR ENERGY CONTRIBUTION RADIAL
207 # FREQ. 42.22 C 11 -- C 15 +9.0% (985.8%) 0.0%
208 # T-DIPOLE 0.1675 C 15 -- H 31 +6.6% (843.6%) 3.3%
209 # TRAVEL 0.0359 C 15 -- H 29 +6.0% (802.8%) 24.5%
210 # RED. MASS 1.7417 C 13 -- C 17 +5.8% (792.7%) 0.0%
211 # EFF. MASS1242.2114
212 if line[1:10] == 'VIBRATION':
213 self.updateprogress(inputfile, "Frequency Information", self.fupdate)
214
215 # get the vib symmetry
216 if len(line.split()) >= 3:
217 sym = line.split()[2]
218 if not hasattr(self, 'vibsyms'):
219 self.vibsyms = []
220 self.vibsyms.append(sym)
221
222 line = inputfile.next()
223 if 'FREQ' in line:
224 if not hasattr(self, 'vibfreqs'):
225 self.vibfreqs = []
226 freq = float(line.split()[1])
227 self.vibfreqs.append(freq)
228
229 line = inputfile.next()
230 if 'T-DIPOLE' in line:
231 if not hasattr(self, 'vibirs'):
232 self.vibirs = []
233 tdipole = float(line.split()[1])
234 # transform to km/mol
235 self.vibirs.append(math.sqrt(tdipole))
236
237 # Orbital eigenvalues, e.g.
238 # ALPHA EIGENVALUES
239 # BETA EIGENVALUES
240 # or just "EIGENVALUES" for closed-shell
241 if 'EIGENVALUES' in line:
242 if not hasattr(self, 'moenergies'):
243 self.moenergies = [] # list of arrays
244
245 energies = []
246 line = inputfile.next()
247 while len(line.split()) > 0:
248 energies.extend([float(i) for i in line.split()])
249 line = inputfile.next()
250 self.moenergies.append(energies)
251
252 # todo:
253 # Partial charges and dipole moments
254 # Example:
255 # NET ATOMIC CHARGES
256
257 if line[:16] == '== MOPAC DONE ==':
258 self.metadata['success'] = True