def __init__(self, nspinor, nsppol, nspden, datar, structure, iorder="c"): """ Args: nspinor: Number of spinorial components. nsppol: Number of spins. nspden: Number of spin density components. datar: [nspden, nx, ny, nz] array with the scalar field in real space. See also ``read_denpot``. structure: |Structure| object describing the crystalline structure. iorder: Order of the array. "c" for C ordering, "f" for Fortran ordering. """ self.nspinor, self.nsppol, self.nspden = nspinor, nsppol, nspden # Convert to Abipy Structure. self._structure = Structure.as_structure(structure) iorder = iorder.lower() assert iorder in ["f", "c"] if iorder == "f": # (z,x,y) --> (x,y,z) datar = transpose_last3dims(datar) # Init Mesh3D mesh_shape = datar.shape[-3:] self._mesh = Mesh3D(mesh_shape, structure.lattice.matrix) # Make sure we have the correct shape. self._datar = np.reshape(datar, (nspden,) + self.mesh.shape)
def __init__(self, nspinor, nsppol, nspden, datar, structure, iorder="c"): """ Args: nspinor: Number of spinorial components. nsppol: Number of spins. nspden: Number of spin density components. datar: numpy array with the scalar field in real space. structure: pymatgen structure iorder: Order of the array. "c" for C ordering, "f" for Fortran ordering. """ self.nspinor = nspinor self.nsppol = nsppol self.nspden = nspden self.structure = structure self.datar = datar if iorder.lower() == "f": # (z,x,y) --> (x,y,z) self.datar = transpose_last3dims(self.datar) # Init Mesh3D mesh_shape = self.datar.shape[-3:] self.mesh = Mesh3D(mesh_shape, structure.lattice_vectors(), pbc=True) # Make sure we have the correct shape. self.datar = np.reshape(self.datar, (nspden,) + self.mesh.shape) # FFT R --> G. self.datag = self.mesh.fft_r2g(self.datar)
def __init__(self, nspinor, nsppol, nspden, datar, structure, iorder="c"): """ Args: nspinor: Number of spinorial components. nsppol: Number of spins. nspden: Number of spin density components. datar: numpy array with the scalar field in real space. shape [..., nx, ny, nz] structure: :class:`Structure` object describing the crystalline structure. iorder: Order of the array. "c" for C ordering, "f" for Fortran ordering. """ self.nspinor, self.nsppol, self.nspden = nspinor, nsppol, nspden self._structure = structure iorder = iorder.lower() assert iorder in ["f", "c"] if iorder == "f": # (z,x,y) --> (x,y,z) datar = transpose_last3dims(datar) # Init Mesh3D mesh_shape = datar.shape[-3:] self._mesh = Mesh3D(mesh_shape, structure.lattice_vectors()) # Make sure we have the correct shape. self._datar = np.reshape(datar, (nspden,) + self.mesh.shape)
def xsf_write_data(file, structure, data, add_replicas=True, cplx_mode=None): """ Write data in the Xcrysden format (XSF) Args: file: file-like object. structure: :class:`Structure` object. data: array-like object in C-order, i.e data[nx,ny,nz] add_replicas: If True, data is padded with redundant data points. in order to have a periodic 3D array of shape=(nx+1,ny+1,nz+1). cplx_mode: string defining the data to print when data is a complex array. Possible choices are (case-insensitive): - "re" for real part. - "im" for imaginary part. - "abs" for the absolute value """ fwrite = file.write # Check this one if add_replicas: data = add_periodic_replicas(data) if np.iscomplexobj(data): if cplx_mode is None: raise TypeError( "cplx_mode must be specified when data is a complex array.") cplx_mode = cplx_mode.lower() if cplx_mode == "re": data = data.real elif cplx_mode == "im": data = data.imag elif cplx_mode == "abs": data = np.abs(data) else: raise ValueError("Wrong value for cplx_mode: %s" % cplx_mode) shape = data.shape ndim = data.ndim if ndim == 3: ngrids = 1 data = np.asarray([data]) elif ndim == 4: ngrids = shape[0] else: raise ValueError("ndim %d is not supported" % ndim) # Xcrysden uses Fortran-order. # Transpose (...,x,y,z) --> (...,z,y,x) to speed up the write below. fdata = transpose_last3dims(data) fgrid = fdata.shape[-3:] cell = structure.lattice_vectors(space="r") origin = np.zeros(3) fwrite('BEGIN_BLOCK_DATAGRID_3D\n') fwrite(' data\n') for dg in range(ngrids): fwrite(" BEGIN_DATAGRID_3Dgrid#" + str(dg + 1) + "\n") fwrite('%d %d %d\n' % shape[-3:]) fwrite('%f %f %f\n' % tuple(origin)) for i in range(3): fwrite('%f %f %f\n' % tuple(cell[i])) for z in range(fgrid[0]): for y in range(fgrid[1]): slice_x = fdata[dg, z, y] fwrite(' '.join(['%f' % d for d in slice_x])) fwrite('\n') fwrite('\n') fwrite(' END_DATAGRID_3D\n') fwrite('END_BLOCK_DATAGRID_3D\n')
def xsf_write_data(file, structure, data, add_replicas=True, cplx_mode=None): """ Write data in the Xcrysden format (XSF) Args: file: file-like object. structure: :class:`Structure` object. data: array-like object in C-order, i.e data[nx,ny,nz] add_replicas: If True, data is padded with redundant data points. in order to have a periodic 3D array of shape=(nx+1,ny+1,nz+1). cplx_mode: string defining the data to print when data is a complex array. Possible choices are (case-insensitive): - "re" for real part. - "im" for imaginary part. - "abs" for the absolute value """ fwrite = file.write # Check this one if add_replicas: data = add_periodic_replicas(data) if np.iscomplexobj(data): if cplx_mode is None: raise TypeError("cplx_mode must be specified when data is a complex array.") cplx_mode = cplx_mode.lower() if cplx_mode == "re": data = data.real elif cplx_mode == "im": data = data.imag elif cplx_mode == "abs": data = np.abs(data) else: raise ValueError("Wrong value for cplx_mode: %s" % cplx_mode) shape = data.shape ndim = data.ndim if ndim == 3: ngrids = 1 data = np.asarray([data]) elif ndim == 4: ngrids = shape[0] else: raise ValueError("ndim %d is not supported" % ndim) # Xcrysden uses Fortran-order. # Transpose (...,x,y,z) --> (...,z,y,x) to speed up the write below. fdata = transpose_last3dims(data) fgrid = fdata.shape[-3:] cell = structure.lattice_vectors(space="r") origin = np.zeros(3) fwrite('BEGIN_BLOCK_DATAGRID_3D\n') fwrite(' data\n') for dg in range(ngrids): fwrite(" BEGIN_DATAGRID_3Dgrid#" + str(dg+1) + "\n") fwrite('%d %d %d\n' % shape[-3:]) fwrite('%f %f %f\n' % tuple(origin)) for i in range(3): fwrite('%f %f %f\n' % tuple(cell[i])) for z in range(fgrid[0]): for y in range(fgrid[1]): slice_x = fdata[dg,z,y] fwrite(' '.join(['%f' % d for d in slice_x]) ) fwrite('\n') fwrite('\n') fwrite(' END_DATAGRID_3D\n') fwrite('END_BLOCK_DATAGRID_3D\n')
def xsf_write_data(file, structure, cdata, add_replicas): """ Write cdata in the Xcrysden format (XSF) Args: file: file-like object. structure: Structure object. cdata: array-like object in C-order add_replicas: If True, cdata is padded with redundant data points. in order to have a periodic 3D array of shape=(nx+1,ny+1,nz+1). """ # TODO warnings.warn("This code should be checked.") fwrite = file.write # Check this one if add_replicas: cdata = add_periodic_replicas(cdata) if cdata.dtype == complex: raise NotImplementedError("Writing of complex arrays not coded yet.") cdata = np.abs(cdata) cshape = cdata.shape ndim = cdata.ndim if ndim == 3: ngrids = 1 cdata = np.asarray([cdata]) elif ndim == 4: ngrids = cshape[0] else: raise ValueError("ndim %d is not supported" % ndim) # Xcrysden uses Fortran-order. # Transpose (...,x,y,z) --> (...,z,y,x) to speed up the write below. fdata = transpose_last3dims(cdata) fgrid = fdata.shape[-3:] cell = structure.lattice_vectors(space="r") #* Bohr_Ang origin = np.zeros(3) fwrite('BEGIN_BLOCK_DATAGRID_3D\n') fwrite(' data\n') for dg in range(ngrids): fwrite(" BEGIN_DATAGRID_3Dgrid#" + str(dg+1) + "\n") fwrite('%d %d %d\n' % cshape[-3:]) fwrite('%f %f %f\n' % tuple(origin)) for i in range(3): fwrite('%f %f %f\n' % tuple(cell[i])) for z in range(fgrid[0]): for y in range(fgrid[1]): slice_x = fdata[dg,z,y] fwrite(' '.join(['%f' % d for d in slice_x]) ) #np.savetxt(file, slice_x) fwrite('\n') fwrite('\n') fwrite(' END_DATAGRID_3D\n') fwrite('END_BLOCK_DATAGRID_3D\n')