def create_FORCE_SETS_from_FORCES_FCx_then_exit(phonon_smat, input_filename: Optional[str], cell_filename: Optional[str], log_level): """Convert FORCES_FC3 or FORCES_FC2 to FORCE_SETS.""" if cell_filename is not None: disp_filename = cell_filename elif input_filename is None: disp_filename = "phono3py_disp.yaml" else: disp_filename = f"phono3py_disp.{input_filename}.yaml" if phonon_smat is not None: forces_filename = "FORCES_FC2" else: forces_filename = "FORCES_FC3" if log_level: print(f'Displacement dataset is read from "{disp_filename}".') print(f'Forces are read from "{forces_filename}"') with open(forces_filename, "r") as f: len_first_line = get_length_of_first_line(f) if len_first_line == 3: file_exists(disp_filename, log_level) file_exists(forces_filename, log_level) ph3yml = Phono3pyYaml() ph3yml.read(disp_filename) if phonon_smat is None: dataset = copy.deepcopy(ph3yml.dataset) smat = ph3yml.supercell_matrix else: dataset = copy.deepcopy(ph3yml.phonon_dataset) smat = ph3yml.phonon_supercell_matrix if smat is None or (phonon_smat is not None and (phonon_smat != smat).any()): if log_level: print("") print("Supercell matrix is inconsistent.") print(f'Supercell matrix read from "{disp_filename}":') print(smat) print("Supercell matrix given by --dim-fc2:") print(phonon_smat) print_error() sys.exit(1) parse_FORCES_FC2(dataset, filename=forces_filename) write_FORCE_SETS(dataset) if log_level: print("FORCE_SETS has been created.") print_end() else: if log_level: print("The file format of %s is already readable by phonopy." % forces_filename) print_end() sys.exit(0)
def _get_ph3py_yaml(disp_filename, ph3py_yaml): _ph3py_yaml = ph3py_yaml # Try to use phono3py.phonon_dataset when the disp file not found if not os.path.isfile(disp_filename): disp_filename = None if _ph3py_yaml is None and os.path.isfile("phono3py_disp.yaml"): _ph3py_yaml = Phono3pyYaml() _ph3py_yaml.read("phono3py_disp.yaml") return _ph3py_yaml
def _extract_datast_from_ph3py_yaml(ph3py_yaml, fc_type): dataset = None if fc_type == "phonon_fc2": if ph3py_yaml and ph3py_yaml.phonon_dataset is not None: # copy dataset # otherwise unit conversion can be applied multiple times. _ph3py_yaml = Phono3pyYaml() _ph3py_yaml.yaml_data = ph3py_yaml.yaml_data _ph3py_yaml.parse() dataset = _ph3py_yaml.phonon_dataset else: if ph3py_yaml and ph3py_yaml.dataset is not None: # copy dataset # otherwise unit conversion can be applied multiple times. _ph3py_yaml = Phono3pyYaml() _ph3py_yaml.yaml_data = ph3py_yaml.yaml_data _ph3py_yaml.parse() dataset = _ph3py_yaml.dataset return dataset
def finalize_phono3py( phono3py: Phono3py, confs_dict, log_level, displacements_mode=False, filename=None, ): """Write phono3py.yaml and then exit. Parameters ---------- phono3py : Phono3py Phono3py instance. confs_dict : dict This contains the settings and command options that the user set. log_level : int Log level. 0 means quiet. displacements_mode : Bool When True, crystal structure is written in the length unit of calculator interface in phono3py_disp.yaml. Otherwise, the default unit (angstrom) is used. filename : str, optional phono3py.yaml is written in this filename. """ if filename is None: yaml_filename = "phono3py.yaml" else: yaml_filename = filename if displacements_mode: _calculator = phono3py.calculator else: _calculator = None _physical_units = get_default_physical_units(_calculator) yaml_settings = {"force_sets": False, "displacements": displacements_mode} ph3py_yaml = Phono3pyYaml(configuration=confs_dict, physical_units=_physical_units, settings=yaml_settings) ph3py_yaml.set_phonon_info(phono3py) ph3py_yaml.calculator = _calculator with open(yaml_filename, "w") as w: w.write(str(ph3py_yaml)) if log_level > 0: print("") if displacements_mode: print(f'Displacement dataset was written in "{yaml_filename}".') else: print(f'Summary of calculation was written in "{yaml_filename}".') print_end() sys.exit(0)
def load(phono3py_yaml=None, # phono3py.yaml-like must be the first argument. supercell_matrix=None, primitive_matrix=None, phonon_supercell_matrix=None, mesh=None, is_nac=True, calculator=None, unitcell=None, supercell=None, nac_params=None, unitcell_filename=None, supercell_filename=None, born_filename=None, forces_fc3_filename=None, forces_fc2_filename=None, fc3_filename=None, fc2_filename=None, fc_calculator=None, factor=None, frequency_scale_factor=None, is_symmetry=True, symmetrize_fc=True, is_mesh_symmetry=True, is_compact_fc=False, symprec=1e-5, log_level=0): """Create Phono3py instance from parameters and/or input files. When unitcell and unitcell_filename are not given, file name that is default for the chosen calculator is looked for in the current directory as the default behaviour. if 'fc3.hdf5' is found, this is read. Unless 'fc3.hdf5' is found and if 'FORCES_FC3' and 'disp_fc3.yaml" are found, these are read and fc3 and fc2 are produced. if 'fc2.hdf5' is found, this is read. Unless 'fc2.hdf5' is found and if 'FORCES_FC2' and 'disp_fc2.yaml" are found, these are read and fc2 is produced. When force_sets_filename and force_constants_filename are not given, 'FORCES_FC3' and 'FORCES_FC2' are looked for in the current directory as the default behaviour. When 'FORCES_FC3' ('FORCES_FC2') is given in the type-1 format, 'disp_fc3.yaml' ('disp_fc2.yaml') is also necessary and read. Parameters ---------- phono3py_yaml : str, optional Filename of "phono3py.yaml"-like file. If this is given, the data such as unit cell structure,supercell matrix, and primitive_matrix, those for non-analytycal term correction are parsed. Default is None. supercell_matrix : array_like, optional Supercell matrix multiplied to input cell basis vectors. shape=(3, ) or (3, 3), where the former is considered a diagonal matrix. Default is the unit matrix. dtype=int primitive_matrix : array_like or str, optional Primitive matrix multiplied to input cell basis vectors. Default is the identity matrix. Default is None, which is equivalent to 'auto'. shape=(3, 3), dtype=float. When 'F', 'I', 'A', 'C', or 'R' is given instead of a 3x3 matrix, the primitive matrix defined at https://spglib.github.io/spglib/definition.html is used. phonon_supercell_matrix : array_like, optional Supercell matrix used for fc2. In phono3py, supercell matrix for fc3 and fc2 can be different to support longer range interaction of fc2 than that of fc3. Unless setting this, supercell_matrix is used. This is only valide when unitcell or unitcell_filename is given. Default is None. mesh : array_like, optional Grid mesh numbers in reciprocal cell. shape=(3,), dtype='intc' is_nac : bool, optional If True, look for 'BORN' file. If False, NAS is turned off. The priority for NAC is nac_params > born_filename > is_nac ('BORN'). Default is True. calculator : str, optional. Calculator used for computing forces. This is used to switch the set of physical units. Default is None, which is equivalent to "vasp". unitcell : PhonopyAtoms, optional Input unit cell. Default is None. The priority for cell is unitcell_filename > supercell_filename > unitcell > supercell. supercell : PhonopyAtoms, optional Input supercell. Default value of primitive_matrix is set to 'auto' (can be overwitten). supercell_matrix is ignored. Default is None. The priority for cell is unitcell_filename > supercell_filename > unitcell > supercell. nac_params : dict, optional Parameters required for non-analytical term correction. Default is None. The priority for NAC is nac_params > born_filename > is_nac. {'born': Born effective charges (array_like, shape=(primitive cell atoms, 3, 3), dtype=float), 'dielectric': Dielectric constant matrix (array_like, shape=(3, 3), dtype=float), 'factor': unit conversion facotr (float)} unitcell_filename : str, optional Input unit cell filename. Default is None. The priority for cell is unitcell_filename > supercell_filename > unitcell > supercell. supercell_filename : str, optional Input supercell filename. When this is specified, supercell_matrix is ignored. Default is None. The priority for cell is 1. unitcell_filename (with supercell_matrix) 2. supercell_filename 3. unitcell (with supercell_matrix) 4. supercell. born_filename : str, optional Filename corresponding to 'BORN', a file contains non-analytical term correction parameters. The priority for NAC is nac_params > born_filename > is_nac ('BORN'). forces_fc3_filename : sequence or str, optional A two-elemental sequence of filenames corresponding to ('FORCES_FC3', 'disp_fc3.yaml') in the type-1 format or a filename (str) corresponding to 'FORCES_FC3' in the type-2 format. Default is None. The priority to calculate or load force constants is fc3_filename > forces_fc3_filename > 'fc3.hdf5' > 'FORCES_FC3'. forces_fc2_filename : str or tuple, optional A two-elemental sequence of filenames corresponding to ('FORCES_FC2', 'disp_fc2.yaml') in the type-1 format or a filename (str) corresponding to 'FORCES_FC2' in the type-2 format. Default is None. The priority to calculate or load force constants is fc2_filename > forces_fc2_filename > 'fc2.hdf5' > 'FORCES_FC2'. fc3_filename : str, optional Filename of a file corresponding to 'fc3.hdf5', a file contains third-order force constants. Default is None. The priority to calculate or load force constants is fc3_filename > forces_fc3_filename > 'fc3.hdf5' > 'FORCES_FC3'. fc2_filename : str, optional Filename of a file corresponding to 'fc2.hdf5', a file contains second-order force constants. Default is None. The priority to calculate or load force constants is fc2_filename > forces_fc2_filename > 'fc2.hdf5' > 'FORCES_FC2'. fc_calculator : str, optional Force constants calculator. Currently only 'alm'. Default is None. factor : float, optional Phonon frequency unit conversion factor. Unless specified, default unit conversion factor for each calculator is used. frequency_scale_factor : float, optional Factor multiplied to calculated phonon frequency. Default is None, i.e., effectively 1. is_symmetry : bool, optional Setting False, crystal symmetry except for lattice translation is not considered. Default is True. symmetrize_fc : bool, optional Setting False, force constants are not symmetrized when creating force constants from displacements and forces. Default is True. is_mesh_symmetry : bool, optional Setting False, reciprocal mesh symmetry is not considered. Default is True. is_compact_fc : bool fc3 shape is False: (supercell, supercell, supecell, 3, 3, 3) True: (primitive, supercell, supecell, 3, 3, 3) where 'supercell' and 'primitive' indicate number of atoms in these cells. Default is False. symprec : float, optional Tolerance used to find crystal symmetry. Default is 1e-5. log_level : int, optional Verbosity control. Default is 0. """ if phono3py_yaml is None: cell, smat, pmat = load_helper.get_cell_settings( supercell_matrix=supercell_matrix, primitive_matrix=primitive_matrix, unitcell=unitcell, supercell=supercell, unitcell_filename=unitcell_filename, supercell_filename=supercell_filename, calculator=calculator, symprec=symprec) if phonon_supercell_matrix is not None: if unitcell is None and unitcell_filename is None: msg = ("phonon_supercell_matrix can be used only when " "unitcell or unitcell_filename is given.") raise RuntimeError(msg) ph_smat = load_helper.get_supercell_matrix(phonon_supercell_matrix) else: ph_smat = None _nac_params = nac_params else: ph3py_yaml = Phono3pyYaml() ph3py_yaml.read(phono3py_yaml) cell = ph3py_yaml.unitcell smat = ph3py_yaml.supercell_matrix ph_smat = ph3py_yaml.phonon_supercell_matrix if smat is None: smat = np.eye(3, dtype='intc', order='C') if primitive_matrix == 'auto': pmat = 'auto' else: pmat = ph3py_yaml.primitive_matrix if is_nac: _nac_params = ph3py_yaml.nac_params else: _nac_params = None # units keywords: factor, nac_factor, distance_to_A units = get_default_physical_units(calculator) if factor is None: _factor = units['factor'] else: _factor = factor ph3py = Phono3py(cell, smat, primitive_matrix=pmat, phonon_supercell_matrix=ph_smat, mesh=mesh, frequency_factor_to_THz=_factor, symprec=symprec, is_symmetry=is_symmetry, is_mesh_symmetry=is_mesh_symmetry, calculator=calculator, log_level=log_level) _nac_params = load_helper.get_nac_params(ph3py.primitive, _nac_params, born_filename, is_nac, units['nac_factor']) if _nac_params is not None: ph3py.nac_params = _nac_params _set_force_constants(ph3py, units, dataset=None, fc3_filename=fc3_filename, fc2_filename=fc2_filename, forces_fc3_filename=forces_fc3_filename, forces_fc2_filename=forces_fc2_filename, fc_calculator=fc_calculator, symmetrize_fc=symmetrize_fc, is_compact_fc=is_compact_fc, log_level=log_level) if mesh is not None: ph3py.init_phph_interaction( frequency_scale_factor=frequency_scale_factor) return ph3py
def load( phono3py_yaml=None, # phono3py.yaml-like must be the first argument. supercell_matrix=None, primitive_matrix=None, phonon_supercell_matrix=None, mesh=None, is_nac=True, calculator=None, unitcell=None, supercell=None, nac_params=None, unitcell_filename=None, supercell_filename=None, born_filename=None, forces_fc3_filename=None, forces_fc2_filename=None, fc3_filename=None, fc2_filename=None, fc_calculator=None, fc_calculator_options=None, factor=None, frequency_scale_factor=None, produce_fc=True, is_symmetry=True, symmetrize_fc=True, is_mesh_symmetry=True, is_compact_fc=False, symprec=1e-5, log_level=0): """Create Phono3py instance from parameters and/or input files. "phono3py_yaml"-like file is parsed unless crystal structure information is given by unitcell_filename, supercell_filename, unitcell (PhonopyAtoms-like), or supercell (PhonopyAtoms-like). Even when "phono3py_yaml"-like file is parse, parameters except for crystal structure can be overwritten. 'fc3.hdf5' is read if found in current directory. Unless 'fc3.hdf5' is found and if 'FORCES_FC3' and 'disp_fc3.yaml" are found, these are read and fc3 and fc2 are produced. if 'fc2.hdf5' is found, this is read. Unless 'fc2.hdf5' is found and if 'FORCES_FC2' and 'disp_fc2.yaml" are found, these are read and fc2 is produced. When force_sets_filename and force_constants_filename are not given, 'FORCES_FC3' and 'FORCES_FC2' are looked for in the current directory as the default behaviour. When 'FORCES_FC3' ('FORCES_FC2') is given in the type-1 format, 'disp_fc3.yaml' ('disp_fc2.yaml') is also necessary and read. Crystal structure ----------------- Means to provide crystal structure(s) and their priority: 1. unitcell_filename (with supercell_matrix) 2. supercell_filename 3. unitcell (with supercell_matrix) 4. supercell. 5. phono3py_yaml-like Force sets or force constants ----------------------------- Optional. Means to provide information to generate force constants and their priority: 1. fc3_filename (fc2_filename) 2. forces_fc3_filename (forces_fc2_filename). Do not forget that for type-1 format, disp_fc3.yaml (disp_fc2.yaml) has to be given, too. 3. 'fc3.hdf5' and 'fc2.hdf5' are searched in current directory. 4. 'FORCES_FC3' and 'FORCES_FC2' are searched in current directory. 'FORCES_FC2' is optional. For type-1 format, 'disp_fc3.yaml' and optionally 'disp_fc2.yaml' are also searched in current directory. When 'FORCES_FC2' is not found, 'FORCES_FC3' is used to create fc2. Parameters for non-analytical term correctiion (NAC) ---------------------------------------------------- Optional. Means to provide NAC parameters and their priority: 1. born_filename 2. nac_params 3. phono3py_yaml_like.nac_params if existed and is_nac=True. 4. 'BORN' is searched in current directory when is_nac=True. Parameters ---------- phono3py_yaml : str, optional Filename of "phono3py.yaml"-like file. If this is given, the data in the file are parsed. Default is None. supercell_matrix : array_like, optional Supercell matrix multiplied to input cell basis vectors. shape=(3, ) or (3, 3), where the former is considered a diagonal matrix. Default is the unit matrix. dtype=int primitive_matrix : array_like or str, optional Primitive matrix multiplied to input cell basis vectors. Default is the identity matrix. Default is None, which is equivalent to 'auto'. shape=(3, 3), dtype=float. When 'F', 'I', 'A', 'C', or 'R' is given instead of a 3x3 matrix, the primitive matrix defined at https://spglib.github.io/spglib/definition.html is used. phonon_supercell_matrix : array_like, optional Supercell matrix used for fc2. In phono3py, supercell matrix for fc3 and fc2 can be different to support longer range interaction of fc2 than that of fc3. Unless setting this, supercell_matrix is used. This is only valide when unitcell or unitcell_filename is given. Default is None. mesh : array_like, optional Grid mesh numbers in reciprocal cell. shape=(3,), dtype='intc' is_nac : bool, optional If True, look for 'BORN' file. If False, NAS is turned off. Default is True. calculator : str, optional. Calculator used for computing forces. This is used to switch the set of physical units. Default is None, which is equivalent to "vasp". unitcell : PhonopyAtoms, optional Input unit cell. Default is None. supercell : PhonopyAtoms, optional Input supercell. With given, default value of primitive_matrix is set to 'auto' (can be overwitten). supercell_matrix is ignored. Default is None. nac_params : dict, optional Parameters required for non-analytical term correction. Default is None. {'born': Born effective charges (array_like, shape=(primitive cell atoms, 3, 3), dtype=float), 'dielectric': Dielectric constant matrix (array_like, shape=(3, 3), dtype=float), 'factor': unit conversion facotr (float)} unitcell_filename : str, optional Input unit cell filename. Default is None. supercell_filename : str, optional Input supercell filename. When this is specified, supercell_matrix is ignored. Default is None. born_filename : str, optional Filename corresponding to 'BORN', a file contains non-analytical term correction parameters. forces_fc3_filename : sequence or str, optional A two-elemental sequence of filenames corresponding to ('FORCES_FC3', 'disp_fc3.yaml') in the type-1 format or a filename (str) corresponding to 'FORCES_FC3' in the type-2 format. Default is None. forces_fc2_filename : str or tuple, optional A two-elemental sequence of filenames corresponding to ('FORCES_FC2', 'disp_fc2.yaml') in the type-1 format or a filename (str) corresponding to 'FORCES_FC2' in the type-2 format. Default is None. fc3_filename : str, optional Filename of a file corresponding to 'fc3.hdf5', a file contains third-order force constants. Default is None. fc2_filename : str, optional Filename of a file corresponding to 'fc2.hdf5', a file contains second-order force constants. Default is None. fc_calculator : str, optional Force constants calculator. Currently only 'alm'. Default is None. fc_calculator_options : str, optional Optional parameters that are passed to the external fc-calculator. This is given as one text string. How to parse this depends on the fc-calculator. For alm, each parameter is splitted by comma ',', and each set of key and value pair is written in 'key = value'. factor : float, optional Phonon frequency unit conversion factor. Unless specified, default unit conversion factor for each calculator is used. frequency_scale_factor : float, optional Factor multiplied to calculated phonon frequency. Default is None, i.e., effectively 1. produce_fc : bool, optional Setting False, force constants are not calculated from displacements and forces. Default is True. is_symmetry : bool, optional Setting False, crystal symmetry except for lattice translation is not considered. Default is True. symmetrize_fc : bool, optional Setting False, force constants are not symmetrized when creating force constants from displacements and forces. Default is True. is_mesh_symmetry : bool, optional Setting False, reciprocal mesh symmetry is not considered. Default is True. is_compact_fc : bool fc3 are created in the array whose shape is True: (primitive, supercell, supecell, 3, 3, 3) False: (supercell, supercell, supecell, 3, 3, 3) and for fc2 True: (primitive, supecell, 3, 3) False: (supercell, supecell, 3, 3) where 'supercell' and 'primitive' indicate number of atoms in these cells. Default is False. symprec : float, optional Tolerance used to find crystal symmetry. Default is 1e-5. log_level : int, optional Verbosity control. Default is 0. """ if (supercell is not None or supercell_filename is not None or unitcell is not None or unitcell_filename is not None): cell, smat, pmat = load_helper.get_cell_settings( supercell_matrix=supercell_matrix, primitive_matrix=primitive_matrix, unitcell=unitcell, supercell=supercell, unitcell_filename=unitcell_filename, supercell_filename=supercell_filename, calculator=calculator, symprec=symprec) if phonon_supercell_matrix is not None: if unitcell is None and unitcell_filename is None: msg = ("phonon_supercell_matrix can be used only when " "unitcell or unitcell_filename is given.") raise RuntimeError(msg) ph_smat = phonon_supercell_matrix else: ph_smat = None _nac_params = nac_params ph3py_yaml = None elif phono3py_yaml is not None: ph3py_yaml = Phono3pyYaml() ph3py_yaml.read(phono3py_yaml) cell = ph3py_yaml.unitcell smat = ph3py_yaml.supercell_matrix ph_smat = ph3py_yaml.phonon_supercell_matrix if smat is None: smat = np.eye(3, dtype='intc', order='C') if primitive_matrix == 'auto': pmat = 'auto' else: pmat = ph3py_yaml.primitive_matrix if nac_params is not None: _nac_params = nac_params elif is_nac: _nac_params = ph3py_yaml.nac_params else: _nac_params = None # units keywords: factor, nac_factor, distance_to_A physical_units = get_default_physical_units(calculator) if factor is None: _factor = physical_units['factor'] else: _factor = factor ph3py = Phono3py(cell, smat, primitive_matrix=pmat, phonon_supercell_matrix=ph_smat, frequency_factor_to_THz=_factor, symprec=symprec, is_symmetry=is_symmetry, is_mesh_symmetry=is_mesh_symmetry, calculator=calculator, log_level=log_level) ph3py.mesh_number = mesh # NAC params if (born_filename is not None or nac_params is not None or is_nac and os.path.isfile("BORN")): ph3py.nac_params = load_helper.get_nac_params( ph3py.primitive, _nac_params, born_filename, is_nac, physical_units['nac_factor'], log_level=log_level) set_dataset_and_force_constants( ph3py, ph3py_yaml=ph3py_yaml, fc3_filename=fc3_filename, fc2_filename=fc2_filename, forces_fc3_filename=forces_fc3_filename, forces_fc2_filename=forces_fc2_filename, fc_calculator=fc_calculator, fc_calculator_options=fc_calculator_options, produce_fc=produce_fc, symmetrize_fc=symmetrize_fc, is_compact_fc=is_compact_fc, log_level=log_level) if mesh is not None: ph3py.init_phph_interaction( frequency_scale_factor=frequency_scale_factor) return ph3py
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)
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)