def get_random_cell(a, nvec): if nvec == 0: return Cell(None) while True: if a <= 0: raise ValueError('The first argument must be strictly positive.') rvecs = np.random.uniform(0, a, (nvec,3)) cell = Cell(rvecs) if cell.volume > a**nvec*0.1: return cell
def _load_vasp_header(f): '''Load the cell and atoms from a VASP file File specification provided here: http://cms.mpi.univie.ac.at/vasp/guide/node59.html **Arguments:** f An open file object **Returns:** ``title``, ``cell``, ``numbers``, ``coordinates`` ''' # read the title title = f.next().strip() # read the universal scaling factor scaling = float(f.next().strip()) # read cell parameters in angstrom, without the universal scaling factor. # each row is one cell vector rvecs = [] for i in xrange(3): rvecs.append([float(w) for w in f.next().split()]) rvecs = np.array(rvecs)*angstrom*scaling # Convert to cell object cell = Cell(rvecs) # note that in older VASP version the following line might be absent vasp_numbers = [periodic[w].number for w in f.next().split()] vasp_counts = [int(w) for w in f.next().split()] numbers = [] for n, c in zip(vasp_numbers, vasp_counts): numbers.extend([n]*c) numbers = np.array(numbers) line = f.next() # the 7th line can optionally indicate selective dynamics if line[0].lower() in ['s']: line = f.next() # parse direct/cartesian switch cartesian = line[0].lower() in ['c', 'k'] # read the coordinates coordinates = [] for line in f: # check if all coordinates are read if (len(line.strip()) == 0) or (len(coordinates) == numbers.shape[0]): break coordinates.append([float(w) for w in line.split()[:3]]) if cartesian: coordinates = np.array(coordinates)*angstrom*scaling else: coordinates = np.dot(np.array(coordinates), rvecs) return title, cell, numbers, coordinates
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_header(f, nskip): '''Load the cell and atoms from a VASP file **Arguments:** f An open file object nskip The number of lines to skip after the line with elements **Returns:** ``title``, ``cell``, ``numbers``, ``coordinates`` ''' # reat the title title = f.next().strip() f.next() # read cell parameters in angstrom. each row is one cell vector rvecs = [] for i in xrange(3): rvecs.append([float(w) for w in f.next().split()]) rvecs = np.array(rvecs) * angstrom # Convert to cell object cell = Cell(rvecs) vasp_numbers = [periodic[w].number for w in f.next().split()] vasp_counts = [int(w) for w in f.next().split()] numbers = [] for n, c in zip(vasp_numbers, vasp_counts): numbers.extend([n] * c) numbers = np.array(numbers) # skip some lines for i in xrange(nskip): f.next() assert f.next().startswith('Direct') # read the fractional coordinates and convert to Cartesian coordinates = [] for line in f: if len(line.strip()) == 0: break coordinates.append([float(w) for w in line.split()[:3]]) coordinates = np.dot(np.array(coordinates), rvecs) return title, cell, numbers, coordinates
def eval_spline(self, cubic_spline, center, output, cell=None): '''Evaluate a spherically symmetric function **Arguments:** cubic_spline A cubic spline with the radial dependence center The center of the spherically symmetric function output The output array **Optional arguments:** cell A unit cell when periodic boundary conditions are used. ''' if cell is None: cell = Cell(None) eval_spline_grid(cubic_spline, center, output, self.points, cell)
def eval_decomposition(self, cubic_splines, center, output, cell=None): '''Evaluate a spherical decomposition **Arguments:** cubic_splines A list cubic splines, where each item is a radial function that is associated with a corresponding real spherical harmonic. center The center of the spherically symmetric function output The output array **Optional arguments:** cell A unit cell when periodic boundary conditions are used. ''' if cell is None: cell = Cell(None) eval_decomposition_grid(cubic_splines, center, output, self.points, cell)
def eval_spline(self, cubic_spline, center, output, cell=None): if cell is None: cell = Cell(None) eval_spline_grid(cubic_spline, center, output, self.points, cell)