Skip to content

Instantly share code, notes, and snippets.

@TAdeJong
Last active May 1, 2020 15:48
Show Gist options
  • Save TAdeJong/5c5ab28425e0518352b2524ca67af5be to your computer and use it in GitHub Desktop.
Save TAdeJong/5c5ab28425e0518352b2524ca67af5be to your computer and use it in GitHub Desktop.
A quick binder with a widget plot the moire peaks of two graphene lattices and an hBN lattice for different angles
numpy
matplotlib
ipywidgets
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1dc9365ab16d44feab5c1fa900baab61",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=1.9, continuous_update=False, description='theta_2', max=5.0, min=-5.0…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from ipywidgets import interactive\n",
"import ipywidgets as widgets\n",
"from itertools import combinations, permutations, product\n",
"\n",
"def colormix(c1, c2):\n",
" if 'red' in [c1,c2]:\n",
" if 'yellow' in [c1,c2]:\n",
" return 'orange'\n",
" elif 'green' in [c1,c2]:\n",
" return 'maroon'\n",
" elif 'purple':\n",
" return 'blue'\n",
" else:\n",
" return 'xkcd:lime'\n",
" \n",
"def rotate(vec, angle):\n",
" R = np.array([[np.cos(angle), -np.sin(angle)], \n",
" [np.sin(angle), np.cos(angle)]])\n",
" return R @ vec\n",
"\n",
"def plot_moire(theta_2, theta_hBN, ordertwo=True):\n",
" r_g = 1 / (0.5*np.sqrt(3)*0.246)\n",
" t1 = np.deg2rad(1)\n",
" k_g = r_g*np.array([np.cos(t1), np.sin(t1)]) \n",
" ks_g = np.stack([rotate(k_g, np.pi/3*i) for i in range(6)])\n",
" fig, ax = plt.subplots(figsize=[15,15])\n",
" t2 = np.deg2rad(theta_2)\n",
" k_g2 = r_g*np.array([np.cos(t1+t2), np.sin(t1+t2)]) \n",
" ks_g2 = np.stack([rotate(k_g2, np.pi/3*i) for i in range(6)])\n",
" r_hBN = 1 / (0.5*np.sqrt(3)*0.2504)\n",
" thBN = np.deg2rad(theta_hBN)\n",
" k_hBN = r_hBN*np.array([np.cos(t1+thBN), np.sin(t1+thBN)]) \n",
" ks_hBN = np.stack([rotate(k_hBN, np.pi/3*i) for i in range(6)])\n",
" ax.scatter(*ks_g.T, s=25, label='graphene 1', c='C0')\n",
" ax.scatter(*ks_g2.T, s=25, label='graphene 2', c='C1')\n",
" ax.scatter(*ks_hBN.T, s=25, label='hBN', c='C2')\n",
" vs = {'red': ks_g-ks_hBN,\n",
" 'yellow': ks_g-ks_g2,\n",
" 'green': ks_g2-ks_hBN\n",
" }\n",
" ax.scatter(*(vs['red']).T, s=75, \n",
" label=f'G1 - hBN ', \n",
" c='red', edgecolor='black')\n",
" ax.scatter(*(vs['yellow']).T, s=75, \n",
" label=f'G1 - G2 ', \n",
" c='yellow', edgecolor='black')\n",
" ax.scatter(*(vs['green']).T, s=75, \n",
" label=f'G2 - hBN', \n",
" c='green', edgecolor='black')\n",
" ax.scatter(0,0,c='C0', s=75)\n",
" if ordertwo:\n",
" for key, key2 in combinations(['yellow', 'green', 'red'], 2):\n",
" vdiffs = np.stack([k1 - k2 for k1,k2 in product(vs[key],vs[key2])])\n",
" plt.scatter(*vdiffs.T, color=colormix(key, key2), edgecolor='black', \n",
" alpha=0.3, s=300, label=key+'-'+key2)\n",
" ax.set_aspect('equal')\n",
" plt.gca().set_xlim([-0.28,0.28])\n",
" plt.gca().set_ylim([-0.28,0.28])\n",
" plt.legend()\n",
" \n",
"interactive(plot_moire, \n",
" theta_2=widgets.FloatSlider(value=1.9, min=-5, max=5,step=0.05, continuous_update=False),\n",
" theta_hBN=widgets.FloatSlider(value=0.9, min=-5, max=5,step=0.05, continuous_update=False)\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:pyL5]",
"language": "python",
"name": "conda-env-pyL5-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment