forked from materialsproject/pymatgen-analysis-defects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
177 lines (148 loc) · 5.65 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from collections import defaultdict
from pathlib import Path
import numpy as np
import pytest
from monty.serialization import loadfn
from pymatgen.analysis.defects.core import PeriodicSite, Substitution
from pymatgen.analysis.defects.thermo import DefectEntry, FormationEnergyDiagram
from pymatgen.analysis.phase_diagram import PhaseDiagram
from pymatgen.core import Element, Structure
from pymatgen.core.periodic_table import Specie
from pymatgen.io.vasp.outputs import WSWQ, Chgcar, Locpot, Procar, Vasprun
@pytest.fixture(scope="session")
def test_dir():
module_dir = Path(__file__).resolve().parent
test_dir = module_dir / "test_files"
return test_dir.resolve()
@pytest.fixture(scope="session")
def gan_struct(test_dir):
return Structure.from_file(test_dir / "GaN.vasp")
@pytest.fixture(scope="session")
def mixed_valence_struct(test_dir):
return Structure(
lattice=np.array([[3.0, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 3.0]]),
species=["Cu+", "Cu+", "Cu2+", "O2-"],
coords=[[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [0.5, 0.5, 0.0], [0.5, 0.0, 0.5]],
)
@pytest.fixture(scope="session")
def stable_entries_Mg_Ga_N(test_dir):
return loadfn(test_dir / "stable_entries_Mg_Ga_N.json")
@pytest.fixture(scope="session")
def defect_Mg_Ga(gan_struct):
ga_site = gan_struct[0]
mg_site = PeriodicSite(Specie("Mg"), ga_site.frac_coords, gan_struct.lattice)
return Substitution(gan_struct, mg_site)
@pytest.fixture(scope="session")
def data_Mg_Ga(test_dir):
"""Get the data in the following format:
{
"bulk_sc": {
"vasp_run": Vasprun,
"locpot": Locpot,
},
"q=1": {
"vasp_run": Vasprun,
"locpot": Locpot,
},
...
}.
"""
root_dir = test_dir / "Mg_Ga"
data = defaultdict(dict)
for fold in root_dir.glob("./*"):
if not fold.is_dir():
continue
data[fold.name] = {
"vasprun": Vasprun(fold / "vasprun.xml.gz"),
"locpot": Locpot.from_file(fold / "LOCPOT.gz"),
}
return data
@pytest.fixture(scope="session")
def defect_entries_and_plot_data_Mg_Ga(data_Mg_Ga, defect_Mg_Ga):
bulk_locpot = data_Mg_Ga["bulk_sc"]["locpot"]
def get_data(q):
computed_entry = data_Mg_Ga[f"q={q}"]["vasprun"].get_computed_entry(
inc_structure=True
)
defect_locpot = data_Mg_Ga[f"q={q}"]["locpot"]
def_entry = DefectEntry(
defect=defect_Mg_Ga, charge_state=q, sc_entry=computed_entry
)
frey_summary = def_entry.get_freysoldt_correction(
defect_locpot=defect_locpot, bulk_locpot=bulk_locpot, dielectric=14
)
return def_entry, frey_summary
defect_entries = dict()
plot_data = dict()
for qq in [-2, -1, 0, 1]:
defect_entry, frey_summary = get_data(qq)
defect_entries[qq] = defect_entry
plot_data[qq] = frey_summary.metadata["plot_data"]
return defect_entries, plot_data
@pytest.fixture(scope="session")
def chgcar_fe3o4(test_dir):
return Chgcar.from_file(test_dir / "CHGCAR.Fe3O4.vasp")
@pytest.fixture(scope="session")
def v_ga(test_dir):
res = dict()
for q1, q2 in [(0, -1), (-1, 0)]:
ccd_dir = test_dir / f"v_Ga/ccd_{q1}_{q2}"
vaspruns = [Vasprun(ccd_dir / f"{i}/vasprun.xml") for i in [0, 1, 2]]
wswq_dir = ccd_dir / "wswqs"
wswq_files = [f for f in wswq_dir.glob("WSWQ*")]
wswq_files.sort(
key=lambda x: int(x.name.split(".")[1])
) # does stem work for non-zipped files?
wswqs = [WSWQ.from_file(f) for f in wswq_files]
# wswqs = [WSWQ.from_file(ccd_dir / "wswqs" / f"WSWQ.{i}.gz") for i in [0, 1, 2]]
res[(q1, q2)] = {
"vaspruns": vaspruns,
"procar": Procar(ccd_dir / "1/PROCAR"),
"wswqs": wswqs,
}
return res
@pytest.fixture(scope="session")
def v_N_GaN(test_dir):
"""More complex."""
bulk_locpot = Locpot.from_file(test_dir / "v_N_GaN/bulk/LOCPOT.gz")
return {
"bulk_locpot": bulk_locpot,
"defect_locpots": {
-1: Locpot.from_file(test_dir / "v_N_GaN/q=-1/LOCPOT.gz"),
0: Locpot.from_file(test_dir / "v_N_GaN/q=0/LOCPOT.gz"),
1: Locpot.from_file(test_dir / "v_N_GaN/q=1/LOCPOT.gz"),
2: Locpot.from_file(test_dir / "v_N_GaN/q=2/LOCPOT.gz"),
},
}
@pytest.fixture(scope="session")
def basic_fed(data_Mg_Ga, defect_entries_and_plot_data_Mg_Ga, stable_entries_Mg_Ga_N):
bulk_vasprun = data_Mg_Ga["bulk_sc"]["vasprun"]
bulk_bs = bulk_vasprun.get_band_structure()
vbm = bulk_bs.get_vbm()["energy"]
bulk_entry = bulk_vasprun.get_computed_entry(inc_structure=False)
defect_entries, _ = defect_entries_and_plot_data_Mg_Ga
def_ent_list = list(defect_entries.values())
# test the constructor with materials project phase diagram
atomic_entries = list(
filter(lambda x: len(x.composition.elements) == 1, stable_entries_Mg_Ga_N)
)
pd = PhaseDiagram(stable_entries_Mg_Ga_N)
# test the constructor with atomic entries
# this is the one we will use for the rest of the tests
fed = FormationEnergyDiagram.with_atomic_entries(
defect_entries=def_ent_list,
atomic_entries=atomic_entries,
vbm=vbm,
inc_inf_values=False,
phase_diagram=pd,
bulk_entry=bulk_entry,
)
assert len(fed.chempot_limits) == 3
# dataframe conversion
df = fed.as_dataframe()
assert df.shape == (4, 5)
# test that you can get the Ga-rich chempot
cp = fed.get_chempots(rich_element=Element("Ga"))
assert cp[Element("Ga")] == pytest.approx(0, abs=1e-2)
fed.band_gap = 2
return fed