Skip to content

Commit 136e7eb

Browse files
committed
fixed tests and docs
Signed-off-by: Nick Papior <[email protected]>
1 parent b7e8137 commit 136e7eb

File tree

5 files changed

+56
-22
lines changed

5 files changed

+56
-22
lines changed

docs/api/default_geom.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ geometries via a `pull request <pr>`_.
1414

1515
All methods return a `Geometry` object.
1616

17+
Some of the geometries are created in section based geometries, such as `heteroribbon`.
18+
This functionality is provided through the `composite_geometry`
19+
1720

1821
Bulk
1922
====
@@ -51,6 +54,8 @@ Surfaces (slabs)
5154
zgnr
5255
graphene_nanoribbon
5356
nanotube
57+
heteroribbon
58+
graphene_heteroribbon
5459

5560

5661
2D materials
@@ -62,3 +67,12 @@ Surfaces (slabs)
6267
honeycomb
6368
bilayer
6469
graphene
70+
71+
72+
Helpers
73+
=======
74+
75+
.. autosummary::
76+
:toctree: generated/
77+
78+
composite_geometry

src/sisl/geom/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
graphene
4343
4444
"""
45+
from ._composite import *
4546
from .basic import *
4647
from .flat import *
4748
from .surfaces import *

src/sisl/geom/composite.py renamed to src/sisl/geom/_composite.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
from dataclasses import dataclass, copy, fields
2+
from abc import abstractmethod
23

3-
from sisl.messages import warn
4+
from sisl.messages import warn, SislError
5+
6+
7+
__all__ = ["composite_geometry"]
48

59

610
@dataclass
711
class _geom_section:
812

13+
@abstractmethod
14+
def build_section(self, geometry):
15+
...
16+
17+
@abstractmethod
18+
def add_section(self, geometry, geometry_addition):
19+
...
20+
921
def _junction_error(self, prev, msg, what):
1022
"""Helper function to raise an error if the junction is not valid.
1123
@@ -14,7 +26,7 @@ def _junction_error(self, prev, msg, what):
1426
"""
1527
msg = f"Error at junction between sections {prev} and {self}. {msg}"
1628
if what == "raise":
17-
raise ValueError(msg)
29+
raise SislError(msg)
1830
elif what == "warn":
1931
warn(msg)
2032

@@ -29,8 +41,8 @@ def composite_geometry(sections, section_cls=_geom_section, **kwargs):
2941
sections: array-like of (_geom_section or tuple or dict)
3042
A list of sections to be added to the ribbon.
3143
32-
Each section is either a `_geom_section` or something that will
33-
be parsed to a `_geom_section`.
44+
Each section is either a `composite_geometry.section` or something that will
45+
be parsed to a `composite_geometry.section`.
3446
section_cls: class, optional
3547
The class to use for parsing sections.
3648
**kwargs:
@@ -50,12 +62,12 @@ def conv(s):
5062

5163
return copy.copy(s)
5264

53-
sections = [conv(section) for section in sections]
54-
5565
# Then loop through all the sections.
5666
geom = None
5767
prev = None
5868
for i, section in enumerate(sections):
69+
section = conv(section)
70+
5971
new_addition = section.build_section(prev)
6072

6173
if i == 0:
@@ -66,3 +78,5 @@ def conv(s):
6678
prev = section
6779

6880
return geom
81+
82+
composite_geometry.section = _geom_section

src/sisl/geom/nanoribbon.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from numbers import Integral
66
import numpy as np
77

8-
from .composite import _geom_section, composite_geometry
8+
from ._composite import _geom_section, composite_geometry
99
from sisl._internal import set_module
1010
from sisl import geom, Atom
1111
from ._common import geometry_define_nsc
1212

1313
__all__ = [
1414
'nanoribbon', 'graphene_nanoribbon', 'agnr', 'zgnr',
15-
'heteroribbon', 'graphene_heteroribbon', '_heteroribbon_section'
15+
'heteroribbon', 'graphene_heteroribbon',
1616
]
1717

1818

@@ -163,6 +163,7 @@ def zgnr(width, bond=1.42, atoms=None):
163163
"""
164164
return graphene_nanoribbon(width, bond, atoms, kind='zigzag')
165165

166+
166167
@set_module("sisl.geom")
167168
@dataclass
168169
class _heteroribbon_section(_geom_section):
@@ -580,14 +581,14 @@ def _parse_shift(self, shift, prev, align):
580581
def heteroribbon(sections, section_cls=_heteroribbon_section, **kwargs):
581582
"""Build a nanoribbon consisting of several nanoribbons of different widths.
582583
583-
This function basically uses `composite_geometry`, but defaulting to the usage
584-
of `_heteroribbon_section` as the section class.
584+
This function uses `composite_geometry`, but defaulting to the usage
585+
of `heteroribbon.section` as the section class.
585586
586-
See `heteroribbon_section` and `composite_geometry` for arguments.
587+
See `heteroribbon.section` and `composite_geometry` for arguments.
587588
588589
Returns
589590
-------
590-
sisl.Geometry:
591+
Geometry:
591592
The final structure of the heteroribbon.
592593
593594
Notes
@@ -616,6 +617,8 @@ def heteroribbon(sections, section_cls=_heteroribbon_section, **kwargs):
616617
"""
617618
return composite_geometry(sections, section_cls=section_cls, **kwargs)
618619

620+
heteroribbon.section=_heteroribbon_section
621+
619622

620623
@set_module("sisl.geom")
621624
def graphene_heteroribbon(sections, section_cls=_heteroribbon_section, bond=1.42, atoms=None, **kwargs):
@@ -626,8 +629,10 @@ def graphene_heteroribbon(sections, section_cls=_heteroribbon_section, bond=1.42
626629
627630
See also
628631
----------
629-
`heteroribbon` : for argument details and how it behaves
632+
heteroribbon : for argument details and how it behaves
630633
"""
631634
if atoms is None:
632635
atoms = Atom(Z=6, R=bond * 1.01)
633636
return composite_geometry(sections, section_cls=section_cls, bond=bond, atoms=atoms, **kwargs)
637+
638+
graphene_heteroribbon.section=_heteroribbon_section

src/sisl/geom/tests/test_geom.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import math as m
99
import numpy as np
1010

11-
from sisl import Atom, Lattice
11+
from sisl import Atom, Lattice, SislError
1212
from sisl.geom import *
1313
from sisl._math_small import cross3, dot3
1414

@@ -153,15 +153,15 @@ def test_graphene_heteroribbon():
153153
def test_graphene_heteroribbon_errors():
154154
# 7-open with 9 can only be perfectly aligned.
155155
graphene_heteroribbon([(7, 1), (9, 1)], align="center", on_lone_atom="raise")
156-
with pytest.raises(ValueError):
156+
with pytest.raises(SislError):
157157
graphene_heteroribbon([(7, 1), (9, 1, -1)], align="center", on_lone_atom="raise")
158158
# From the bottom
159159
graphene_heteroribbon([(7, 1), (9, 1, -1)], align="bottom", on_lone_atom="raise")
160-
with pytest.raises(ValueError):
160+
with pytest.raises(SislError):
161161
graphene_heteroribbon([(7, 1), (9, 1, 0)], align="bottom", on_lone_atom="raise")
162162
# And from the top
163163
graphene_heteroribbon([(7, 1), (9, 1, 1)], align="top", on_lone_atom="raise")
164-
with pytest.raises(ValueError):
164+
with pytest.raises(SislError):
165165
graphene_heteroribbon([(7, 1), (9, 1, -1)], align="top", on_lone_atom="raise")
166166

167167

@@ -170,16 +170,16 @@ def test_graphene_heteroribbon_errors():
170170
)
171171

172172
# Odd section with open end
173-
with pytest.raises(ValueError):
173+
with pytest.raises(SislError):
174174
grap_heteroribbon([(7, 3), (5, 2)])
175175

176176
# Shift limits are imposed correctly
177177
# In this case -2 < shift < 1
178178
grap_heteroribbon([(7, 3), (11, 2, 0)])
179179
grap_heteroribbon([(7, 3), (11, 2, -1)])
180-
with pytest.raises(ValueError):
180+
with pytest.raises(SislError):
181181
grap_heteroribbon([(7, 3), (11, 2, 1)])
182-
with pytest.raises(ValueError):
182+
with pytest.raises(SislError):
183183
grap_heteroribbon([(7, 3), (11, 2, -2)])
184184

185185
# Periodic boundary conditions work properly
@@ -189,10 +189,10 @@ def test_graphene_heteroribbon_errors():
189189

190190
# Even ribbons should only be shifted towards the center
191191
grap_heteroribbon([(10, 2), (8, 2, -1)])
192-
with pytest.raises(ValueError):
192+
with pytest.raises(SislError):
193193
grap_heteroribbon([(10, 2), (8, 2, 1)])
194194
grap_heteroribbon([(10, 1), (8, 2, 1)],) #pbc=False)
195-
with pytest.raises(ValueError):
195+
with pytest.raises(SislError):
196196
grap_heteroribbon([(10, 1), (8, 2, -1)],) #pbc=False)
197197

198198

0 commit comments

Comments
 (0)