diff --git a/kwant/builder.py b/kwant/builder.py index 79e1f0198c0d2f882bd9cbd736117c2e572e8d08..2ca81a946faf5c9c50201c67a34398a69d46d41c 100644 --- a/kwant/builder.py +++ b/kwant/builder.py @@ -1681,7 +1681,13 @@ class Builder: if hop_range > 1: # Automatically increase the period, potentially warn the user. - new_lead = Builder(sym.subgroup((hop_range,))) + new_lead = Builder( + sym.subgroup((hop_range,)), + conservation_law=lead_builder.conservation_law, + time_reversal=lead_builder.time_reversal, + particle_hole=lead_builder.particle_hole, + chiral=lead_builder.chiral, + ) with reraise_warnings(): new_lead.fill(lead_builder, lambda site: True, lead_builder.sites(), max_sites=float('inf')) diff --git a/kwant/kpm.py b/kwant/kpm.py index a06d7c6d19af5a3a5266e5fff68103cc9f7b3b04..db19f4ea6504f3170cbe438be4904864dd405121 100644 --- a/kwant/kpm.py +++ b/kwant/kpm.py @@ -219,6 +219,14 @@ class SpectralDensity: num_vectors=num_vectors, accumulate=accumulate_vectors) else: + if not isinstance(vector_factory, Iterable): + raise TypeError('vector_factory must be iterable') + try: + len(vector_factory) + except TypeError: + if num_vectors is None: + raise ValueError('num_vectors must be provided if' + 'vector_factory has no length.') self._vector_factory = _VectorFactory( vector_factory, num_vectors=num_vectors, diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py index c6f0874865968079ae24b1e8c8f373fd674acd71..84e3ef024721e9812d4b7e502a09eb440853dd83 100644 --- a/kwant/tests/test_builder.py +++ b/kwant/tests/test_builder.py @@ -535,17 +535,17 @@ def test_hamiltonian_evaluation(): def test_raising(fsyst, hop): a, b = hop # exceptions are converted to kwant.UserCodeError and we add our message - with raises(kwant.UserCodeError) as ctx: + with raises(kwant.UserCodeError) as exc_info: fsyst.hamiltonian(a, a) msg = 'Error occurred in user-supplied value function "onsite_raises"' - assert msg in ctx.exconly() + assert msg in str(exc_info.value) for hop in [(a, b), (b, a)]: - with raises(kwant.UserCodeError) as ctx: + with raises(kwant.UserCodeError) as exc_info: fsyst.hamiltonian(*hop) msg = ('Error occurred in user-supplied ' 'value function "hopping_raises"') - assert msg in ctx.exconly() + assert msg in str(exc_info.value) # test with finite system new_hop = (fam(-1, 0), fam(0, 0)) @@ -843,7 +843,7 @@ def test_fill_sticky(): def test_attach_lead(): - fam = builder.SimpleSiteFamily() + fam = builder.SimpleSiteFamily(norbs=1) fam_noncommensurate = builder.SimpleSiteFamily(name='other') syst = builder.Builder() @@ -878,7 +878,12 @@ def test_attach_lead(): # add some further-than-nearest-neighbor hoppings hop_range = 3 - lead = builder.Builder(VerySimpleSymmetry(1)) + lead = builder.Builder( + VerySimpleSymmetry(1), + conservation_law=np.eye(1), + time_reversal=np.eye(1), + particle_hole=np.eye(1), + chiral=np.eye(1)) lead[fam(0)] = 1 for i in range(1, hop_range + 1): lead[fam(0), fam(i)] = 1 @@ -886,6 +891,10 @@ def test_attach_lead(): expanded_lead = syst.leads[-1].builder assert expanded_lead.symmetry.period == hop_range assert len(list(expanded_lead.sites())) == hop_range + assert expanded_lead.conservation_law is lead.conservation_law + assert expanded_lead.time_reversal is lead.time_reversal + assert expanded_lead.particle_hole is lead.particle_hole + assert expanded_lead.chiral is lead.chiral # check that we can actually finalize the system syst.finalized() diff --git a/kwant/tests/test_operator.py b/kwant/tests/test_operator.py index 6eb0e631a3d0794846bbd078898a27b4c829cab4..f6e5271ba658e683ac5a2e02abf3c3ba311de61f 100644 --- a/kwant/tests/test_operator.py +++ b/kwant/tests/test_operator.py @@ -425,14 +425,14 @@ def test_arg_passing(A): # test missing params op = A(fsyst, onsite=lambda x, a, b: 1) params = dict(a=1) - with raises(TypeError) as exc: + with raises(TypeError): op(wf, params=params) - with raises(TypeError) as exc: + with raises(TypeError): op.act(wf, params=params) - with raises(TypeError) as exc: + with raises(TypeError): op.bind(params=params) if hasattr(op, 'tocoo'): - with raises(TypeError) as exc: + with raises(TypeError): op.tocoo(params=params) @@ -445,19 +445,19 @@ def test_arg_passing(A): if has_tocoo: tocoo_should_be = op.tocoo(args=canonical_args).toarray() - with raises(TypeError) as exc: + with raises(TypeError) as exc_info: op(wf, args=canonical_args, params=params) - assert 'mutually exclusive' in str(exc) - with raises(TypeError) as exc: + assert 'mutually exclusive' in str(exc_info.value) + with raises(TypeError) as exc_info: op.act(wf, args=canonical_args, params=params) - assert 'mutually exclusive' in str(exc) - with raises(TypeError) as exc: + assert 'mutually exclusive' in str(exc_info.value) + with raises(TypeError) as exc_info: op.bind(args=canonical_args, params=params) - assert 'mutually exclusive' in str(exc) + assert 'mutually exclusive' in str(exc_info.value) if has_tocoo: - with raises(TypeError) as exc: + with raises(TypeError) as exc_info: op.tocoo(args=canonical_args, params=params) - assert 'mutually exclusive' in str(exc) + assert 'mutually exclusive' in str(exc_info.value) np.testing.assert_array_equal( call_should_be, op(wf, params=params))