def read_grid(self, name, idx=0): """ Reads a grid in the current SIESTA.nc file Enables the reading and processing of the grids created by SIESTA """ # Swap as we swap back in the end geom = self.read_geom().swapaxes(0, 2) # Shorthand g = self.groups['GRID'] # Create the grid nx = len(g.dimensions['nx']) ny = len(g.dimensions['ny']) nz = len(g.dimensions['nz']) # Shorthand variable name v = g.variables[name] # Create the grid, SIESTA uses periodic, always grid = Grid([nz, ny, nx], bc=Grid.Periodic, dtype=v.dtype) if len(v[:].shape) == 3: grid.grid = v[:, :, :] else: grid.grid = v[idx, :, :, :] try: u = v.unit if u == 'Ry': # Convert to ev grid *= Ry2eV except: # Simply, we have no units pass # Read the grid, we want the z-axis to be the fastest # looping direction, hence x,y,z == 0,1,2 grid = grid.swapaxes(0, 2) grid.set_geom(geom) return grid
def read_grid(self, name, spin=0): """ Reads a grid in the current Siesta.nc file Enables the reading and processing of the grids created by Siesta Parameters ---------- name : str name of the grid variable to read spin : int or array_like, optional the spin-index for retrieving one of the components. If a vector is passed it refers to the fraction per indexed component. I.e. ``[0.5, 0.5]`` will return sum of half the first two components. Default to the first component. """ # Swap as we swap back in the end geom = self.read_geometry().swapaxes(0, 2) # Shorthand g = self.groups['GRID'] # Create the grid nx = len(g.dimensions['nx']) ny = len(g.dimensions['ny']) nz = len(g.dimensions['nz']) # Shorthand variable name v = g.variables[name] # Create the grid, Siesta uses periodic, always grid = Grid([nz, ny, nx], bc=Grid.PERIODIC, dtype=v.dtype) # Unit-conversion BohrC2AngC = Bohr2Ang**3 unit = { 'Rho': 1. / BohrC2AngC, 'RhoInit': 1. / BohrC2AngC, 'RhoTot': 1. / BohrC2AngC, 'RhoDelta': 1. / BohrC2AngC, 'RhoXC': 1. / BohrC2AngC, 'RhoBader': 1. / BohrC2AngC, 'Chlocal': 1. / BohrC2AngC, }.get(name, 1.) if len(v[:].shape) == 3: grid.grid = v[:, :, :] * unit elif isinstance(spin, Integral): grid.grid = v[spin, :, :, :] * unit else: if len(spin) > v.shape[0]: raise SileError( self.__class__.__name__ + '.read_grid requires spin to be an integer or ' 'an array of length equal to the number of spin components.' ) grid.grid[:, :, :] = v[0, :, :, :] * (spin[0] * unit) for i, scale in enumerate(spin[1:]): grid.grid[:, :, :] += v[1 + i, :, :, :] * (scale * unit) try: if v.unit == 'Ry': # Convert to ev grid *= Ry2eV except: # Allowed pass due to pythonic reading pass # Read the grid, we want the z-axis to be the fastest # looping direction, hence x,y,z == 0,1,2 grid = grid.swapaxes(0, 2) grid.set_geom(geom) return grid