Skip to content

Commit abf6871

Browse files
committed
fixed tests and docs
Signed-off-by: Nick Papior <[email protected]>
1 parent 4524b51 commit abf6871

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 .bilayer import *
4748
from .category 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
@@ -7,14 +7,14 @@
77
import numpy as np
88

99
from sisl import Atom, geom
10-
from .composite import _geom_section, composite_geometry
10+
from ._composite import _geom_section, composite_geometry
1111
from sisl._internal import set_module
1212

1313
from ._common import geometry_define_nsc
1414

1515
__all__ = [
1616
'nanoribbon', 'graphene_nanoribbon', 'agnr', 'zgnr',
17-
'heteroribbon', 'graphene_heteroribbon', '_heteroribbon_section'
17+
'heteroribbon', 'graphene_heteroribbon',
1818
]
1919

2020

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

168+
168169
@set_module("sisl.geom")
169170
@dataclass
170171
class _heteroribbon_section(_geom_section):
@@ -582,14 +583,14 @@ def _parse_shift(self, shift, prev, align):
582583
def heteroribbon(sections, section_cls=_heteroribbon_section, **kwargs):
583584
"""Build a nanoribbon consisting of several nanoribbons of different widths.
584585
585-
This function basically uses `composite_geometry`, but defaulting to the usage
586-
of `_heteroribbon_section` as the section class.
586+
This function uses `composite_geometry`, but defaulting to the usage
587+
of `heteroribbon.section` as the section class.
587588
588-
See `heteroribbon_section` and `composite_geometry` for arguments.
589+
See `heteroribbon.section` and `composite_geometry` for arguments.
589590
590591
Returns
591592
-------
592-
sisl.Geometry:
593+
Geometry:
593594
The final structure of the heteroribbon.
594595
595596
Notes
@@ -618,6 +619,8 @@ def heteroribbon(sections, section_cls=_heteroribbon_section, **kwargs):
618619
"""
619620
return composite_geometry(sections, section_cls=section_cls, **kwargs)
620621

622+
heteroribbon.section=_heteroribbon_section
623+
621624

622625
@set_module("sisl.geom")
623626
def graphene_heteroribbon(sections, section_cls=_heteroribbon_section, bond=1.42, atoms=None, **kwargs):
@@ -628,8 +631,10 @@ def graphene_heteroribbon(sections, section_cls=_heteroribbon_section, bond=1.42
628631
629632
See also
630633
----------
631-
`heteroribbon` : for argument details and how it behaves
634+
heteroribbon : for argument details and how it behaves
632635
"""
633636
if atoms is None:
634637
atoms = Atom(Z=6, R=bond * 1.01)
635638
return composite_geometry(sections, section_cls=section_cls, bond=bond, atoms=atoms, **kwargs)
639+
640+
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
@@ -9,7 +9,7 @@
99
import math as m
1010
import numpy as np
1111

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

@@ -152,15 +152,15 @@ def test_graphene_heteroribbon():
152152
def test_graphene_heteroribbon_errors():
153153
# 7-open with 9 can only be perfectly aligned.
154154
graphene_heteroribbon([(7, 1), (9, 1)], align="center", on_lone_atom="raise")
155-
with pytest.raises(ValueError):
155+
with pytest.raises(SislError):
156156
graphene_heteroribbon([(7, 1), (9, 1, -1)], align="center", on_lone_atom="raise")
157157
# From the bottom
158158
graphene_heteroribbon([(7, 1), (9, 1, -1)], align="bottom", on_lone_atom="raise")
159-
with pytest.raises(ValueError):
159+
with pytest.raises(SislError):
160160
graphene_heteroribbon([(7, 1), (9, 1, 0)], align="bottom", on_lone_atom="raise")
161161
# And from the top
162162
graphene_heteroribbon([(7, 1), (9, 1, 1)], align="top", on_lone_atom="raise")
163-
with pytest.raises(ValueError):
163+
with pytest.raises(SislError):
164164
graphene_heteroribbon([(7, 1), (9, 1, -1)], align="top", on_lone_atom="raise")
165165

166166

@@ -169,16 +169,16 @@ def test_graphene_heteroribbon_errors():
169169
)
170170

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

175175
# Shift limits are imposed correctly
176176
# In this case -2 < shift < 1
177177
grap_heteroribbon([(7, 3), (11, 2, 0)])
178178
grap_heteroribbon([(7, 3), (11, 2, -1)])
179-
with pytest.raises(ValueError):
179+
with pytest.raises(SislError):
180180
grap_heteroribbon([(7, 3), (11, 2, 1)])
181-
with pytest.raises(ValueError):
181+
with pytest.raises(SislError):
182182
grap_heteroribbon([(7, 3), (11, 2, -2)])
183183

184184
# Periodic boundary conditions work properly
@@ -188,10 +188,10 @@ def test_graphene_heteroribbon_errors():
188188

189189
# Even ribbons should only be shifted towards the center
190190
grap_heteroribbon([(10, 2), (8, 2, -1)])
191-
with pytest.raises(ValueError):
191+
with pytest.raises(SislError):
192192
grap_heteroribbon([(10, 2), (8, 2, 1)])
193193
grap_heteroribbon([(10, 1), (8, 2, 1)],) #pbc=False)
194-
with pytest.raises(ValueError):
194+
with pytest.raises(SislError):
195195
grap_heteroribbon([(10, 1), (8, 2, -1)],) #pbc=False)
196196

197197

0 commit comments

Comments
 (0)