def get_displaced_structures(pmg_structure, atom_disp=0.01, supercell_matrix=None, yaml_fname=None, **kwargs): """ Generate a set of symmetrically inequivalent displaced structures for phonon calculations. Args: pmg_structure (Structure): A pymatgen structure object. atom_disp (float): Atomic displacement. Default is 0.01 $\\AA$. supercell_matrix (3x3 array): Scaling matrix for supercell. yaml_fname (string): If not None, it represents the full path to the outputting displacement yaml file, e.g. disp.yaml. **kwargs: Parameters used in Phonopy.generate_displacement method. Return: A list of symmetrically inequivalent structures with displacements, in which the first element is the perfect supercell structure. """ is_plusminus = kwargs.get("is_plusminus", "auto") is_diagonal = kwargs.get("is_diagonal", True) is_trigonal = kwargs.get("is_trigonal", False) ph_structure = get_phonopy_structure(pmg_structure) if supercell_matrix is None: supercell_matrix = np.eye(3) * np.array((1, 1, 1)) phonon = Phonopy(unitcell=ph_structure, supercell_matrix=supercell_matrix) phonon.generate_displacements(distance=atom_disp, is_plusminus=is_plusminus, is_diagonal=is_diagonal, is_trigonal=is_trigonal) if yaml_fname is not None: displacements = phonon.get_displacements() directions = phonon.get_displacement_directions() write_disp_yaml(displacements=displacements, supercell=phonon.get_supercell(), directions=directions, filename=yaml_fname) # Supercell structures with displacement disp_supercells = phonon.get_supercells_with_displacements() # Perfect supercell structure init_supercell = phonon.get_supercell() # Structure list to be returned structure_list = [get_pmg_structure(init_supercell)] for c in disp_supercells: if c is not None: structure_list.append(get_pmg_structure(c)) return structure_list
class PhononFC3Base(TaskElement): """PhononFC3Base class This is an interface to anharmonic phonopy. """ def __init__(self, directory=None, name=None, supercell_matrix=None, primitive_matrix=None, distance=None, is_diagonal=True, check_imaginary=True, cutoff_frequency=None, lattice_tolerance=None, force_tolerance=None, pressure_target=None, stress_tolerance=None, max_increase=None, max_iteration=None, min_iteration=None, is_cell_relaxed=False, traverse=False): TaskElement.__init__(self) self._directory = directory if not name: self._name = directory else: self._name = name self._task_type = "anharmonic_phonon" self._supercell_matrix = supercell_matrix self._primitive_matrix = primitive_matrix self._distance = distance self._is_diagonal = is_diagonal self._check_imaginary = check_imaginary self._cutoff_frequency = cutoff_frequency # determine imaginary freq. self._lattice_tolerance = lattice_tolerance self._pressure_target = pressure_target self._stress_tolerance = stress_tolerance self._force_tolerance = force_tolerance self._max_increase = max_increase self._max_iteration = max_iteration self._min_iteration = min_iteration self._traverse = traverse self._is_cell_relaxed = is_cell_relaxed self._stage = 0 self._tasks = [] self._energy = None self._cell = None self._phonon = None # Phonopy object self._phonon_fc3 = None # Phono3py object self._phonon_fc3_tasks = None def get_phonon(self): return self._phonon def get_phonon_fc3(self): for i, task in enumerate(self._phonon_fc3_tasks[1:]): forces_fc3.append(task.get_properties()['forces'][-1]) disp_dataset = self._phonon_fc3.get_displacement_dataset() self._phonon_fc3.produce_fc3(forces_fc3) return self._phonon_fc3 def get_cell(self): if self._is_cell_relaxed: return self._cell else: return self._phonon_fc3_tasks[0].get_cell() def get_energy(self): """Return energies at geometry optimization steps""" return self._energy def set_status(self): if self._stage == 0: task = self._tasks[0] if task.done(): status = task.get_status() if status == "done": self._status = "next" else: self._status = status else: done = True terminate = False for i, task in enumerate(self._tasks): done &= task.done() terminate |= (task.get_status() == "terminate") if done: if terminate: self._status = "terminate" else: self._status = "next" self._write_yaml() def begin(self): if not self._job: print("set_job has to be executed.") raise RuntimeError if self._is_cell_relaxed: self._phonon_fc3_tasks = [None] self._set_stage1() else: self._set_stage0() def end(self): pass def done(self): return (self._status == "terminate" or self._status == "done" or self._status == "max_iteration" or self._status == "next" or self._status == "imaginary_mode") def __next__(self): return self.next() def next(self): if self._stage == 0: if "next" in self._status: self._energy = self._tasks[0].get_energy() self._comment = "%s\\n%f" % ( self._tasks[0].get_space_group()['international'], self._energy) self._set_stage1() return self._tasks elif "terminate" in self._status and self._traverse == "restart": self._traverse = False self._set_stage0() return self._tasks else: raise StopIteration elif self._stage == 1: if "next" in self._status: disp_dataset = self._phonon_fc3.get_displacement_dataset() for disp1, task in zip(disp_dataset['first_atoms'], self._tasks): disp1['forces'] = task.get_properties()['forces'][-1] write_FORCE_SETS(disp_dataset) self._phonon.set_displacement_dataset(disp_dataset) self._phonon.produce_force_constants( calculate_full_force_constants=False) if self._exist_imaginary_mode(): self._status = "imaginary_mode" self._write_yaml() self._tasks = [] raise StopIteration else: self._set_stage2() return self._tasks elif "terminate" in self._status and self._traverse == "restart": self._reset_stage1() return self._tasks else: raise StopIteration elif self._stage == 2: if "next" in self._status: self._status = "done" forces_fc3 = [] for i, task in enumerate(self._phonon_fc3_tasks[1:]): forces_fc3.append(task.get_properties()['forces'][-1]) disp_dataset = self._phonon_fc3.get_displacement_dataset() write_FORCES_FC3(disp_dataset, forces_fc3) self._tasks = [] raise StopIteration elif "terminate" in self._status and self._traverse == "restart": self._reset_stage2() return self._tasks else: raise StopIteration else: # stage2 pass def _set_stage0(self): self._status = "equilibrium" task = self._get_equilibrium_task() self._phonon_fc3_tasks = [task] self._tasks = [task] def _set_stage1(self): self._set_phonon_fc3() if self._check_imaginary: self._stage = 1 self._status = "fc2_displacements" disp_dataset = self._phonon_fc3.get_displacement_dataset() self._tasks = self._get_displacement_tasks( stop=len(disp_dataset['first_atoms'])) self._phonon_fc3_tasks += self._tasks else: self._set_stage2() def _reset_stage1(self): self._traverse = False disp_terminated = [] for i, task in enumerate(self._tasks): if task.get_status() == "terminate": disp_terminated.append(i) disp_dataset = self._phonon_fc3.get_displacement_dataset() tasks = self._get_displacement_tasks( stop=len(disp_dataset['first_atoms'])) self._tasks = [] for i in disp_terminated: self._tasks.append(tasks[i]) self._phonon_fc3_tasks[i + 1] = tasks[i] self._status = "fc2_displacements" def _set_stage2(self): self._stage = 2 self._status = "fc3_displacements" if self._check_imaginary: disp_dataset = self._phonon_fc3.get_displacement_dataset() start_index = len(disp_dataset['first_atoms']) else: start_index = 0 self._tasks = self._get_displacement_tasks(start=start_index) self._phonon_fc3_tasks += self._tasks def _reset_stage2(self): self._traverse = False disp_terminated = [] for i, task in enumerate(self._tasks): if task.get_status() == "terminate": disp_terminated.append(i) if self._check_imaginary: disp_dataset = self._phonon_fc3.get_displacement_dataset() start_index = len(disp_dataset['first_atoms']) else: start_index = 0 tasks = self._get_displacement_tasks(start=start_index) self._tasks = [] for i in disp_terminated: self._tasks.append(tasks[i]) self._phonon_fc3_tasks[i + 1 + start_index] = tasks[i] self._status = "fc3_displacements" def _set_phonon_fc3(self): cell = self.get_cell() phonopy_cell = cell2atoms(cell) self._phonon = Phonopy(phonopy_cell, self._supercell_matrix, primitive_matrix=self._primitive_matrix, dynamical_matrix_decimals=14, force_constants_decimals=14) self._phonon_fc3 = Phono3py(phonopy_cell, self._supercell_matrix, primitive_matrix=self._primitive_matrix) self._phonon_fc3.generate_displacements(distance=self._distance, is_diagonal=self._is_diagonal) supercell = self._phonon_fc3.get_supercell() disp_dataset = self._phonon_fc3.get_displacement_dataset() self._phonon.set_displacement_dataset(disp_dataset) write_poscar(cell, "POSCAR-unitcell") write_disp_yaml(self._phonon.get_displacements(), supercell, directions=self._phonon.get_displacement_directions()) write_disp_fc3_yaml(disp_dataset, supercell) def _exist_imaginary_mode(self): if self._primitive_matrix is None: pmat = np.eye(3) else: pmat = self._primitive_matrix exact_point_matrix = np.dot(np.linalg.inv(self._supercell_matrix), pmat).T max_integer = np.rint(np.amax(np.abs(np.linalg.inv(exact_point_matrix)))) q_points = [] for i in np.arange(-max_integer, max_integer + 1): for j in np.arange(-max_integer, max_integer + 1): for k in np.arange(-max_integer, max_integer + 1): q = np.dot(exact_point_matrix, [i, j, k]) if (-1 < q).all() and (q < 1).all(): q_points.append(q) self._phonon.set_qpoints_phonon(q_points) frequencies = self._phonon.get_qpoints_phonon()[0] if (frequencies < self._cutoff_frequency).any(): self._log = "Stop at phonon calculation due to imaginary modes" return True else: return False def _write_yaml(self): w = open("%s.yaml" % self._directory, 'w') if self._lattice_tolerance is not None: w.write("lattice_tolerance: %f\n" % self._lattice_tolerance) if self._stress_tolerance is not None: w.write("stress_tolerance: %f\n" % self._stress_tolerance) w.write("pressure_target: %f\n" % self._pressure_target) w.write("force_tolerance: %f\n" % self._force_tolerance) if self._max_increase is None: w.write("max_increase: unset\n") else: w.write("max_increase: %f\n" % self._max_increase) w.write("max_iteration: %d\n" % self._max_iteration) w.write("min_iteration: %d\n" % self._min_iteration) w.write("supercell_matrix:\n") for row in self._supercell_matrix: w.write("- [ %3d, %3d, %3d ]\n" % tuple(row)) if self._primitive_matrix is not None: w.write("primitive_matrix:\n") for row in self._primitive_matrix: w.write("- [ %6.3f, %6.3f, %6.3f ]\n" % tuple(row)) w.write("distance: %f\n" % self._distance) if self._phonon_fc3_tasks[0] is not None: w.write("iteration: %d\n" % self._phonon_fc3_tasks[0].get_stage()) if self._energy: w.write("electric_total_energy: %20.10f\n" % self._energy) w.write("status: %s\n" % self._status) w.write("tasks:\n") for task in self._phonon_fc3_tasks: if task and task.get_status(): w.write("- name: %s\n" % task.get_name()) w.write(" status: %s\n" % task.get_status()) w.close()
def phonopy_run(phonon, single=True, filename='FORCE_SETS'): """Run the phonon calculations, using PhonoPy. The force constants are then stored in the Phonon ASE object. input: phonon: ASE Phonon object with Atoms and Calculator single: when True, the forces are computed only for a single step, and then exit. This allows to split the loop in independent iterations. When calling again the 'run' method, already computed steps are ignored, missing steps are completed, until no more are needed. When set to False, all steps are done in a row. output: True when a calculation step was performed, False otherwise or no more is needed. nb_of_iterations: the number of steps remaining """ from phonopy import Phonopy from phonopy.structure.atoms import Atoms as PAtoms from phonopy.structure.atoms import PhonopyAtoms import phonopy.file_IO as file_IO # we first look if an existing phonon pickle exists. This is the case if we # are running with iterative calls while return value is True. The first call # will then create the objects, which are subsequently updated until False. # Set calculator if provided # assert phonon.calc is not None, "Provide calculator in Phonon __init__ method" # Atoms in the supercell -- repeated in the lattice vector directions # beginning with the last supercell = phonon.atoms * phonon.N_c # create a PhonopyAtoms object cell = PhonopyAtoms(phonon.atoms.get_chemical_symbols(), positions=phonon.atoms.get_positions(), cell=phonon.atoms.get_cell(), magmoms=None) # is there an existing PhonoPy calculation ? # using factor=6.46541380e-2=VaspToeV if os.path.exists('FORCE_SETS'): phonpy = Phonopy(cell, numpy.diag(phonon.N_c), primitive_matrix=None, dynamical_matrix_decimals=None, force_constants_decimals=None, symprec=1e-05, is_symmetry=True, use_lapack_solver=False, log_level=1) force_sets = file_IO.parse_FORCE_SETS(filename='FORCE_SETS') phonpy.set_displacement_dataset(force_sets) # inactivate magmoms in supercell as some calculators do not provide that phonpy._supercell.magmoms = None phonpy.produce_force_constants(calculate_full_force_constants=False) else: # create a PhonoPy Phonon object. phonpy = Phonopy(cell, numpy.diag(phonon.N_c)) # generate displacements (minimal set) phonpy.generate_displacements(distance=0.01) # iterative call for all displacements set_of_forces, flag, nb_of_iterations = phonopy_run_calculate( phonon, phonpy, supercell, single) if flag is True: return nb_of_iterations # some more work is required sys.stdout.write('[ASE/Phonopy] Computing force constants\n') # use symmetry to derive forces in equivalent displacements phonpy.produce_force_constants(forces=set_of_forces) # generate disp.yaml and FORCE_SETS (for later use) displacements = phonpy.get_displacements() directions = phonpy.get_displacement_directions() file_IO.write_disp_yaml(displacements, phonpy.get_supercell(), directions=directions) file_IO.write_FORCE_SETS(phonpy.get_displacement_dataset()) # store as additional data in atoms 'info' phonon.atoms.info["phonopy"] = phonpy # save the PhonoPy object fid = opencew("phonopy.pkl") if fid is not None and rank == 0: print("[ASE/Phonopy] Writing %s" % "phonopy.pkl") pickle.dump(phonpy, fid, protocol=2) fid.close() # transfer results to the ASE phonon object # Number of atoms (primitive cell) natoms = len(phonon.indices) # Number of unit cells (supercell) N = numpy.prod(phonon.N_c) # Phonopy: force_constants size is [N*natoms,N*natoms,3,3] # Phi[i,j,a,b] with [i,j = atom in supercell] and [a,b=xyz] force_constants = phonpy.get_force_constants() # the atoms [i] which are moved are in the first cell of the supercell, i.e.Ni=0 # the forces are then stored for all atoms [Nj,j] as [3,3] matrices # we compute the sum on all supercells, which all contain n atoms. C_N = numpy.zeros((N, 3 * natoms, 3 * natoms), dtype=complex) Ni = 0 for Nj in range(N): for ni in range(natoms): Nni = ni for nj in range(natoms): # compute Nn indices Nnj = Nj * natoms + nj # get fc 3x3 matrix C_N[Nj, (3 * ni):(3 * ni + 3), (3 * nj):(3 * nj + 3)] += force_constants[Nni][Nnj] # convert to ASE storage # ASE: phonon.C_N size is be [N, 3*natoms, 3*natoms] # Phi[i,j] = Phi[j,i] phonon.C_N = C_N # fill dynamical matrix (mass prefactor) phonon.D_N = phonon.C_N.copy() # Add mass prefactor m_a = phonon.atoms.get_masses() phonon.m_inv_x = numpy.repeat(m_a[phonon.indices]**-0.5, 3) M_inv = numpy.outer(phonon.m_inv_x, phonon.m_inv_x) for D in phonon.D_N: D *= M_inv return 0 # nothing left to do