Coding with FileReader objects¶
Here, you will find tutorials and explanations for using AaronTools’
functions, classes, and subroutines pertaining to our object for reading
files: AaronTools.fileIO.FileReader().
Note that, by default, FileReader will read the geometry and
limited other information (charge, multiplicity, etc) from a QM output file.
Use just_geom=False to fully parse QM output.
See Calculation Information.
Creation¶
A FileReader object can be created by passing the path
to a file. To begin, let’s import FileReader:
from AaronTools.fileIO import FileReader
fr = FileReader("benzene.log")
The file “benzene.log” will be read as a Gaussian output file
because of the .log extension.
Creating a Geometry from this FileReader is as
simple as passing it to Geometry:
from AaronTools.geometry import Geometry
geom = Geometry(fr)
If the file has an unexpected extension, we should specify the format for AaronTools:
fr = FileReader(("benzene.gout", "log", None))
Here, we’ve replaced the filename with a tuple containing
the path to the file
the format AaronTools should read the file as
the file content
Because we haven’t read anything from the file yet,
we specify None in our tuple.
The content can also be a string containing the contents of a
file of the specified format or a file-like object (e.g. sys.stdin).
Below is a table containing AaronTools’ expectations for file extensions:
file type |
extension |
|---|---|
Gaussian input |
com or gjf |
Gaussian output |
log |
ORCA output |
out |
XYZ |
xyz |
structure data file |
sd or mol |
Psi4 output |
dat or out |
Getting More Information¶
FileReader initialization accepts two keywords:
get_all and just_geom.
Let’s look at what those do.
Multiple Structures¶
The get_all keyword tells FileReader to
store all the structures it finds while reading the
file (e.g. all steps in an optimization or an XYZ trajectory).
These structures are stored in the FileReader’s all_geom attribute.
This will be a list of dictionaries, with one of the keys being atoms
and the other being either comment or data, depending on the file format.
The data key is used for QM output files, and stores whatever data has
been parsed at the time the corresponding structure was read.
The comment key is used for files that do not have computed data
to parse (e.g., XYZ or Mol files). With get_all=False,
the FileReader’s only structure will be the last one in the file.
Below is sample code for turning each structure in a
FileReader into a Geometry, which are then stored in the list
geom_list:
from AaronTools.geometry import Geometry
from AaronTools.fileIO import FileReader
fr = FileReader('benzene.log', get_all=True)
geom_list = []
for struc in fr.all_geom:
geom_list.append(Geometry(struc["atoms"]))
Below is similar code for reading an XYZ file with multiple structures that will also copy over the comment information from the original XYZ entry
from AaronTools.geometry import Geometry
from AaronTools.fileIO import FileReader
fr = FileReader('mols.xyz', get_all=True)
geom_list = []
for struc in fr.all_geom:
geom_list.append(Geometry(struc["atoms"], comment=struc["comment"]))
Calculation Information¶
By default, a FileReader will only read the molecular structure.
The just_geom keyword controls whether other information (e.g. data from a QM output file)
is also read.
This additional information will be stored as a dictionary in the
FileReader’s other attribute.
This data can also be accessed by using the FileReader
as if it was a dictionary (i.e. fr["energy"] instead
of fr.other["energy"]).
For example, the following will first check wither a Psi4 job finished (fr["finished"])
and, if so, print the final energy (fr["energy"]):
from AaronTools.fileIO import FileReader
fr = FileReader('output.dat', just_geom=False)
if fr["finished"]:
print(f"Final energy = {fr["energy"]}")
else:
print("Did not finish")
A list of dictionary keys, what they are, and which of our file parsers can grab them can be found on this page.