Skip to content

Commit

Permalink
Asf fix (#856)
Browse files Browse the repository at this point in the history
* fix for com and cleanup frac_to_cart conversion

* add unittest for structure instantiation
  • Loading branch information
montoyjh authored and shyuep committed Oct 3, 2017
1 parent 482ee1d commit 997405c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
43 changes: 15 additions & 28 deletions pymatgen/analysis/adsorption.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def find_surface_sites_by_height(self, slab, height=0.9, xy_tol=0.05):
unique_sites, unique_perp_fracs = [], []
for site in surf_sites:
this_perp = site.coords - np.dot(site.coords, self.mvec)
this_perp_frac = cart_to_frac(slab.lattice, this_perp)
this_perp_frac = slab.lattice.get_fractional_coords(this_perp)
if not in_coord_list_pbc(unique_perp_fracs, this_perp_frac):
unique_sites.append(site)
unique_perp_fracs.append(this_perp_frac)
Expand Down Expand Up @@ -300,12 +300,12 @@ def find_adsorption_sites(self, distance=2.0, put_inside=True,
for key, sites in ads_sites.items():
# Pare off outer sites for bridge/hollow
if key in ['bridge', 'hollow']:
frac_coords = [cart_to_frac(self.slab.lattice, ads_site)
frac_coords = [self.slab.lattice.get_fractional_coords(ads_site)
for ads_site in sites]
frac_coords = [frac_coord for frac_coord in frac_coords
if (frac_coord[0] > 1 and frac_coord[0] < 4
and frac_coord[1] > 1 and frac_coord[1] < 4)]
sites = [frac_to_cart(self.slab.lattice, frac_coord)
sites = [self.slab.lattice.get_cartesian_coords(frac_coord)
for frac_coord in frac_coords]
if near_reduce:
sites = self.near_reduce(sites, threshold=near_reduce)
Expand All @@ -316,8 +316,9 @@ def find_adsorption_sites(self, distance=2.0, put_inside=True,
sites = self.symm_reduce(sites, threshold=symm_reduce)
# we substract the distance from the bottom site if we
# wish to adsorb at the surface below the center of mass
com = frac_to_cart(self.slab.lattice,
self.slab.center_of_mass)
weights = [s.species_and_occu.weight for s in self.slab]
com = np.average(self.slab.frac_coords, weights=weights, axis=0)
com = self.slab.lattice.get_cartesian_coords(com)

sites = [site - distance * self.mvec if
site[2] < com[2] else
Expand All @@ -340,7 +341,7 @@ def symm_reduce(self, coords_set, threshold=1e-6):
symm_ops = surf_sg.get_symmetry_operations()
unique_coords = []
# Convert to fractional
coords_set = [cart_to_frac(self.slab.lattice, coords)
coords_set = [self.slab.lattice.get_fractional_coords(coords)
for coords in coords_set]
for coords in coords_set:
incoord = False
Expand All @@ -352,7 +353,7 @@ def symm_reduce(self, coords_set, threshold=1e-6):
if not incoord:
unique_coords += [coords]
# convert back to cartesian
return [frac_to_cart(self.slab.lattice, coords)
return [self.slab.lattice.get_cartesian_coords(coords)
for coords in unique_coords]

def near_reduce(self, coords_set, threshold=1e-4):
Expand All @@ -365,12 +366,12 @@ def near_reduce(self, coords_set, threshold=1e-4):
threshold (float): threshold value for distance
"""
unique_coords = []
coords_set = [cart_to_frac(self.slab.lattice, coords)
coords_set = [self.slab.lattice.get_fractional_coords(coords)
for coords in coords_set]
for coord in coords_set:
if not in_coord_list_pbc(unique_coords, coord, threshold):
unique_coords += [coord]
return [frac_to_cart(self.slab.lattice, coords)
return [self.slab.lattice.get_cartesian_coords(coords)
for coords in unique_coords]

def ensemble_center(self, site_list, indices, cartesian=True):
Expand Down Expand Up @@ -577,8 +578,8 @@ def put_coord_inside(lattice, cart_coordinate):
"""
converts a cartesian coordinate such that it is inside the unit cell.
"""
fc = cart_to_frac(lattice, cart_coordinate)
return frac_to_cart(lattice, [c - np.floor(c) for c in fc])
fc = lattice.get_fractional_coords(cart_coordinate)
return lattice.get_cartesian_coords([c - np.floor(c) for c in fc])


def reorient_z(structure):
Expand All @@ -590,21 +591,7 @@ def reorient_z(structure):
sop = get_rot(struct)
struct.apply_operation(sop)
return struct


def frac_to_cart(lattice, frac_coord):
"""
converts fractional coordinates to cartesian
"""
return np.dot(np.transpose(lattice.matrix), frac_coord)


def cart_to_frac(lattice, cart_coord):
"""
converts cartesian coordinates to fractional
"""
return np.dot(np.linalg.inv(np.transpose(lattice.matrix)), cart_coord)



# Get color dictionary
colors = loadfn(os.path.join(os.path.dirname(vis.__file__),
Expand Down Expand Up @@ -638,8 +625,8 @@ def plot_slab(slab, ax, scale=0.8, repeat=5, window=1.5,
sites = sorted(slab.sites, key=lambda x: x.coords[2])
alphas = 1 - decay * (np.max(coords[:, 2]) - coords[:, 2])
alphas = alphas.clip(min=0)
corner = [0, 0, cart_to_frac(slab.lattice, coords[-1])[-1]]
corner = frac_to_cart(slab.lattice, corner)[:2]
corner = [0, 0, slab.lattice.get_fractional_coords(coords[-1])[-1]]
corner = slab.lattice.get_cartesian_coords(corner)[:2]
verts = orig_cell[:2, :2]
lattsum = verts[0] + verts[1]
# Draw circles at sites and stack them accordingly
Expand Down
13 changes: 6 additions & 7 deletions pymatgen/analysis/tests/test_adsorption.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ def setUp(self):
self.asf_111_bottom = AdsorbateSiteFinder(self.slab_dict["111"],
top_surface=False)
self.asf_110 = AdsorbateSiteFinder(self.slab_dict["110"])
self.asf_struct = AdsorbateSiteFinder(
Structure.from_sites(self.slab_dict["111"].sites))

def test_init(self):
asf_100 = AdsorbateSiteFinder(self.slab_dict["100"])
asf_111 = AdsorbateSiteFinder(self.slab_dict["111"])

def test_from_bulk_and_miller(self):
# Standard site finding
asf = AdsorbateSiteFinder.from_bulk_and_miller(self.structure, (1, 1, 1))
Expand Down Expand Up @@ -70,6 +72,8 @@ def test_find_adsorption_sites(self):
sites = self.asf_110.find_adsorption_sites()
self.assertEqual(len(sites['all']), 4)
sites = self.asf_211.find_adsorption_sites()
# Test on structure
sites = self.asf_struct.find_adsorption_sites()

def test_generate_adsorption_structures(self):
co = Molecule("CO", [[0, 0, 0], [0, 0, 1.23]])
Expand Down Expand Up @@ -110,12 +114,7 @@ def test_functions(self):
slab = self.slab_dict["111"]
rot = get_rot(slab)
reoriented = reorient_z(slab)
self.assertArrayAlmostEqual(slab.frac_coords[0],
cart_to_frac(slab.lattice,
slab.cart_coords[0]))
self.assertArrayAlmostEqual(slab.cart_coords[0],
frac_to_cart(slab.lattice,
slab.frac_coords[0]))


if __name__ == '__main__':
unittest.main()

0 comments on commit 997405c

Please sign in to comment.