def reduce_ugrid(ugrid, stride, chop): '''Reduce the uniform grid **Arguments:** ugrid The uniform integration grid. stride The reduction factor. chop The number of slices to chop of the grid in each direction. Returns: a reduced ugrid object ''' if (chop < 0): raise ValueError('Chop must be positive or zero.') if ((ugrid.shape - chop) % stride != 0).any(): raise ValueError( 'The stride is not commensurate with all three grid demsions.') new_shape = (ugrid.shape - chop) / stride grid_rvecs = ugrid.grid_rvecs * stride new_ugrid = UniformGrid(ugrid.origin, grid_rvecs, new_shape, ugrid.pbc) return new_ugrid
def write_random_lta_cube(dn, fn_cube): '''Write a randomized cube file''' # start from an existing cube file mol = IOData.from_file(context.get_fn('test/lta_gulp.cif')) # Define a uniform grid with only 1000 points, to make the tests fast. ugrid = UniformGrid(np.zeros(3, float), mol.cell.rvecs * 0.1, np.array([10, 10, 10]), np.array([1, 1, 1])) # Write to the file dn/fn_cube mol.cube_data = np.random.uniform(0, 1, ugrid.shape) mol.grid = ugrid mol.to_file(os.path.join(dn, fn_cube)) return mol
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
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, }
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 }, }