Skip to content

Commit

Permalink
wip: put back site sorting and blackfy
Browse files Browse the repository at this point in the history
  • Loading branch information
maurov committed Oct 20, 2023
1 parent 9ea47b1 commit b58b75b
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions larch/xrd/struct2xas.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
try:
import pandas as pd
from pandas.io.formats.style import Styler

HAS_PANDAS = True
except ImportError:
HAS_PANDAS = False

try:
import py3Dmol

HAS_PY3DMOL = True
except ImportError:
HAS_PY3DMOL = False
Expand Down Expand Up @@ -101,7 +103,7 @@ def structure_folders():
making sure each exists
"""
folders = {}
for name in ('feff', 'fdmnes', 'mp_structs'):
for name in ("feff", "fdmnes", "mp_structs"):
folders[name] = unixpath(os.path.join(user_larchdir, name))
mkdir(folders[name])
return folders
Expand Down Expand Up @@ -246,7 +248,7 @@ def read_structure(self, file):
self.nframes = 1
self.cif = CifParser(self.file)
self.struct = Structure.from_file(self.file)
#self.struct = self.cif.get_structures()[0] #: NOT WORKING!
# self.struct = self.cif.get_structures()[0] #: NOT WORKING!
logger.debug("structure created from a CIF file")
elif self.file_ext == ".xyz":
self.is_xyz = True
Expand All @@ -263,7 +265,7 @@ def read_structure(self, file):
self.molecules = None
self.mol = None
self.nframes = 1
self.struct = Structure.from_dict(json.load(open(self.file, 'r')))
self.struct = Structure.from_dict(json.load(open(self.file, "r")))
logger.debug("structure created from JSON file")

else:
Expand Down Expand Up @@ -395,9 +397,9 @@ def get_abs_sites(self):

# Get multiples sites for absorber atom
for idx, sites in enumerate(sym_struct.equivalent_sites):
# sites = sorted(
# sites, key=lambda s: tuple(abs(x) for x in s.frac_coords)
# )
sites = sorted(
sites, key=lambda s: tuple(abs(x) for x in s.frac_coords)
)
site = sites[0]
abs_row = [idx, site.species_string]
abs_row.append([j for j in np.round(site.frac_coords, 4)])
Expand Down Expand Up @@ -768,8 +770,9 @@ def make_cluster(self, radius):
atoms = sorted(atoms, key=lambda x: x[2])
return atoms

def make_input_fdmnes(self, radius=7, parent_path=None,
template=None, green=True, **kwargs):
def make_input_fdmnes(
self, radius=7, parent_path=None, template=None, green=True, **kwargs
):
"""
Create a fdmnes input from a template.
Expand Down Expand Up @@ -801,15 +804,19 @@ def make_input_fdmnes(self, radius=7, parent_path=None,

if template is None:
template = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"templates", "fdmnes.tmpl")
os.path.dirname(os.path.realpath(__file__)), "templates", "fdmnes.tmpl"
)

if parent_path is None:
parent_path = self.folders['fdmnes']

self.outdir = os.path.join(parent_path, self.file_name, self.abs_atom,
f"frame{self.frame}",
f"site{self.abs_site}")
parent_path = self.folders["fdmnes"]

self.outdir = os.path.join(
parent_path,
self.file_name,
self.abs_atom,
f"frame{self.frame}",
f"site{self.abs_site}",
)

method = "green" if green else ""
absorber = ""
Expand Down Expand Up @@ -848,9 +855,9 @@ def make_input_fdmnes(self, radius=7, parent_path=None,

unique_sites = []
for sites in analyzer.get_symmetrized_structure().equivalent_sites:
# sites = sorted(
# sites, key=lambda s: tuple(abs(x) for x in s.frac_coords)
# )
sites = sorted(
sites, key=lambda s: tuple(abs(x) for x in s.frac_coords)
)
unique_sites.append((sites[0], len(sites)))
sites = str()
if self.full_occupancy:
Expand Down Expand Up @@ -938,9 +945,17 @@ def make_input_fdmnes(self, radius=7, parent_path=None,

logger.info(f"written FDMNES input -> {fnout}")

def make_input_feff(self, radius=7, parent_path=None,
template=None, feff_comment="*", edge="K",
sig2=None, debye=None, **kwargs):
def make_input_feff(
self,
radius=7,
parent_path=None,
template=None,
feff_comment="*",
edge="K",
sig2=None,
debye=None,
**kwargs,
):
"""
Create a FEFF input from a template.
Expand Down Expand Up @@ -977,14 +992,21 @@ def make_input_feff(self, radius=7, parent_path=None,
replacements["version"] = __version__

if parent_path is None:
parent_path = self.folders['feff']
self.outdir = os.path.join(parent_path, self.file_name, self.abs_atom,
f"frame{self.frame}", f"site{self.abs_site}")
parent_path = self.folders["feff"]
self.outdir = os.path.join(
parent_path,
self.file_name,
self.abs_atom,
f"frame{self.frame}",
f"site{self.abs_site}",
)

if template is None:
template = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"templates", "feff_exafs.tmpl")
"templates",
"feff_exafs.tmpl",
)

if sig2 is None:
use_sig2 = "*"
Expand Down Expand Up @@ -1343,11 +1365,10 @@ def save_cif_from_mp(api_key, material_id, parent_path=None):
"""
if parent_path is None:
parent_path = structure_folders()['mp_structs']
parent_path = structure_folders()["mp_structs"]

from pymatgen.ext.matproj import _MPResterLegacy


cif = _MPResterLegacy(api_key).get_data(material_id, prop="cif")
pf = _MPResterLegacy(api_key).get_data(material_id, prop="pretty_formula")[0][
"pretty_formula"
Expand Down Expand Up @@ -1386,21 +1407,23 @@ def save_mp_structure(api_key, material_id, parent_path=None):
"""

if parent_path is None:
parent_path = structure_folders()['mp_structs']
parent_path = structure_folders()["mp_structs"]

try:
from mp_api.client import MPRester
except ImportError:
print("need to install mp_api: pip install mp_api")

mpr = MPRester(api_key)
results = mpr.summary.search(material_ids=[material_id], fields=["structure", "formula_pretty"])
mpr = MPRester(api_key)
results = mpr.summary.search(
material_ids=[material_id], fields=["structure", "formula_pretty"]
)
formula = results[0].formula_pretty
structure = results[0].structure

outfile = os.path.join(parent_path, f"{formula}_{material_id}.mpjson")
with open(outfile, 'w') as fh:
fh.write(structure.to_json())
with open(outfile, "w") as fh:
fh.write(structure.to_json())
logger.info(f"saved {material_id} to {outfile}")

return outfile

0 comments on commit b58b75b

Please sign in to comment.