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

# In[1]:


# Tutorial 2.2.4. Organizing a simulation script
# ==============================================
#
# Physics background
# ------------------
#  Conductance of a quantum wire; subbands
#
# Note: Does the same as quantum_write_revisited.py, but features
#       better code organization


# In[2]:


from matplotlib import pyplot
import kwant


# In[3]:


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[4]:


def make_system(L, W, a=1, t=1.0):
    lat = kwant.lattice.square(a, norbs=1)

    syst = kwant.Builder()
    syst[(lat(i, j) for i in range(L) for j in range(W))] = 4 * t
    syst[lat.neighbors()] = -t

    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())

    return syst


# In[5]:


def plot_conductance(syst, energies):
    # Compute conductance
    data = []
    for energy in energies:
        smatrix = kwant.smatrix(syst, energy)
        data.append(smatrix.transmission(1, 0))

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


# In[6]:


def main():
    syst = make_system(W=10, L=30)

    # Check that the system looks as intended.
    kwant.plot(syst)

    # Finalize the system.
    fsyst = syst.finalized()

    # We should see conductance steps.
    plot_conductance(fsyst, energies=[0.01 * i for i in range(100)])


# In[7]:


# Call the main function if the script gets executed (as opposed to imported).
# See <https://docs.python.org/library/__main__.html>.
if __name__ == '__main__':
    main()

