Exemplo n.º 1
0
def _read_cube_header(f):
    # Read the title
    title = f.readline().strip()
    # skip the second line
    f.readline()

    def read_grid_line(line):
        """Read a grid line from the cube file"""
        words = line.split()
        return (int(words[0]),
                np.array([float(words[1]),
                          float(words[2]),
                          float(words[3])], float)
                # all coordinates in a cube file are in atomic units
                )

    # number of atoms and origin of the grid
    natom, origin = read_grid_line(f.readline())
    # numer of grid points in A direction and step vector A, and so on
    shape0, axis0 = read_grid_line(f.readline())
    shape1, axis1 = read_grid_line(f.readline())
    shape2, axis2 = read_grid_line(f.readline())
    shape = np.array([shape0, shape1, shape2], int)
    axes = np.array([axis0, axis1, axis2])

    cell = Cell(axes * shape.reshape(-1, 1))
    ugrid = UniformGrid(origin, axes, shape, np.ones(3, int))

    def read_coordinate_line(line):
        """Read an atom number and coordinate from the cube file"""
        words = line.split()
        return (int(words[0]), float(words[1]),
                np.array([float(words[2]),
                          float(words[3]),
                          float(words[4])], float)
                # all coordinates in a cube file are in atomic units
                )

    numbers = np.zeros(natom, int)
    pseudo_numbers = np.zeros(natom, float)
    coordinates = np.zeros((natom, 3), float)
    for i in xrange(natom):
        numbers[i], pseudo_numbers[i], coordinates[i] = read_coordinate_line(
            f.readline())
        # If the pseudo_number field is zero, we assume that no effective core
        # potentials were used.
        if pseudo_numbers[i] == 0.0:
            pseudo_numbers[i] = numbers[i]

    return title, coordinates, numbers, cell, ugrid, pseudo_numbers
Exemplo n.º 2
0
def _load_vasp_grid(filename):
    '''Load a grid data file from VASP 5

       **Arguments:**

       filename
            The VASP filename

       **Returns:** a dictionary containing: ``title``, ``coordinates``,
       ``numbers``, ``cell``, ``grid``, ``cube_data``.
    '''
    with open(filename) as f:
        # Load header
        title, cell, numbers, coordinates = _load_vasp_header(f, 0)

        # read the shape of the data
        shape = np.array([int(w) for w in f.next().split()])

        # read data
        cube_data = np.zeros(shape, float)
        counter = 0
        for line in f:
            if counter >= cube_data.size:
                break
            for w in line.split():
                i0, i1, i2 = _unravel_counter(counter, shape)
                # Fill in the data with transposed indexes. In horton, X is
                # the slowest index while Z is the fastest.
                cube_data[i0, i1, i2] = float(w)
                counter += 1
        assert counter == cube_data.size

    return {
        'title':
        title,
        'coordinates':
        coordinates,
        'numbers':
        numbers,
        'cell':
        cell,
        'grid':
        UniformGrid(np.zeros(3), cell.rvecs / shape.reshape(-1, 1), shape,
                    np.ones(3, int)),
        'cube_data':
        cube_data,
    }
Exemplo n.º 3
0
def load_vasp_grid(filename):
    '''Load a grid data file from VASP 5'''
    with open(filename) as f:
        # Load header
        cell, numbers, coordinates = _load_vasp_header(f, 0)

        # read the shape of the data
        shape = np.array([int(w) for w in f.next().split()])

        # read data
        cube_data = np.zeros(shape, float)
        counter = 0
        for line in f:
            if counter >= cube_data.size:
                break
            for w in line.split():
                i0, i1, i2 = unravel_counter(counter, shape)
                # Fill in the data with transposed indexes. In horton, X is
                # the slowest index while Z is the fastest.
                cube_data[i0, i1, i2] = float(w)
                counter += 1
        assert counter == cube_data.size

    return {
        'coordinates':
        coordinates,
        'numbers':
        numbers,
        'cell':
        cell,
        'grid':
        UniformGrid(np.zeros(3), cell.rvecs / shape.reshape(-1, 1), shape,
                    np.ones(3, int)),
        'extra': {
            'cube_data': cube_data
        },
    }