Commit b84bb0aa authored by Kloss's avatar Kloss
Browse files

add checks for non-matching boundary conditions

parent 014e9dcb
......@@ -270,6 +270,9 @@ class WaveFunction:
time_is_valid = extended_system.time_is_valid
else:
# true finite systems (no leads) so no need for boundary conditions
if boundaries is not None:
raise ValueError('No boundary conditions must be provided '
'for a system without leads.')
H0 = syst.hamiltonian_submatrix(params=tparams, sparse=True)
solution_is_valid = None
time_is_valid = None
......
......@@ -798,6 +798,14 @@ def test_WaveFunction_open_system__init__raises(psi_init, syst, energy, params):
@closed_sytem()
def test_WaveFunction_closed_system__init__raises(psi_init, syst, energy, params):
# provide boundaries to a closed system
boundaries, tmax = make_boundaries(nb_leads=len(syst.leads), tmax=6)
with pytest.raises(ValueError) as exc:
WaveFunction.from_kwant(psi_init=psi_init, syst=syst, boundaries=boundaries,
energy=energy, params=params)
msg = 'No boundary conditions must be provided for a system without leads.'
assert msg in str(exc.value)
params_with_time = params.copy()
params_with_time['time'] = 0
with pytest.raises(KeyError) as exc:
......
......@@ -305,6 +305,12 @@ def hamiltonian_with_boundaries(syst, boundaries, params):
The Hamiltonian of the central system, evaluated at t=0,
with boundary conditions attached, and accompanying metadata.
"""
if not len(syst.leads) == len(boundaries):
raise ValueError('Number of leads= {} does not match '
'the number of boundaries provided= {}'
.format(len(syst.leads), len(boundaries)))
# generate time-independent Hamiltonian and boundary condition
# matrices and glue them together
boundaries = [bc(lead, params) for lead, bc in zip(syst.leads, boundaries)]
......
......@@ -221,6 +221,26 @@ def test_hamiltonian(lead_maker):
assert not Hext.time_is_valid(tmax + eps)
@pytest.mark.parametrize('lead_maker', [make_simple_lead])
def test_hamiltonian_with_boundaries_exceptions(lead_maker):
# construct
syst = make_system_with_leads(kwant.lattice.square(norbs=1), lead_maker)
fsyst = syst.finalized()
# too less boundary conditions
boundaries = [leads.SimpleBoundary(10)]
with pytest.raises(ValueError) as exc:
system.hamiltonian_with_boundaries(fsyst, boundaries, params={})
assert 'Number of leads= 2 does not match the number of boundaries provided= 1' in str(exc.value)
# too many boundary conditions
boundaries = [leads.SimpleBoundary(10)] * 3
with pytest.raises(ValueError) as exc:
system.hamiltonian_with_boundaries(fsyst, boundaries, params={})
assert 'Number of leads= 2 does not match the number of boundaries provided= 3' in str(exc.value)
def test_is_time_dependent_function():
def time_dependent(zeit):
pass
......
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