def test_remove_higher_energy_comp(): comp_es = CompositionEnergies( {Composition('Mg1'): CompositionEnergy(-1.9, "mp-2"), Composition('Mg2'): CompositionEnergy(-4.0, "mp-3"), Composition('O2'): CompositionEnergy(-4.0, "mp-4")}) actual = remove_higher_energy_comp(comp_es) expected = CompositionEnergies( {Composition('Mg2'): CompositionEnergy(-4.0, "mp-3"), Composition('O2'): CompositionEnergy(-4.0, "mp-4"),}) assert actual == expected
def make_composition_energies(args): if args.yaml_file: composition_energies = CompositionEnergies.from_yaml(args.yaml_file) else: composition_energies = CompositionEnergies() for d in args.dirs: vasprun = Vasprun(d / defaults.vasprun) composition = vasprun.final_structure.composition energy = float(vasprun.final_energy) # original type is FloatWithUnit composition_energies[composition] = CompositionEnergy(energy, "local") composition_energies.to_yaml_file()
def make_composition_energies(args): if args.yaml_file: composition_energies = CompositionEnergies.from_yaml(args.yaml_file) else: composition_energies = CompositionEnergies() for d in args.dirs: outcar = Outcar(d / defaults.outcar) composition = Structure.from_file(d / defaults.contcar).composition energy = float(outcar.final_energy) # original type is FloatWithUnit composition_energies[composition] = CompositionEnergy(energy, "local") composition_energies.to_yaml_file()
def make_composition_energies_from_mp( elements: List[str], atom_energy_yaml: Optional[str] = None, ) -> CompositionEnergies: """Obtain the energies from Materials Project. When the atom_energy_yaml is provided, the total energies are aligned via atom energies. """ properties = ["task_id", "full_formula", "final_energy"] query = MpQuery(elements, properties=properties) comp_es = {} if atom_energy_yaml: energies = loadfn(atom_energy_yaml) diff = {e: energies[e] - mp_energies[e] for e in elements} else: diff = {e: 0.0 for e in elements} for m in query.materials: key = Composition(m["full_formula"]) energy = m["final_energy"] for k, v in key.as_dict().items(): energy += diff[k] * v comp_es[key] = CompositionEnergy(energy, m["task_id"]) print(comp_es) comp_es = remove_higher_energy_comp(comp_es) print(comp_es) return CompositionEnergies(comp_es)
def comp_energies_corr(): return CompositionEnergies({ Composition('O8'): CompositionEnergy(-39.58364375 + diff["O"] * 8, "mp-1"), Composition('Mg3'): CompositionEnergy(-4.79068775 + diff["Mg"] * 3, "mp-2"), Composition('Mg1O1'): CompositionEnergy(-11.96742144 + diff["Mg"] + diff["O"], "mp-3") })
def comp_energies(): return CompositionEnergies({ Composition('O8'): CompositionEnergy(-39.58364375, "mp-1"), Composition('Mg3'): CompositionEnergy(-4.79068775, "mp-2"), Composition('Mg1O1'): CompositionEnergy(-11.96742144, "mp-3") })
def composition_energies(): return CompositionEnergies({ Composition("H"): CompositionEnergy(0.0, "a"), Composition("H4O2"): CompositionEnergy(-4.0, "c"), Composition("O"): CompositionEnergy(1.0, "b"), Composition("Cl"): CompositionEnergy(12.0, "f"), Composition("O2Cl"): CompositionEnergy(3.0, "e"), Composition("O2Cl2"): CompositionEnergy(6.0, "d") })
def test_composition_energies_from_mp(mocker, tmpdir): tmpdir.chdir() args = Namespace(elements=["H"], atom_energy_yaml="a.yaml") mock = mocker.patch( "pydefect.cli.main_util_functions.make_composition_energies_from_mp") mock.return_value = CompositionEnergies( {Composition("H"): CompositionEnergy(1.0, "mp-1")}) composition_energies_from_mp(args) mock.assert_called_once_with(elements=["H"], atom_energy_yaml="a.yaml") actual = Path("composition_energies.yaml").read_text() expected = """H1: energy: 1.0 source: mp-1 """ assert actual == expected
def test_make_standard_and_relative_energies(mocker, tmpdir): tmpdir.chdir() args = Namespace(composition_energies_yaml="a.yaml") mock = mocker.patch("pydefect.cli.main_functions.CompositionEnergies") mock.from_yaml.return_value = CompositionEnergies({ Composition("H"): CompositionEnergy(-1.0), Composition("He"): CompositionEnergy(-2.0), Composition("HHe"): CompositionEnergy(-5.0) }) make_standard_and_relative_energies(args) mock.from_yaml.assert_called_once_with("a.yaml") actual = Path("standard_energies.yaml").read_text() expected = """H: -1.0 He: -2.0 """ assert actual == expected actual = Path("relative_energies.yaml").read_text() expected = """HHe: -1.0 """ assert actual == expected
def make_composition_energies_from_mp(elements: List[str], atom_energy_yaml: Optional[str] = None, ) -> CompositionEnergies: """Obtain the energies from Materials Project. When the atom_energy_yaml is provided, the total energies are aligned via atom energies. """ entries: List[ComputedEntry] = MpEntries(elements).materials comp_es = {} if atom_energy_yaml: energies = loadfn(atom_energy_yaml) diff = {e: energies[e] - mp_energies[e] for e in elements} else: diff = {e: 0.0 for e in elements} for e in entries: key = e.composition energy = e.energy for k, v in key.as_dict().items(): energy += diff[k] * v comp_es[key] = CompositionEnergy(energy, e.entry_id) comp_es = remove_higher_energy_comp(comp_es) return CompositionEnergies(comp_es)
def make_standard_and_relative_energies(args): comp_energies = CompositionEnergies.from_yaml( args.composition_energies_yaml) std_energies, rel_energies = comp_energies.std_rel_energies std_energies.to_yaml_file() rel_energies.to_yaml_file()
def test_composition_energies_from_dict(): actual = CompositionEnergies.from_dict({"Mg": -1.0}) expected = CompositionEnergies( {Composition("Mg"): CompositionEnergy(-1.0)}) assert actual == expected actual = actual.std_rel_energies