Skip to content

Commit

Permalink
uniform options for CifParser, and pymatgen imports
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Aug 15, 2023
1 parent 28ba79b commit 325cb6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
11 changes: 11 additions & 0 deletions larch/xrd/amcsd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import SingletonThreadPool

try:
from pymatgen.io.cif import CifParser
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Molecule, IMolecule, IStructure
except:
CifParser = SpacegroupAnalyzer = None
Molecule = IMolecule = IStructure = None

from larch.utils.physical_constants import ATOM_SYMS, ATOM_NAMES

__version__ = '1'

PMG_CIF_OPTS = dict(occupancy_tolerance=10, site_tolerance=5e-3)


def make_engine(dbname):
"create engine for sqlite connection"
return create_engine('sqlite:///%s' % (dbname),
Expand Down
18 changes: 6 additions & 12 deletions larch/xrd/cif2feff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
import random
from io import StringIO

HAS_PYMATGEN = False
try:
from pymatgen.io.cif import CifParser
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Molecule
HAS_PYMATGEN = True
except:
HAS_PYMATGEN = False

from xraydb import atomic_symbol, atomic_number, xray_edge
from larch.utils import fix_varname, strict_ascii, gformat

from .amcsd_utils import PMG_CIF_OPTS, CifParser, Molecule, SpacegroupAnalyzer

def get_atom_map(structure):
"""generalization of pymatgen atom map
Returns:
Expand Down Expand Up @@ -43,10 +37,10 @@ def read_cif_structure(ciftext):
-------
pymatgen Structure object
"""
if not HAS_PYMATGEN:
raise ImportError('pymatgen required')
if CifParser is None:
raise ValueError("CifParser from pymatgen not available. Try 'pip install pymatgen'.")
try:
cifstructs = CifParser(StringIO(ciftext), site_tolerance=5.e-4)
cifstructs = CifParser(StringIO(ciftext), **PMG_CIF_OPTS)
parse_ok = True
file_found = True
except:
Expand All @@ -55,7 +49,7 @@ def read_cif_structure(ciftext):
if os.path.exists(ciftext):
file_found = True
try:
cifstructs = CifParser(ciftext, site_tolerance=5.e-4)
cifstructs = CifParser(ciftext, **PMG_CIF_OPTS)
parse_ok = True
except:
parse_ok = False
Expand Down
26 changes: 10 additions & 16 deletions larch/xrd/structure2feff.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import os
import random

HAS_PYMATGEN = False
try:
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Molecule, IMolecule, IStructure
HAS_PYMATGEN = True
except:
HAS_PYMATGEN = False
from .amcsd_utils import (SpacegroupAnalyzer, Molecule, IMolecule, IStructure)

from xraydb import atomic_symbol, atomic_number, xray_edge
from larch.utils.strutils import fix_varname, strict_ascii
Expand Down Expand Up @@ -43,16 +37,16 @@ def read_structure(structure_text, fmt="cif"):
-------
pymatgen Structure object or Molecule object
"""
if not HAS_PYMATGEN:
raise ImportError('pymatgen required')
if Molecule is None:
raise ImportError("pymatgen required. Try 'pip install pymatgen'.")
try:
if fmt.lower() in ('cif', 'poscar', 'contcar', 'chgcar', 'locpot', 'cssr', 'vasprun.xml'):
struct = IStructure.from_str(structure_text, fmt, merge_tol=5.e-4)
else:
struct = IMolecule.from_str(structure_text, fmt)
parse_ok = True
file_found = True

except:
parse_ok = False
file_found = False
Expand Down Expand Up @@ -100,9 +94,9 @@ def parse_structure(structure_text, fmt='cif', fname="default.filename"):
struct = read_structure(structure_text, fmt=fmt)
except ValueError:
return '# could not read structure file'

return {'formula': struct.composition.reduced_formula, 'sites': struct.sites, 'structure_text': structure_text, 'fmt': fmt, 'fname': fname}


def structure2feffinp(structure_text, absorber, edge=None, cluster_size=8.0, absorber_site=1,
site_index=None, extra_titles=None, with_h=False, version8=True, fmt='cif'):
Expand Down Expand Up @@ -146,16 +140,16 @@ def structure2feffinp(structure_text, absorber, edge=None, cluster_size=8.0, abs
struct = read_structure(structure_text, fmt=fmt)
except ValueError:
return '# could not read structure file'

is_molecule = False

if isinstance(struct, IStructure):
sgroup = SpacegroupAnalyzer(struct).get_symmetry_dataset()
space_group = sgroup["international"]
else:
space_group = 'Molecule'
is_molecule = True


if isinstance(absorber, int):
absorber = atomic_symbol(absorber_z)
Expand Down Expand Up @@ -236,7 +230,7 @@ def structure2feffinp(structure_text, absorber, edge=None, cluster_size=8.0, abs
out_text.append(f'* using absorber at site {1+absorber_index:d} in the list below')
out_text.append(f'* selected as absorber="{absorber:s}", absorber_site={absorber_site:d}')
out_text.append('* index X Y Z species')

for i, site in enumerate(struct):
# The method of obtaining the cooridanates depends on whether the structure is a molecule or not
if is_molecule:
Expand Down

0 comments on commit 325cb6c

Please sign in to comment.