Skip to content

Commit

Permalink
Add OccupancyComparator (#857)
Browse files Browse the repository at this point in the history
* OccupancyComparator for StructureMatcher + tests

* Remove debug print from critic2 caller
  • Loading branch information
mkhorton authored and shyuep committed Oct 4, 2017
1 parent 997405c commit 16a0320
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
27 changes: 27 additions & 0 deletions pymatgen/analysis/structure_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,33 @@ def get_hash(self, composition):
"""
return 1

class OccupancyComparator(AbstractComparator):
"""
A Comparator that matches occupancies on sites,
irrespective of the species of those sites.
"""

def are_equal(self, sp1, sp2):
"""
Args:
sp1: First species. A dict of {specie/element: amt} as per the
definition in Site and PeriodicSite.
sp2: Second species. A dict of {specie/element: amt} as per the
definition in Site and PeriodicSite.
Returns:
True if sets of occupancies (amt) are equal on both sites.
"""
set1 = set(sp1.element_composition.values())
set2 = set(sp2.element_composition.values())
if set1 == set2:
return True
else:
return False

def get_hash(self, composition):
# Difficult to define sensible hash
return 1

class StructureMatcher(MSONable):
"""
Expand Down
22 changes: 21 additions & 1 deletion pymatgen/analysis/tests/test_structure_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import numpy as np

from pymatgen.analysis.structure_matcher import StructureMatcher, \
ElementComparator, FrameworkComparator, OrderDisorderElementComparator
ElementComparator, FrameworkComparator, OrderDisorderElementComparator, \
OccupancyComparator
from monty.json import MontyDecoder
from pymatgen.core.operations import SymmOp
from pymatgen import Structure, Element, Lattice
Expand Down Expand Up @@ -660,6 +661,25 @@ def test_ordered_primitive_to_disordered_supercell(self):
self.assertTrue(sm_sites.fit(s1, s2))
self.assertFalse(sm_atoms.fit(s1, s2))

def test_occupancy_comparator(self):

lp = Lattice.orthorhombic(10, 20, 30)
pcoords = [[0, 0, 0],
[0.5, 0.5, 0.5]]
s1 = Structure(lp, [{'Na':0.6, 'K':0.4}, 'Cl'], pcoords)
s2 = Structure(lp, [{'Xa':0.4, 'Xb':0.6}, 'Cl'], pcoords)
s3 = Structure(lp, [{'Xa':0.5, 'Xb':0.5}, 'Cl'], pcoords)

sm_sites = StructureMatcher(ltol=0.2, stol=0.3, angle_tol=5,
primitive_cell=False, scale=True,
attempt_supercell=True,
allow_subset=True,
supercell_size = 'num_sites',
comparator=OccupancyComparator())

self.assertTrue(sm_sites.fit(s1, s2))
self.assertFalse(sm_sites.fit(s1, s3))

def test_electronegativity(self):
sm = StructureMatcher(ltol=0.2, stol=0.3, angle_tol=5)

Expand Down
3 changes: 0 additions & 3 deletions pymatgen/command_line/critic2_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,6 @@ def _parse_stdout(self, stdout):
if l[2] == "n":
critic2_idx = int(l[0]) - 1
frac_coord = np.array([float(l[3]), float(l[4]), float(l[5])]) % 1
if critic2_idx != kd.query(frac_coord)[1]:
print(critic2_idx)
print(frac_coord)
node_mapping[critic2_idx] = kd.query(frac_coord)[1]

if len(node_mapping) != len(self.structure):
Expand Down

0 comments on commit 16a0320

Please sign in to comment.