Debian Codegraph @

doc/sphinx/how_to_parse.rst

raw
1 How to parse and write
2 ======================
3
4 This page outlines the various ways cclib can be used to parse and write logfiles, and provides several examples to get you started.
5
6 From Python
7 +++++++++++
8
9 Importing cclib and parsing a file is a few lines of Python code, making it simple to access data from the output file of any supported computational chemistry program. For example:
10
11 .. code-block:: python
12
13 import cclib
14
15 filename = "logfile.out"
16 parser = cclib.io.ccopen(filename)
17 data = parser.parse()
18 print("There are %i atoms and %i MOs" % (data.natom, data.nmo))
19
20 A newer command, ``ccread``, combines both the format detection and parsing steps:
21
22 .. code-block:: python
23
24 import cclib
25
26 filename = "logfile.out"
27 data = cclib.io.ccread(filename)
28 print("There are %i atoms and %i MOs" % (data.natom, data.nmo))
29
30 From command line
31 +++++++++++++++++
32
33 The cclib package provides four scripts to parse and write data: ``ccget``, ``ccwrite``, ``cda``, and ``ccframe``.
34
35 1. **ccget** is used to parse attribute data from output files.
36 2. **ccwrite** has the ability to list out all valid attribute data that can be parsed from an output format. It has the added feature of writing the output file into four different formats i.e. ``json``, ``cjson``, ``cml``, ``xyz``.
37 3. **cda** is used for the chemical decomposition analysis of output files.
38 4. **ccframe** is used to write data tables from output files.
39
40 This page describes how to use the ccget, ccwrite and ccframe scripts to obtain data from output files.
41
42 ccget
43 -----
44
45 The data types that can be parsed from the output file depends on the type of computation being conducted. The name of the output file used to show example usage is ``Benzeneselenol.out``.
46
47 Data type can be parsed from the output file by following this format::
48
49 ccget <attribute> [<attribute>] <CompChemLogFile> [<CompChemLogFile>]
50
51 where ``attribute`` can be any one of the attribute names available `here`_.
52
53 .. _`here`: data_dev.html
54
55 1. Atomic Charges
56
57 The atomic charges are obtained by using the ``atomcharges`` attribute::
58
59 $ ccget atomcharges Benzeneselenol.out
60 Attempting to read Benzeneselenol.out
61 atomcharges:
62 {'mulliken': array([-0.49915 , 0.056965, 0.172161, 0.349794, -0.153072, 0.094583,
63 0.016487, 0.050249, 0.002149, 0.01161 , 0.053777, -0.173671,
64 0.018118])}
65
66 2. Electronic Energies
67
68 The molecular electronic energies after SCF (DFT) optimization of the input molecule are printed by using the ``scfenergies`` attribute::
69
70 $ ccget scfenergies Benzeneselenol.out
71 Attempting to read Benzeneselenol.out
72 scfenergies:
73 [-71671.43702915 -71671.4524142 -71671.4534768 -71671.45447492
74 -71671.4556548 -71671.45605671 -71671.43194906 -71671.45761021
75 -71671.45850275 -71671.39630296 -71671.45915119 -71671.45935854
76 -71671.4594614 -71671.45947338 -71671.45948807 -71671.4594946
77 -71671.4594946 ]
78
79
80 3. Geometry Targets
81
82 The targets for convergence of geometry optimization can be obtained by using the ``geotargets`` attribute::
83
84 $ ccget geotargets Benzeneselenol.out
85 Attempting to read Benzeneselenol.out
86 geotargets:
87 [ 0.00045 0.0003 0.0018 0.0012 ]
88
89 Chaining of attributes
90 ^^^^^^^^^^^^^^^^^^^^^^
91
92 ccget provides the user with the option to chain attributes to obtain more than one type of data with a command call. The attributes can be chained in any particular order. A few chained examples are provided below.
93
94 1. Molecular Orbitals and Multiplicity
95
96 The number of molecular orbitals and the number of basis functions used to optimize the molecule can be obtained by running the following command::
97
98 $ ccget nmo nbasis Benzeneselenol.out
99 Attempting to read Benzeneselenol.out
100 nmo:
101 405
102 nbasis:
103 407
104
105 2. Enthalpy and Vibrational Frequency
106
107 The enthalpy and the vibrational frequencies of the optimized molecule is conducted is obtained below::
108
109 $ ccget enthalpy vibfreqs Benzeneselenol.out
110 Attempting to read Benzeneselenol.out
111 enthalpy:
112 -2633.77264
113 vibfreqs:
114 [ 129.5512 170.6681 231.4278 304.8614 407.8299 472.5026
115 629.9087 679.9032 693.2509 746.7694 812.5113 850.2578
116 915.8742 987.1252 988.1785 1002.8922 1038.1073 1091.4005
117 1102.3417 1183.3857 1209.2727 1311.3497 1355.6441 1471.4447
118 1510.1919 1611.9088 1619.0156 2391.2487 3165.1596 3171.3909
119 3182.0753 3188.5786 3198.0359]
120
121 ccwrite
122 -------
123
124 The same Benzeneselenol.out file used in the previous examples will be used as the input file for ccwrite. When the ccwrite script is used with a valid input, it prints out the valid attributes that can be parsed from the file.
125
126 Command line format::
127
128 ccwrite <OutputFileFormat> <CompChemLogFile> [<CompChemLogFile>]
129
130 The valid output file formats are ``cjson``, ``cml``, and ``xyz``.
131
132 1. `Chemical markup language`_ (CML)::
133
134 $ ccwrite cml Benzeneselenol.out
135 Attempting to parse Benzeneselenol.out
136 cclib can parse the following attributes from Benzeneselenol.out:
137 atomcharges
138 atomcoords
139 atomnos
140 charge
141 coreelectrons
142 enthalpy
143 geotargets
144 geovalues
145 grads
146 homos
147 moenergies
148 mosyms
149 mult
150 natom
151 nbasis
152 nmo
153 optdone
154 optstatus
155 scfenergies
156 scftargets
157 temperature
158 vibdisps
159 vibfreqs
160 vibirs
161 vibsyms
162
163 .. _`chemical markup language`: http://www.xml-cml.org/
164
165 A ``Benzeneselenol.cml`` output file is generated in the same directory as the ``Benzeneselenol.out`` file:
166
167 .. code-block:: xml
168
169 <?xml version='1.0' encoding='utf-8'?>
170 <molecule id="Benzeneselenol.out" xmlns="http://www.xml-cml.org/schema">
171 <atomArray>
172 <atom elementType="C" id="a1" x3="-2.8947620000" y3="-0.0136420000" z3="-0.0015280000" />
173 <atom elementType="C" id="a2" x3="-2.2062510000" y3="1.1938510000" z3="-0.0025210000" />
174 <atom elementType="C" id="a3" x3="-0.8164260000" y3="1.2153020000" z3="-0.0022010000" />
175 <atom elementType="C" id="a4" x3="-0.1033520000" y3="0.0183920000" z3="0.0031060000" />
176 <atom elementType="C" id="a5" x3="-0.7906630000" y3="-1.1943840000" z3="0.0058500000" />
177 <atom elementType="C" id="a6" x3="-2.1799570000" y3="-1.2059710000" z3="0.0017890000" />
178 <atom elementType="H" id="a7" x3="-3.9758430000" y3="-0.0253010000" z3="-0.0029040000" />
179 <atom elementType="H" id="a8" x3="-2.7502340000" y3="2.1291370000" z3="-0.0052760000" />
180 <atom elementType="H" id="a9" x3="-0.2961840000" y3="2.1630180000" z3="-0.0073260000" />
181 <atom elementType="H" id="a10" x3="-0.2474670000" y3="-2.1302310000" z3="0.0132260000" />
182 <atom elementType="H" id="a11" x3="-2.7028960000" y3="-2.1530750000" z3="0.0036640000" />
183 <atom elementType="Se" id="a12" x3="1.8210800000" y3="-0.0433780000" z3="-0.0038760000" />
184 <atom elementType="H" id="a13" x3="2.0043580000" y3="1.4100070000" z3="0.1034490000" />
185 </atomArray>
186 <bondArray>
187 <bond atomRefs2="a9 a3" order="1" />
188 <bond atomRefs2="a8 a2" order="1" />
189 <bond atomRefs2="a12 a4" order="1" />
190 <bond atomRefs2="a12 a13" order="1" />
191 <bond atomRefs2="a7 a1" order="1" />
192 <bond atomRefs2="a2 a3" order="2" />
193 <bond atomRefs2="a2 a1" order="1" />
194 <bond atomRefs2="a3 a4" order="1" />
195 <bond atomRefs2="a1 a6" order="2" />
196 <bond atomRefs2="a6 a11" order="1" />
197 <bond atomRefs2="a6 a5" order="1" />
198 <bond atomRefs2="a4 a5" order="2" />
199 <bond atomRefs2="a5 a10" order="1" />
200 </bondArray>
201 </molecule>
202
203 2. XYZ_
204
205 Using ``xyz`` as the ``<OutputFileFormat>`` with ``Benzeneselenol.out``, we obtain the following ``Benzeneselenol.xyz`` file::
206
207 13
208 Benzeneselenol.out: Geometry 17
209 C -2.8947620000 -0.0136420000 -0.0015280000
210 C -2.2062510000 1.1938510000 -0.0025210000
211 C -0.8164260000 1.2153020000 -0.0022010000
212 C -0.1033520000 0.0183920000 0.0031060000
213 C -0.7906630000 -1.1943840000 0.0058500000
214 C -2.1799570000 -1.2059710000 0.0017890000
215 H -3.9758430000 -0.0253010000 -0.0029040000
216 H -2.7502340000 2.1291370000 -0.0052760000
217 H -0.2961840000 2.1630180000 -0.0073260000
218 H -0.2474670000 -2.1302310000 0.0132260000
219 H -2.7028960000 -2.1530750000 0.0036640000
220 Se 1.8210800000 -0.0433780000 -0.0038760000
221 H 2.0043580000 1.4100070000 0.1034490000
222
223 .. _XYZ: https://en.wikipedia.org/wiki/XYZ_file_format
224
225 ccframe
226 -------
227
228 This script creates complete tables of data from output files in some of the formats supported by pandas_.
229 Since the pandas library is not a dependency of cclib, `it must be installed <https://pandas.pydata.org/pandas-docs/stable/install.html>`_ separately.
230
231 .. _pandas: https://pandas.pydata.org/
232
233 A complete data table can be parsed from many output files by following this format::
234
235 ccframe -O <OutputDest> <CompChemLogFile> [<CompChemLogFile>...]
236
237 The argument for ``-O`` indicates the data file to be written and its extension specifies the filetype (e.g. csv, h5/hdf/hdf5, json, pickle/pkl, xlsx).
238 Since higher-dimensional attributes (e.g. ``atomcoords``) are handled as plain text in some file formats (such as Excel XLSX or CSV), we recommend storing JSON or HDF5 files.