#!/usr/bin/env python
# coding: utf-8

# In[1]:


# Tutorial 2.3.2. Spatially dependent values through functions
# ============================================================
#
# Physics background
# ------------------
#  transmission through a quantum well
#
# Kwant features highlighted
# --------------------------
#  - Functions as values in Builder

import kwant

# For plotting
from matplotlib import pyplot


# In[2]:


import matplotlib
import matplotlib.pyplot
from matplotlib_inline.backend_inline import set_matplotlib_formats

matplotlib.rcParams['figure.figsize'] = matplotlib.pyplot.figaspect(1) * 2
set_matplotlib_formats('svg')


# In[3]:


W, L, L_well = 10, 30, 10

def potential(site, pot):
    (x, y) = site.pos
    if (L - L_well) / 2 < x < (L + L_well) / 2:
        return pot
    else:
        return 0


# In[4]:


a = 1
t = 1.0

def onsite(site, pot):
    return 4 * t + potential(site, pot)

lat = kwant.lattice.square(a, norbs=1)
syst = kwant.Builder()

syst[(lat(x, y) for x in range(L) for y in range(W))] = onsite
syst[lat.neighbors()] = -t


# In[5]:


#### Define and attach the leads. ####
lead = kwant.Builder(kwant.TranslationalSymmetry((-a, 0)))
lead[(lat(0, j) for j in range(W))] = 4 * t
lead[lat.neighbors()] = -t
syst.attach_lead(lead)
syst.attach_lead(lead.reversed())

syst = syst.finalized()


# In[6]:


def plot_conductance(syst, energy, welldepths):

    # Compute conductance
    data = []
    for welldepth in welldepths:
        smatrix = kwant.smatrix(syst, energy, params=dict(pot=-welldepth))
        data.append(smatrix.transmission(1, 0))

    pyplot.figure()
    pyplot.plot(welldepths, data)
    pyplot.xlabel("well depth [t]")
    pyplot.ylabel("conductance [e^2/h]")
    pyplot.show()


# In[7]:


plot_conductance(syst, energy=0.2,
                 welldepths=[0.01 * i for i in range(100)])

