Commit 1a077ad2 authored by Christoph Groth's avatar Christoph Groth
Browse files

make showing code examples in the tutorial text robust

parent 60d6fb90
......@@ -5,6 +5,7 @@ MANIFEST
/kwant/*.c
/kwant/*/*.c
/kwant/_static_version.py
/tutorial
/build
/dist
/doc/build
......
......@@ -12,9 +12,7 @@ recursive-include kwant *.pxd
recursive-include kwant *.c
recursive-include kwant *.h
recursive-include kwant test_*.py
recursive-include examples *.py
recursive-include tutorial *.py
include doc/other/*[a-zA-Z]
include doc/Makefile
......
......@@ -119,8 +119,9 @@ doctest:
# Make the image generation scripts by patching tutorial scipts.
.SECONDARY:
%.py: %.py.diff
@cp ../tutorial/$(notdir $@) $(dir $@)
@grep -v '^#HIDDEN' source/tutorial/$(notdir $@) >$@
@patch $@ $<
# Generation of tutorial images. This requires some make trickery, see
......
......@@ -6,5 +6,6 @@
for f in [0-9]-*.py; do
# We use custom labels to suppress the time stamps which are unnecessary
# here and would only lead to noise in version control.
diff -u --label original --label modified ../../../tutorial/$f $f >$f.diff
grep -v '#HIDDEN' ../tutorial/$f |
diff -u --label original --label modified - $f >$f.diff
done
......@@ -8,16 +8,23 @@
# - Making scattering region and leads
# - Using the simple sparse solver for computing Landauer conductance
#HIDDEN_BEGIN_dwhx
import kwant
#HIDDEN_END_dwhx
# First, define the tight-binding system
#HIDDEN_BEGIN_goiq
sys = kwant.Builder()
#HIDDEN_END_goiq
# Here, we are only working with square lattices
#HIDDEN_BEGIN_suwo
a = 1
lat = kwant.lattice.Square(a)
#HIDDEN_END_suwo
#HIDDEN_BEGIN_zfvr
t = 1.0
W = 10
L = 30
......@@ -35,6 +42,7 @@ for i in xrange(L):
#hopping in x-direction
if i > 0:
sys[lat(i, j), lat(i - 1, j)] = -t
#HIDDEN_END_zfvr
# Then, define the leads:
......@@ -42,9 +50,12 @@ for i in xrange(L):
# (Note: in the current version, TranslationalSymmetry takes a
# realspace vector)
#HIDDEN_BEGIN_xcmc
sym_lead0 = kwant.TranslationalSymmetry([lat.vec((-1, 0))])
lead0 = kwant.Builder(sym_lead0)
#HIDDEN_END_xcmc
#HIDDEN_BEGIN_ndez
for j in xrange(W):
lead0[lat(0, j)] = 4 * t
......@@ -52,8 +63,10 @@ for j in xrange(W):
lead0[lat(0, j), lat(0, j - 1)] = -t
lead0[lat(1, j), lat(0, j)] = -t
#HIDDEN_END_ndez
# Then the lead to the right
#HIDDEN_BEGIN_xhqc
sym_lead1 = kwant.TranslationalSymmetry([lat.vec((1, 0))])
lead1 = kwant.Builder(sym_lead1)
......@@ -65,22 +78,30 @@ for j in xrange(W):
lead1[lat(0, j), lat(0, j - 1)] = -t
lead1[lat(1, j), lat(0, j)] = -t
#HIDDEN_END_xhqc
# Then attach the leads to the system
#HIDDEN_BEGIN_fskr
sys.attach_lead(lead0)
sys.attach_lead(lead1)
#HIDDEN_END_fskr
# Plot it, to make sure it's OK
#HIDDEN_BEGIN_wsgh
kwant.plot(sys)
#HIDDEN_END_wsgh
# Finalize the system
#HIDDEN_BEGIN_dngj
sys = sys.finalized()
#HIDDEN_END_dngj
# Now that we have the system, we can compute conductance
#HIDDEN_BEGIN_buzn
energies = []
data = []
for ie in xrange(100):
......@@ -93,9 +114,11 @@ for ie in xrange(100):
# lead 1
energies.append(energy)
data.append(smatrix.transmission(1, 0))
#HIDDEN_END_buzn
# Use matplotlib to write output
# We should see conductance steps
#HIDDEN_BEGIN_lliv
from matplotlib import pyplot
pyplot.figure()
......@@ -103,3 +126,4 @@ pyplot.plot(energies, data)
pyplot.xlabel("energy [in units of t]")
pyplot.ylabel("conductance [in units of e^2/h]")
pyplot.show()
#HIDDEN_END_lliv
......@@ -10,6 +10,7 @@
# Note: Does the same as tutorial1a.py, but using other features of kwant
#
#HIDDEN_BEGIN_xkzy
import kwant
# For plotting
......@@ -22,33 +23,45 @@ def make_system(a=1, t=1.0, W=10, L=30):
lat = kwant.lattice.Square(a)
sys = kwant.Builder()
#HIDDEN_END_xkzy
#### Define the scattering region. ####
#HIDDEN_BEGIN_vvjt
sys[(lat(x, y) for x in range(L) for y in range(W))] = 4 * t
#HIDDEN_END_vvjt
#HIDDEN_BEGIN_nooi
for hopping in lat.nearest:
sys[sys.possible_hoppings(*hopping)] = -t
#HIDDEN_END_nooi
#### Define the leads. ####
# First the lead to the left, ...
# (Note: in the current version, TranslationalSymmetry takes a
# realspace vector)
#HIDDEN_BEGIN_iepx
sym_lead0 = kwant.TranslationalSymmetry([lat.vec((-1, 0))])
lead0 = kwant.Builder(sym_lead0)
lead0[(lat(0, j) for j in xrange(W))] = 4 * t
for hopping in lat.nearest:
lead0[lead0.possible_hoppings(*hopping)] = -t
#HIDDEN_END_iepx
# ... then the lead to the right. We use a method that returns a copy of
# `lead0` with its direction reversed.
#HIDDEN_BEGIN_xkdo
lead1 = lead0.reversed()
#HIDDEN_END_xkdo
#### Attach the leads and return the system. ####
#HIDDEN_BEGIN_yxot
sys.attach_lead(lead0)
sys.attach_lead(lead1)
return sys
#HIDDEN_END_yxot
#HIDDEN_BEGIN_ayuk
def plot_conductance(sys, energies):
# Compute conductance
......@@ -62,8 +75,10 @@ def plot_conductance(sys, energies):
pyplot.xlabel("energy [in units of t]")
pyplot.ylabel("conductance [in units of e^2/h]")
pyplot.show()
#HIDDEN_END_ayuk
#HIDDEN_BEGIN_cjel
def main():
sys = make_system()
......@@ -75,9 +90,12 @@ def main():
# We should see conductance steps.
plot_conductance(sys, energies=[0.01 * i for i in xrange(100)])
#HIDDEN_END_cjel
# Call the main function if the script gets executed (as opposed to imported).
# See <http://docs.python.org/library/__main__.html>.
#HIDDEN_BEGIN_ypbj
if __name__ == '__main__':
main()
#HIDDEN_END_ypbj
......@@ -18,6 +18,7 @@ import kwant
from matplotlib import pyplot
#HIDDEN_BEGIN_eusz
def make_system(a=1, t=1.0, W=10, r1=10, r2=20):
# Start with an empty tight-binding system and a single square lattice.
# `a` is the lattice constant (by default set to 1 for simplicity).
......@@ -32,11 +33,14 @@ def make_system(a=1, t=1.0, W=10, r1=10, r2=20):
(x, y) = pos
rsq = x ** 2 + y ** 2
return (r1 ** 2 < rsq < r2 ** 2)
#HIDDEN_END_eusz
# and add the corresponding lattice points using the `shape`-function
#HIDDEN_BEGIN_lcak
sys[lat.shape(ring, (0, r1 + 1))] = 4 * t
for hopping in lat.nearest:
sys[sys.possible_hoppings(*hopping)] = -t
#HIDDEN_END_lcak
# In order to introduce a flux through the ring, we introduce a phase
# on the hoppings on the line cut through one of the arms
......@@ -44,6 +48,7 @@ def make_system(a=1, t=1.0, W=10, r1=10, r2=20):
# since we want to change the flux without modifying Builder repeatedly,
# we define the modified hoppings as a function that takes the flux
# through the global variable phi.
#HIDDEN_BEGIN_lvkt
def fluxphase(site1, site2):
return exp(1j * phi)
......@@ -57,9 +62,11 @@ def make_system(a=1, t=1.0, W=10, r1=10, r2=20):
# Modify only those hopings in x-direction that cross the branch cut
sys[(hop for hop in sys.possible_hoppings((1, 0), lat, lat)
if crosses_branchcut(hop))] = fluxphase
#HIDDEN_END_lvkt
#### Define the leads. ####
# left lead
#HIDDEN_BEGIN_qwgr
sym_lead0 = kwant.TranslationalSymmetry([lat.vec((-1, 0))])
lead0 = kwant.Builder(sym_lead0)
......@@ -70,14 +77,17 @@ def make_system(a=1, t=1.0, W=10, r1=10, r2=20):
lead0[lat.shape(lead_shape, (0, 0))] = 4 * t
for hopping in lat.nearest:
lead0[lead0.possible_hoppings(*hopping)] = -t
#HIDDEN_END_qwgr
# Then the lead to the right
# [again, obtained using reversed()]
lead1 = lead0.reversed()
#### Attach the leads and return the system. ####
#HIDDEN_BEGIN_skbz
sys.attach_lead(lead0)
sys.attach_lead(lead1)
#HIDDEN_END_skbz
return sys
......
......@@ -13,6 +13,10 @@ from matplotlib import pyplot
# global variable governing the behavior of potential() in
# make_system()
#HIDDEN The following code line is included verbatim in the tutorial text
#HIDDEN because nested code examples are not supported. Remember to update
#HIDDEN the tutorial text when you modify this line.
#HIDDEN_BEGIN_ehso
pot = 0
def make_system(a=1, t=1.0, W=10, L=30, L_well=10):
......@@ -31,13 +35,16 @@ def make_system(a=1, t=1.0, W=10, L=30, L_well=10):
return pot
else:
return 0
#HIDDEN_END_ehso
#HIDDEN_BEGIN_coid
def onsite(site):
return 4 * t + potential(site)
sys[(lat(x, y) for x in range(L) for y in range(W))] = onsite
for hopping in lat.nearest:
sys[sys.possible_hoppings(*hopping)] = -t
#HIDDEN_END_coid
#### Define the leads. ####
# First the lead to the left, ...
......@@ -64,6 +71,7 @@ def make_system(a=1, t=1.0, W=10, L=30, L_well=10):
def plot_conductance(sys, energy, welldepths):
# We specify that we want to not only read, but also write to a
# global variable.
#HIDDEN_BEGIN_sqvr
global pot
# Compute conductance
......@@ -80,6 +88,7 @@ def plot_conductance(sys, energy, welldepths):
pyplot.xlabel("well depth [in units of t]")
pyplot.ylabel("conductance [in units of e^2/h]")
pyplot.show()
#HIDDEN_END_sqvr
def main():
......
......@@ -16,13 +16,17 @@ import kwant
from matplotlib import pyplot
# For matrix support
#HIDDEN_BEGIN_xumz
import numpy
#HIDDEN_END_xumz
# define Pauli-matrices for convenience
#HIDDEN_BEGIN_hwbt
sigma_0 = numpy.eye(2)
sigma_x = numpy.array([[0, 1], [1, 0]])
sigma_y = numpy.array([[0, -1j], [1j, 0]])
sigma_z = numpy.array([[1, 0], [0, -1]])
#HIDDEN_END_hwbt
def make_system(a=1, t=1.0, alpha=0.5, e_z=0.08, W=10, L=30):
......@@ -33,6 +37,7 @@ def make_system(a=1, t=1.0, alpha=0.5, e_z=0.08, W=10, L=30):
sys = kwant.Builder()
#### Define the scattering region. ####
#HIDDEN_BEGIN_uxrm
sys[(lat(x, y) for x in range(L) for y in range(W))] = 4 * t * sigma_0 + \
e_z * sigma_z
# hoppings in x-direction
......@@ -41,12 +46,14 @@ def make_system(a=1, t=1.0, alpha=0.5, e_z=0.08, W=10, L=30):
# hoppings in y-directions
sys[sys.possible_hoppings((0, 1), lat, lat)] = -t * sigma_0 + \
1j * alpha * sigma_x
#HIDDEN_END_uxrm
#### Define the leads. ####
# left lead
sym_lead0 = kwant.TranslationalSymmetry([lat.vec((-1, 0))])
lead0 = kwant.Builder(sym_lead0)
#HIDDEN_BEGIN_yliu
lead0[(lat(0, j) for j in xrange(W))] = 4 * t * sigma_0 + e_z * sigma_z
# hoppings in x-direction
lead0[lead0.possible_hoppings((1, 0), lat, lat)] = -t * sigma_0 - \
......@@ -54,6 +61,7 @@ def make_system(a=1, t=1.0, alpha=0.5, e_z=0.08, W=10, L=30):
# hoppings in y-directions
lead0[lead0.possible_hoppings((0, 1), lat, lat)] = -t * sigma_0 + \
1j * alpha * sigma_x
#HIDDEN_END_yliu
# Then the lead to the right
# (again, obtained using reverse()
......
......@@ -14,6 +14,7 @@ from math import pi
from matplotlib import pyplot
#HIDDEN_BEGIN_zxip
def make_lead(a=1, t=1.0, W=10):
# Start with an empty lead with a single square lattice
lat = kwant.lattice.Square(a)
......@@ -32,8 +33,10 @@ def make_lead(a=1, t=1.0, W=10):
lead[lat(1, j), lat(0, j)] = -t
return lead
#HIDDEN_END_zxip
#HIDDEN_BEGIN_pejz
def plot_bandstructure(lead, momenta):
# Use the method ``energies`` of the finalized lead to compute
# the bandstructure
......@@ -53,6 +56,7 @@ def main():
momenta = [-pi + 0.02 * pi * i for i in xrange(101)]
plot_bandstructure(lead, momenta)
#HIDDEN_END_pejz
# Call the main function if the script gets executed (as opposed to imported).
......
......@@ -13,7 +13,9 @@ from cmath import exp
import kwant
# For eigenvalue computation
#HIDDEN_BEGIN_tibv
import scipy.linalg as la
#HIDDEN_END_tibv
# For plotting
from matplotlib import pyplot
......@@ -23,6 +25,7 @@ def make_system(a=1, t=1.0, r=10):
# Start with an empty tight-binding system and a single square lattice.
# `a` is the lattice constant (by default set to 1 for simplicity).
#HIDDEN_BEGIN_qlyd
lat = kwant.lattice.Square(a)
sys = kwant.Builder()
......@@ -46,8 +49,10 @@ def make_system(a=1, t=1.0, r=10):
# It's a closed system for a change, so no leads
return sys
#HIDDEN_END_qlyd
#HIDDEN_BEGIN_yvri
def plot_spectrum(sys, Bfields):
# global variable B controls the magnetic field
global B
......@@ -74,6 +79,7 @@ def plot_spectrum(sys, Bfields):
pyplot.xlabel("magnetic field [some arbitrary units]")
pyplot.ylabel("energy [in units of t]")
pyplot.show()
#HIDDEN_END_yvri
def main():
......
......@@ -21,11 +21,14 @@ from matplotlib import pyplot
# Define the graphene lattice
sin_30, cos_30 = (1 / 2, sqrt(3) / 2)
#HIDDEN_BEGIN_hnla
graphene = kwant.make_lattice([(1, 0), (sin_30, cos_30)],
[(0, 0), (0, 1 / sqrt(3))])
a, b = graphene.sublattices
#HIDDEN_END_hnla
#HIDDEN_BEGIN_shzy
def make_system(r=10, w=2.0, pot=0.1):
#### Define the scattering region. ####
......@@ -43,18 +46,26 @@ def make_system(r=10, w=2.0, pot=0.1):
return pot * tanh(d / w)
sys[graphene.shape(circle, (0, 0))] = potential
#HIDDEN_END_shzy
# specify the hoppings of the graphene lattice in the
# format expected by possibe_hoppings()
#HIDDEN_BEGIN_hsmc
hoppings = (((0, 0), b, a), ((0, 1), b, a), ((-1, 1), b, a))
#HIDDEN_END_hsmc
#HIDDEN_BEGIN_bfwb
for hopping in hoppings:
sys[sys.possible_hoppings(*hopping)] = -1
#HIDDEN_END_bfwb
# Modify the scattering region
#HIDDEN_BEGIN_efut
del sys[a(0, 0)]
sys[a(-2, 1), b(2, 2)] = -1
#HIDDEN_END_efut
#### Define the leads. ####
#HIDDEN_BEGIN_aakh
# left lead
sym0 = kwant.TranslationalSymmetry([graphene.vec((-1, 0))])
......@@ -80,10 +91,14 @@ def make_system(r=10, w=2.0, pot=0.1):
lead1[graphene.shape(lead1_shape, (0, 0))] = pot
for hopping in hoppings:
lead1[lead1.possible_hoppings(*hopping)] = -1
#HIDDEN_END_aakh
#HIDDEN_BEGIN_kmmw
return sys, [lead0, lead1]
#HIDDEN_END_kmmw
#HIDDEN_BEGIN_zydk
def compute_evs(sys):
# Compute some eigenvalues of the closed system
sparse_mat = sys.hamiltonian_submatrix(sparse=True)
......@@ -96,6 +111,7 @@ def compute_evs(sys):
print evs
except:
pass
#HIDDEN_END_zydk
def plot_conductance(sys, energies):
......@@ -124,6 +140,11 @@ def plot_bandstructure(flead, momenta):
pyplot.show()
#HIDDEN The part of the following code block which begins with plotter_symbols
#HIDDEN is included verbatim in the tutorial text because nested code examples
#HIDDEN are not supported. Remember to update the tutorial text when you
#HIDDEN modify this block.
#HIDDEN_BEGIN_itkk
def main():
pot = 0.1
sys, leads = make_system(pot=pot)
......@@ -137,9 +158,12 @@ def main():
# Plot the closed system without leads.
kwant.plot(sys, symbols=plotter_symbols)
#HIDDEN_END_itkk
# Compute some eigenvalues.
#HIDDEN_BEGIN_jmbi
compute_evs(sys.finalized())
#HIDDEN_END_jmbi
# Attach the leads to the system.
for lead in leads:
......
......@@ -18,6 +18,7 @@ from math import pi
# For plotting
from matplotlib import pyplot
#HIDDEN_BEGIN_nbvn
tau_x = np.array([[0, 1], [1, 0]])
tau_z = np.array([[1, 0], [0, -1]])
......@@ -40,6 +41,7 @@ def make_lead(a=1, t=1.0, mu=0.7, Delta=0.1, W=10):
lead[lat(1, j), lat(0, j)] = -t * tau_z
return lead
#HIDDEN_END_nbvn
def plot_bandstructure(lead, momenta):
......
......@@ -13,13 +13,16 @@ import kwant
from matplotlib import pyplot
#HIDDEN_BEGIN_zuuw
def make_system(a=1, W=10, L=10, barrier=1.5, barrierpos=(3, 4),
mu=0.4, Delta=0.1, Deltapos=4, t=1.0):
# Start with an empty tight-binding system and two square lattices,
# corresponding to electron and hole degree of freedom
lat_e = kwant.lattice.Square(a)
lat_h = kwant.lattice.Square(a)
#HIDDEN_END_zuuw
#HIDDEN_BEGIN_pqmp
sys = kwant.Builder()
#### Define the scattering region. ####
......@@ -42,8 +45,10 @@ def make_system(a=1, W=10, L=10, barrier=1.5, barrierpos=(3, 4),
# electrons and holes
sys[((lat_e(x, y), lat_h(x, y)) for x in range(Deltapos, L)
for y in range(W))] = Delta
#HIDDEN_END_pqmp
#### Define the leads. ####
#HIDDEN_BEGIN_ttth
# left electron lead
sym_lead0 = kwant.TranslationalSymmetry([lat_e.vec((-1, 0))])
lead0 = kwant.Builder(sym_lead0)
......@@ -61,10 +66,12 @@ def make_system(a=1, W=10, L=10, barrier=1.5, barrierpos=(3, 4),
# hoppings in x and y-direction
lead1[lead1.possible_hoppings((1, 0), lat_h, lat_h)] = t
lead1[lead1.possible_hoppings((0, 1), lat_h, lat_h)] = t
#HIDDEN_END_ttth
# Then the lead to the right
# this one is superconducting and thus is comprised of electrons
# AND holes
#HIDDEN_BEGIN_mhiw
sym_lead2 = kwant.TranslationalSymmetry([lat_e.vec((1, 0))])
lead2 = kwant.Builder(sym_lead2)
......@@ -76,15 +83,19 @@ def make_system(a=1, W=10, L=10, barrier=1.5, barrierpos=(3, 4),
lead2[lead2.possible_hoppings((1, 0), lat_h, lat_h)] = t
lead2[lead2.possible_hoppings((0, 1), lat_h, lat_h)] = t
lead2[((lat_e(0, j), lat_h(0, j)) for j in xrange(W))] = Delta
#HIDDEN_END_mhiw
#### Attach the leads and return the system. ####
#HIDDEN_BEGIN_ozsr
sys.attach_lead(lead0)
sys.attach_lead(lead1)
sys.attach_lead(lead2)
return sys
#HIDDEN_END_ozsr
#HIDDEN_BEGIN_jbjt
def plot_conductance(sys, energies):
# Compute conductance
data = []
......@@ -94,6 +105,7 @@ def plot_conductance(sys, energies):
data.append(smatrix.submatrix(0, 0).shape[0] -
smatrix.transmission(0, 0) +
smatrix.transmission(1, 0))
#HIDDEN_END_jbjt
pyplot.figure()
pyplot.plot(energies, data)
......
This directory contains the example scripts of the tutorial.
Note for kwant developers
-------------------------
......
......@@ -16,8 +16,9 @@ with a hard wall confinement :math:`V(y)` in y-direction.
In order to use kwant, we need to import it:
.. literalinclude:: ../../../tutorial/1-quantum_wire.py
:lines: 11
.. literalinclude:: 1-quantum_wire.py
:start-after: #HIDDEN_BEGIN_dwhx
:end-before: #HIDDEN_END_dwhx
Enabling kwant is as easy as this [#]_ !
......@@ -26,15 +27,17 @@ and leads. For this we make use of the `~kwant.builder.Builder` class
that allows for a convenient way to define the system. For this we need to
create an instance of the `~kwant.builder.Builder` class:
.. literalinclude::