def readCp2KCoeff(path, nOrbitals, nOrbFuns): """ MO coefficients are stored in Column-major order. :parameter path: Path to the file containing the MO coefficients :type path: String :parameter nOrbitals: Number of MO to read :type nOrbitals: Int :param nOrbFuns: Number of orbital functions :type nOrbFuns: Int :returns: Molecular orbitals and orbital energies """ def remove_trailing(xs): "Remove the last lines of the MOs output" words = ["Fermi", "H**O-LUMO"] if any([x in words for x in xs[-1]]): xs.pop(-1) return remove_trailing(xs) else: return xs # Check if the Molecular orbitals came from a restart with open(path, "r") as f: xs = list(islice(f, 4)) if "AFTER SCF STEP -1" in "".join(xs): move_restart_coeff(path) # Open the MO file with open(path, "r") as f: xss = f.readlines() # remove empty lines and comments rs = list(filter(None, map(lambda x: x.split(), xss))) rs = remove_trailing(rs[1:]) # remove header and trail comments # Split the list in chunks containing the orbitals info # in block cotaining a maximum of two columns of MOs chunks = chunksOf(rs, nOrbFuns + 3) eigenVals = np.empty(nOrbitals) coefficients = np.empty((nOrbFuns, nOrbitals)) convert_to_float = np.vectorize(float) for i, xs in enumerate(chunks): j = 2 * i es = xs[1] css = [l[4:] for l in xs[3:]] # There is an odd number of MO and this is the last one if len(es) == 1: eigenVals[-1] = float(es[0]) coefficients[:, -1] = convert_to_float(np.concatenate(css)) else: # rearrange the coeff css = np.transpose(convert_to_float(css)) eigenVals[j:j + 2] = es coefficients[:, j] = css[0] coefficients[:, j + 1] = css[1] return InfoMO(eigenVals, coefficients)
def test_multiple_geometries(): """ Test the reading of multiples molecular geometries from a file. """ path_xyz = 'test/test_files/five_points_ethylene.xyz' with open(path_xyz, 'r') as f: ls = f.readlines() xs = [flatten(x) for x in chunksOf(ls, 8)] assert list(map(parse_string_xyz, xs)) == manyXYZ(path_xyz)
def split_file_geometries(path_xyz): """ Reads a set of molecular geometries in xyz format and returns a list of string, where is element a molecular geometry :returns: String list containing the molecular geometries. """ # Read Cartesian Coordinates with open(path_xyz, 'r') as f: xss = f.readlines() numat = int(xss[0].split()[0]) return list(map(''.join, chunksOf(xss, numat + 2)))
def compute_normalization_cgfs(cgfs: List) -> List: """ Compute the Normalization constant of the whole CGFs for an atom in spherical coordinates. """ # Number of CGFs for each angularM CGFs_cartesians = {'S': 1, 'P': 3, 'D': 6, 'F': 10} norms = [[ compute_normalizations(g, chunk) for chunk in chunksOf(list(vals), CGFs_cartesians[g]) ] for g, vals in groupby(cgfs, lambda x: x.orbType[0])] return list(chain(*chain(*norms)))
def read_blocks_from_file(start: str, end: str, file_name: str) -> Matrix: """ Read a matrix printed in block format :param start: token identifying the start of the block. :param end: characters signaling end of block. :param file_name: Name of the file containing the matrix printed in blocks :returns: Numpy array """ p = parse_section(start, end) raw = parse_file(p, file_name)[0].splitlines() number_of_basis = int(raw[0].split()[0]) lines = raw[1:] # Matrix elements are printed in block of 6 columns nblocks = number_of_basis // 6 rest = number_of_basis % 6 nblocks = nblocks if rest == 0 else nblocks + 1 # a block start with a line header then the data blocks = [read_block(block) for block in chunksOf(lines, number_of_basis + 1)] return np.concatenate(blocks, axis=1)
def test_chunksOf(): """ chunksOf([0, 1, 2, 3], 1) == [[0], [1], [2], [3]] """ assert list(chunksOf([0, 1, 2, 3], 1)) == [[0], [1], [2], [3]]