Commit 8806b473 authored by Joseph Weston's avatar Joseph Weston
Browse files

move SimpleSiteFamily to 'tests.builder'

It is only used in the tests anyway. This way we export less
API from the builder module.
parent 359e56ef
......@@ -10,7 +10,6 @@ Types
Builder
HoppingKind
SimpleSiteFamily
BuilderLead
SelfEnergyLead
ModesLead
......
......@@ -11,7 +11,7 @@ import warnings
import operator
import collections
import copy
from functools import total_ordering, wraps, update_wrapper
from functools import wraps, update_wrapper
from itertools import islice, chain
import textwrap
import bisect
......@@ -29,42 +29,9 @@ from ._common import (ensure_isinstance, get_parameters, reraise_warnings,
interleave, deprecate_args, memoize)
__all__ = ['Builder', 'SimpleSiteFamily', 'Symmetry', 'HoppingKind', 'Lead',
__all__ = ['Builder', 'Symmetry', 'HoppingKind', 'Lead',
'BuilderLead', 'SelfEnergyLead', 'ModesLead', 'add_peierls_phase']
################ Site families
@total_ordering
class SimpleSiteFamily(SiteFamily):
"""A site family used as an example and for testing.
A family of sites tagged by any python objects where object satisfied
condition ``object == eval(repr(object))``.
It exists to provide a basic site family that can be used for testing the
builder module without other dependencies. It can be also used to tag
sites with non-numeric objects like strings should this every be useful.
Due to its low storage efficiency for numbers it is not recommended to use
`SimpleSiteFamily` when `kwant.lattice.Monatomic` would also work.
"""
def __init__(self, name=None, norbs=None):
canonical_repr = '{0}({1}, {2})'.format(self.__class__, repr(name),
repr(norbs))
super().__init__(canonical_repr, name, norbs)
def normalize_tag(self, tag):
tag = tuple(tag)
try:
if eval(repr(tag)) != tag:
raise RuntimeError()
except:
raise TypeError('It must be possible to recreate the tag from '
'its representation.')
return tag
def validate_hopping(hopping):
"""Verify that the argument is a valid hopping."""
......
......@@ -23,12 +23,43 @@ from kwant import builder, system
from kwant._common import ensure_rng, KwantDeprecationWarning
@ft.total_ordering
class SimpleSiteFamily(system.SiteFamily):
"""A site family used as an example and for testing.
A family of sites tagged by any python objects where object satisfied
condition ``object == eval(repr(object))``.
It exists to provide a basic site family that can be used for testing the
builder module without other dependencies. It can be also used to tag
sites with non-numeric objects like strings should this every be useful.
Due to its low storage efficiency for numbers it is not recommended to use
`SimpleSiteFamily` when `kwant.lattice.Monatomic` would also work.
"""
def __init__(self, name=None, norbs=None):
canonical_repr = '{0}({1}, {2})'.format(self.__class__, repr(name),
repr(norbs))
super().__init__(canonical_repr, name, norbs)
def normalize_tag(self, tag):
tag = tuple(tag)
try:
if eval(repr(tag)) != tag:
raise RuntimeError()
except:
raise TypeError('It must be possible to recreate the tag from '
'its representation.')
return tag
def test_bad_keys():
def setitem(key):
syst[key] = None
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
syst = builder.Builder()
failures = [
......@@ -97,9 +128,9 @@ def test_bad_keys():
def test_site_families():
syst = builder.Builder()
fam = builder.SimpleSiteFamily(norbs=1)
ofam = builder.SimpleSiteFamily(norbs=1)
yafam = builder.SimpleSiteFamily('another_name', norbs=1)
fam = SimpleSiteFamily(norbs=1)
ofam = SimpleSiteFamily(norbs=1)
yafam = SimpleSiteFamily('another_name', norbs=1)
syst[fam(0)] = 7
assert syst[fam(0)] == 7
......@@ -117,8 +148,8 @@ def test_site_families():
assert fam != 'a'
# test site families sorting
fam1 = builder.SimpleSiteFamily(norbs=1)
fam2 = builder.SimpleSiteFamily(norbs=2)
fam1 = SimpleSiteFamily(norbs=1)
fam2 = SimpleSiteFamily(norbs=2)
assert fam1 < fam2 # string '1' is lexicographically less than '2'
......@@ -162,7 +193,7 @@ class VerySimpleSymmetry(builder.Symmetry):
# made.
def check_construction_and_indexing(sites, sites_fd, hoppings, hoppings_fd,
unknown_hoppings, sym=None):
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
syst = builder.Builder(sym)
t, V = 1.0j, 0.0
syst[sites] = V
......@@ -212,7 +243,7 @@ def check_construction_and_indexing(sites, sites_fd, hoppings, hoppings_fd,
def test_construction_and_indexing():
# Without symmetry
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
sites = [fam(0, 0), fam(0, 1), fam(1, 0)]
hoppings = [(fam(0, 0), fam(0, 1)),
(fam(0, 1), fam(1, 0)),
......@@ -250,7 +281,7 @@ def test_hermitian_conjugation():
raise ValueError
syst = builder.Builder()
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
syst[fam(0)] = syst[fam(1)] = ta.identity(2)
syst[fam(0), fam(1)] = f
......@@ -266,7 +297,7 @@ def test_hermitian_conjugation():
def test_value_equality_and_identity():
m = ta.array([[1, 2], [3j, 4j]])
syst = builder.Builder()
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
syst[fam(0)] = m
syst[fam(1)] = m
......@@ -573,7 +604,7 @@ def test_hamiltonian_evaluation(vectorize):
edges = [(0, 1), (0, 2), (0, 3), (1, 2)]
syst = builder.Builder(vectorize=vectorize)
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
sites = [fam(*tag) for tag in tags]
hoppings = [(sites[i], sites[j]) for i, j in edges]
if vectorize:
......@@ -664,7 +695,7 @@ def test_vectorized_hamiltonian_evaluation():
tags = [(0, 0), (1, 1), (2, 2), (3, 3)]
edges = [(0, 1), (0, 2), (0, 3), (1, 2)]
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
sites = [fam(*tag) for tag in tags]
hops = [(fam(*tags[i]), fam(*tags[j])) for (i, j) in edges]
......@@ -804,7 +835,7 @@ def test_dangling():
# / \
# 3-0---2-4-5 6-7 8
syst = builder.Builder()
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
syst[(fam(i) for i in range(9))] = None
syst[[(fam(0), fam(1)), (fam(1), fam(2)), (fam(2), fam(0))]] = None
syst[[(fam(0), fam(3)), (fam(2), fam(4)), (fam(4), fam(5))]] = None
......@@ -1077,8 +1108,8 @@ def test_fill_sticky():
def test_attach_lead():
fam = builder.SimpleSiteFamily(norbs=1)
fam_noncommensurate = builder.SimpleSiteFamily(name='other', norbs=1)
fam = SimpleSiteFamily(norbs=1)
fam_noncommensurate = SimpleSiteFamily(name='other', norbs=1)
syst = builder.Builder()
syst[fam(1)] = 0
......@@ -1146,7 +1177,7 @@ def test_attach_lead_incomplete_unit_cell():
def test_neighbors_not_in_single_domain():
sr = builder.Builder()
lead = builder.Builder(VerySimpleSymmetry(-1))
fam = builder.SimpleSiteFamily(norbs=1)
fam = SimpleSiteFamily(norbs=1)
sr[(fam(x, y) for x in range(3) for y in range(3) if x >= y)] = 0
sr[builder.HoppingKind((1, 0), fam)] = 1
sr[builder.HoppingKind((0, 1), fam)] = 1
......@@ -1201,7 +1232,7 @@ def test_closest():
def test_update():
lat = builder.SimpleSiteFamily(norbs=1)
lat = SimpleSiteFamily(norbs=1)
syst = builder.Builder()
syst[[lat(0,), lat(1,)]] = 1
......@@ -1296,7 +1327,7 @@ def test_invalid_HoppingKind():
def test_ModesLead_and_SelfEnergyLead():
lat = builder.SimpleSiteFamily(norbs=1)
lat = SimpleSiteFamily(norbs=1)
hoppings = [builder.HoppingKind((1, 0), lat),
builder.HoppingKind((0, 1), lat)]
rng = Random(123)
......@@ -1378,9 +1409,9 @@ def test_site_pickle():
def test_discrete_symmetries():
lat = builder.SimpleSiteFamily(name='ccc', norbs=2)
lat2 = builder.SimpleSiteFamily(name='bla', norbs=1)
lat3 = builder.SimpleSiteFamily(name='dd', norbs=4)
lat = SimpleSiteFamily(name='ccc', norbs=2)
lat2 = SimpleSiteFamily(name='bla', norbs=1)
lat3 = SimpleSiteFamily(name='dd', norbs=4)
cons_law = {lat: np.diag([1, 2]), lat2: 0}
syst = builder.Builder(conservation_law=cons_law,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment