def __init__(self, **kwargs): self.forceconstants = kwargs.pop('forceconstants') self.is_classic = bool(kwargs.pop('is_classic', False)) if 'temperature' in kwargs: self.temperature = float(kwargs['temperature']) self.folder = kwargs.pop('folder', FOLDER_NAME) self.kpts = kwargs.pop('kpts', (1, 1, 1)) self._grid_type = kwargs.pop('grid_type', 'C') self._reciprocal_grid = Grid(self.kpts, order=self._grid_type) self.is_unfolding = kwargs.pop('is_unfolding', False) if self.is_unfolding: logging.info('Using unfolding.') self.kpts = np.array(self.kpts) self.min_frequency = kwargs.pop('min_frequency', 0) self.max_frequency = kwargs.pop('max_frequency', None) self.broadening_shape = kwargs.pop('broadening_shape', 'gauss') self.is_nw = kwargs.pop('is_nw', False) self.third_bandwidth = kwargs.pop('third_bandwidth', None) self.storage = kwargs.pop('storage', 'formatted') self.is_symmetrizing_frequency = kwargs.pop( 'is_symmetrizing_frequency', False) self.is_antisymmetrizing_velocity = kwargs.pop( 'is_antisymmetrizing_velocity', False) self.is_balanced = kwargs.pop('is_balanced', False) self.atoms = self.forceconstants.atoms self.supercell = np.array(self.forceconstants.supercell) self.n_k_points = int(np.prod(self.kpts)) self.n_atoms = self.forceconstants.n_atoms self.n_modes = self.forceconstants.n_modes self.n_phonons = self.n_k_points * self.n_modes self.is_able_to_calculate = True self.hbar = units._hbar if self.is_classic: self.hbar = self.hbar * 1e-6
def from_supercell(cls, atoms, supercell, grid_type, value=None, folder='kALDo'): _direct_grid = Grid(supercell, grid_type) replicated_positions = _direct_grid.grid(is_wrapping=False).dot(atoms.cell)[:, np.newaxis, :] + atoms.positions[ np.newaxis, :, :] inst = cls(atoms=atoms, replicated_positions=replicated_positions, supercell=supercell, value=value, folder=folder) inst._direct_grid = _direct_grid return inst
def read_third_order_matrix(third_file, atoms, supercell, order='C'): n_unit_atoms = atoms.positions.shape[0] n_replicas = np.prod(supercell) third_order = np.zeros((n_unit_atoms, 3, n_replicas, n_unit_atoms, 3, n_replicas, n_unit_atoms, 3)) second_cell_list = [] third_cell_list = [] current_grid = Grid(supercell, order=order).grid(is_wrapping=True) list_of_index = current_grid list_of_replicas = list_of_index.dot(atoms.cell) with open(third_file, 'r') as file: line = file.readline() n_third = int(line) for i in range(n_third): file.readline() file.readline() second_cell_position = np.fromstring(file.readline(), dtype=np.float, sep=' ') second_cell_index = second_cell_position.dot( np.linalg.inv(atoms.cell)).round(0).astype(int) second_cell_list.append(second_cell_index) # create mask to find the index second_cell_id = (list_of_index[:] == second_cell_index).prod( axis=1) second_cell_id = np.argwhere(second_cell_id).flatten() third_cell_position = np.fromstring(file.readline(), dtype=np.float, sep=' ') third_cell_index = third_cell_position.dot( np.linalg.inv(atoms.cell)).round(0).astype(int) third_cell_list.append(third_cell_index) # create mask to find the index third_cell_id = (list_of_index[:] == third_cell_index).prod(axis=1) third_cell_id = np.argwhere(third_cell_id).flatten() atom_i, atom_j, atom_k = np.fromstring( file.readline(), dtype=np.int, sep=' ') - 1 for _ in range(27): values = np.fromstring(file.readline(), dtype=np.float, sep=' ') alpha, beta, gamma = values[:3].round(0).astype(int) - 1 third_order[atom_i, alpha, second_cell_id, atom_j, beta, third_cell_id, atom_k, gamma] = values[3] third_order = third_order.reshape( (n_unit_atoms * 3, n_replicas * n_unit_atoms * 3, n_replicas * n_unit_atoms * 3)) return third_order
def __init__(self, *kargs, **kwargs): Observable.__init__(self, *kargs, **kwargs) self.atoms = kwargs['atoms'] replicated_positions = kwargs['replicated_positions'] self.supercell = kwargs['supercell'] try: self.value = kwargs['value'] except KeyError: self.value = None self._replicated_atoms = None self.replicated_positions = replicated_positions.reshape( (-1, self.atoms.positions.shape[0], self.atoms.positions.shape[1])) self.n_replicas = np.prod(self.supercell) self._cell_inv = None self._replicated_cell_inv = None self._list_of_replicas = None n_replicas, n_unit_atoms, _ = self.replicated_positions.shape atoms_positions = self.atoms.positions detected_grid = np.round( (replicated_positions.reshape((n_replicas, n_unit_atoms, 3)) - atoms_positions[np.newaxis, :, :]).dot( np.linalg.inv(self.atoms.cell))[:, 0, :], 0).astype(np.int) grid_c = Grid(grid_shape=self.supercell, order='C') grid_fortran = Grid(grid_shape=self.supercell, order='F') if (grid_c.grid(is_wrapping=False) == detected_grid).all(): grid_type = 'C' logging.debug("Using C-style position grid") elif (grid_fortran.grid(is_wrapping=False) == detected_grid).all(): grid_type = 'F' logging.debug("Using fortran-style position grid") else: err_msg = "Unable to detect grid type" logging.error(err_msg) raise TypeError(err_msg) self._direct_grid = Grid(self.supercell, grid_type)
def read_third_order_matrix_2(third_file, atoms, third_supercell, order='C'): supercell = third_supercell n_unit_atoms = atoms.positions.shape[0] n_replicas = np.prod(supercell) current_grid = Grid(third_supercell, order=order).grid(is_wrapping=True) list_of_index = current_grid list_of_replicas = list_of_index.dot(atoms.cell) replicated_cell = atoms.cell * supercell # replicated_cell_inv = np.linalg.inv(replicated_cell) coords = [] data = [] second_cell_positions = [] third_cell_positions = [] atoms_coords = [] sparse_data = [] with open(third_file, 'r') as file: line = file.readline() n_third = int(line) for i in range(n_third): file.readline() file.readline() second_cell_position = np.fromstring(file.readline(), dtype=np.float, sep=' ') second_cell_positions.append(second_cell_position) d_1 = list_of_replicas[:, :] - second_cell_position[np.newaxis, :] # d_1 = wrap_coordinates(d_1, replicated_cell, replicated_cell_inv) mask_second = np.linalg.norm(d_1, axis=1) < 1e-5 second_cell_id = np.argwhere(mask_second).flatten() third_cell_position = np.fromstring(file.readline(), dtype=np.float, sep=' ') third_cell_positions.append(third_cell_position) d_2 = list_of_replicas[:, :] - third_cell_position[np.newaxis, :] # d_2 = wrap_coordinates(d_2, replicated_cell, replicated_cell_inv) mask_third = np.linalg.norm(d_2, axis=1) < 1e-5 third_cell_id = np.argwhere(mask_third).flatten() atom_i, atom_j, atom_k = np.fromstring( file.readline(), dtype=np.int, sep=' ') - 1 atoms_coords.append([atom_i, atom_j, atom_k]) small_data = [] for _ in range(27): values = np.fromstring(file.readline(), dtype=np.float, sep=' ') alpha, beta, gamma = values[:3].round(0).astype(int) - 1 coords.append([ atom_i, alpha, second_cell_id, atom_j, beta, third_cell_id, atom_k, gamma ]) data.append(values[3]) small_data.append(values[3]) sparse_data.append(small_data) third_order = COO(np.array(coords).T, np.array(data), shape=(n_unit_atoms, 3, n_replicas, n_unit_atoms, 3, n_replicas, n_unit_atoms, 3)) third_order = third_order.reshape( (n_unit_atoms * 3, n_replicas * n_unit_atoms * 3, n_replicas * n_unit_atoms * 3)) return third_order, np.array(sparse_data), np.array( second_cell_positions), np.array(third_cell_positions), np.array( atoms_coords)