def setUp(self): with open(os.path.join(test_dir, "NaCl_phonon_bandstructure.json"), "r", encoding='utf-8') as f: d = json.load(f) self.bs = PhononBandStructureSymmLine.from_dict(d) with open(os.path.join(test_dir, "Si_phonon_bandstructure.json"), "r", encoding='utf-8') as f: d = json.load(f) self.bs2 = PhononBandStructureSymmLine.from_dict(d)
def __init__( self, qpoints, frequencies, gruneisenparameters, lattice, eigendisplacements=None, labels_dict=None, coords_are_cartesian=False, structure=None, ): """ Args: qpoints: list of qpoints as numpy arrays, in frac_coords of the given lattice by default frequencies: list of phonon frequencies in eV as a numpy array with shape (3*len(structure), len(qpoints)) gruneisenparameters: list of Grueneisen parameters as a numpy array with the shape (3*len(structure), len(qpoints)) lattice: The reciprocal lattice as a pymatgen Lattice object. Pymatgen uses the physics convention of reciprocal lattice vectors WITH a 2*pi coefficient eigendisplacements: the phonon eigendisplacements associated to the frequencies in cartesian coordinates. A numpy array of complex numbers with shape (3*len(structure), len(qpoints), len(structure), 3). he First index of the array refers to the band, the second to the index of the qpoint, the third to the atom in the structure and the fourth to the cartesian coordinates. labels_dict: (dict) of {} this links a qpoint (in frac coords or cartesian coordinates depending on the coords) to a label. coords_are_cartesian: Whether the qpoint coordinates are cartesian. structure: The crystal structure (as a pymatgen Structure object) associated with the band structure. This is needed if we provide projections to the band structure """ GruneisenPhononBandStructure.__init__( self, qpoints=qpoints, frequencies=frequencies, gruneisenparameters=gruneisenparameters, lattice=lattice, eigendisplacements=eigendisplacements, labels_dict=labels_dict, coords_are_cartesian=coords_are_cartesian, structure=structure, ) PhononBandStructureSymmLine._reuse_init( self, eigendisplacements=eigendisplacements, frequencies=frequencies, has_nac=False, qpoints=qpoints)
def setUp(self): with open( os.path.join(PymatgenTest.TEST_FILES_DIR, "NaCl_phonon_bandstructure.json"), encoding="utf-8", ) as f: d = json.load(f) self.bs = PhononBandStructureSymmLine.from_dict(d) with open( os.path.join(PymatgenTest.TEST_FILES_DIR, "Si_phonon_bandstructure.json"), encoding="utf-8", ) as f: d = json.load(f) self.bs2 = PhononBandStructureSymmLine.from_dict(d)
def setUp(self) -> None: self.phonopyfinite = PhonopyFiniteDisplacements( filename=os.path.join(testfile_dir, "phonopy.yaml")) self.phonopyfinite.run_all() self.phonopyfinite2 = PhonopyFiniteDisplacements( filename=os.path.join(testfile_dir, "phonopy.yaml")) self.phonopyfinite2.run_all() self.phonopyfinite3 = PhonopyFiniteDisplacements( filename=os.path.join(testfile_dir, "phonopy_manipulated.yaml")) self.phonopyfinite3.run_all() self.compare = ComparePhononBS( self.phonopyfinite.phonon_band_structure_pymatgen, self.phonopyfinite2.phonon_band_structure_pymatgen) self.compare2 = ComparePhononBS( self.phonopyfinite.phonon_band_structure_pymatgen, self.phonopyfinite3.phonon_band_structure_pymatgen) self.band1_dict = self.phonopyfinite2.phonon_band_structure_pymatgen.as_dict( ) new_bands = [] for iband, band in enumerate(self.band1_dict["bands"]): new_bands.append([]) for frequency in band: new_bands[iband].append(frequency + 0.1) self.band1_dict["bands"] = new_bands self.bandnew = PhononBandStructureSymmLine.from_dict(self.band1_dict) self.compare3 = ComparePhononBS( self.phonopyfinite.phonon_band_structure_pymatgen, self.bandnew)
def setUp(self): with open( os.path.join(PymatgenTest.TEST_FILES_DIR, "NaCl_phonon_bandstructure.json")) as f: d = json.loads(f.read()) self.bs = PhononBandStructureSymmLine.from_dict(d) self.plotter = PhononBSPlotter(self.bs)
def get_pmg_bs(self, phbands, labels_list): """ Generates a PhononBandStructureSymmLine starting from a abipy PhononBands object Args: phbands: abipy PhononBands labels_list: list of labels used to generate the path Returns: An instance of PhononBandStructureSymmLine """ structure = phbands.structure n_at = len(structure) qpts = np.array(phbands.qpoints.frac_coords) ph_freqs = np.array(phbands.phfreqs) displ = np.array(phbands.phdispl_cart) labels_dict = {} for i, (q, l) in enumerate(zip(qpts, labels_list)): if l: labels_dict[l] = q # set LO-TO at gamma if "Gamma" in l: if i > 0 and not labels_list[i-1]: ph_freqs[i] = phbands._get_non_anal_freqs(qpts[i-1]) displ[i] = phbands._get_non_anal_phdispl(qpts[i-1]) if i < len(qpts)-1 and not labels_list[i+1]: ph_freqs[i] = phbands._get_non_anal_freqs(qpts[i+1]) displ[i] = phbands._get_non_anal_phdispl(qpts[i+1]) ind = self.match_bands(displ, phbands.structure, phbands.amu) ph_freqs = ph_freqs[np.arange(len(ph_freqs))[:, None], ind] displ = displ[np.arange(displ.shape[0])[:, None, None], ind[..., None], np.arange(displ.shape[2])[None, None, :]] ph_freqs = np.transpose(ph_freqs) * eV_to_THz displ = np.transpose(np.reshape(displ, (len(qpts), 3*n_at, n_at, 3)), (1, 0, 2, 3)) return PhononBandStructureSymmLine(qpoints=qpts, frequencies=ph_freqs, lattice=structure.reciprocal_lattice, has_nac=phbands.non_anal_ph is not None, eigendisplacements=displ, labels_dict=labels_dict, structure=structure)
def get_phonon_band_structure_symm_line_from_fc( structure: Structure, supercell_matrix: np.ndarray, force_constants: np.ndarray, line_density: float = 20.0, symprec: float = 0.01, **kwargs, ) -> PhononBandStructureSymmLine: """ Get a phonon band structure along a high symmetry path from phonopy force constants. Args: structure: A structure. supercell_matrix: The supercell matrix used to generate the force constants. force_constants: The force constants in phonopy format. line_density: The density along the high symmetry path. symprec: Symmetry precision passed to phonopy and used for determining the band structure path. **kwargs: Additional kwargs passed to the Phonopy constructor. Returns: The line mode band structure. """ structure_phonopy = get_phonopy_structure(structure) phonon = Phonopy(structure_phonopy, supercell_matrix=supercell_matrix, symprec=symprec, **kwargs) phonon.set_force_constants(force_constants) kpath = HighSymmKpath(structure, symprec=symprec) kpoints, labels = kpath.get_kpoints(line_density=line_density, coords_are_cartesian=False) phonon.run_qpoints(kpoints) frequencies = phonon.qpoints.get_frequencies().T labels_dict = {a: k for a, k in zip(labels, kpoints) if a != ""} return PhononBandStructureSymmLine(kpoints, frequencies, structure.lattice, labels_dict=labels_dict)
def get_band_structure(self): lattice = Lattice(self.header['cell']) structure = Structure(lattice, species=self.header['symbols'], coords=self.header['positions']) # I think this is right? Need to test somehow... mass_weights = 1 / np.sqrt(self.header['masses']) displacements = self.eigenvectors * mass_weights[np.newaxis, np.newaxis, :, np.newaxis] return PhononBandStructureSymmLine(self.qpts, self.frequencies, lattice.reciprocal_lattice, structure=structure, eigendisplacements=displacements, labels_dict=self.labels)
def phonon_bandplot( filename, poscar=None, prefix=None, directory=None, dim=None, born=None, qmesh=None, spg=None, primitive_axis=None, line_density=60, units="THz", symprec=0.01, mode="bradcrack", kpt_list=None, eigenvectors=None, labels=None, height=6.0, width=6.0, style=None, no_base_style=False, ymin=None, ymax=None, image_format="pdf", dpi=400, plt=None, fonts=None, dos=None, to_json=None, from_json=None, to_web=None, legend=None, ): """A script to plot phonon band structure diagrams. Args: filename (str): Path to phonopy output. Can be a band structure yaml file, ``FORCE_SETS``, ``FORCE_CONSTANTS``, or ``force_constants.hdf5``. poscar (:obj:`str`, optional): Path to POSCAR file of unitcell. Not required if plotting the phonon band structure from a yaml file. If not specified, the script will search for a POSCAR file in the current directory. prefix (:obj:`str`, optional): Prefix for file names. directory (:obj:`str`, optional): The directory in which to save files. dim (:obj:`list`, optional): supercell matrix born (:obj:`str`, optional): Path to file containing Born effective charges. Should be in the same format as the file produced by the ``phonopy-vasp-born`` script provided by phonopy. qmesh (:obj:`list` of :obj:`int`, optional): Q-point mesh to use for calculating the density of state. Formatted as a 3x1 :obj:`list` of :obj:`int`. spg (:obj:`str` or :obj:`int`, optional): The space group international number or symbol to override the symmetry determined by spglib. This is not recommended and only provided for testing purposes. This option will only take effect when ``mode = 'bradcrack'``. primitive_axis (:obj:`list` or :obj:`str`, optional): The transformation matrix from the conventional to primitive cell. Only required when the conventional cell was used as the starting structure. Should be provided as a 3x3 :obj:`list` of :obj:`float`. Alternatively the string 'auto' may be used to request that a primitive matrix is determined automatically. line_density (:obj:`int`, optional): Density of k-points along the path. units (:obj:`str`, optional): Units of phonon frequency. Accepted (case-insensitive) values are Thz, cm-1, eV, meV. symprec (:obj:`float`, optional): Tolerance for space-group-finding operations mode (:obj:`str`, optional): Method used for calculating the high-symmetry path. The options are: bradcrack Use the paths from Bradley and Cracknell. See [brad]_. pymatgen Use the paths from pymatgen. See [curt]_. seekpath Use the paths from SeeK-path. See [seek]_. kpt_list (:obj:`list`, optional): List of k-points to use, formatted as a list of subpaths, each containing a list of fractional k-points. For example:: [ [[0., 0., 0.], [0., 0., 0.5]], [[0.5, 0., 0.], [0.5, 0.5, 0.]] ] Will return points along ``0 0 0 -> 0 0 1/2 | 1/2 0 0 -> 1/2 1/2 0`` labels (:obj:`list`, optional): The k-point labels. These should be provided as a :obj:`list` of :obj:`str` for each subpath of the overall path. For example:: [ ['Gamma', 'Z'], ['X', 'M'] ] combined with the above example for ``kpt_list`` would indicate the path: Gamma -> Z | X -> M. If no labels are provided, letters from A -> Z will be used instead. eigenvectors (:obj:`bool`, optional): Write the eigenvectors to the yaml file. (Always True if to_web is set.) dos (str): Path to Phonopy total dos .dat file height (:obj:`float`, optional): The height of the plot. width (:obj:`float`, optional): The width of the plot. ymin (:obj:`float`, optional): The minimum energy on the y-axis. ymax (:obj:`float`, optional): The maximum energy on the y-axis. style (:obj:`list` or :obj:`str`, optional): (List of) matplotlib style specifications, to be composed on top of Sumo base style. no_base_style (:obj:`bool`, optional): Prevent use of sumo base style. This can make alternative styles behave more predictably. image_format (:obj:`str`, optional): The image file format. Can be any format supported by matplotlib, including: png, jpg, pdf, and svg. Defaults to pdf. dpi (:obj:`int`, optional): The dots-per-inch (pixel density) for the image. plt (:obj:`matplotlib.pyplot`, optional): A :obj:`matplotlib.pyplot` object to use for plotting. fonts (:obj:`list`, optional): Fonts to use in the plot. Can be a a single font, specified as a :obj:`str`, or several fonts, specified as a :obj:`list` of :obj:`str`. to_json (:obj:`str`, optional): JSON file to dump the Pymatgen band path object. from_json (:obj:`list` or :obj:`str`, optional): (List of) JSON bandpath data filename(s) to import and overlay. to_web (:obj:`str`, optional): JSON file to write data for visualisation with http://henriquemiranda.github.io/phononwebsite legend (:obj:`list` or :obj:`None`, optional): Legend labels. If None, don't add a legend. With a list length equal to from_json, label json inputs only. With one extra list entry, label all lines beginning with new plot. Returns: A matplotlib pyplot object. """ save_files = False if plt else True # don't save if pyplot object provided if isinstance(from_json, str): from_json = [from_json] if eigenvectors is None: if to_web is None: eigenvectors = False else: eigenvectors = True elif not eigenvectors and to_web is not None: raise ValueError("Cannot set eigenvectors=False and write web JSON") if filename is None: if from_json is None: filename = "FORCE_SETS" bs, phonon = _bs_from_filename( filename, poscar, dim, symprec, spg, kpt_list, labels, primitive_axis, units, born, mode, eigenvectors, line_density, ) else: logging.info( f"No input data, using file {from_json[0]} to construct plot") with open(from_json[0]) as f: bs = PhononBandStructureSymmLine.from_dict(json.load(f)) from_json = from_json[1:] phonon = None else: bs, phonon = _bs_from_filename( filename, poscar, dim, symprec, spg, kpt_list, labels, primitive_axis, units, born, mode, eigenvectors, line_density, ) if to_json is not None: logging.info(f"Writing symmetry lines to {to_json}") with open(to_json, "wt") as f: f.write(bs.to_json()) if to_web is not None: logging.info(f"Writing visualisation JSON to {to_web}") bs.write_phononwebsite(to_web) # Replace dos filename with data array if dos is not None: if phonon is None: logging.error("Cannot use phonon DOS without Phonopy") sys.exit() if isfile(dos): dos = np.genfromtxt(dos, comments="#") elif dos: phonon.set_mesh( qmesh, is_gamma_center=False, is_eigenvectors=True, is_mesh_symmetry=False, ) phonon.set_total_DOS() dos_freq, dos_val = phonon.get_total_DOS() dos = np.zeros((len(dos_freq), 2)) dos[:, 0], dos[:, 1] = dos_freq, dos_val plotter = SPhononBSPlotter(bs) plt = plotter.get_plot( units=units, ymin=ymin, ymax=ymax, height=height, width=width, plt=plt, fonts=fonts, dos=dos, style=style, no_base_style=no_base_style, from_json=from_json, legend=legend, ) if save_files: basename = f"phonon_band.{image_format}" filename = f"{prefix}_{basename}" if prefix else basename if directory: filename = os.path.join(directory, filename) if dpi is None: dpi = rcParams["figure.dpi"] plt.savefig(filename, format=image_format, dpi=dpi, bbox_inches="tight") filename = save_data_files(bs, prefix=prefix, directory=directory) return filename else: return plt
def setUp(self): with open(os.path.join(test_dir, "NaCl_phonon_bandstructure.json"), "r") as f: d = json.loads(f.read()) self.bs = PhononBandStructureSymmLine.from_dict(d) self.plotter = PhononBSPlotter(self.bs)
def get_ph_bs_symm_line_from_dict(bands_dict, has_nac=False, labels_dict=None): r""" Creates a pymatgen PhononBandStructure object from the dictionary extracted by the band.yaml file produced by phonopy. The labels will be extracted from the dictionary, if present. If the 'eigenvector' key is found the eigendisplacements will be calculated according to the formula:: exp(2*pi*i*(frac_coords \\dot q) / sqrt(mass) * v and added to the object. Args: bands_dict: the dictionary extracted from the band.yaml file has_nac: True if the data have been obtained with the option --nac option. Default False. labels_dict: dict that links a qpoint in frac coords to a label. Its value will replace the data contained in the band.yaml. """ structure = get_structure_from_dict(bands_dict) qpts = [] frequencies = [] eigendisplacements = [] phonopy_labels_dict = {} for p in bands_dict["phonon"]: q = p["q-position"] qpts.append(q) bands = [] eig_q = [] for b in p["band"]: bands.append(b["frequency"]) if "eigenvector" in b: eig_b = [] for i, eig_a in enumerate(b["eigenvector"]): v = np.zeros(3, np.complex) for x in range(3): v[x] = eig_a[x][0] + eig_a[x][1] * 1j eig_b.append( eigvec_to_eigdispl( v, q, structure[i].frac_coords, structure.site_properties["phonopy_masses"][i], )) eig_q.append(eig_b) frequencies.append(bands) if "label" in p: phonopy_labels_dict[p["label"]] = p["q-position"] if eig_q: eigendisplacements.append(eig_q) qpts = np.array(qpts) # transpose to match the convention in PhononBandStructure frequencies = np.transpose(frequencies) if eigendisplacements: eigendisplacements = np.transpose(eigendisplacements, (1, 0, 2, 3)) rec_latt = Lattice(bands_dict["reciprocal_lattice"]) labels_dict = labels_dict or phonopy_labels_dict ph_bs = PhononBandStructureSymmLine( qpts, frequencies, rec_latt, has_nac=has_nac, labels_dict=labels_dict, structure=structure, eigendisplacements=eigendisplacements, ) return ph_bs
def get_plot(self, units='THz', ymin=None, ymax=None, width=None, height=None, dpi=None, plt=None, fonts=None, dos=None, dos_aspect=3, color=None, style=None, no_base_style=False, from_json=None, legend=None): """Get a :obj:`matplotlib.pyplot` object of the phonon band structure. Args: units (:obj:`str`, optional): Units of phonon frequency. Accepted (case-insensitive) values are Thz, cm-1, eV, meV. ymin (:obj:`float`, optional): The minimum energy on the y-axis. ymax (:obj:`float`, optional): The maximum energy on the y-axis. width (:obj:`float`, optional): The width of the plot. height (:obj:`float`, optional): The height of the plot. dpi (:obj:`int`, optional): The dots-per-inch (pixel density) for the image. fonts (:obj:`list`, optional): Fonts to use in the plot. Can be a a single font, specified as a :obj:`str`, or several fonts, specified as a :obj:`list` of :obj:`str`. plt (:obj:`matplotlib.pyplot`, optional): A :obj:`matplotlib.pyplot` object to use for plotting. dos (:obj:`np.ndarray`): 2D Numpy array of total DOS data dos_aspect (float): Width division for vertical DOS color (:obj:`str` or :obj:`tuple`, optional): Line/fill colour in any matplotlib-accepted format style (:obj:`list`, :obj:`str`, or :obj:`dict`): Any matplotlib style specifications, to be composed on top of Sumo base style. no_base_style (:obj:`bool`, optional): Prevent use of sumo base style. This can make alternative styles behave more predictably. from_json (:obj:`list` or :obj:`None`, optional): List of paths to :obj:`pymatgen.phonon.bandstructure.PhononBandStructureSymmline` JSON dump files. These are used to generate additional plots displayed under the data attached to this plotter. The k-point path should be the same as the main plot; the reciprocal lattice is adjusted to fit the scaling of the main data input. Returns: :obj:`matplotlib.pyplot`: The phonon band structure plot. """ if from_json is None: from_json = [] if legend is None: legend = [''] * (len(from_json) + 1) else: if len(legend) == 1 + len(from_json): pass elif len(legend) == len(from_json): legend = [''] + list(legend) else: raise ValueError('Inappropriate number of legend entries') if color is None: color = 'C0' # Default to first colour in matplotlib series if dos is not None: plt = pretty_subplot(1, 2, width=width, height=height, sharex=False, sharey=True, dpi=dpi, plt=plt, gridspec_kw={'width_ratios': [dos_aspect, 1], 'wspace': 0}) ax = plt.gcf().axes[0] else: plt = pretty_plot(width, height, dpi=dpi, plt=plt) ax = plt.gca() def _plot_lines(data, ax, color=None, alpha=1, zorder=1): """Pull data from any PhononBSPlotter and add to axis""" dists = data['distances'] freqs = data['frequency'] # nd is branch index, nb is band index, nk is kpoint index for nd, nb in itertools.product(range(len(data['distances'])), range(self._nb_bands)): f = freqs[nd][nb] # plot band data ax.plot(dists[nd], f, ls='-', c=color, zorder=zorder) data = self.bs_plot_data() _plot_lines(data, ax, color=color) for i, bs_json in enumerate(from_json): with open(bs_json, 'rt') as f: json_data = json.load(f) json_data['lattice_rec'] = json.loads( self._bs.lattice_rec.to_json()) bs = PhononBandStructureSymmLine.from_dict(json_data) # bs.lattice_rec = self._bs.lattice_rec # raise Exception(bs.qpoints) json_plotter = PhononBSPlotter(bs) json_data = json_plotter.bs_plot_data() if json_plotter._nb_bands != self._nb_bands: raise Exception('Number of bands in {} does not match ' 'main plot'.format(bs_json)) _plot_lines(json_data, ax, color='C{}'.format(i + 1), zorder=0.5) if any(legend): # Don't show legend if all entries are empty string from matplotlib.lines import Line2D ax.legend([Line2D([0], [0], color='C{}'.format(i)) for i in range(len(legend))], legend) self._maketicks(ax, units=units) self._makeplot(ax, plt.gcf(), data, width=width, height=height, ymin=ymin, ymax=ymax, dos=dos, color=color) plt.tight_layout() plt.subplots_adjust(wspace=0) return plt