def read_force_constants_from_hdf5(filename='force_constants.hdf5', p2s_map=None, calculator=None): """Convert force constants physical unit Each calculator interface has own default force constants physical unit. This method reads the physical unit of force constants if it exists and if the read physical unit is different from the default one, the force constants values are converted to have the default physical units. Note ---- This method is also used from phonopy script. """ fc = read_force_constants_hdf5(filename=filename, p2s_map=p2s_map) fc_unit = read_physical_unit_in_force_constants_hdf5(filename=filename) if fc_unit is None: return fc else: factor = get_force_constant_conversion_factor(fc_unit, calculator) return fc * factor
def read_force_constants_from_hdf5(filename="force_constants.hdf5", p2s_map=None, calculator=None): """Convert force constants physical unit. Each calculator interface has own default force constants physical unit. This method reads 'physical_unit' in force constants hdf5 file and if this is different from the one for 'calculator', the force constants are converted to have the physical unit of the calculator. Note ---- This method is also used from phonopy script. """ fc, fc_unit = read_force_constants_hdf5(filename=filename, p2s_map=p2s_map, return_physical_unit=True) if fc_unit is None: return fc else: factor = get_force_constant_conversion_factor(fc_unit, calculator) return fc * factor
def phonopy_to_abinit(unit_cell, supercell_matrix, out_ddb_path, ngqpt=None, qpt_list=None, force_constants=None, force_sets=None, born=None, primitive_matrix="auto", symprec=1e-5, tolsym=None, supercell=None, calculator=None, manager=None, workdir=None, pseudos=None, verbose=False): """ Converts the data from phonopy to an abinit DDB file. The data can be provided in form of arrays or paths to the phonopy files that should be parsed. The minimal input should contains the FORCE_CONSTANTS or FORCE_SETS. If BORN is present the Born effective charges (BEC) and dielectric tensor will also be added to the DDB. The best agreement is obtained with supercell_matrix and ngqpt being equivalent (i.e. supercell_matrix a diagonal matrix with ngqpt as diagonal elements). Non diagonal supercell_matrix are allowed as well, but the information encoded in the DDB will be the result of an interpolation done through phonopy. Phonopy is used to convert the IFC to the dynamical matrix. However, in order to determine the list of q-points in the irreducible Brillouin zone and to prepare the base for the final DDB file, abinit will be called for a very short and inexpensive run. Performs a check to verify if the two codes identify the same symmetries and it gives a warning in case of failure. Mismatching symmetries may lead to incorrect conversions. Args: unit_cell: a |Structure| object that identifies the unit cell used for the phonopy calculation. supercell_matrix: a 3x3 array representing the supercell matrix used to generated the forces with phonopy. out_ddb_path: a full path to the file where the new DDB will be written ngqpt: a list of 3 elements indicating the grid of q points that will be used in the DDB. qpt_list: alternatively to ngqpt an explicit list of q-points can be provided here. At least one among ngqpt and qpt_list should be defined. force_constants: an array with shape (num atoms unit cell, num atoms supercell, 3, 3) containing the force constants. Alternatively a string with the path to the FORCE_CONSTANTS file. This or force_set should be defined. If both given this has precedence. force_sets: a dictionary obtained from the force sets generated with phonopy. Alternatively a string with the path to the FORCE_SETS file. This or force_constants should be defined. born: a dictionary with "dielectric" and "born" keywords as obtained from the nac_params in phonopy. Alternatively a string with the path to the BORN file. Notice that the "factor" attribute is not taken into account, so the values should be in default phonopy units. primitive_matrix: a 3x3 array with the primitive matrix passed to Phonopy. "auto" will use spglib to try to determine it automatically. If the DDB file should contain the actual unit cell this should be the identity matrix. symprec: distance tolerance in Cartesian coordinates to find crystal symmetry in phonopy. It might be that the value should be tuned so that it leads to the the same symmetries as in the abinit calculation. tolsym: Gives the tolerance to identify symmetries in abinit. See abinit documentation for more details. supercell: if given it should represent the supercell used to get the force constants, without any perturbation. It will be used to match it to the phonopy supercell and sort the IFC in the correct order. calculator: a string with the name of the calculator. Will be used to set the conversion factor for the force constants coming from phonopy. manager: |TaskManager| object. If None, the object is initialized from the configuration file pseudos: List of filenames or list of |Pseudo| objects or |PseudoTable| object. It will be used by abinit to generate the base DDB file. If None the abipy.data.hgh_pseudos.HGH_TABLE table will be used. verbose: verbosity level. Set it to a value > 0 to get more information workdir: path to the directory where the abinit calculation will be executed. Returns: a DdbFile instance of the file written in out_ddb_path. """ if ngqpt is None and qpt_list is None: raise ValueError( "at least one among nqgpt and qpt_list should be defined") if force_sets is None and force_constants is None: raise ValueError( "at least one of force_sets and force_constants should be provided" ) phon_at = get_phonopy_structure(unit_cell) if isinstance(force_constants, str): force_constants = parse_FORCE_CONSTANTS(filename=force_constants) elif force_constants is not None: force_constants = np.array(force_constants) force_sets = None if isinstance(force_sets, str): force_sets = parse_FORCE_SETS(filename=force_sets) # no nac_params here, otherwise they will be used for the interpolation phonon = Phonopy(phon_at, supercell_matrix, primitive_matrix=primitive_matrix, nac_params=None, symprec=symprec, calculator=calculator) primitive = get_pmg_structure(phonon.primitive) if isinstance(born, str): born = parse_BORN(phonon.primitive, filename=born) if supercell is not None: ph_supercell = get_pmg_structure(phonon.supercell) if not np.allclose(supercell.lattice.matrix, ph_supercell.lattice.matrix): raise RuntimeError("The lattice of the supercells do not match") sc_mapping = [] for i, site_orig in enumerate(supercell): for j, site_ph in enumerate(ph_supercell): d = supercell.lattice.get_distance_and_image( site_orig.frac_coords, site_ph.frac_coords)[0] if d < 1e-5: sc_mapping.append(j) break else: raise RuntimeError( f"Could not find a match for site {i} with coords " f"{site_orig.cart_coords} in the supercell.") # cross check that the same atom was not matched twice n_matches = len(set(sc_mapping)) if n_matches < len(supercell): raise RuntimeError( f"Found matches for {n_matches} different atoms in the supercell: {sc_mapping}" ) force_constants = force_constants[:, sc_mapping] if force_constants is not None: phonon.set_force_constants(force_constants) else: phonon.dataset = force_sets phonon.produce_force_constants() if calculator: units = get_default_physical_units(calculator) fc_factor = get_force_constant_conversion_factor( units["force_constants_unit"], None) phonon.set_force_constants(phonon.force_constants * fc_factor) if pseudos is None: from abipy.data.hgh_pseudos import HGH_TABLE pseudos = HGH_TABLE inp = minimal_scf_input(primitive, pseudos) # get the qpoints list if not defined if qpt_list is None: inp["ngkpt"] = ngqpt qpt_list = inp.abiget_ibz(verbose=verbose)[0] dm_list = get_dm(phonon, qpt_list, primitive) if born is not None: # for the conversion of the BEC the zion (i.e. the ionic charge of the pseudo) # it is an additive factor and should be the same that goes in the header of the DDB, # so take it from the pseudos used to generate it. zion = inp.valence_electrons_per_atom born_data = generate_born_deriv(born, zion, primitive) else: born_data = None inp = minimal_scf_input(primitive, pseudos) if tolsym is not None: inp["tolsym"] = tolsym task = inp.run_in_shell(workdir=workdir, manager=manager, verbose=verbose) # use the output of abinit to check that the spacegroup identified by # phonopy and abinit are the same. with GsrFile(task.opath_from_ext("GSR.nc")) as gsr: abi_spg = gsr.structure.abi_spacegroup.spgid spglib_spg = phonon.symmetry.dataset["number"] if abi_spg != spglib_spg: warnings.warn( "The space group number obtained based on the DDB symmetries differs " f"from the one calculated with spglib: {abi_spg} versus " f"{spglib_spg}. The convertion may be incorrect. Try changing symprec or tolsym." ) tmp_ddb_path = task.opath_from_ext("DDB") ddb = DdbFile(tmp_ddb_path) # remove the blocks generated by the calculation and that are meaningless ddb.remove_block(dord=0) ddb.remove_block(dord=1) add_data_ddb(ddb, dm_list, qpt_list, born_data) ddb.write(out_ddb_path) new_ddb = DdbFile(out_ddb_path) return new_ddb