Skip to content

Commit

Permalink
Made atom.coords be atom.coords_fractional
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Nov 11, 2018
1 parent f73da8a commit f3d3d6f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
22 changes: 11 additions & 11 deletions crystals/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Atom(object):
Fractional occupancy. If None (default), occupancy is set to 1.0.
"""

__slots__ = ("element", "coords", "displacement", "magmom", "occupancy")
__slots__ = ("element", "coords_fractional", "displacement", "magmom", "occupancy")

def __init__(
self, element, coords, displacement=None, magmom=None, occupancy=1.0, **kwargs
Expand All @@ -47,14 +47,14 @@ def __init__(
magmom = ELEM_TO_MAGMOM[element]

self.element = element.title()
self.coords = np.asfarray(coords)
self.coords_fractional = np.asfarray(coords)
self.displacement = np.asfarray(displacement or (0, 0, 0))
self.magmom = magmom
self.occupancy = occupancy

def __repr__(self):
return "< Atom {:<2} @ ({:.2f}, {:.2f}, {:.2f}) >".format(
self.element, *tuple(self.coords)
self.element, *tuple(self.coords_fractional)
)

# TODO: add `distance_from` function for atoms on a lattice
Expand All @@ -66,7 +66,7 @@ def __eq__(self, other):
isinstance(other, self.__class__)
and (self.element == other.element)
and (self.magmom == other.magmom)
and np.allclose(self.coords, other.coords, atol=1e-3)
and np.allclose(self.coords_fractional, other.coords_fractional, atol=1e-3)
and np.allclose(self.displacement, other.displacement, atol=1e-3)
)

Expand All @@ -75,7 +75,7 @@ def __hash__(self):
(
self.element,
self.magmom,
tuple(np.round(self.coords, 3)),
tuple(np.round(self.coords_fractional, 3)),
tuple(np.round(self.displacement, 3)),
)
)
Expand Down Expand Up @@ -133,16 +133,16 @@ def ase_atom(self, lattice=None, **kwargs):

return ase.Atom(
symbol=self.element,
position=self.xyz(lattice),
position=self.coords_cartesian(lattice),
magmom=self.magmom,
mass=self.mass,
**kwargs
)

@lru_cache()
def xyz(self, lattice):
def coords_cartesian(self, lattice):
"""
Real-space position of the atom
Real-space position of the atom on a lattice.
Parameters
----------
Expand All @@ -154,7 +154,7 @@ def xyz(self, lattice):
pos : `~numpy.ndarray`, shape (3,)
Atomic position
"""
return real_coords(self.coords, lattice.lattice_vectors)
return real_coords(self.coords_fractional, lattice.lattice_vectors)

def debye_waller_factor(self, G, out=None):
"""
Expand Down Expand Up @@ -192,13 +192,13 @@ def transform(self, *matrices):
Transformation matrices.
"""
for matrix in matrices:
self.coords = transform(matrix, self.coords)
self.coords_fractional = transform(matrix, self.coords_fractional)

def __array__(self, *args, **kwargs):
""" Returns an array [Z, x, y, z] """
arr = np.empty(shape=(4,), *args, **kwargs)
arr[0] = self.atomic_number
arr[1::] = self.coords
arr[1::] = self.coords_fractional
return arr


Expand Down
2 changes: 1 addition & 1 deletion crystals/crystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def symmetry_expansion(atoms, symmetry_operators):
for sym_op in symmetry_operators:
new = copy(atm)
new.transform(sym_op)
new.coords[:] = np.mod(new.coords, 1)
new.coords_fractional[:] = np.mod(new.coords_fractional, 1)
uniques.add(new)
yield from uniques

Expand Down
4 changes: 2 additions & 2 deletions docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ The real-space position with respect to a :class:`Crystal` or :class:`Lattice` c
>>> graphite = Crystal.from_database('C')
>>>
>>> carbon = list(graphite)[-1]
>>> fractional = carbon.coords
>>> real = carbon.xyz(lattice = graphite)
>>> fractional = carbon.coords_fractional
>>> real = carbon.coords_cartesian(lattice = graphite)

The distance between two atoms can be calculated by taking their difference::

Expand Down
12 changes: 6 additions & 6 deletions tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ def test_equality(self):
self.assertEqual(self.atom, self.atom)
self.assertEqual(self.atom, other)

other.coords = self.atom.coords + 1
other.coords_fractional = self.atom.coords_fractional + 1
self.assertNotEqual(self.atom, other)

def test_trivial_transform(self):
""" Test Atom.transform() with the identity """
before = np.array(self.atom.coords, copy=True)
before = np.array(self.atom.coords_fractional, copy=True)
self.atom.transform(np.eye(3))
after = np.array(self.atom.coords, copy=True)
after = np.array(self.atom.coords_fractional, copy=True)

self.assertSequenceEqual(tuple(before), tuple(after))

def test_transform_back_and_forth(self):
""" Test Atom.transform() with a random transformation back and forth """
before = np.array(self.atom.coords, copy=True)
before = np.array(self.atom.coords_fractional, copy=True)

transf = random_transform()
self.atom.transform(transf)
self.atom.transform(np.linalg.inv(transf))
after = np.array(self.atom.coords, copy=True)
after = np.array(self.atom.coords_fractional, copy=True)

# No assert sequence almost equal
for x1, x2 in zip(tuple(before), tuple(after)):
Expand All @@ -84,7 +84,7 @@ def test_atom_array(self):
arr = np.array(self.atom)
self.assertTupleEqual(arr.shape, (4,))
self.assertEqual(arr[0], self.atom.atomic_number)
self.assertTrue(np.allclose(arr[1::], self.atom.coords))
self.assertTrue(np.allclose(arr[1::], self.atom.coords_fractional))


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def test_fractional_atoms(self):
with tempfile.TemporaryDirectory() as temp_dir:
with PDBParser("1fbb", download_dir=temp_dir) as parser:
for atm in parser.atoms():
self.assertLessEqual(atm.coords.max(), 1)
self.assertGreaterEqual(atm.coords.min(), 0)
self.assertLessEqual(atm.coords_fractional.max(), 1)
self.assertGreaterEqual(atm.coords_fractional.min(), 0)

def test_symmetry_operators(self):
""" Test that the non-translation part of the symmetry_operators is an invertible
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_atomic_positions(self):
bio_pdb_generic_atoms = set()
for atm in biopdb_atoms:
coords = np.round(
frac_coords(atm.coord, parser.lattice_vectors()), 3
frac_coords(atm.coord_fractional, parser.lattice_vectors()), 3
)
elem = atm.element.title()
bio_pdb_generic_atoms.add(
Expand All @@ -155,7 +155,7 @@ def test_atomic_positions(self):

crystals_generic_atoms = set()
for atm in crystals_atoms:
coords = np.round(atm.coords, 3)
coords = np.round(atm.coords_fractional, 3)
crystals_generic_atoms.add(
GenericAtom(atm.element, tuple(coords))
)
Expand Down Expand Up @@ -185,8 +185,8 @@ def test_fractional_atoms(self):
with self.subTest(name.split("\\")[-1]):
with CIFParser(name) as p:
for atm in p.atoms():
self.assertLessEqual(atm.coords.max(), 1)
self.assertGreaterEqual(atm.coords.min(), 0)
self.assertLessEqual(atm.coords_fractional.max(), 1)
self.assertGreaterEqual(atm.coords_fractional.min(), 0)

def test_symmetry_operators(self):
""" Test that the non-translation part of the symmetry_operators is an invertible
Expand Down

0 comments on commit f3d3d6f

Please sign in to comment.