Commit f31331ec authored by Kloss's avatar Kloss

catch problem with energy mode mapping

parent cd29e9ae
......@@ -9,6 +9,7 @@ import collections.abc
import numpy as np
import scipy.interpolate
from scipy.optimize import linear_sum_assignment
import warnings
from kwant.physics import Bands
......@@ -540,7 +541,7 @@ def _match_functions(func, xmin=-1, xmax=1, tol=1E-8, min_iter=10,
for i in range(min_iter):
order(xs[i], xs[i + 1])
except ValueError as err:
print(err)
warnings.warn(err)
x = np.array(sorted(ordered.keys()))
y = np.array([ordered[xx][0:3] for xx in x])
......@@ -837,13 +838,15 @@ class BandSketching:
-------
mode : int
kwant scattering mode index
The mode index fulfills: `0 <= mode < nbands` where `nbands`
is the total number of bands of the spectrum.
If no open mode could be found, `mode = -1` is returned.
Notes
-----
An exception is raised if the momentum interval [kmin, kmax] is badly
chose, such that either no mode / multiple modes are found, and
therefore no no unique (energy, band) -> mode mapping is possible,
an exception is raised.
therefore no unique (energy, band) -> mode mapping is possible,
a warning is printed.
"""
# type checks
assert _is_type(energy, 'real_number')
......@@ -853,10 +856,11 @@ class BandSketching:
k = self.intersect(energy, band, kmin=kmin, kmax=kmax)
if len(k) != 1: # test for existance and uniqueness
raise ValueError('no unique band-mode mapping: energy={energy}, '
'band={band}, interval=[{kmin}, {kmax}], k={k}.'
.format(energy=energy, band=band,
kmin=kmin, kmax=kmax, k=k))
msg = ('no unique band-mode mapping: energy={energy}, band={band}, '
'interval=[{kmin}, {kmax}], k={k}.'
.format(energy=energy, band=band, kmin=kmin, kmax=kmax, k=k))
warnings.warn(msg)
return -1
return self.momentum_to_scattering_mode(k[0], band)
def intersect(self, f, band, derivative_order=0,
......
......@@ -6,6 +6,7 @@
from functools import partial
import numpy as np
from numpy.testing import assert_array_almost_equal, assert_array_equal
import pytest
from pytest import raises
import kwant
import kwant_spectrum as ks
......@@ -250,11 +251,18 @@ def test_band_analysis_methods():
assert mode(0.1, 2) == mode(-0.1, 2) == 2
assert mode(1.2, 2) == mode(-1.2, 2) == 1
assert mode(2.1, 2) == mode(-2.1, 2) == 0
assert mode(0, 2)
# band-mode mapping from energy
mode = ba1.energy_to_scattering_mode
assert mode(energy=0, band=2, kmin=0, kmax=np.pi) == 2
assert mode(energy=1, band=2, kmin=0, kmax=np.pi) == 1
assert mode(energy=3, band=2, kmin=0, kmax=np.pi) == 0
# check what happens if no mode can be found
with pytest.warns(UserWarning) as record:
mode = ba1.energy_to_scattering_mode(0., 0, kmin=0, kmax=1)
assert len(record) == 1
assert 'no unique band-mode mapping' in record[0].message.args[0]
assert mode == -1
# check that result is similar to reordered kwant result
bands = kwant.physics.Bands(make_lead().finalized())
energies = [bands(k)[order] for k, order in zip(ba.x, ba.ordering)]
......
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