diff --git a/notebook/lattice-vibration/NGLTrajectoryClass.py b/notebook/lattice-vibration/NGLTrajectoryClass.py index 89b9daa..c44f385 100644 --- a/notebook/lattice-vibration/NGLTrajectoryClass.py +++ b/notebook/lattice-vibration/NGLTrajectoryClass.py @@ -1,5 +1,6 @@ import numpy as np import os +import logging import matplotlib.pyplot as plt import ipywidgets as widgets @@ -8,7 +9,9 @@ from ase.io.trajectory import Trajectory from sympy import * from NGLUtilsClass import NGLWidgets - +from ipywidgets import Output +import sys +sys.stdout = open('/dev/stdout', 'w') class NGLTrajectory(NGLWidgets): def __init__(self, trajectory): @@ -71,11 +74,13 @@ def __init__(self, trajectory): self.x = 0 self.y = 0 self.ka = 0 + #target_k=np.pi/2 self.ka_array = np.linspace(-2 * np.pi, 2 * np.pi, 101) self.idx = 50 # idx corresponding to ka=0 self.optic = False - self.init_delay = 20 + + def addArrows(self, *args): ''' @@ -428,6 +433,7 @@ def initialize_dispersion_plot(self, *args): # Selected frequency point (self.point,) = self.ax.plot([], [], ".", c="crimson", markersize=15) + self.point.set_data((np.pi/2, 1)) # default to non-trivial k-point self.ax.set_xlabel("k") self.ax.set_ylabel("$\omega$") @@ -458,6 +464,15 @@ def band_dispersion(self, *args): self.lines_op.set_data(([], [])) self.lines_ac_out.set_data(([], [])) self.lines_op_out.set_data(([], [])) + + # set default k point for monatomic chain + self.x=np.pi/2 + self.idx = (np.abs(self.ka_array - self.x)).argmin() + self.ka = self.ka_array[self.idx] + + w = self.w[self.idx] + self.point.set_data((self.ka, w)) + elif self.button_chain.value == "diatomic": # Reduce figure width to "half" the size, since x axis is half as big now. @@ -480,6 +495,13 @@ def band_dispersion(self, *args): # Remove monoatomic chain lines self.lines.set_data(([], [])) self.lines_out.set_data(([], [])) + + # set default k point for diatomic chain + self.x=np.pi/2 + self.idx = (np.abs(self.ka_array - self.x)).argmin() + self.ka = self.ka_array[self.idx] + w= self.w_ac[self.idx] + self.point.set_data((self.ka, w)) # First BZ limit self.ax.plot([-np.pi, -np.pi], [0, 2.2], "k--", linewidth=1) @@ -490,16 +512,24 @@ def band_dispersion(self, *args): # 2.2 works well for initial height self.ax.set_ybound(0, 2.2) - self.point.set_data((0, 0)) + # set default point on dispersion self.fig.canvas.mpl_connect("button_press_event", self.onclick) plt.ion() def onclick(self, event): + if event==None: + + self.x=0.0 + self.y=0.0 + else: + self.x = event.xdata + self.y = event.ydata + + """ Determine frequency and k point upon click on band dispersion figure """ - self.x = event.xdata - self.y = event.ydata + # Get the idx of the closest k in the array self.idx = (np.abs(self.ka_array - self.x)).argmin() diff --git a/notebook/lattice-vibration/NGLUtilsClass.py b/notebook/lattice-vibration/NGLUtilsClass.py index 243c7c7..6498d20 100644 --- a/notebook/lattice-vibration/NGLUtilsClass.py +++ b/notebook/lattice-vibration/NGLUtilsClass.py @@ -95,7 +95,7 @@ def __init__(self, trajectory) -> None: r"Arrow amplitude", layout=self.layout_description ) self.slider_amp_arrow = widgets.FloatSlider( - value=2.0, + value=0.5, min=0.1, max=5.01, step=0.1, diff --git a/notebook/lattice-vibration/Phonon_1D.ipynb b/notebook/lattice-vibration/Phonon_1D.ipynb index 718d32e..aa3d72f 100644 --- a/notebook/lattice-vibration/Phonon_1D.ipynb +++ b/notebook/lattice-vibration/Phonon_1D.ipynb @@ -8,13 +8,13 @@ "\n", " Go back to index\n", "\n", - "**Source code:** https://github.com/osscar-org/quantum-mechanics/blob/master/notebook/statistical-mechanics/ising_model.ipynb\n", + "**Source code:** https://github.com/osscar-org/quantum-mechanics/blob/master/notebook/lattice-vibration/Phonon_1D.ipynb\n", "\n", "
\n", "\n", "## **Tasks and exercises**\n", "\n", - "1. Visualize the lattice vibration at $k=0$ and $k=\\frac{\\pi}{a}$, what characteristics do you observe ? \n", + "1. Visualize the lattice vibration at $k=0$ and $k=\\frac{\\pi}{a}$. What characteristics do you observe ? \n", "
\n", " Solution\n", " The atom indexed $n$ has a displacement given by $u=\\exp(i(n\\cdot ka-\\omega t))$.\n", @@ -41,18 +41,18 @@ "3. Visualize the phonon dispersion for a diatomic chain. What is the difference between the acoustic and optical branch at $k=0$ and $k=\\pm\\frac{\\pi}{2a}$ ? How do the atoms displacements relate to frequency ?\n", "
\n", " Solution\n", - " At $k=0$, in the acoustic branch, as in the monoatomic chain, the atoms do not move. Instead, in the optical branch the two atoms are moving out-of-phase. The latter one has therefore a high frequency.\n", + " At $k=0$, in the acoustic branch, as in the monoatomic chain, the atoms do not move. Instead, in the optical branch the two atoms are moving out-of-phase. The latter therefore has a high frequency.\n", " \n", - " At $k=\\pm\\frac{\\pi}{2a}$, in the acoustic branch the light atoms stand still whearas the heavier atoms move. In the optical branch, the opposite is observed. Since frequency is proportional to $\\sqrt{1/M}$, the frequency is higher when lighter atoms move. \n", + " At $k=\\pm\\frac{\\pi}{2a}$, in the acoustic branch, the light atoms stand still whearas the heavier atoms move. In the optical branch, the opposite is observed. Since frequency is proportional to $\\sqrt{1/M}$, the frequency is higher when lighter atoms move. \n", "
\n", " \n", " \n", - "4. Adjust the mass ratio of the molecules. What happens when the ratio is high ? When the ratio is close to unity ? How does the band dispersion evolve ?\n", + "4. Adjust the mass ratio of the molecules. What happens when the ratio is high ? How about when the ratio is close to unity ? How does the band dispersion change ?\n", "
\n", " Solution\n", - " As the ratio grows, the forbidden frequencies gap grows as well. At $k=\\pm\\frac{\\pi}{2a}$, the gap is proportional to $\\sqrt{1/M_{gray}}-\\sqrt{1/M_{red}}$.\n", + " As the ratio grows, the gap of forbidden frequencies grows as well. At $k=\\pm\\frac{\\pi}{2a}$, the gap is proportional to $\\sqrt{1/M_{gray}}-\\sqrt{1/M_{red}}$.\n", " \n", - " Furthermore, at $k=0$, we have $\\frac{u_{red}}{u_{gray}}=\\frac{-M_{gray}}{M_{red}}$, and thus the heavier atom amplitude decreases as it gets heavier.\n", + " Furthermore, at $k=0$, we have $\\frac{u_{red}}{u_{gray}}=\\frac{-M_{gray}}{M_{red}}$, and thus the oscillation amplitude of the heavier atom decreases as it gets heavier.\n", "
\n", " \n", " \n", @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -80,17 +80,18 @@ "matplotlib.rcParams['figure.facecolor']='w'\n", "from ase.io.trajectory import Trajectory\n", "from ase import Atoms\n", + "import time\n", "\n", "from NGLTrajectoryClass import NGLTrajectory\n", "\n", "import ipywidgets as widgets\n", - "from ipywidgets import jslink,HBox, VBox, HTMLMath,Label,IntSlider, Tab,Play, Layout\n", + "from ipywidgets import jslink,HBox, VBox, HTMLMath,Label,IntSlider, Tab,Play, Layout, AppLayout\n", "from IPython.display import display" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -108,6 +109,10 @@ " }, false);\n", "\"\"\")\n", "\n", + "\n", + "view = handler.view \n", + "# view.on_displayed(loop)\n", + "\n", "widgets=[\n", " handler.slider_C,\n", " handler.slider_M,\n", @@ -118,8 +123,9 @@ " handler.widgetList.append(widget)\n", "\n", "handler.slider_atom_radius.value=0.1\n", - "handler.slider_arrow_radius.value=0.1\n", - "handler.tick_box_arrows.value=False\n", + "handler.slider_arrow_radius.value=0.05\n", + "handler.tick_box_arrows.value=True\n", + "handler.addArrows() # add arrows by default\n", "handler.set_player_parameters(delay=handler.init_delay)\n", "handler.set_view_parameters(clipDist=1,quality='low')\n", "\n", @@ -131,12 +137,15 @@ "handler.compute_dispersion()\n", "handler.compute_dispersion_relation()\n", "\n", - "handler.compute_trajectory_1D()\n", "handler.band_dispersion()\n", "\n", + "\n", "handler.set_view_dimensions()\n", "handler.view.stage.set_parameters(mouse_preset=\"pymol\")\n", "\n", + "handler.compute_trajectory_1D()\n", + "\n", + "\n", "\n", "chain= HBox([handler.button_chain_description,handler.button_chain])\n", "mass_ratio=handler.output_ratio\n", @@ -206,9 +215,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "%%html\n", "