def test_parse_ibands(value, expected): if not isinstance(expected, dict): with expected: parse_ibands(value) else: parsed = parse_ibands(value) parsed = {s: i.tolist() for s, i in parsed.items()} assert parsed == expected
def wave(**kwargs): """Extract wavefunction coefficients from a WAVECAR""" from pymatgen.io.vasp import BSVasprun from amset.constants import defaults from amset.electronic_structure.common import ( get_band_structure, get_ibands, get_zero_weighted_kpoint_indices, ) from amset.tools.common import echo_ibands from amset.wavefunction.io import write_coefficients output = kwargs.pop("output") planewave_cutoff = kwargs.pop("planewave_cutoff") pawpyseed = kwargs.pop("pawpyseed") energy_cutoff = kwargs.pop("energy_cutoff") if not energy_cutoff: energy_cutoff = defaults["energy_cutoff"] if kwargs["directory"]: vasprun_file = Path(kwargs["directory"]) / "vasprun.xml" else: vasprun_file = kwargs["vasprun"] try: vr = BSVasprun(vasprun_file) except FileNotFoundError: vr = BSVasprun(str(vasprun_file) + ".gz") zwk_mode = kwargs.pop("zero_weighted_kpoints") if not zwk_mode: zwk_mode = defaults["zero_weighted_kpoints"] bs = get_band_structure(vr, zero_weighted=zwk_mode) if "bands" in kwargs and kwargs["bands"] is not None: ibands = parse_ibands(kwargs["bands"]) else: ibands = get_ibands(energy_cutoff, bs) ikpoints = get_zero_weighted_kpoint_indices(vr, zwk_mode) click.echo("******* Getting wavefunction coefficients *******\n") echo_ibands(ibands, bs.is_spin_polarized) click.echo("") if pawpyseed: coeffs, gpoints, kpoints = _wavefunction_pawpy(bs, ibands, planewave_cutoff, ikpoints, **kwargs) else: coeffs, gpoints = _wavefunction_vasp(ibands, planewave_cutoff, ikpoints, **kwargs) kpoints = np.array([k.frac_coords for k in bs.kpoints]) structure = vr.final_structure click.echo("Writing coefficients to {}".format(output)) write_coefficients(coeffs, gpoints, kpoints, structure, filename=output)
def read(bulk_folder, deformation_folders, **kwargs): """ Read deformation calculations and extract deformation potentials. """ from pymatgen.symmetry.analyzer import SpacegroupAnalyzer from pymatgen.util.string import unicodeify_spacegroup from amset.constants import defaults from amset.deformation.common import get_formatted_tensors from amset.deformation.io import parse_calculation, write_deformation_potentials from amset.deformation.potentials import ( calculate_deformation_potentials, extract_bands, get_strain_mapping, get_symmetrized_strain_mapping, strain_coverage_ok, ) from amset.electronic_structure.common import get_ibands from amset.electronic_structure.kpoints import get_kpoints_from_bandstructure from amset.electronic_structure.symmetry import expand_bandstructure energy_cutoff = kwargs.pop("energy_cutoff") if not energy_cutoff: energy_cutoff = defaults["energy_cutoff"] zwk_mode = kwargs.pop("zero_weighted_kpoints") if not zwk_mode: zwk_mode = defaults["zero_weighted_kpoints"] symprec = _parse_symprec(kwargs["symprec"]) symprec_deformation = kwargs["symprec_deformation"] click.echo("Reading bulk (undeformed) calculation") bulk_calculation = parse_calculation(bulk_folder, zero_weighted_kpoints=zwk_mode) bulk_structure = bulk_calculation["bandstructure"].structure deformation_calculations = [] for deformation_folder in deformation_folders: click.echo("Reading deformation calculation in {}".format(deformation_folder)) deformation_calculation = parse_calculation( deformation_folder, zero_weighted_kpoints=zwk_mode ) if check_calculation(bulk_calculation, deformation_calculation): deformation_calculations.append(deformation_calculation) sga = SpacegroupAnalyzer(bulk_structure, symprec=symprec) spg_symbol = unicodeify_spacegroup(sga.get_space_group_symbol()) spg_number = sga.get_space_group_number() click.echo("\nSpacegroup: {} ({})".format(spg_symbol, spg_number)) lattice_match = reciprocal_lattice_match( bulk_calculation["bandstructure"], symprec=symprec ) if not lattice_match: click.echo( "\nWARNING: Reciprocal lattice and k-lattice belong to different\n" " class of lattices. Often results are still useful but\n" " it is recommended to regenerate deformations without\n" " symmetry using: amset deform create --symprec N" ) strain_mapping = get_strain_mapping(bulk_structure, deformation_calculations) click.echo("\nFound {} strains:".format(len(strain_mapping))) fmt_strain = get_formatted_tensors(strain_mapping.keys()) click.echo(" - " + "\n - ".join(fmt_strain)) strain_mapping = get_symmetrized_strain_mapping( bulk_structure, strain_mapping, symprec=symprec, symprec_deformation=symprec_deformation, ) click.echo("\nAfter symmetrization found {} strains:".format(len(strain_mapping))) fmt_strain = get_formatted_tensors(strain_mapping.keys()) click.echo(" - " + "\n - ".join(fmt_strain)) if not strain_coverage_ok(list(strain_mapping.keys())): click.echo("\nERROR: Strains do not cover full tensor, check calculations") sys.exit() click.echo("\nCalculating deformation potentials") bulk_calculation["bandstructure"] = expand_bandstructure( bulk_calculation["bandstructure"], symprec=symprec ) deformation_potentials = calculate_deformation_potentials( bulk_calculation, strain_mapping ) print_deformation_summary(bulk_calculation["bandstructure"], deformation_potentials) if "bands" in kwargs and kwargs["bands"] is not None: ibands = parse_ibands(kwargs["bands"]) else: ibands = get_ibands(energy_cutoff, bulk_calculation["bandstructure"]) echo_ibands(ibands, bulk_calculation["bandstructure"].is_spin_polarized) deformation_potentials = extract_bands(deformation_potentials, ibands) kpoints = get_kpoints_from_bandstructure(bulk_calculation["bandstructure"]) filename = write_deformation_potentials( deformation_potentials, kpoints, bulk_structure, filename=kwargs["output"] ) click.echo("\nDeformation potentials written to {}".format(filename))