-
Notifications
You must be signed in to change notification settings - Fork 875
/
Copy pathoutputs.py
429 lines (363 loc) · 14.5 KB
/
outputs.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""
This module defines classes for parsing the FEFF output files.
Currently supports the xmu.dat, ldos.dat output files are for non-spin case.
"""
from __future__ import annotations
import re
from collections import defaultdict
from typing import TYPE_CHECKING
import numpy as np
from monty.io import zopen
from monty.json import MSONable
from pymatgen.core import Element
from pymatgen.electronic_structure.core import Orbital, Spin
from pymatgen.electronic_structure.dos import CompleteDos, Dos
from pymatgen.io.feff import Header, Potential, Tags
if TYPE_CHECKING:
from typing_extensions import Self
__author__ = "Alan Dozier, Kiran Mathew, Chen Zheng"
__credits__ = "Anubhav Jain, Shyue Ping Ong"
__copyright__ = "Copyright 2011, The Materials Project"
__version__ = "1.0.3"
__maintainer__ = "Alan Dozier"
__email__ = "[email protected]"
__status__ = "Beta"
__date__ = "April 7, 2013"
class LDos(MSONable):
"""Parser for ldos files ldos01, ldos02, ..."""
def __init__(self, complete_dos, charge_transfer):
"""
Args:
complete_dos (CompleteDos): complete dos object
charge_transfer (dict): computed charge transfer between atoms
dictionary.
"""
self.complete_dos = complete_dos
self.charge_transfer = charge_transfer
@classmethod
def from_file(cls, feff_inp_file: str = "feff.inp", ldos_file: str = "ldos") -> Self:
"""
Creates LDos object from raw Feff ldos files by
by assuming they are numbered consecutively, i.e. ldos01.dat
ldos02.dat...
Args:
feff_inp_file (str): input file of run to obtain structure
ldos_file (str): output ldos file of run to obtain dos info, etc.
"""
header_str = Header.header_string_from_file(feff_inp_file)
header = Header.from_str(header_str)
structure = header.struct
n_sites = len(structure)
parameters = Tags.from_file(feff_inp_file)
if "RECIPROCAL" in parameters:
pot_dict = {}
pot_read_start = re.compile(".*iz.*lmaxsc.*xnatph.*xion.*folp.*")
pot_read_end = re.compile(".*ExternalPot.*switch.*")
pot_inp = re.sub(r"feff.inp", r"pot.inp", feff_inp_file)
dos_index = 1
begin = 0
with zopen(pot_inp, mode="r") as potfile:
for line in potfile:
if len(pot_read_end.findall(line)) > 0:
break
if begin == 1:
begin += 1
continue
if begin == 2:
z_number = int(line.strip().split()[0])
ele_name = Element.from_Z(z_number).name
if ele_name not in pot_dict:
pot_dict[ele_name] = dos_index
else:
pot_dict[ele_name] = min(dos_index, pot_dict[ele_name])
dos_index += 1
if len(pot_read_start.findall(line)) > 0:
begin = 1
else:
pot_string = Potential.pot_string_from_file(feff_inp_file)
dicts = Potential.pot_dict_from_str(pot_string)
pot_dict = dicts[0]
with zopen(f"{ldos_file}00.dat", mode="r") as file:
lines = file.readlines()
e_fermi = float(lines[0].split()[4])
dos_energies = []
ldos = {}
for idx in range(1, len(pot_dict) + 1):
if len(str(idx)) == 1:
ldos[idx] = np.loadtxt(f"{ldos_file}0{idx}.dat")
else:
ldos[idx] = np.loadtxt(f"{ldos_file}{idx}.dat")
for idx in range(len(ldos[1])):
dos_energies.append(ldos[1][idx][0])
all_pdos: list[dict] = []
vorb = {"s": Orbital.s, "p": Orbital.py, "d": Orbital.dxy, "f": Orbital.f0}
forb = {"s": 0, "p": 1, "d": 2, "f": 3}
d_length = len(ldos[1])
for idx in range(n_sites):
pot_index = pot_dict[structure.species[idx].symbol]
all_pdos.append(defaultdict(dict))
for k, v in vorb.items():
density = [ldos[pot_index][j][forb[k] + 1] for j in range(d_length)]
updos = density
downdos = None
if downdos:
all_pdos[-1][v] = {Spin.up: updos, Spin.down: downdos}
else:
all_pdos[-1][v] = {Spin.up: updos}
pdos = all_pdos
vorb2 = {0: Orbital.s, 1: Orbital.py, 2: Orbital.dxy, 3: Orbital.f0}
pdoss = {structure[i]: {v: pdos[i][v] for v in vorb2.values()} for i in range(len(pdos))}
forb = {"s": 0, "p": 1, "d": 2, "f": 3}
t_dos = [0] * d_length
for idx in range(n_sites):
pot_index = pot_dict[structure.species[idx].symbol]
for forb_val in forb.values():
density = [ldos[pot_index][j][forb_val + 1] for j in range(d_length)]
for j in range(d_length):
t_dos[j] = t_dos[j] + density[j]
_t_dos: dict = {Spin.up: t_dos}
dos = Dos(e_fermi, dos_energies, _t_dos)
complete_dos = CompleteDos(structure, dos, pdoss)
charge_transfer = LDos.charge_transfer_from_file(feff_inp_file, ldos_file)
return cls(complete_dos, charge_transfer)
@staticmethod
def charge_transfer_from_file(feff_inp_file, ldos_file):
"""Get charge transfer from file.
Args:
feff_inp_file (str): name of feff.inp file for run
ldos_file (str): ldos filename for run, assume consecutive order,
i.e., ldos01.dat, ldos02.dat....
Returns:
dictionary of dictionaries in order of potential sites
({"p": 0.154, "s": 0.078, "d": 0.0, "tot": 0.232}, ...)
"""
cht = {}
parameters = Tags.from_file(feff_inp_file)
if "RECIPROCAL" in parameters:
dicts = [{}]
pot_dict = {}
dos_index = 1
begin = 0
pot_inp = re.sub(r"feff.inp", r"pot.inp", feff_inp_file)
pot_readstart = re.compile(".*iz.*lmaxsc.*xnatph.*xion.*folp.*")
pot_readend = re.compile(".*ExternalPot.*switch.*")
with zopen(pot_inp, mode="r") as potfile:
for line in potfile:
if len(pot_readend.findall(line)) > 0:
break
if begin == 1:
z_number = int(line.strip().split()[0])
ele_name = Element.from_Z(z_number).name
if len(pot_dict) == 0:
pot_dict[0] = ele_name
elif len(pot_dict) > 0:
pot_dict[max(pot_dict) + 1] = ele_name
begin += 1
continue
if begin == 2:
z_number = int(line.strip().split()[0])
ele_name = Element.from_Z(z_number).name
dicts[0][ele_name] = dos_index
dos_index += 1
if len(pot_dict) == 0:
pot_dict[0] = ele_name
elif len(pot_dict) > 0:
pot_dict[max(pot_dict) + 1] = ele_name
if len(pot_readstart.findall(line)) > 0:
begin = 1
else:
pot_string = Potential.pot_string_from_file(feff_inp_file)
dicts = Potential.pot_dict_from_str(pot_string)
pot_dict = dicts[1]
for idx in range(len(dicts[0]) + 1):
if len(str(idx)) == 1:
with zopen(f"{ldos_file}0{idx}.dat", mode="rt") as file:
lines = file.readlines()
s = float(lines[3].split()[2])
p = float(lines[4].split()[2])
d = float(lines[5].split()[2])
f1 = float(lines[6].split()[2])
tot = float(lines[1].split()[4])
cht[str(idx)] = {pot_dict[idx]: {"s": s, "p": p, "d": d, "f": f1, "tot": tot}}
else:
with zopen(f"{ldos_file}{idx}.dat", mode="rt") as file:
lines = file.readlines()
s = float(lines[3].split()[2])
p = float(lines[4].split()[2])
d = float(lines[5].split()[2])
f1 = float(lines[6].split()[2])
tot = float(lines[1].split()[4])
cht[str(idx)] = {pot_dict[idx]: {"s": s, "p": p, "d": d, "f": f1, "tot": tot}}
return cht
def charge_transfer_to_str(self):
"""Get charge transfer as string."""
ch = self.charge_transfer
chts = ["\nCharge Transfer\n\nabsorbing atom"]
for i in range(len(ch)):
for atom, v2 in ch[str(i)].items():
a = [
"\n",
atom,
"\n",
"s ",
str(v2["s"]),
"\n",
"p ",
str(v2["p"]),
"\n",
"d ",
str(v2["d"]),
"\n",
"f ",
str(v2["f"]),
"\n",
"tot ",
str(v2["tot"]),
"\n",
]
chts.extend(a)
return "".join(chts)
class Xmu(MSONable):
r"""
Parser for data in 'xmu.dat' file.
The file 'xmu.dat' contains XANES, EXAFS or NRIXS data depending on the
situation; \mu, \mu_0, and \chi = \chi * \mu_0 / \mu_0 / (edge+50eV) as
functions of absolute energy E, relative energy E - E_f and wave number k.
Default attributes:
xmu: Photon absorption cross section of absorbing atom in material
Energies: Energies of data point
relative_energies: E - E_fermi
wavenumber: k=\sqrt(E -E_fermi)
mu: The total absorption cross-section.
mu0: The embedded atomic background absorption.
chi: fine structure.
Edge: Absorption Edge
Absorbing atom: Species of absorbing atom
Material: Formula of material
Source: Source of structure
Calculation: Type of Feff calculation performed
"""
def __init__(self, header, parameters, absorbing_atom, data):
"""
Args:
header: Header object
parameters: Tags object
absorbing_atom (str | int): absorbing atom symbol or index
data (numpy.ndarray, Nx6): cross_sections.
"""
self.header = header
self.parameters = parameters
self.absorbing_atom = absorbing_atom
self.data = np.array(data)
@classmethod
def from_file(cls, xmu_dat_file: str = "xmu.dat", feff_inp_file: str = "feff.inp") -> Self:
"""Get Xmu from file.
Args:
xmu_dat_file (str): filename and path for xmu.dat
feff_inp_file (str): filename and path of feff.inp input file
Returns:
Xmu object
"""
data = np.loadtxt(xmu_dat_file)
header = Header.from_file(feff_inp_file)
parameters = Tags.from_file(feff_inp_file)
pots = Potential.pot_string_from_file(feff_inp_file)
# site index (Note: in feff it starts from 1)
# else case is species symbol
absorbing_atom = parameters["TARGET"] if "RECIPROCAL" in parameters else pots.splitlines()[3].split()[2]
return cls(header, parameters, absorbing_atom, data)
@property
def energies(self):
"""The absolute energies in eV."""
return self.data[:, 0]
@property
def relative_energies(self):
"""Energy with respect to the Fermi level E - E_f."""
return self.data[:, 1]
@property
def wavenumber(self):
r"""Get the wave number in units of \AA^-1. k=\sqrt(E - E_f) where E is
the energy and E_f is the Fermi level computed from electron gas theory
at the average interstitial charge density.
"""
return self.data[:, 2]
@property
def mu(self):
"""The total absorption cross-section."""
return self.data[:, 3]
@property
def mu0(self):
"""The embedded atomic background absorption."""
return self.data[:, 4]
@property
def chi(self):
"""The normalized fine structure."""
return self.data[:, 5]
@property
def e_fermi(self):
"""The Fermi level in eV."""
return self.energies[0] - self.relative_energies[0]
@property
def source(self):
"""Source identification from Header file."""
return self.header.source
@property
def calc(self):
"""Type of Feff calculation, XANES or EXAFS."""
return "XANES" if "XANES" in self.parameters else "EXAFS"
@property
def material_formula(self):
"""Chemical formula of material from feff.inp file."""
try:
form = self.header.formula
except IndexError:
form = "No formula provided"
return "".join(map(str, form))
@property
def edge(self):
"""Excitation edge."""
return self.parameters["EDGE"]
def as_dict(self):
"""Get dict representations of Xmu object."""
dct = MSONable.as_dict(self)
dct["data"] = self.data.tolist()
return dct
class Eels(MSONable):
"""Parse eels.dat file."""
def __init__(self, data):
"""
Args:
data (numpy.ndarray): data from eels.dat file
"""
self.data = np.array(data)
@property
def energies(self):
"""The energies in eV."""
return self.data[:, 0]
@property
def total_spectrum(self):
"""The total eels spectrum."""
return self.data[:, 1]
@property
def atomic_background(self) -> np.ndarray:
"""The atomic background of EELS."""
return self.data[:, 2]
@property
def fine_structure(self) -> np.ndarray:
"""The fine structure of EELS."""
return self.data[:, 3]
@classmethod
def from_file(cls, eels_dat_file: str = "eels.dat") -> Self:
"""Parse eels spectrum.
Args:
eels_dat_file (str): filename and path for eels.dat
Returns:
Eels
"""
data = np.loadtxt(eels_dat_file)
return cls(data)
def as_dict(self) -> dict:
"""Get dict representations of Xmu object."""
dct = MSONable.as_dict(self)
dct["data"] = self.data.tolist()
return dct