From 4800cfe06070bc4751c86fc387e8f04e0880ef0f Mon Sep 17 00:00:00 2001
From: Anton Akhmerov <anton.akhmerov@gmail.com>
Date: Fri, 30 Mar 2018 14:20:25 +0200
Subject: [PATCH] add 3d plot to lecture 6

---
 .gitlab-ci.yml |  1 +
 code/nfem.py   | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lecture_6.md   |  8 +++--
 3 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 code/nfem.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index bedcb5e4..4d970c55 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -25,6 +25,7 @@ build and upload the contents:
   script:
     # Compile lectures
     - python code/band_structures.py
+    - python code/nfem.py
     - gitbook install
     - gitbook build
     - "rsync -rv _book/* solidstate@tnw-tn1.tudelft.net:"
diff --git a/code/nfem.py b/code/nfem.py
new file mode 100644
index 00000000..7570a968
--- /dev/null
+++ b/code/nfem.py
@@ -0,0 +1,79 @@
+import numpy as np
+from matplotlib import pyplot
+import plotly.offline as py
+import plotly.graph_objs as go
+
+
+pi = np.pi
+
+def E(k_x, k_y):
+    delta = np.array([-2*pi, 0, 2*pi])
+    H = np.diag(
+        ((k_x + delta)[:, np.newaxis]**2
+        + (k_y + delta)[np.newaxis]**2).flatten()
+    )
+    return tuple(np.linalg.eigvalsh(H + 5)[:3])
+
+E = np.vectorize(E, otypes=(float, float, float))
+
+momenta = np.linspace(-2*pi, 2*pi, 100)
+kx, ky = momenta[:, np.newaxis], momenta[np.newaxis, :]
+
+bands = E(kx, ky)
+
+# Extended Brillouin zone scheme
+pad = .3
+first_BZ = ((abs(kx) < pi + pad) & (abs(ky) < pi + pad))
+second_BZ = (
+    ((abs(kx) > pi - pad) | (abs(ky) > pi - pad))
+    & ((abs(kx + ky) < 2*pi + pad) & (abs(kx - ky) < 2*pi + pad))
+)
+third_BZ = (
+    (abs(kx + ky) > 2*pi - pad) | (abs(kx - ky) > 2*pi - pad)
+)
+
+bands[0][~first_BZ] = np.nan
+bands[1][~second_BZ] = np.nan
+#bands[2][~third_BZ] = np.nan
+
+# Actually plotting
+
+fig = go.Figure(
+    data = [
+        go.Surface(
+            z=band / 5,
+            colorscale=color,
+            opacity=opacity,
+            showscale=False,
+            hoverinfo=False,
+            x=momenta,
+            y=momenta,
+        )
+        for band, color, opacity 
+        in zip(bands[:2], 
+               ['#cf483d', '#3d88cf'], 
+               (1, 0.9))
+    ],
+    layout = go.Layout(
+        title='Nearly free electrons in 2D',
+        autosize=True,
+        width=500,
+        height=500,
+        hovermode=False,
+        scene=dict(
+            yaxis={"title": "k_y"},
+            xaxis={"title": "k_x"},
+            zaxis={"title": "E"},
+        )
+    )
+)
+
+out = rf"""
+<head>
+<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
+</head>
+<body>{py.plot(fig, show_link=False, output_type='div', include_plotlyjs=False)}
+</body>
+"""
+with open('figures/nfem_2d.html', 'w') as f:
+    f.write(out)
\ No newline at end of file
diff --git a/lecture_6.md b/lecture_6.md
index b7aa3c57..ae55ccd5 100644
--- a/lecture_6.md
+++ b/lecture_6.md
@@ -170,9 +170,11 @@ Sequence of steps (same procedure as in 1D, but harder because of the need to im
 2. Plot the free electron model Fermi surface and the Brillouin zones.
 3. Apply the perturbation where the Fermi surface crosses the Brillouin zone (due to avoided level crossings).
 
-![](figures/nearly_free_FS.svg)
+The resulting band structure looks like this (in the extended Brillouin zone scheme):
 
-If $V$ is sufficiently weak, the material can be conducting even with 2 electrons per unit cell!
+<iframe width="600", height="600" src="figures/nfem_2d.html" frameBorder="0" align="center"></iframe>
+
+Observe that the top of the first band is above the bottom of the lowest band. Therefore if $V$ is sufficiently weak, the material can be conducting even with 2 electrons per unit cell!
 
 A larger $V$ makes the Fermi surface more square-like and eventually makes the material insulating.
 
@@ -201,4 +203,4 @@ A joint adsorbtion of a photon and a phonon collision may excite an electron acr
 * If the lattice potential is weak, the dispersion can be obtained by copying $p^2/2m$ into different Brillouin zones, and opening gaps at every level crossing. Each gap is equal to the Fourier component of the lattice potential.
 * If the number of electrons per unit cell is odd, the material must be conducting.
 * Each band hosts $2N$ eletrons, therefore a material with odd number of electrons is a metal; that with an even number of electrons may be an insulator.
-* Light adsorption is a tool to measure the band gap, and it distinguishes **direct** from **indirect** band gaps.
\ No newline at end of file
+* Light adsorption is a tool to measure the band gap, and it distinguishes **direct** from **indirect** band gaps.
-- 
GitLab