Skip to content

Commit

Permalink
Added lattice parameter to atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Nov 11, 2018
1 parent 76939e9 commit de657ba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
47 changes: 29 additions & 18 deletions crystals/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ class Atom(object):
Fractional occupancy. If None (default), occupancy is set to 1.0.
"""

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

def __init__(
self, element, coords, lattice=None, displacement=None, magmom=None, occupancy=1.0, **kwargs
self,
element,
coords,
lattice=None,
displacement=None,
magmom=None,
occupancy=1.0,
**kwargs
):
if isinstance(element, int):
element = NUM_TO_ELEM[element]
Expand All @@ -50,6 +64,7 @@ def __init__(

self.element = element.title()
self.coords_fractional = np.asfarray(coords)
self.lattice = lattice
self.displacement = np.asfarray(displacement or (0, 0, 0))
self.magmom = magmom
self.occupancy = occupancy
Expand Down Expand Up @@ -109,14 +124,12 @@ def atomic_number(self):
def mass(self):
return ELEM_TO_MASS[self.element]

def ase_atom(self, lattice=None, **kwargs):
def ase_atom(self, **kwargs):
"""
Returns an ``ase.Atom`` object.
Parameters
----------
lattice : Lattice, optional
Lattice on which the atoms sit. Default is free space.
kwargs
Keyword arguments are passed to the ``ase.Atom`` constructor.
Expand All @@ -130,33 +143,31 @@ def ase_atom(self, lattice=None, **kwargs):
"""
import ase

if lattice is None:
lattice = Lattice(np.eye(3))

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

@lru_cache()
def coords_cartesian(self, lattice):
@property
def coords_cartesian(self):
"""
Real-space position of the atom on a lattice.
Parameters
----------
lattice : Lattice or iterable
Lattice or Crystal instance in which the atom is located.
Real-space position of the atom on the lattice.
Returns
-------
pos : `~numpy.ndarray`, shape (3,)
Atomic position
Raises
------
RuntimeError : if this atom is not place on a lattice
"""
return real_coords(self.coords_fractional, lattice.lattice_vectors)
if not self.lattice:
return real_coords(self.coords_fractional, np.eye(3))
return real_coords(self.coords_fractional, self.lattice.lattice_vectors)

def debye_waller_factor(self, G, out=None):
"""
Expand Down
3 changes: 3 additions & 0 deletions crystals/crystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Crystal(AtomicStructure, Lattice):
builtins = frozenset(map(lambda fn: fn.stem, CIF_ENTRIES))

def __init__(self, unitcell, lattice_vectors, source=None, **kwargs):
unitcell = list(unitcell)
for atom in unitcell:
atom.lattice = self
super().__init__(atoms=unitcell, lattice_vectors=lattice_vectors, **kwargs)
self.source = source

Expand Down
6 changes: 3 additions & 3 deletions tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def test_atom_array(self):
self.assertTupleEqual(arr.shape, (4,))
self.assertEqual(arr[0], self.atom.atomic_number)
self.assertTrue(np.allclose(arr[1::], self.atom.coords_fractional))

def test_distance(self):
""" Test the distance between atoms """
atm1 = Atom("He", [0,0,0])
atm2 = Atom("He", [1,0,0])
atm1 = Atom("He", [0, 0, 0])
atm2 = Atom("He", [1, 0, 0])
self.assertEqual(atm1 - atm2, 1)


Expand Down
5 changes: 4 additions & 1 deletion tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def test_atomic_positions(self):
bio_pdb_generic_atoms = set()
for atm in biopdb_atoms:
coords = np.round(
frac_coords(atm.coord_fractional, parser.lattice_vectors()), 3
frac_coords(
atm.coord_fractional, parser.lattice_vectors()
),
3,
)
elem = atm.element.title()
bio_pdb_generic_atoms.add(
Expand Down

0 comments on commit de657ba

Please sign in to comment.