{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0a860db-0f72-4785-81f4-831e48b3a49f",
   "metadata": {},
   "source": [
    "# Activity 6: Effect of the temperature\n",
    "\n",
    "\n",
    "## Surface and bulk effects of the temperature\n",
    "\n",
    "In this example, we will look at the effects of the temperature on the X-Ray photoelectron diffraction from a copper substrate. We will base our python script on a paper published in 1986 by R. Trehan and S. Fadley. In their work, they performed azimutal scans of a copper(001) surface at 2 different polar angles: one at grazing incidence and one at 45° for incresing temperatures from 298K to roughly 1000K.\n",
    "\n",
    "For each azimutal scan, they looked at the anisotropy of the signal, that is:\n",
    "\n",
    "```{math}\n",
    ":label: eq-anisotropy\n",
    "\\frac{I_{max} - I_{min}}{I_{max}}=\\frac{\\Delta I}{I_{max}}\n",
    "```\n",
    "\n",
    "This value is representative of how clear are the *modulations* of the signal.  They also proposed single scattering calculations that reproduced well these results.\n",
    "\n",
    "We will reproduce this kind of calculations to introduce the parameters that control the vibrational damping.\n",
    "\n",
    "(msd-paper)=\n",
    ":::{seealso}\n",
    "based on this paper from R. Trehan and C.S. Fadley\n",
    "[Phys. Rev. B **34** p6784–98 (2012)](https://doi.org/10.1103/PhysRevB.34.6784)\n",
    ":::\n",
    "\n",
    "### The script\n",
    "\n",
    "Since we want to distinguish between bulk (low polar angles, $\\theta = 45°$ in the paper) and surface effects (large polar angles, $\\theta = 83°$ in the paper), we need to compute scans for different emitters and different depths in the cluster.\n",
    "\n",
    "The script contains 3 functions:\n",
    "1. The `create_clusters` function will build clusters with increasing number of planes, with the emitter being in the deepest plane for each cluster.\n",
    "2. The function `compute` will compute the azimuthal scan for a given cluster.\n",
    "3. The `analysis` function will sum the intensity from each plane for a given temperature. This will be the *substrate total signal* (more on this in the {ref}`path-filtering` section). The function will also compute the anisotropy (equation {eq}`eq-anisotropy`).\n",
    "\n",
    "MsSpec can introduce temperature effects in two ways: either by using the Debye Waller model or by doing an average over the phase shifts. We will explore the latter in this example.\n",
    "\n",
    "Regardless of the option chosen to model temperature effects, we need to define mean square displacement (MSD) values of the atoms in our cluster. We will use the Debye model for this. The MSD is defined as follows:\n",
    "\n",
    ":::{math}\n",
    ":label: eq-debye\n",
    "<U_j^2(T)> = \\frac{3\\hbar^2 T}{M K_B \\Theta_D^2}\n",
    ":::\n",
    "\n",
    "where $\\hbar$ is the reduce Planck's constant, $T$ is the temperature, $M$ is the mass of the atom, $k_B$ is the Boltzmann's constant and $\\Theta_D$ is the Debye temperature of the sample.\n",
    "\n",
    "To get an idea of the typical order of magnitude for MSD, the figure below plots the values of MSD for copper for temperatures ranging from 300 K to 1000 K.\n",
    "\n",
    ":::{figure-md} msd-fig\n",
    "<img src=\"msd.jpg\" alt=\"Cu MSD\" width=\"600px\" align=\"center\">\n",
    "\n",
    "Variation of MSD for copper versus temperature using equation {eq}`eq-debye`\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69b8c17d-ab66-4092-a492-005f05d80495",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "::::{tab-set}\n",
    "\n",
    ":::{tab-item} <i class=\"fa-solid fa-circle-question\"></i> Quiz\n",
    "With the help of the [MsSpec documentation](https://msspec.cnrs.fr/parameters.html) and the second paragraph p6791 of the [article cited above](#msd-paper),\n",
    "complete the hilighted lines in the following script to compute the anisotropy of Cu(2p) $\\phi$-scans for polar angle $\\theta$=45° and 83°.\n",
    "\n",
    "How is varying the anisotropy versus the temperature. How can you qualitatively explain this variation ?\n",
    "\n",
    "```{literalinclude} Cu_temperature.py\n",
    ":lineno-match:\n",
    ":emphasize-lines: 40-41,44-48\n",
    "```\n",
    "\n",
    ":::\n",
    "\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dbafb54-cdb8-43fb-ba9e-ec1696e39731",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "```{toggle}\n",
    "\n",
    ":::{literalinclude} Cu_temperature_completed.py\n",
    ":lineno-match:\n",
    ":emphasize-lines: 40-41,44-48\n",
    ":::\n",
    "\n",
    ":::{figure-md} anisotropy-fig\n",
    "<img src=\"anisotropies.png\" alt=\"anisotropies\" width=\"600px\" align=\"center\">\n",
    "\n",
    "Variation of anisotropy as a function of temperature and polar angle for Cu(2p).\n",
    ":::\n",
    "\n",
    "The anisotropy decreases when the temperature is increased due to the increased disorder in the structure coming from thermal agitation. This variation in anisotropy is more pronounced for grazing incidence angles since surface atoms are expected to vibrate more than bulk ones and the expected mean depth of no-loss emission is $\\sim 1$ atomic layer at $\\theta = 83°$ and 3-4 layers at $\\theta = 45°$ as estimated by $\\Lambda_e\\sin\\theta$ (where $\\Lambda_e$ is the photoelectron mean free path at 560 eV).\n",
    "\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}