def read_grid(self, name='gridfunc', idx=0, *args, **kwargs): """ Reads a grid in the current SIESTA.grid.nc file Enables the reading and processing of the grids created by SIESTA """ # Swap as we swap back in the end sc = self.read_sc().swapaxes(0, 2) # Create the grid nx = len(self._dimension('n1')) ny = len(self._dimension('n2')) nz = len(self._dimension('n3')) if name is None: v = self._variable('gridfunc') else: v = self._variable(name) # Create the grid, SIESTA uses periodic, always grid = Grid([nz, ny, nx], bc=Grid.Periodic, sc=sc, dtype=v.dtype) if len(v[:].shape) == 3: grid.grid = v[:, :, :] else: grid.grid = v[idx, :, :, :] # Read the grid, we want the z-axis to be the fastest # looping direction, hence x,y,z == 0,1,2 return grid.swapaxes(0, 2)
def read_grid(self, name='gridfunc', idx=0, *args, **kwargs): """ Reads a grid in the current SIESTA.grid.nc file Enables the reading and processing of the grids created by SIESTA """ # Swap as we swap back in the end sc = self.read_supercell().swapaxes(0, 2) # Create the grid nx = len(self._dimension('n1')) ny = len(self._dimension('n2')) nz = len(self._dimension('n3')) if name is None: v = self._variable('gridfunc') else: v = self._variable(name) # Create the grid, SIESTA uses periodic, always grid = Grid([nz, ny, nx], bc=Grid.Periodic, sc=sc, dtype=v.dtype) if len(v.shape) == 3: grid.grid[:, :, :] = v[:, :, :] else: grid.grid[:, :, :] = v[idx, :, :, :] # Read the grid, we want the z-axis to be the fastest # looping direction, hence x,y,z == 0,1,2 return grid.swapaxes(0, 2)
def read_grid(self, *args, **kwargs): """ Reads the TranSiesta potential input grid """ # Create the grid na = len(self._dimension('a')) nb = len(self._dimension('b')) nc = len(self._dimension('c')) v = self._variable('V') # Create the grid, Siesta uses periodic, always grid = Grid([nc, nb, na], bc=Grid.PERIODIC, dtype=v.dtype) grid.grid[:, :, :] = v[:, :, :] / eV2Ry # Read the grid, we want the z-axis to be the fastest # looping direction, hence x,y,z == 0,1,2 return grid.swapaxes(0, 2)
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
def read_grid(self, spin=0, name='gridfunc', *args, **kwargs): """ Reads a grid in the current Siesta.grid.nc file Enables the reading and processing of the grids created by Siesta Parameters ---------- spin : int or array_like, optional specify the retrieved values name : str, optional the name for the grid-function (do not supply for standard Siesta output) geometry: Geometry, optional add the Geometry to the Grid """ # Default to *index* variable spin = kwargs.get('index', spin) # Determine the name of this file f = osp.basename(self.file) # File names are made up of # ElectrostaticPotential.grid.nc # So the first one should be ElectrostaticPotential try: # <>.grid.nc base = f.split('.')[-3] except: base = 'None' # Unit-conversion BohrC2AngC = Bohr2Ang**3 unit = { 'Rho': 1. / BohrC2AngC, 'DeltaRho': 1. / BohrC2AngC, 'RhoXC': 1. / BohrC2AngC, 'RhoInit': 1. / BohrC2AngC, 'Chlocal': 1. / BohrC2AngC, 'TotalCharge': 1. / BohrC2AngC, 'BaderCharge': 1. / BohrC2AngC, 'ElectrostaticPotential': Ry2eV, 'TotalPotential': Ry2eV, 'Vna': Ry2eV, }.get(base, None) # Fall-back if unit is None: unit = 1. show_info = True else: show_info = False # Swap as we swap back in the end sc = self.read_supercell().swapaxes(0, 2) # Create the grid nx = len(self._dimension('n1')) ny = len(self._dimension('n2')) nz = len(self._dimension('n3')) if name is None: v = self._variable('gridfunc') else: v = self._variable(name) # Create the grid, Siesta uses periodic, always grid = Grid([nz, ny, nx], bc=Grid.PERIODIC, sc=sc, dtype=v.dtype, geometry=kwargs.get("geometry", None)) if v.ndim == 3: grid.grid[:, :, :] = v[:, :, :] * unit elif isinstance(spin, Integral): grid.grid[:, :, :] = v[spin, :, :, :] * unit else: if len(spin) > v.shape[0]: raise SileError( f"{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 if show_info: info( f"{self.__class__.__name__}.read_grid cannot determine the units of the grid. " "The units may not be in sisl units.") # Read the grid, we want the z-axis to be the fastest # looping direction, hence x,y,z == 0,1,2 return grid.swapaxes(0, 2)
class TestGrid(object): def setUp(self): alat = 1.42 sq3h = 3.**.5 * 0.5 self.sc = SuperCell(np.array( [[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) * alat, nsc=[3, 3, 1]) self.g = Grid([10, 10, 100], sc=self.sc) def tearDown(self): del self.sc del self.g def test_append(self): g = self.g.append(self.g, 0) assert_true(np.allclose(g.grid.shape, [20, 10, 100])) g = self.g.append(self.g, 1) assert_true(np.allclose(g.grid.shape, [10, 20, 100])) g = self.g.append(self.g, 2) assert_true(np.allclose(g.grid.shape, [10, 10, 200])) def test_size(self): assert_true(np.allclose(self.g.grid.shape, [10, 10, 100])) def test_item(self): assert_true( np.allclose(self.g[1:2, 1:2, 2:3], self.g.grid[1:2, 1:2, 2:3])) def test_dcell(self): assert_true(np.all(self.g.dcell * self.g.cell >= 0)) def test_dvol(self): assert_true(self.g.dvol > 0) def test_shape(self): assert_true(np.all(self.g.shape == self.g.grid.shape)) def test_dtype(self): assert_true(self.g.dtype == self.g.grid.dtype) def test_copy(self): assert_true(self.g.copy() == self.g) def test_swapaxes(self): g = self.g.swapaxes(0, 1) assert_true(np.allclose(self.g.cell[0, :], g.cell[1, :])) assert_true(np.allclose(self.g.cell[1, :], g.cell[0, :])) def test_interp(self): shape = np.array(self.g.shape, np.int32) g = self.g.interp(shape * 2) g1 = g.interp(shape) assert_true(np.allclose(self.g.grid, g1.grid)) def test_index1(self): mid = np.array(self.g.shape, np.int32) // 2 idx = self.g.index(self.sc.center()) print(mid, idx) assert_true(np.all(mid == idx)) def test_sum(self): for i in range(3): assert_true(self.g.sum(i).shape[i] == 1) def test_mean(self): for i in range(3): assert_true(self.g.mean(i).shape[i] == 1) def test_cross_section(self): for i in range(3): assert_true(self.g.cross_section(1, i).shape[i] == 1) def test_remove_part(self): for i in range(3): assert_true(self.g.remove_part(1, i, above=True).shape[i] == 1) def test_sub_part(self): for i in range(3): assert_true(self.g.sub_part(1, i, above=False).shape[i] == 1) def test_sub(self): for i in range(3): assert_true(self.g.sub(1, i).shape[i] == 1) for i in range(3): assert_true(self.g.sub([1, 2], i).shape[i] == 2) def test_remove(self): for i in range(3): assert_true(self.g.remove(1, i).shape[i] == self.g.shape[i] - 1) for i in range(3): assert_true( self.g.remove([1, 2], i).shape[i] == self.g.shape[i] - 2) def test_argumentparser(self): self.g.ArgumentParser()
class TestGrid(object): def setUp(self): alat = 1.42 sq3h = 3.**.5 * 0.5 self.sc = SuperCell(np.array([[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) * alat, nsc=[3, 3, 1]) self.g = Grid([10, 10, 100], sc=self.sc) def tearDown(self): del self.sc del self.g def test_append(self): g = self.g.append(self.g, 0) assert_true(np.allclose(g.grid.shape, [20, 10, 100])) g = self.g.append(self.g, 1) assert_true(np.allclose(g.grid.shape, [10, 20, 100])) g = self.g.append(self.g, 2) assert_true(np.allclose(g.grid.shape, [10, 10, 200])) def test_size(self): assert_true(np.allclose(self.g.grid.shape, [10, 10, 100])) def test_item(self): assert_true(np.allclose(self.g[1:2, 1:2, 2:3], self.g.grid[1:2, 1:2, 2:3])) def test_dcell(self): assert_true(np.all(self.g.dcell*self.g.cell >= 0)) def test_dvol(self): assert_true(self.g.dvol > 0) def test_shape(self): assert_true(np.all(self.g.shape == self.g.grid.shape)) def test_dtype(self): assert_true(self.g.dtype == self.g.grid.dtype) def test_copy(self): assert_true(self.g.copy() == self.g) def test_swapaxes(self): g = self.g.swapaxes(0, 1) assert_true(np.allclose(self.g.cell[0,:], g.cell[1,:])) assert_true(np.allclose(self.g.cell[1,:], g.cell[0,:])) def test_interp(self): shape = np.array(self.g.shape, np.int32) g = self.g.interp(shape * 2) g1 = g.interp(shape) assert_true(np.allclose(self.g.grid, g1.grid)) def test_index1(self): mid = np.array(self.g.shape, np.int32) // 2 idx = self.g.index(self.sc.center()) print(mid, idx) assert_true(np.all(mid == idx)) def test_sum(self): for i in range(3): assert_true(self.g.sum(i).shape[i] == 1) def test_mean(self): for i in range(3): assert_true(self.g.mean(i).shape[i] == 1) def test_cross_section(self): for i in range(3): assert_true(self.g.cross_section(1, i).shape[i] == 1) def test_remove_part(self): for i in range(3): assert_true(self.g.remove_part(1, i, above=True).shape[i] == 1) def test_sub_part(self): for i in range(3): assert_true(self.g.sub_part(1, i, above=False).shape[i] == 1) def test_sub(self): for i in range(3): assert_true(self.g.sub(1, i).shape[i] == 1) for i in range(3): assert_true(self.g.sub([1,2], i).shape[i] == 2) def test_remove(self): for i in range(3): assert_true(self.g.remove(1, i).shape[i] == self.g.shape[i]-1) for i in range(3): assert_true(self.g.remove([1,2], i).shape[i] == self.g.shape[i]-2) def test_argumentparser(self): self.g.ArgumentParser()
class TestGrid(object): def setUp(self): alat = 1.42 sq3h = 3.**.5 * 0.5 self.sc = SuperCell(np.array( [[1.5, sq3h, 0.], [1.5, -sq3h, 0.], [0., 0., 10.]], np.float64) * alat, nsc=[3, 3, 1]) self.g = Grid([10, 10, 100], sc=self.sc) self.g[:, :, :] = 2. g = Grid(sc=self.sc) def tearDown(self): del self.sc del self.g def test_append(self): g = self.g.append(self.g, 0) assert_true(np.allclose(g.grid.shape, [20, 10, 100])) g = self.g.append(self.g, 1) assert_true(np.allclose(g.grid.shape, [10, 20, 100])) g = self.g.append(self.g, 2) assert_true(np.allclose(g.grid.shape, [10, 10, 200])) def test_set(self): v = self.g[0, 0, 0] self.g[0, 0, 0] = 3 assert_true(self.g.grid[0, 0, 0] == 3) assert_true(self.g[0, 0, 0] == 3) self.g[0, 0, 0] = v assert_true(self.g[0, 0, 0] == v) def test_size(self): assert_true(np.allclose(self.g.grid.shape, [10, 10, 100])) def test_item(self): assert_true( np.allclose(self.g[1:2, 1:2, 2:3], self.g.grid[1:2, 1:2, 2:3])) def test_dcell(self): assert_true(np.all(self.g.dcell * self.g.cell >= 0)) def test_dvol(self): assert_true(self.g.dvol > 0) def test_shape(self): assert_true(np.all(self.g.shape == self.g.grid.shape)) def test_dtype(self): assert_true(self.g.dtype == self.g.grid.dtype) def test_copy(self): assert_true(self.g.copy() == self.g) assert_false(self.g.copy() != self.g) def test_add(self): g = self.g + self.g assert_true(np.allclose(g.grid, (self.g * 2).grid)) g = self.g.copy() g *= 2 assert_true(np.allclose(g.grid, (self.g * 2).grid)) g = self.g.copy() g /= 2 assert_true(np.allclose(g.grid, (self.g / 2).grid)) def test_swapaxes(self): g = self.g.swapaxes(0, 1) assert_true(np.allclose(self.g.cell[0, :], g.cell[1, :])) assert_true(np.allclose(self.g.cell[1, :], g.cell[0, :])) def test_interp(self): shape = np.array(self.g.shape, np.int32) g = self.g.interp(shape * 2) g1 = g.interp(shape) # Sadly the interpolation does not work as it really # should... # One cannot interp down/up and retrieve the same # grid... Perhaps this is ok, but not good... :( assert_true(np.allclose(self.g.grid, g1.grid)) def test_index1(self): mid = np.array(self.g.shape, np.int32) // 2 idx = self.g.index(self.sc.center()) assert_true(np.all(mid == idx)) def test_sum(self): for i in range(3): assert_true(self.g.sum(i).shape[i] == 1) def test_mean(self): for i in range(3): assert_true(self.g.mean(i).shape[i] == 1) def test_cross_section(self): for i in range(3): assert_true(self.g.cross_section(1, i).shape[i] == 1) def test_remove_part(self): for i in range(3): assert_true(self.g.remove_part(1, i, above=True).shape[i] == 1) def test_sub_part(self): for i in range(3): assert_true(self.g.sub_part(1, i, above=False).shape[i] == 1) assert_true( self.g.sub_part(1, i, above=True).shape[i] == self.g.shape[i] - 1) def test_sub(self): for i in range(3): assert_true(self.g.sub(1, i).shape[i] == 1) for i in range(3): assert_true(self.g.sub([1, 2], i).shape[i] == 2) def test_remove(self): for i in range(3): assert_true(self.g.remove(1, i).shape[i] == self.g.shape[i] - 1) for i in range(3): assert_true( self.g.remove([1, 2], i).shape[i] == self.g.shape[i] - 2) def test_argumentparser(self): self.g.ArgumentParser()