-
Notifications
You must be signed in to change notification settings - Fork 0
FileRead
xuetaoshi edited this page Apr 11, 2017
·
2 revisions
Read Gaussian trajectory log file based on regex patterns and then do proper processing.
-
Initialization: FileRead(file_str="", n_atom=-1)
The initialization of this object will attempt to determine the number of atoms in the molecule and set up a pattern dictionary, partially based on the number of atoms.- Parameters:
- file_str: string, python File object
optional. However, if both this and the next optional argument were not passed in during initialization, an error would be raised. This argument corresponds to either string of a log file, or the file handle of such file from which the initialization attempts to determine the number of atoms if not directly passed in by the next argument. - n_atom: integer
optional, default=-1, which is a flag to signify this value was not directly passed in. This gives the number of atoms in the molecule for the Gaussian log files to read.
- file_str: string, python File object
- Parameters:
-
FileRead.pattern_dict =
{ "Electric Field": r"An electric field of +([\-+]*[.\d]+D*[\-+]*[\d]*) +([\-+]*[.\d]+D*[\-+]*[\d]*) +" r"([\-+]*[.\d]+D*[\-+]*[\d]*)|Standard basis: .+(?:\n.+){1,2} basis functions,", "Electric Field Alt": r"Electric field = +([\-+]*[.\d]+D*[\-+]*[\d]*) +([\-+]*[.\d]+D*[\-+]*[\d]*) +" r"([\-+]*[.\d]+D*[\-+]*[\d]*)|Standard basis: .+(?:\n.+){1,2} basis functions,", "Cartesian Coordinates #VarRow": r"Cartesian coordinates.+\n" + r".+ X= +([\-+]*[.\d]+D*[\-+]*[\d]*)" r" +Y= +([\-+]*[.\d]+D*[\-+]*[\d]*)" r" +Z= +([\-+]*[.\d]+D*[\-+]*[\d]*)\n" * self.n_atom, "Mass Weighted Velocity #VarRow": r"MW [cC]artesian velocity:.*\n" + r" I= +\d+ X= +([\-+]*[.\d]+D*[\-+]*[\d]*)" r" +Y= +([\-+]*[.\d]+D*[\-+]*[\d]*)" r" +Z= +([\-+]*[.\d]+D*[\-+]*[\d]*)\n" * self.n_atom, "Time": r"Time \(fs\) +([\-+]*[.\d]+D*[\-+]*[\d]*)", "Total Energy": r"ETot += +([\-+]*[.\d]+D*[\-+]*[\d]*)", "Kinetic Energy": r"EKin = +([\-+]*[.\d]+D*[\-+]*[\d]*);", "Potential Energy": r"EPot = +([\-+]*[.\d]+D*[\-+]*[\d]*);", "Total Angular Momentum": r"Jtot = +([\-+]*[.\d]+D*[\-+]*[\d]*) H-BAR", "Angular Momemtum Components": r"Angular momentum \(instantaneous\)\n +JX =" r" +([\-+]*[.\d]+D*[\-+]*[\d]*) +JY = +([\-+]*[.\d]+D*[\-+]*[\d]*)" r" +JZ = +([\-+]*[.\d]+D*[\-+]*[\d]*)", "Mulliken Charges #VarRow": r"(?<=\n) Mulliken charges(?::| and spin densities:)(?! with)\n.+\n" + r" +\d+ +\S+ +([\-+]*[.\d]+)(?:\n| +[\-+]*[.\d]+\n)" * self.n_atom, "Dipole Moment": r" Dipole moment .+:\n +X= +([\-+]*[.\d]+D*[\-+]*[\d]*)" r" +Y= +([\-+]*[.\d]+D*[\-+]*[\d]*) +Z= +([\-+]*[.\d]+D*[\-+]*[\d]*)" }- There are three types of patterns:
- Fixed dimension:
for example, "Time": r"Time (fs) +([-+][.\d]+D[-+][\d])" This means a 1-dimensional array will be read from every data block at each time step. - Variable row number:
for example, "Mulliken Charges #VarRow": r"(?<=\n) Mulliken charges(?::| and spin densities:)(?! with)\n.+\n"- r" +\d+ +\S+ +([-+][.\d]+)(?:\n| +[-+][.\d]+\n)" * self.n_atom This means a 2-dimensional array will be read from every data block at each time step. In order to preserve the shape of such array, the regex pattern is generated by a variable, self.n_atom, which is the number of atoms determined previously. "#VarRow" in the key of this dictionary entry signifies the follow-up method to treat such data type accordingly.
- Variable column number:
similar to Variable row number type. Right now there is not any data type needed in this category, but the functionality is built in already.
- Fixed dimension:
- So far the only variable in the above mentioned variable type patterns is the number of atoms, and can only be the number of atoms.
- There are three types of patterns:
-
FileRead.findall(file_str, data_type)
The method to extract the data from a log file.-
Parameters:
- file_str: string, python File object
first argument. The raw string of a log file or the file handle of said file to be read from. - data_type: string
second argument. The string of the key in the pattern_dict dictionary. An error will be raised if such entry was not added before using this method.
- file_str: string, python File object
-
Returns: numpy array.
-
Example:
Suppose all the Cartesian coordinates are to be extracted from a log file. The following lines will do the job:fh = open("trajectory.log", "r") reader = FileRead(file_str=fh) cts = reader.findall(fh, "Cartesian Coordinates #VarRow") fh.close()Array cts is now a multi-dimensional array containing such data. cts[points, n_atoms, xyz]
-