Thank you Joe!
In the simple code below (first code), I am adding two sites to the left side of a square lattice and I want to attach the lead to them.
Although the starting point is clearly in the lead_shape, but it complains that 'None of the starting sites is in the desired shape'

There are of course several ways around this, e.g
changing the lead_shape to the following:

def lead_shape(site):
(x, y) = site.pos
return (y==5 and  x<=0)

Could you please tell me why is that so?

I am asking this, because I have the same problem in my original code mentioned before (attached below).
The problem is in this section:

else:               #countinuous lead
def lead_shape(site):
(x, y, z) = site.pos
return (x==-0.7 and y==0 and z<=-a)
t00=0.0
params = dict(t00=0., t0=2., e1=0.)
Leadham ="(t00*k_x**2)*sigma_0 + (t00*k_y**2)*sigma_0 - (t0*k_z**2)*sigma_0 + (2*t0+e1)*sigma_0"
template = kwant.continuum.discretize(Leadham, grid_spacing=a)
sym = kwant.TranslationalSymmetry([0, 0, -a])
dn_lead = kwant.Builder(sym, conservation_law=-sigma_z)
dn_lead.fill(template, lead_shape, (-0.7, 0, -a))
syst.attach_lead(dn_lead)

Although I have manually added the site with this coordinate (-0.7, 0, -a) to the system

Regards
Patrik

#simple example
------------------------------------------------------------------------------------------------------
from numpy import *
from numpy.linalg import *
import pickle
import sys
import os
import string
import heapq
import kwant
import tinyarray
from matplotlib import pyplot
import scipy.sparse.linalg
import scipy.linalg

a=1
t = 1.0
W = 10
L = 30
e=4.0
sigma_z = tinyarray.array([[1, 0], [0, -1]])

ct=True
if ct:
def rec(pos):
(x, y) = pos
return (0 <= y < W and 0 <= x < L)

def lead_shape(site):
(x, y) = site.pos
return (y==5 and  x<=-2)

t0=0.0
params = dict(t=1., t0=0.)
hamiltonian = "t*k_x**2 * identity(2) + t*k_y**2 * identity(2) +  t0 * identity(2)"
template = kwant.continuum.discretize(hamiltonian, grid_spacing=a)
print(template)
syst1 = kwant.Builder()
lat = kwant.lattice.square(a, norbs=2)
syst1[lat.shape(rec, (0, 0))] = e * identity(2)
syst1[lat(-1,5)] = e * identity(2)
syst1[lat(-2,5)] = e * identity(2)
syst1[lat.neighbors()] = -t * identity(2)

sym = kwant.TranslationalSymmetry([-a, 0])
lead1 = kwant.Builder(sym, conservation_law=-sigma_z)
lead1.fill(template, lead_shape, (-2, 5))

syst1.attach_lead(lead1)
syst1.attach_lead(lead1.reversed())
syst1 = syst1.finalized()
system=kwant.plot(syst1)
------------------------------------------------------------------------------------------------------------------------

#my code
----------------------------------------------------------------------------------------------------------------------
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial import *
from matplotlib import rcParams
from numpy import *
from numpy.linalg import *
import pickle
import sys
import os
import string
import heapq
import kwant
import tinyarray
from matplotlib import pyplot

chiral=True
if chiral:
p = pi/5    #phi
t = 0.66    #theta
a = 0.34
x = 1.4
e1 = 0
e2 = 0.3
t2=0.1
t1=-x*t2
t0 = 2
lam=-0.08
t_so1 = 0.01 #spin-orbit coupling param
t_so2 = x*t_so1 #spin-orbit coupling param
tl=tr=0.3
N = 30
sigma_0 = tinyarray.array([[1, 0], [0, 1]])
sigma_x = tinyarray.array([[0, 1], [1, 0]])
sigma_y = tinyarray.array([[0, -1j], [1j, 0]])
sigma_z = tinyarray.array([[1, 0], [0, -1]])
no=2            #number of orbitals
def sigma_v1(ap):         # pauli metrix along the vertical axis
value=sigma_z*cos(t)+sin(t)*(sigma_x*sin(ap)-sigma_y*cos(ap))
return value

def sigma_v2(ap):         # pauli metrix along the vertical axis
value=sigma_z*cos(t)-sin(t)*(sigma_x*sin(ap)-sigma_y*cos(ap))
return value

def family_color(sites):
return 'black' #if site.family == sites

def hopping_lw(site1, site2):
return 0.08

class Amorphous(kwant.builder.SiteFamily):
def __init__(self, coords):
self.coords = coords
super(Amorphous, self).__init__("amorphous", "",no)

def normalize_tag(self, tag):
try:
tag = int(tag[0])
except:
raise KeyError

if 0 <= tag < len(coords):
return tag
else:
raise KeyError

def pos(self, tag):
return self.coords[tag]

coords=[(0.0000000000, 0.0000000000, 0.0000000000), (-0.1336881039, 0.4114496766, 0.3400000000), (-0.4836881039, 0.6657395614, 0.6800000000), (-0.9163118961, 0.6657395614, 1.0200000000), (-1.2663118961, 0.4114496766, 1.3600000000), (-1.4000000000, 0.0000000000, 1.7000000000), (-1.2663118961, -0.4114496766, 2.0400000000), (-0.9163118961, -0.6657395614, 2.3800000000), (-0.4836881039, -0.6657395614, 2.7200000000), (-0.1336881039, -0.4114496766, 3.0600000000), (0.0000000000, -0.0000000000, 3.4000000000), (-0.1336881039, 0.4114496766, 3.7400000000), (-0.4836881039, 0.6657395614, 4.0800000000), (-0.9163118961, 0.6657395614, 4.4200000000), (-1.2663118961, 0.4114496766, 4.7600000000), (-1.4000000000, 0.0000000000, 5.1000000000), (-1.2663118961, -0.4114496766, 5.4400000000), (-0.9163118961, -0.6657395614, 5.7800000000), (-0.4836881039, -0.6657395614, 6.1200000000), (-0.1336881039, -0.4114496766, 6.4600000000), (0.0000000000, -0.0000000000, 6.8000000000), (-0.1336881039, 0.4114496766, 7.1400000000), (-0.4836881039, 0.6657395614, 7.4800000000), (-0.9163118961, 0.6657395614, 7.8200000000), (-1.2663118961, 0.4114496766, 8.1600000000), (-1.4000000000, 0.0000000000, 8.5000000000), (-1.2663118961, -0.4114496766, 8.8400000000), (-0.9163118961, -0.6657395614, 9.1800000000), (-0.4836881039, -0.6657395614, 9.5200000000), (-0.1336881039, -0.4114496766, 9.8600000000), (-1.4000000000, 0.0000000000, 0.0000000000), (-1.2663118961, -0.4114496766, 0.3400000000), (-0.9163118961, -0.6657395614, 0.6800000000), (-0.4836881039, -0.6657395614, 1.0200000000), (-0.1336881039, -0.4114496766, 1.3600000000), (0.0000000000, -0.0000000000, 1.7000000000), (-0.1336881039, 0.4114496766, 2.0400000000), (-0.4836881039, 0.6657395614, 2.3800000000), (-0.9163118961, 0.6657395614, 2.7200000000), (-1.2663118961, 0.4114496766, 3.0600000000), (-1.4000000000, 0.0000000000, 3.4000000000), (-1.2663118961, -0.4114496766, 3.7400000000), (-0.9163118961, -0.6657395614, 4.0800000000), (-0.4836881039, -0.6657395614, 4.4200000000), (-0.1336881039, -0.4114496766, 4.7600000000), (0.0000000000, -0.0000000000, 5.1000000000), (-0.1336881039, 0.4114496766, 5.4400000000), (-0.4836881039, 0.6657395614, 5.7800000000), (-0.9163118961, 0.6657395614, 6.1200000000), (-1.2663118961, 0.4114496766, 6.4600000000), (-1.4000000000, 0.0000000000, 6.8000000000), (-1.2663118961, -0.4114496766, 7.1400000000), (-0.9163118961, -0.6657395614, 7.4800000000), (-0.4836881039, -0.6657395614, 7.8200000000), (-0.1336881039, -0.4114496766, 8.1600000000), (0.0000000000, -0.0000000000, 8.5000000000), (-0.1336881039, 0.4114496766, 8.8400000000), (-0.4836881039, 0.6657395614, 9.1800000000), (-0.9163118961, 0.6657395614, 9.5200000000), (-1.2663118961, 0.4114496766, 9.8600000000)]
amorphous_lat = Amorphous(coords)

syst = kwant.Builder()

for i in range(N):
syst[amorphous_lat(i)] = e1*sigma_0
syst[amorphous_lat(N+i)] = e2*sigma_0
syst[amorphous_lat(i), amorphous_lat(N+i)] = lam*sigma_0
if i > 0:
syst[amorphous_lat(i), amorphous_lat(i-1)] = t1*sigma_0 + 1j*t_so1*(sigma_v1(i*p)+sigma_v1((i-1)*p))
syst[amorphous_lat(N+i),amorphous_lat(N+i-1)] = t2*sigma_0 + 1j*t_so2*(sigma_v2(i*p)+sigma_v2((i-1)*p))

prim_vecs=tinyarray.array([(a,0.,0.),(0.,a,0.),(0.,0.,a)])

offset1=tinyarray.array((-0.7, 0.0, 0.0))
lat1=kwant.lattice.Monatomic(prim_vecs, offset1, norbs=no)

syst[lat1(0, 0, -1)] = e1*sigma_0
syst[amorphous_lat(0), lat1(0, 0, -1)] = tl*sigma_0
syst[amorphous_lat(N), lat1(0, 0, -1)] = tl*sigma_0

discrete=False      #discrete lead

if discrete:
sym = kwant.TranslationalSymmetry([0, 0, -a])
dn_lead = kwant.Builder(sym, conservation_law=-sigma_z)

dn_lead[lat1(0, 0, -2)] = e1*sigma_0
dn_lead[lat1.neighbors()] = t0*sigma_0
syst.attach_lead(dn_lead)

else:               #countinuous lead
def lead_shape(site):
(x, y, z) = site.pos
return (x==-0.7 and y==0 and z<=-a)
t00=0.0
params = dict(t00=0., t0=2., e1=0.)
Leadham ="(t00*k_x**2)*sigma_0 + (t00*k_y**2)*sigma_0 - (t0*k_z**2)*sigma_0 + (2*t0+e1)*sigma_0"
template = kwant.continuum.discretize(Leadham, grid_spacing=a)
sym = kwant.TranslationalSymmetry([0, 0, -a])
dn_lead = kwant.Builder(sym, conservation_law=-sigma_z)
dn_lead.fill(template, lead_shape, (-0.7, 0, -a))
syst.attach_lead(dn_lead)

sym1 = kwant.TranslationalSymmetry([0, 0, a])
up_lead = kwant.Builder(sym1, conservation_law=-sigma_z)

syst[lat1(0, 0, N)] = e1*sigma_0
syst[amorphous_lat(N-1), lat1(0, 0, N)] = tr*sigma_0
syst[amorphous_lat(2*N-1), lat1(0, 0, N)] = tr*sigma_0

up_lead[lat1(0, 0, N+1)] = e1*sigma_0
up_lead[lat1.neighbors()] = t0*sigma_0
syst.attach_lead(up_lead)

system=kwant.plot(syst, site_lw=0.1, site_color=family_color, hop_lw=hopping_lw)

trans=True
if trans:
syst = syst.finalized()
energies = []
datau = []

for ie in range(-320,520):
energy = ie * 0.001
smatrix = kwant.smatrix(syst, energy=energy)
energies.append(energy)
Gu=smatrix.transmission((1, 0), 0)
Gd=smatrix.transmission((1, 1), 0)
datau.append(Gu)

fig = pyplot.figure()
pyplot.plot(energies, datau, 'b--')
pyplot.legend(['Gu'], loc='upper left')
pyplot.xlim([-0.32,0.52])
pyplot.ylim([-0.03,1.05])
pyplot.show()
------------------------------------------------------------------------------------------------------------------------------------------------------------------

On 8 August 2017 at 13:12, Joseph Weston wrote:
Dear Patrik,

> When I replace the 'if continuous' section with the following section
> it will complain.
>
>     if continuous:
>         def lead_shape(site):
>             (x, y, z) = site.pos
>             return (x == -0.7 and y == 0.0 and z <= -a)
>         t00=0.0
>         Leadham
> ="t00*sigma_0*k_x**2+t00*sigma_0*k_y**2-t0*sigma_0*k_z**2+(2*t0+e1)*sigma_0"
>         template = kwant.continuum.discretize(Leadham, grid_spacing=a)
>         dn_lead.fill(template, lead_shape, lat1(0, 0, -1))
>         syst.attach_lead(dn_lead)

The error message I got when running the code was:

> ValueError: Lead to be attached contains no sites.

This error seems perfectly clear to me; you have defined your
"lead_shape" function incorrectly, such that no sites are added to your
"dn_lead" system.

Happy Kwanting,

Joe