예제 #1
0
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)
예제 #2
0
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 = [''.join(x) for x in chunksOf(ls, 8)]

    assert list(map(parse_string_xyz, xs)) == manyXYZ(path_xyz)
예제 #3
0
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 = [''.join(x) for x in chunksOf(ls, 8)]

    assert list(map(parse_string_xyz, xs)) == manyXYZ(path_xyz)
예제 #4
0
def split_file_geometries(pathXYZ):
    """
    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(pathXYZ) as f:
        xss = f.readlines()

    numat = int(xss[0].split()[0])
    return list(map(''.join, chunksOf(xss, numat + 2)))
예제 #5
0
def split_file_geometries(pathXYZ: str) -> list:
    """
    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(pathXYZ) 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)))
예제 #7
0
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)
예제 #8
0
def parse_molecular_orbitals(file_name: str) -> Tuple:
    """
    Read the Molecular orbital from the orca output
    """
    n_contracted = try_search_pattern("# of contracted basis functions",
                                      file_name).split()[-1]

    # Parse the blocks of MOs
    start = 'MOLECULAR ORBITALS'
    end = '\n\n'
    block = parse_file(parse_section(start, end), file_name).asList()

    # split the block in lines  discarding the first line
    lines = block[0].splitlines()[1:]

    # Lines in each block
    n_contracted = int(n_contracted)
    block_lines = n_contracted + 4

    tuple_energies, tuple_coeffs = tuple(
        zip(*(read_column_orbitals(xs)
              for xs in chunksOf(lines, block_lines))))

    return InfoMO(np.hstack(tuple_energies), np.hstack(tuple_coeffs))
예제 #9
0
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]]
예제 #10
0
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]]