def read_crystal_structure(filename=None, interface_mode=None, chemical_symbols=None, yaml_mode=False): if filename is None: unitcell_filename = get_default_cell_filename(interface_mode, yaml_mode) else: unitcell_filename = filename if not os.path.exists(unitcell_filename): if filename is None: return None, (unitcell_filename + " (default file name)", ) else: return None, (unitcell_filename, ) if yaml_mode: from phonopy.interface.phonopy_yaml import PhonopyYaml phpy_yaml = PhonopyYaml() phpy_yaml.read(unitcell_filename) unitcell = phpy_yaml.get_unitcell() return unitcell, (unitcell_filename, ) if interface_mode is None or interface_mode == 'vasp': from phonopy.interface.vasp import read_vasp if chemical_symbols is None: unitcell = read_vasp(unitcell_filename) else: unitcell = read_vasp(unitcell_filename, symbols=chemical_symbols) return unitcell, (unitcell_filename, ) if interface_mode == 'abinit': from phonopy.interface.abinit import read_abinit unitcell = read_abinit(unitcell_filename) return unitcell, (unitcell_filename, ) if interface_mode == 'pwscf': from phonopy.interface.pwscf import read_pwscf unitcell, pp_filenames = read_pwscf(unitcell_filename) return unitcell, (unitcell_filename, pp_filenames) if interface_mode == 'wien2k': from phonopy.interface.wien2k import parse_wien2k_struct unitcell, npts, r0s, rmts = parse_wien2k_struct(unitcell_filename) return unitcell, (unitcell_filename, npts, r0s, rmts) if interface_mode == 'elk': from phonopy.interface.elk import read_elk unitcell, sp_filenames = read_elk(unitcell_filename) return unitcell, (unitcell_filename, sp_filenames) if interface_mode == 'siesta': from phonopy.interface.siesta import read_siesta unitcell, atypes = read_siesta(unitcell_filename) return unitcell, (unitcell_filename, atypes) if interface_mode == 'crystal': from phonopy.interface.crystal import read_crystal unitcell, conv_numbers = read_crystal(unitcell_filename) return unitcell, (unitcell_filename, conv_numbers)
def __init__(self, configuration=None, calculator=None, physical_units=None): self.configuration = None self.calculator = None self.physical_units = None self.settings = {} PhonopyYaml.__init__(self, configuration=configuration, calculator=calculator, physical_units=physical_units) # Written in self.set_phonon_info self.unitcell = None self.primitive = None self.supercell = None self.yaml = None self.supercell_matrix = None self.phonon_supercell_matrix = None self.primitive_matrix = None self.nac_params = None self.supercell_matrix = None self.s2p_map = None self.u2p_map = None self.version = None # Overwrite this self.command_name = "phono3py" for key in self.settings: self.settings[key] = False self.settings['born_effective_charge'] = True self.settings['dielectric_constant'] = True
def _read_phonopy_yaml(filename, command_name): cell_filename = None for fname in (filename, "%s_disp.yaml" % command_name, "%s.yaml" % command_name): if fname and os.path.isfile(fname): cell_filename = fname break if cell_filename is None: return None, ("%s_disp.yaml" % command_name, "%s.yaml" % command_name, "", "") phpy = PhonopyYaml() try: phpy.read(cell_filename) cell = phpy.unitcell calculator = None if command_name in phpy.yaml: if 'calculator' in phpy.yaml[command_name]: calculator = phpy.yaml[command_name]['calculator'] if ('supercell_matrix' in phpy.yaml and phpy.yaml['supercell_matrix'] is not None): smat = phpy.supercell_matrix else: smat = None if ('primitive_matrix' in phpy.yaml and phpy.yaml['primitive_matrix'] is not None): pmat = phpy.primitive_matrix else: pmat = None return cell, (cell_filename, calculator, smat, pmat) except TypeError: return None, (cell_filename, None, None, None)
def read_crystal_structure(filename=None, interface_mode=None, chemical_symbols=None, yaml_mode=False): if filename is None: unitcell_filename = get_default_cell_filename(interface_mode, yaml_mode) else: unitcell_filename = filename if not os.path.exists(unitcell_filename): if filename is None: return None, (unitcell_filename + " (default file name)",) else: return None, (unitcell_filename,) if yaml_mode: from phonopy.interface.phonopy_yaml import PhonopyYaml phpy_yaml = PhonopyYaml() phpy_yaml.read(unitcell_filename) unitcell = phpy_yaml.get_unitcell() return unitcell, (unitcell_filename,) if interface_mode is None or interface_mode == 'vasp': from phonopy.interface.vasp import read_vasp if chemical_symbols is None: unitcell = read_vasp(unitcell_filename) else: unitcell = read_vasp(unitcell_filename, symbols=chemical_symbols) return unitcell, (unitcell_filename,) if interface_mode == 'abinit': from phonopy.interface.abinit import read_abinit unitcell = read_abinit(unitcell_filename) return unitcell, (unitcell_filename,) if interface_mode == 'pwscf': from phonopy.interface.pwscf import read_pwscf unitcell, pp_filenames = read_pwscf(unitcell_filename) return unitcell, (unitcell_filename, pp_filenames) if interface_mode == 'wien2k': from phonopy.interface.wien2k import parse_wien2k_struct unitcell, npts, r0s, rmts = parse_wien2k_struct(unitcell_filename) return unitcell, (unitcell_filename, npts, r0s, rmts) if interface_mode == 'elk': from phonopy.interface.elk import read_elk unitcell, sp_filenames = read_elk(unitcell_filename) return unitcell, (unitcell_filename, sp_filenames) if interface_mode == 'siesta': from phonopy.interface.siesta import read_siesta unitcell, atypes = read_siesta(unitcell_filename) return unitcell, (unitcell_filename, atypes)
def test_write_phonopy_yaml_extra(self): phonopy = self._get_phonon() settings = { 'force_sets': True, 'displacements': True, 'force_constants': True, 'born_effective_charge': True, 'dielectric_constant': True } phpy_yaml = PhonopyYaml(calculator='vasp', settings=settings) phpy_yaml.set_phonon_info(phonopy)
def get_phonopy_yaml_txt(structure, settings, supercell_matrix=None, primitive_matrix=None, calculator=None): unitcell = phonopy_atoms_from_structure(structure) ph = Phonopy(unitcell, supercell_matrix=settings['supercell_matrix'], primitive_matrix='auto', calculator=calculator) phpy_yaml = PhonopyYaml() phpy_yaml.set_phonon_info(ph) return str(phpy_yaml)
def _read_phonopy_yaml(filename, command_name): cell_filename = _get_cell_filename(filename, command_name) if cell_filename is None: return None, (None, None) phpy = PhonopyYaml() try: phpy.read(cell_filename) except TypeError: # yaml.load returns str: File format seems not YAML. return None, (cell_filename, None) except yaml.parser.ParserError: return None, (cell_filename, None) cell = phpy.unitcell return cell, (cell_filename, phpy)
def run_task(self, fw_spec): unitcell = read_vasp("POSCAR-unitcell") phonon = phonopy.Phonopy(unitcell, self.get("supercell")) supercell = phonon.get_supercell() phonon.generate_displacements() supercells = phonon.supercells_with_displacements ids = np.arange(len(supercells)) + 1 write_supercells_with_displacements(supercell, supercells, ids) units = get_default_physical_units("vasp") phpy_yaml = PhonopyYaml(physical_units=units, settings={ 'force_sets': False, 'born_effective_charge': False, 'dielectric_constant': False, 'displacements': True }) phpy_yaml.set_phonon_info(phonon) with open("phonopy_disp.yaml", 'w') as w: w.write(str(phpy_yaml))
def test_write_phonopy_yaml_extra(ph_nacl_nofcsym: Phonopy): """Test PhonopyYaml.set_phonon_info, __str__, yaml_data, parse. settings parameter controls amount of yaml output. In this test, more data than the default are dumped and those are tested. """ phonon = ph_nacl_nofcsym settings = { "force_sets": True, "displacements": True, "force_constants": True, "born_effective_charge": True, "dielectric_constant": True, } phpy_yaml = PhonopyYaml(calculator="vasp", settings=settings) phpy_yaml.set_phonon_info(phonon) phpy_yaml_test = PhonopyYaml() phpy_yaml_test.yaml_data = yaml.safe_load(StringIO(str(phpy_yaml))) phpy_yaml_test.parse() np.testing.assert_allclose(phpy_yaml.force_constants, phpy_yaml_test.force_constants, atol=1e-8) np.testing.assert_allclose(phpy_yaml.nac_params["born"], phpy_yaml_test.nac_params["born"], atol=1e-8) np.testing.assert_allclose( phpy_yaml.nac_params["dielectric"], phpy_yaml_test.nac_params["dielectric"], atol=1e-8, ) np.testing.assert_allclose( phpy_yaml.nac_params["factor"], phpy_yaml_test.nac_params["factor"], atol=1e-8, ) disps, forces = get_displacements_and_forces(phpy_yaml.dataset) disps_test, forces_test = get_displacements_and_forces( phpy_yaml_test.dataset) np.testing.assert_allclose(forces, forces_test, atol=1e-8) np.testing.assert_allclose(disps, disps_test, atol=1e-8)
def test_write_phonopy_yaml(ph_nacl_nofcsym: Phonopy, helper_methods): """Test PhonopyYaml.set_phonon_info, __str__, yaml_data, parse.""" phonon = ph_nacl_nofcsym phpy_yaml = PhonopyYaml(calculator="vasp") phpy_yaml.set_phonon_info(phonon) phpy_yaml_test = PhonopyYaml() phpy_yaml_test.yaml_data = yaml.safe_load(StringIO(str(phpy_yaml))) phpy_yaml_test.parse() helper_methods.compare_cells_with_order(phpy_yaml.primitive, phpy_yaml_test.primitive) helper_methods.compare_cells_with_order(phpy_yaml.unitcell, phpy_yaml_test.unitcell) helper_methods.compare_cells_with_order(phpy_yaml.supercell, phpy_yaml_test.supercell) assert phpy_yaml.version == phpy_yaml_test.version np.testing.assert_allclose(phpy_yaml.supercell_matrix, phpy_yaml_test.supercell_matrix, atol=1e-8) np.testing.assert_allclose(phpy_yaml.primitive_matrix, phpy_yaml_test.primitive_matrix, atol=1e-8)
def test_write_phonopy_yaml(self): phonopy = self._get_phonon() phpy_yaml = PhonopyYaml(calculator='vasp') phpy_yaml.set_phonon_info(phonopy)
def setUp(self): filename = "POSCAR.yaml" self._cell = PhonopyYaml(filename).get_atoms()
"""Example to obtain PhonopyYaml instance.""" import phonopy from phonopy.interface.phonopy_yaml import PhonopyYaml phonon = phonopy.load( supercell_matrix=[[2, 0, 0], [0, 2, 0], [0, 0, 2]], primitive_matrix=[[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]], unitcell_filename="POSCAR-unitcell", force_sets_filename="FORCE_SETS", born_filename="BORN", ) phpy_yaml = PhonopyYaml(calculator="vasp", settings={"force_constants": True}) phpy_yaml.set_phonon_info(phonon) print(phpy_yaml)
def create_FORCE_SETS(interface_mode, force_filenames, symmetry_tolerance=None, is_wien2k_p1=False, force_sets_zero_mode=False, disp_filename='disp.yaml', force_sets_filename='FORCE_SETS', log_level=0): if log_level > 0: if interface_mode: print("Calculator interface: %s" % interface_mode) print("Displacements were read from \"%s\"." % disp_filename) if disp_filename == 'disp.yaml': print('') print("NOTE:") print(" From phonopy v2.0, displacements are written into " "\"phonopy_disp.yaml\".") print(" \"disp.yaml\" is still supported for reading, but is " "deprecated.") print('') if force_sets_zero_mode: print("Forces in %s are subtracted from forces in all " "other files." % force_filenames[0]) if disp_filename == 'disp.yaml': disp_dataset, supercell = parse_disp_yaml(filename=disp_filename, return_cell=True) else: phpy = PhonopyYaml() phpy.read(disp_filename) supercell = phpy.supercell disp_dataset = phpy.dataset if 'natom' in disp_dataset: # type-1 dataset num_atoms = disp_dataset['natom'] num_displacements = len(disp_dataset['first_atoms']) dataset_type = 1 elif 'displacements' in disp_dataset: # type-2 dataset num_atoms = disp_dataset['displacements'].shape[1] num_displacements = disp_dataset['displacements'].shape[0] dataset_type = 2 else: raise RuntimeError("Number of atoms could not be retrieved from %s" % disp_filename) if force_sets_zero_mode: num_displacements += 1 if interface_mode in (None, 'vasp', 'abinit', 'elk', 'qe', 'siesta', 'cp2k', 'crystal', 'dftbp', 'turbomole'): force_sets = get_force_sets(interface_mode, num_atoms, num_displacements, force_filenames, disp_filename=disp_filename, check_number_of_files=True, verbose=(log_level > 0)) elif interface_mode == 'wien2k': from phonopy.interface.wien2k import parse_set_of_forces if not _check_number_of_files(num_displacements, force_filenames, disp_filename): force_sets = [] else: disps, _ = get_displacements_and_forces(disp_dataset) force_sets = parse_set_of_forces( disps, force_filenames, supercell, is_distribute=(not is_wien2k_p1), symmetry_tolerance=symmetry_tolerance, verbose=(log_level > 0)) else: force_sets = [] if force_sets: if force_sets_zero_mode: force_sets = _subtract_residual_forces(force_sets) if dataset_type == 1: for forces, disp in zip(force_sets, disp_dataset['first_atoms']): disp['forces'] = forces elif dataset_type == 2: disp_dataset['forces'] = np.array(force_sets, dtype='double', order='C') else: raise RuntimeError("FORCE_SETS could not be created.") write_FORCE_SETS(disp_dataset, filename=force_sets_filename) if log_level > 0: if force_sets: print("%s has been created." % force_sets_filename) else: print("%s could not be created." % force_sets_filename) return 0
def _get_unitcell(self, filename): phpy_yaml = PhonopyYaml() phpy_yaml.read(filename) return phpy_yaml.unitcell
def load(phonopy_yaml=None, # phonopy.yaml-like must be the first argument. supercell_matrix=None, primitive_matrix=None, is_nac=True, calculator=None, unitcell=None, supercell=None, nac_params=None, unitcell_filename=None, supercell_filename=None, born_filename=None, force_sets_filename=None, force_constants_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_compact_fc=True, symprec=1e-5, log_level=0): """Create Phonopy instance from parameters and/or input files. "phonopy_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 "phonopy_yaml"-like file is parse, parameters except for crystal structure can be overwritten. Phonopy default files of 'FORCE_SETS' and 'BORN' are parsed when they are found in current directory and those data are not yet provided by other means. 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. phonopy_yaml Force sets or force constants ----------------------------- Optional. Means to provide information to generate force constants and their priority: 1. force_constants_filename 2. force_sets_filename 3. phonopy_yaml if force constants are found in phonoy_yaml. 4. phonopy_yaml if forces are found in phonoy_yaml.dataset. 5. 'FORCE_CONSTANTS' is searched in current directory. 6. 'force_constants.hdf5' is searched in current directory. 7. 'FORCE_SETS' is searched in current directory. When both of 3 and 4 are satisfied but not others, force constants and dataset are stored in Phonopy instance, but force constants are not produced from dataset. Parameters for non-analytical term correctiion (NAC) ---------------------------------------------------- Optional. Means to provide NAC parameters and their priority: 1. born_filename 2. nac_params 3. phonopy_yaml.nac_params if existed and is_nac=True. 4. 'BORN' is searched in current directory when is_nac=True. Parameters ---------- phonopy_yaml : str, optional Filename of "phonopy.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 None, which is equivalent to 'auto'. For array_like, shape=(3, 3), dtype=float. When 'F', 'I', 'A', 'C', or 'R' is given instead of a 3x3 matrix, the primitive matrix for the character found at https://spglib.github.io/spglib/definition.html is used. 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. force_sets_filename : str, optional Filename of a file corresponding to 'FORCE_SETS', a file contains sets of forces and displacements. Default is None. force_constants_filename : str, optional Filename of a file corresponding to 'FORCE_CONSTANTS' or 'force_constants.hdf5', a file contains 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_compact_fc : bool Force constants are produced in the array whose shape is True: (primitive, supecell, 3, 3) False: (supercell, supecell, 3, 3) where 'supercell' and 'primitive' indicate number of atoms in these cells. Default is True. 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, log_level=log_level) _calculator = calculator _nac_params = nac_params _dataset = None _fc = None elif phonopy_yaml is not None: phpy_yaml = PhonopyYaml() phpy_yaml.read(phonopy_yaml) cell = phpy_yaml.unitcell smat = phpy_yaml.supercell_matrix if smat is None: smat = np.eye(3, dtype='intc', order='C') if primitive_matrix is not None: pmat = get_primitive_matrix(primitive_matrix, symprec=symprec) else: pmat = phpy_yaml.primitive_matrix if nac_params is not None: _nac_params = nac_params elif is_nac: _nac_params = phpy_yaml.nac_params else: _nac_params = None _dataset = phpy_yaml.dataset _fc = phpy_yaml.force_constants if calculator is None: _calculator = phpy_yaml.calculator else: _calculator = calculator else: msg = ("Cell information could not found. " "Phonopy instance loading failed.") raise RuntimeError(msg) if log_level and _calculator is not None: print("Set \"%s\" mode." % _calculator) # units keywords: factor, nac_factor, distance_to_A units = get_default_physical_units(_calculator) if factor is None: _factor = units['factor'] else: _factor = factor phonon = Phonopy(cell, smat, primitive_matrix=pmat, factor=_factor, frequency_scale_factor=frequency_scale_factor, symprec=symprec, is_symmetry=is_symmetry, calculator=_calculator, log_level=log_level) # NAC params if born_filename is not None or _nac_params is not None or is_nac: ret_nac_params = load_helper.get_nac_params( primitive=phonon.primitive, nac_params=_nac_params, born_filename=born_filename, is_nac=is_nac, nac_factor=units['nac_factor'], log_level=log_level) if ret_nac_params is not None: phonon.nac_params = ret_nac_params # Displacements, forces, and force constants load_helper.set_dataset_and_force_constants( phonon, _dataset, _fc, force_constants_filename=force_constants_filename, force_sets_filename=force_sets_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) return phonon
def _get_unitcell(self, filename): phpy_yaml = PhonopyYaml() phpy_yaml.read(filename) unitcell = phpy_yaml.get_unitcell()
def create_FORCE_SETS( interface_mode, force_filenames, symmetry_tolerance=None, wien2k_P1_mode=False, force_sets_zero_mode=False, disp_filename="disp.yaml", force_sets_filename="FORCE_SETS", write_forcesets_yaml=False, log_level=0, ): """Create FORCE_SETS from phonopy_disp.yaml and calculator output files. Reading disp.yaml instead of phonopy_disp.yaml is deprecated. """ if log_level > 0: if interface_mode: print("Calculator interface: %s" % interface_mode) print('Displacements were read from "%s".' % disp_filename) if disp_filename == "disp.yaml": print("") print("NOTE:") print(" From phonopy v2.0, displacements are written into " '"phonopy_disp.yaml".') print(' "disp.yaml" is still supported for reading except for ' "Wien2k interface, ") print(" but is deprecated.") print("") if force_sets_zero_mode: print("Forces in %s are subtracted from forces in all " "other files." % force_filenames[0]) if disp_filename == "disp.yaml": if interface_mode == "wien2k": disp_dataset, supercell = parse_disp_yaml(filename=disp_filename, return_cell=True) else: disp_dataset = parse_disp_yaml(filename=disp_filename) else: phpy_yaml = PhonopyYaml() phpy_yaml.read(disp_filename) supercell = phpy_yaml.supercell disp_dataset = phpy_yaml.dataset if "natom" in disp_dataset: # type-1 dataset num_atoms = disp_dataset["natom"] num_displacements = len(disp_dataset["first_atoms"]) dataset_type = 1 elif "displacements" in disp_dataset: # type-2 dataset num_atoms = disp_dataset["displacements"].shape[1] num_displacements = disp_dataset["displacements"].shape[0] dataset_type = 2 else: raise RuntimeError("Number of atoms could not be retrieved from %s" % disp_filename) if force_sets_zero_mode: num_displacements += 1 if not check_number_of_force_files(num_displacements, force_filenames, disp_filename): force_sets = [] elif interface_mode == "wien2k": force_sets = get_force_sets_wien2k( force_filenames, supercell, disp_dataset, wien2k_P1_mode=wien2k_P1_mode, symmetry_tolerance=symmetry_tolerance, verbose=(log_level > 0), ) else: force_sets = get_force_sets( interface_mode, num_atoms, force_filenames, verbose=(log_level > 0), ) if force_sets: if force_sets_zero_mode: force_sets = _subtract_residual_forces(force_sets) if dataset_type == 1: for forces, disp in zip(force_sets, disp_dataset["first_atoms"]): disp["forces"] = forces elif dataset_type == 2: disp_dataset["forces"] = np.array(force_sets, dtype="double", order="C") else: raise RuntimeError("FORCE_SETS could not be created.") write_FORCE_SETS(disp_dataset, filename=force_sets_filename) if log_level > 0: print('"%s" has been created.' % force_sets_filename) if disp_filename != "disp.yaml" and write_forcesets_yaml: phpy_yaml.dataset = disp_dataset with open("phonopy_force_sets.yaml", "w") as w: w.write(str(phpy_yaml)) if log_level > 0: print('"%s" has been created.' % "phonopy_force_sets.yaml") else: if log_level > 0: print("%s could not be created." % force_sets_filename)
def create_FORCE_SETS(interface_mode, force_filenames, symmetry_tolerance=None, wien2k_P1_mode=False, force_sets_zero_mode=False, disp_filename='disp.yaml', force_sets_filename='FORCE_SETS', log_level=0): """Create FORCE_SETS from phonopy_disp.yaml and calculator output files. Reading disp.yaml instead of phonopy_disp.yaml is deprecated. """ if log_level > 0: if interface_mode: print("Calculator interface: %s" % interface_mode) print("Displacements were read from \"%s\"." % disp_filename) if disp_filename == 'disp.yaml': print('') print("NOTE:") print(" From phonopy v2.0, displacements are written into " "\"phonopy_disp.yaml\".") print(" \"disp.yaml\" is still supported for reading except for " "Wien2k interface, ") print(" but is deprecated.") print('') if force_sets_zero_mode: print("Forces in %s are subtracted from forces in all " "other files." % force_filenames[0]) if disp_filename == 'disp.yaml': if interface_mode == 'wien2k': disp_dataset, supercell = parse_disp_yaml(filename=disp_filename, return_cell=True) else: disp_dataset = parse_disp_yaml(filename=disp_filename) else: phpy = PhonopyYaml() phpy.read(disp_filename) supercell = phpy.supercell disp_dataset = phpy.dataset if 'natom' in disp_dataset: # type-1 dataset num_atoms = disp_dataset['natom'] num_displacements = len(disp_dataset['first_atoms']) dataset_type = 1 elif 'displacements' in disp_dataset: # type-2 dataset num_atoms = disp_dataset['displacements'].shape[1] num_displacements = disp_dataset['displacements'].shape[0] dataset_type = 2 else: raise RuntimeError("Number of atoms could not be retrieved from %s" % disp_filename) if force_sets_zero_mode: num_displacements += 1 if not check_number_of_force_files(num_displacements, force_filenames, disp_filename): force_sets = [] elif interface_mode == 'wien2k': force_sets = get_force_sets_wien2k( num_displacements, force_filenames, disp_filename, supercell, disp_dataset, wien2k_P1_mode=wien2k_P1_mode, symmetry_tolerance=symmetry_tolerance, verbose=(log_level > 0)) else: force_sets = get_force_sets(interface_mode, num_atoms, num_displacements, force_filenames, disp_filename=disp_filename, verbose=(log_level > 0)) if force_sets: if force_sets_zero_mode: force_sets = _subtract_residual_forces(force_sets) if dataset_type == 1: for forces, disp in zip(force_sets, disp_dataset['first_atoms']): disp['forces'] = forces elif dataset_type == 2: disp_dataset['forces'] = np.array(force_sets, dtype='double', order='C') else: raise RuntimeError("FORCE_SETS could not be created.") write_FORCE_SETS(disp_dataset, filename=force_sets_filename) if log_level > 0: if force_sets: print("%s has been created." % force_sets_filename) else: print("%s could not be created." % force_sets_filename) return 0
import phonopy from phonopy.interface.phonopy_yaml import PhonopyYaml phonon = phonopy.load(supercell_matrix=[[2, 0, 0], [0, 2, 0], [0, 0, 2]], primitive_matrix=[[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]], unitcell_filename="POSCAR-unitcell", force_sets_filename="FORCE_SETS", born_filename="BORN") phpy_yaml = PhonopyYaml(calculator='vasp', settings={'force_constants': True}) phpy_yaml.set_phonon_info(phonon) print(phpy_yaml)
def load( phonopy_yaml=None, # phonopy.yaml-like must be the first argument. supercell_matrix=None, primitive_matrix=None, is_nac=True, calculator=None, unitcell=None, supercell=None, nac_params=None, unitcell_filename=None, supercell_filename=None, born_filename=None, force_sets_filename=None, force_constants_filename=None, fc_calculator=None, factor=None, frequency_scale_factor=None, symprec=1e-5, is_symmetry=True, log_level=0): """Create Phonopy 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. When force_sets_filename and force_constants_filename are not given, 'FORCE_SETS' is looked for in the current directory as the default behaviour. Parameters ---------- phonopy_yaml : str, optional Filename of "phonopy.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. 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://atztogo.github.io/spglib/definition.html is used. 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 cell. 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. 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. 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'). force_sets_filename : str, optional Filename of a file corresponding to 'FORCE_SETS', a file contains sets of forces and displacements. Default is None. The priority for force constants is force_constants_filename > force_sets_filename > 'FORCE_SETS'. force_constants_filename : str, optional Filename of a file corresponding to 'FORCE_CONSTANTS' or 'force_constants.hdf5', a file contains force constants. Default is None. The priority for force constants is force_constants_filename > force_sets_filename > 'FORCE_SETS'. 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. symprec : float, optional Tolerance used to find crystal symmetry. Default is 1e-5. is_symmetry : bool, optional Setting False, crystal symmetry except for lattice translation is not considered. Default is True. log_level : int, optional Verbosity control. Default is 0. """ if phonopy_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) _nac_params = nac_params _dataset = None _fc = None else: phpy_yaml = PhonopyYaml() phpy_yaml.read(phonopy_yaml) cell = phpy_yaml.unitcell smat = phpy_yaml.supercell_matrix if smat is None: smat = np.eye(3, dtype='intc', order='C') if primitive_matrix is 'auto': pmat = 'auto' else: pmat = phpy_yaml.primitive_matrix if is_nac: _nac_params = phpy_yaml.nac_params else: _nac_params = None _dataset = phpy_yaml.dataset _fc = phpy_yaml.force_constants # units keywords: factor, nac_factor, distance_to_A units = get_default_physical_units(calculator) if factor is None: _factor = units['factor'] else: _factor = factor phonon = Phonopy(cell, smat, primitive_matrix=pmat, factor=_factor, frequency_scale_factor=frequency_scale_factor, symprec=symprec, is_symmetry=is_symmetry, calculator=calculator, log_level=log_level) load_helper.set_nac_params(phonon, _nac_params, born_filename, is_nac, units['nac_factor']) if _fc is None: load_helper.set_force_constants( phonon, dataset=_dataset, force_constants_filename=force_constants_filename, force_sets_filename=force_sets_filename, calculator=calculator, fc_calculator=fc_calculator) else: phonon.force_constants = _fc return phonon
from phonopy import Phonopy from phonopy.interface.vasp import read_vasp from phonopy.file_IO import parse_FORCE_SETS, parse_BORN from phonopy.interface.phonopy_yaml import PhonopyYaml unitcell = read_vasp("POSCAR") phonon = Phonopy(unitcell, [[2, 0, 0], [0, 2, 0], [0, 0, 2]], primitive_matrix=[[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]) force_sets = parse_FORCE_SETS() phonon.set_displacement_dataset(force_sets) phonon.produce_force_constants() primitive = phonon.get_primitive() nac_params = parse_BORN(primitive, filename="BORN") phonon.set_nac_params(nac_params) phpy_yaml = PhonopyYaml(calculator='vasp', show_force_constants=True) phpy_yaml.set_phonon_info(phonon) print(phpy_yaml)
def test_read_poscar_yaml(self): filename = "POSCAR.yaml" phpy_yaml = PhonopyYaml(filename) print(phpy_yaml)