Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an as_supercell which returns geometry in supercell #635

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ we hit release version 1.0.0.
## [0.14.3] - YYYY-MM-DD

### Added
- added `Geometry.as_supercell` to create the supercell structure,
thanks to @pfebrer for the suggestion
- added `Lattice.to` and `Lattice.new` to function the same
as `Geometry`, added Lattice.to["Cuboid"]
- added `Atom.to`, currently only `to.Sphere()`
Expand Down
40 changes: 40 additions & 0 deletions src/sisl/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,46 @@ def as_primary(self, na_primary: int, axes=(0, 1, 2), ret_super: bool=False):
return geom, supercell
return geom

def as_supercell(self) -> Geometry:
"""Create a new geometry equivalent to ``self * self.nsc``, where the indices are ordered as the supercells

Returns
-------
Geometry
the supercell expanded and reordered Geometry
"""
# Get total number of atoms
na = len(self)
# create the big supercell geometry in the simplest (linear) way
sc = self * self.nsc

# remove nsc, this supercell should hold all information
sc.set_nsc([1, 1, 1])

# get off-set for first atom
# this is used to correct the indices created after having shifted
# everything
f0 = self.fxyz[0]

# translate the supercell such that the 0, 0, 0 (primary cell)
# is located at the origin.
sc = sc.translate(-(self.nsc // 2) @ self.cell)

# Calculate the translation table such that the ordering in `sc` can
# be made to look like the `self` supercell indices
isc_sc = np.rint(sc.xyz[::na] @ self.icell.T - f0).astype(np.int32)
isc_self = self.a2isc(np.arange(self.n_s) * na)
def new_sub(isc):
return (abs(isc_sc - isc).sum(1) == 0).nonzero()[0][0]

# Create the translation table for the indices
translate = np.array([new_sub(isc) for isc in isc_self])
# make sure all atoms are present
translate = np.repeat(translate * na, na).reshape(-1, na) + np.arange(na)

# re-arrange the atoms and return
return sc.sub(translate.ravel())

def reorder(self) -> None:
""" Reorders atoms according to first occurence in the geometry

Expand Down
14 changes: 14 additions & 0 deletions src/sisl/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,20 @@ def test_translate2uc_axes():
assert np.allclose(gr_once.xyz, gr_individual.xyz)


def test_as_supercell_graphene():
gr = sisl_geom.graphene()
grsc = gr.as_supercell()
assert np.allclose(grsc.xyz[:len(gr)], gr.xyz)
assert np.allclose(grsc.axyz(np.arange(gr.na_s)), gr.axyz(np.arange(gr.na_s)))


def test_as_supercell_fcc():
g = sisl_geom.fcc(2 ** 0.5, Atom(1, R=1.0001))
gsc = g.as_supercell()
assert np.allclose(gsc.xyz[:len(g)], g.xyz)
assert np.allclose(gsc.axyz(np.arange(g.na_s)), g.axyz(np.arange(g.na_s)))


def test_sc_warn():
with pytest.warns(SislDeprecation):
lattice = sisl_geom.graphene().sc