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 get_forces_txt(force_sets): w = StringIO.StringIO() from phono3py.file_IO import write_FORCES_FC3 write_FORCES_FC3(force_sets.get_datasets3(), force_sets.get_forces3(), fp=w) w.seek(0) lines = w.read() w.close() return lines
def create_supercells_with_displacements(phono3py): phono3py.generate_displacements(distance=0.03) scells_with_disps = phono3py.get_supercells_with_displacements() # from phonopy.interface.vasp import write_vasp # for i, scell in enumerate(scells_with_disps): # write_vasp("POSCAR-%05d" % (i + 1), scell) # # print(scell) # A dataset of displacements. The dictionary format is shown at # phono3py.phonon3.displacement_fc3.get_third_order_displacements. print("Displacement sets") disp_dataset = phono3py.get_displacement_dataset() count = 0 for i, disp1 in enumerate(disp_dataset['first_atoms']): print("%4d: %4d %s" % ( count + 1, disp1['number'] + 1, np.around(disp1['displacement'], decimals=3))) count += 1 distances = [] for i, disp1 in enumerate(disp_dataset['first_atoms']): for j, disp2 in enumerate(disp1['second_atoms']): print("%4d: %4d-%4d (%6.3f) %s %s" % ( count + 1, disp1['number'] + 1, disp2['number'] + 1, disp2['pair_distance'], np.around(disp1['displacement'], decimals=3), np.around(disp2['displacement'], decimals=3))) distances.append(disp2['pair_distance']) count += 1 # Find unique pair distances distances = np.array(distances) distances_int = (distances * 1e5).astype(int) unique_distances = np.unique(distances_int) * 1e-5 # up to 5 decimals print("Unique pair distances") print(unique_distances) # FORCES_FC3 is created as follows: from phono3py.file_IO import write_FORCES_FC3 # force_sets is a simple force array: # [len(scells_with_disps), num_supercell_atoms, 3]. force_sets = parse_FORCES_FC3(disp_dataset, filename="FORCES_FC3") write_FORCES_FC3(disp_dataset, force_sets, filename="FORCES_FC3_new")
def create_supercells_with_displacements(phono3py): phono3py.generate_displacements(distance=0.03) scells_with_disps = phono3py.get_supercells_with_displacements() print(len(scells_with_disps)) # from phonopy.interface.vasp import write_vasp # for i, scell in enumerate(scells_with_disps): # write_vasp("POSCAR-%05d" % (i + 1), scell) # # print(scell) # A dataset of displacements. The dictionary format is shown at # phono3py.phonon3.displacement_fc3.get_third_order_displacements. disp_dataset = phono3py.get_displacement_dataset() # FORCES_FC3 is created as follows: from phono3py.file_IO import write_FORCES_FC3 # force_sets is a simple force array: # [len(scells_with_disps), num_supercell_atoms, 3]. force_sets = parse_FORCES_FC3(disp_dataset, filename="FORCES_FC3") write_FORCES_FC3(disp_dataset, force_sets, filename="FORCES_FC3_new")
def create_FORCES_FC3_and_FORCES_FC2_then_exit(settings, input_filename, output_filename, log_level): interface_mode = settings.calculator ##################### # Create FORCES_FC3 # ##################### if settings.create_forces_fc3 or settings.create_forces_fc3_file: if input_filename is None: disp_fc3_filename = 'disp_fc3.yaml' else: disp_fc3_filename = 'disp_fc3.' + input_filename + '.yaml' ph3py_yaml = None disp_filenames = files_exist(['phono3py_disp.yaml', disp_fc3_filename], log_level, is_any=True) if disp_filenames[0] == 'phono3py_disp.yaml': try: ph3py_yaml = Phono3pyYaml() ph3py_yaml.read('phono3py_disp.yaml') if ph3py_yaml.calculator is not None: interface_mode = ph3py_yaml.calculator # overwrite disp_filename = 'phono3py_disp.yaml' except KeyError: file_exists('disp_fc3.yaml', log_level) if log_level > 0: print("\"phono3py_disp.yaml\" was found but wasn't used.") disp_filename = disp_fc3_filename else: disp_filename = disp_filenames[0] if ph3py_yaml is None: file_exists(disp_filename, log_level) disp_dataset = parse_disp_fc3_yaml(filename=disp_filename) else: disp_dataset = ph3py_yaml.dataset if log_level: print('') print("Displacement dataset was read from \"%s\"." % disp_filename) num_atoms = disp_dataset['natom'] num_disps = len(disp_dataset['first_atoms']) for d1 in disp_dataset['first_atoms']: for d2 in d1['second_atoms']: if 'included' not in d2 or d2['included']: num_disps += 1 if settings.create_forces_fc3_file: file_exists(settings.create_forces_fc3_file, log_level) force_filenames = [ x.strip() for x in open(settings.create_forces_fc3_file) ] else: force_filenames = settings.create_forces_fc3 for filename in force_filenames: file_exists(filename, log_level) if log_level > 0: print("Number of displacements: %d" % num_disps) print("Number of supercell files: %d" % len(force_filenames)) if not check_number_of_force_files(num_disps, force_filenames, disp_filename): force_sets = [] else: force_sets = get_force_sets(interface_mode, num_atoms, num_disps, force_filenames, disp_filename=disp_filename, verbose=(log_level > 0)) if settings.subtract_forces: force_filename = settings.subtract_forces file_exists(force_filename, log_level) force_set_zero = get_force_sets(interface_mode, num_atoms, 1, [ force_filename, ], verbose=(log_level > 0))[0] for fs in force_sets: fs -= force_set_zero if log_level > 0: print( "Forces in \'%s\' were subtracted from supercell forces." % force_filename) if force_sets: write_FORCES_FC3(disp_dataset, force_sets, filename="FORCES_FC3") if log_level: print("") print("%s has been created." % "FORCES_FC3") print_end() sys.exit(0) else: if log_level: print("") print("%s could not be created." % "FORCES_FC3") print_error() sys.exit(1) ##################### # Create FORCES_FC2 # ##################### if settings.create_forces_fc2: if input_filename is None: disp_filename = 'disp_fc2.yaml' else: disp_filename = 'disp_fc2.' + input_filename + '.yaml' file_exists(disp_filename, log_level) disp_dataset = parse_disp_fc2_yaml(filename=disp_filename) if log_level: print("Displacement dataset was read from \"%s\"." % disp_filename) num_atoms = disp_dataset['natom'] num_disps = len(disp_dataset['first_atoms']) force_filenames = settings.create_forces_fc2 for filename in force_filenames: file_exists(filename, log_level) if log_level > 0: print("Number of displacements: %d" % num_disps) print("Number of supercell files: %d" % len(force_filenames)) force_sets = get_force_sets(interface_mode, num_atoms, num_disps, force_filenames, disp_filename, verbose=(log_level > 0)) if settings.subtract_forces: force_filename = settings.subtract_forces file_exists(force_filename, log_level) force_set_zero = get_force_sets(interface_mode, num_atoms, 1, [ force_filename, ], verbose=(log_level > 0))[0] for fs in force_sets: fs -= force_set_zero if log_level > 0: print( "Forces in \'%s\' were subtracted from supercell forces." % force_filename) if force_sets: write_FORCES_FC2(disp_dataset, forces_fc2=force_sets, filename="FORCES_FC2") if log_level: print("") print("%s has been created." % "FORCES_FC2") print_end() sys.exit(0) else: if log_level: print("") print("%s could not be created." % "FORCES_FC2") print_error() sys.exit(1)
# get phono3py data from database force_sets = wc.out.force_sets structure = wc.out.final_structure ph_settings = wc.inp.ph_settings supercell = get_supercell(phonopy_bulk_from_structure(structure), ph_settings.dict.supercell, symprec=ph_settings.dict.symmetry_precision) write_disp_fc3_yaml(force_sets.get_data_sets3(), supercell, filename='disp_fc3.yaml') write_FORCES_FC3(force_sets.get_data_sets3(), force_sets.get_forces3(), filename='FORCES_FC3') with open('POSCAR-unitcell', mode='w') as f: f.writelines(get_poscar_txt(structure)) if 'nac_data' in wc.get_outputs_dict(): nac_data = wc.out.nac_data print('Writing BORN file') with open('BORN', mode='w') as f: f.writelines( get_BORN_txt(nac_data, structure=structure, parameters=ph_settings)) if 'force_constants_2order' and 'force_constants_3order' in wc.get_outputs_dict( ):
def create_FORCES_FC3_and_FORCES_FC2_then_exit(settings, input_filename, log_level): """Create FORCES_FC3 and FORCES_FC2 from files.""" interface_mode = settings.calculator ph3py_yaml = None ##################### # Create FORCES_FC3 # ##################### if settings.create_forces_fc3 or settings.create_forces_fc3_file: if input_filename is None: disp_fc3_filename = "disp_fc3.yaml" else: disp_fc3_filename = "disp_fc3." + input_filename + ".yaml" disp_filenames = files_exist(["phono3py_disp.yaml", disp_fc3_filename], log_level, is_any=True) disp_filename = disp_filenames[0] if "phono3py_disp.yaml" in disp_filename: ph3py_yaml = Phono3pyYaml() ph3py_yaml.read(disp_filename) if ph3py_yaml.calculator is not None: interface_mode = ph3py_yaml.calculator # overwrite disp_dataset = ph3py_yaml.dataset else: file_exists(disp_filename, log_level) disp_dataset = parse_disp_fc3_yaml(filename=disp_filename) if log_level: print("") print('Displacement dataset was read from "%s".' % disp_filename) num_atoms = disp_dataset["natom"] num_disps = len(disp_dataset["first_atoms"]) for d1 in disp_dataset["first_atoms"]: for d2 in d1["second_atoms"]: if "included" not in d2 or d2["included"]: num_disps += 1 if settings.create_forces_fc3_file: file_exists(settings.create_forces_fc3_file, log_level) force_filenames = [ x.strip() for x in open(settings.create_forces_fc3_file) ] else: force_filenames = settings.create_forces_fc3 for filename in force_filenames: file_exists(filename, log_level) if log_level > 0: print("Number of displacements: %d" % num_disps) print("Number of supercell files: %d" % len(force_filenames)) if not check_number_of_force_files(num_disps, force_filenames, disp_filename): force_sets = [] else: force_sets = get_force_sets( interface_mode, num_atoms, force_filenames, verbose=(log_level > 0), ) if settings.subtract_forces: force_filename = settings.subtract_forces file_exists(force_filename, log_level) force_set_zero = get_force_sets( interface_mode, num_atoms, [ force_filename, ], verbose=(log_level > 0), )[0] for fs in force_sets: fs -= force_set_zero if log_level > 0: print("Forces in '%s' were subtracted from supercell forces." % force_filename) if force_sets: write_FORCES_FC3(disp_dataset, forces_fc3=force_sets, filename="FORCES_FC3") if log_level: print("") print("%s has been created." % "FORCES_FC3") print_end() sys.exit(0) else: if log_level: print("") print("%s could not be created." % "FORCES_FC3") print_error() sys.exit(1) ##################### # Create FORCES_FC2 # ##################### if settings.create_forces_fc2: if input_filename is None: disp_fc2_filename = "disp_fc2.yaml" else: disp_fc2_filename = "disp_fc2." + input_filename + ".yaml" disp_filenames = files_exist(["phono3py_disp.yaml", disp_fc2_filename], log_level, is_any=True) if "phono3py_disp.yaml" in disp_filenames[0]: # ph3py_yaml is not None, phono3py_disp.yaml is already read. if ph3py_yaml is None: disp_filename = disp_filenames[0] ph3py_yaml = Phono3pyYaml() ph3py_yaml.read(disp_filename) if ph3py_yaml.calculator is not None: interface_mode = ph3py_yaml.calculator # overwrite disp_dataset = ph3py_yaml.phonon_dataset else: disp_filename = disp_filenames[0] file_exists(disp_filename, log_level) disp_dataset = parse_disp_fc2_yaml(filename=disp_filename) if log_level: print('Displacement dataset was read from "%s".' % disp_filename) num_atoms = disp_dataset["natom"] num_disps = len(disp_dataset["first_atoms"]) force_filenames = settings.create_forces_fc2 for filename in force_filenames: file_exists(filename, log_level) if log_level > 0: print("Number of displacements: %d" % num_disps) print("Number of supercell files: %d" % len(force_filenames)) force_sets = get_force_sets( interface_mode, num_atoms, force_filenames, verbose=(log_level > 0), ) if settings.subtract_forces: force_filename = settings.subtract_forces file_exists(force_filename, log_level) force_set_zero = get_force_sets( interface_mode, num_atoms, [ force_filename, ], verbose=(log_level > 0), )[0] for fs in force_sets: fs -= force_set_zero if log_level > 0: print("Forces in '%s' were subtracted from supercell forces." % force_filename) if force_sets: write_FORCES_FC2(disp_dataset, forces_fc2=force_sets, filename="FORCES_FC2") if log_level: print("") print("%s has been created." % "FORCES_FC2") print_end() sys.exit(0) else: if log_level: print("") print("%s could not be created." % "FORCES_FC2") print_error() sys.exit(1)