{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Modelling atomic chains\n",
    "\n",
    "In Periodic problems and plane-wave discretisations we already\n",
    "summarised the net effect of Bloch's theorem.\n",
    "In this notebook, we will explore some basic facts about periodic systems,\n",
    "starting from the very simplest model, a tight-binding monoatomic chain.\n",
    "The solutions to the hands-on exercises are given at the bottom of the page."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Monoatomic chain\n",
    "\n",
    "In this model, each site of an infinite 1D chain is a degree of freedom, and\n",
    "the Hilbert space is $\\ell^2(\\mathbb Z)$, the space of square-summable\n",
    "biinfinite sequences $(\\psi_n)_{n \\in \\mathbb Z}$.\n",
    "\n",
    "Each site interacts by a \"hopping term\" with its neighbors, and the\n",
    "Hamiltonian is\n",
    "$$\n",
    "H = \\left(\\begin{array}{ccccc}\n",
    "  \\dots&\\dots&\\dots&\\dots&\\dots \\\\\n",
    "  \\dots& 0 & 1 & 0 & \\dots\\\\\n",
    "  \\dots&1 & 0 &1&\\dots \\\\\n",
    "  \\dots&0 & 1 & 0& \\dots  \\\\\n",
    "  \\dots&\\dots&\\dots&\\dots&…\n",
    "\\end{array}\\right)\n",
    "$$\n",
    "\n",
    "> **Exercise 1**\n",
    ">\n",
    "> Find the eigenstates and eigenvalues of this Hamiltonian by\n",
    "> solving the second-order recurrence relation.\n",
    "\n",
    "> **Exercise 2**\n",
    ">\n",
    "> Do the same when the system is truncated to a finite number of $N$\n",
    "> sites with periodic boundary conditions.\n",
    "\n",
    "We are now going to code this:"
   ],
   "metadata": {}
  },
  {
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "build_monoatomic_hamiltonian (generic function with 1 method)"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "cell_type": "code",
   "source": [
    "function build_monoatomic_hamiltonian(N::Integer, t)\n",
    "    H = zeros(N, N)\n",
    "    for n = 1:N-1\n",
    "        H[n, n+1] = H[n+1, n] = t\n",
    "    end\n",
    "    H[1, N] = H[N, 1] = t  # Periodic boundary conditions\n",
    "    H\n",
    "end"
   ],
   "metadata": {},
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "> **Exercise 3**\n",
    ">\n",
    "> Compute the eigenvalues and eigenvectors of this Hamiltonian.\n",
    "> Plot them, and check whether they agree with theory."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Diatomic chain\n",
    "Now we are going to consider a diatomic chain `A B A B ...`, where the coupling\n",
    "`A<->B` ($t_1$) is different from the coupling `B<->A` ($t_2$). We will use a new\n",
    "index $\\alpha$ to denote the `A` and `B` sites, so that wavefunctions are now\n",
    "sequences $(\\psi_{\\alpha n})_{\\alpha \\in \\{1, 2\\}, n \\in \\mathbb Z}$.\n",
    "\n",
    "> **Exercise 4**\n",
    ">\n",
    "> Show that eigenstates of this system can be looked for in the form\n",
    "> $$\n",
    ">    \\psi_{\\alpha n} = u_{\\alpha} e^{ikn}\n",
    "> $$\n",
    "\n",
    "> **Exercise 5**\n",
    ">\n",
    "> Show that, if $\\psi$ is of the form above\n",
    "> $$\n",
    ">    (H \\psi)_{\\alpha n} = (H_k u)_\\alpha e^{ikn},\n",
    "> $$\n",
    "> where\n",
    "> ```\n",
    "> H_k = \\left(\\begin{array}{cc}\n",
    "> 0                & t_1 + t_2 e^{-ik}\\\\\n",
    "> t_1 + t_2 e^{ik} & 0\n",
    "> \\end{array}\\right)\n",
    "> ```\n",
    "\n",
    "Let's now check all this numerically:"
   ],
   "metadata": {}
  },
  {
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "plot_wavefunction (generic function with 1 method)"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "cell_type": "code",
   "source": [
    "function build_diatomic_hamiltonian(N::Integer, t1, t2)\n",
    "    # Build diatomic Hamiltonian with the two couplings\n",
    "    # ... <-t2->   A <-t1-> B <-t2->   A <-t1-> B <-t2->   ...\n",
    "    # We introduce unit cells as such:\n",
    "    # ... <-t2-> | A <-t1-> B <-t2-> | A <-t1-> B <-t2-> | ...\n",
    "    # Thus within a cell the A<->B coupling is t1 and across cell boundaries t2\n",
    "\n",
    "    H = zeros(2, N, 2, N)\n",
    "    A, B = 1, 2\n",
    "    for n = 1:N\n",
    "        H[A, n, B, n] = H[B, n, A, n] = t1  # Coupling within cell\n",
    "    end\n",
    "    for n = 1:N-1\n",
    "        H[B, n, A, n+1] = H[A, n+1, B, n] = t2  # Coupling across cells\n",
    "    end\n",
    "    H[A, 1, B, N] = H[B, N, A, 1] = t2  # Periodic BCs (A in cell1 with B in cell N)\n",
    "    reshape(H, 2N, 2N)\n",
    "end\n",
    "\n",
    "function build_diatomic_Hk(k::Integer, t1, t2)\n",
    "    # Returns Hk such that H (u e^ikn) = (Hk u) e^ikn\n",
    "    #\n",
    "    # intra-cell AB hopping of t1, plus inter-cell hopping t2 between\n",
    "    # site B (no phase shift) and site A (phase shift e^ik)\n",
    "    [0                 t1 + t2*exp(-im*k);\n",
    "     t1 + t2*exp(im*k) 0                 ]\n",
    "end\n",
    "\n",
    "using Plots\n",
    "function plot_wavefunction(ψ)\n",
    "    p = plot(real(ψ[1:2:end]), label=\"Re A\")\n",
    "    plot!(p, real(ψ[2:2:end]), label=\"Re B\")\n",
    "end"
   ],
   "metadata": {},
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "source": [
    "> **Exercise 6**\n",
    ">\n",
    "> Check the above assertions. Use a $k$ of the form\n",
    "> $2 π \\frac{l}{N}$ in order to have a $\\psi$ that has the periodicity\n",
    "> of the supercell ($N$)."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "> **Exercise 7**\n",
    ">\n",
    "> Plot the band structure, i.e. the eigenvalues of $H_k$ as a function of $k$\n",
    "> Use the function `build_diatomic_Hk` to build the Hamiltonians.\n",
    "> Compare with the eigenvalues of the (\"supercell\") Hamiltonian from\n",
    "> `build_diatomic_hamiltonian`. In the case $t_1 = t_2$, how do the bands follow\n",
    "> from the previous study of the monoatomic chain?"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "> **Exercise 8**\n",
    ">\n",
    "> Repeat the above analysis in the case of a finite-difference\n",
    "> discretization of a continuous Hamiltonian $H = - \\frac 1 2 \\Delta + V(x)$\n",
    "> where $V$ is periodic\n",
    "> *Hint:* It is advisable to work through Comparing discretization techniques\n",
    "> before tackling this question."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Solutions\n",
    "\n",
    "### Exercise 1\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 2\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 3\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 4\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 5\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 6\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 7\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated.\n",
    "\n",
    "### Exercise 8\n",
    "> **TODO**\n",
    ">\n",
    "> This solution has not yet been written. Any help with a PR is appreciated."
   ],
   "metadata": {}
  }
 ],
 "nbformat_minor": 3,
 "metadata": {
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.11.4"
  },
  "kernelspec": {
   "name": "julia-1.11",
   "display_name": "Julia 1.11.4",
   "language": "julia"
  }
 },
 "nbformat": 4
}