{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "42cfa8b1-20d2-47e4-a1f0-161c4517df2c",
   "metadata": {},
   "source": [
    "(path-filtering)=\n",
    "# Activity 7: Large clusters and path filtering"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f802e59-3ebc-4c5d-a034-42a706044d87",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "So far you have seen that multiple scattering calculations with MsSpec can use two different algorithms: *matrix inversion* or *Rehr Albers series expansion*. When matrix inversion becomes impossible because the kinetic energy is too high and the number of atoms is too large, serial expansion is the alternative. This algorithm requires very little memory but it processes the scattering paths sequentially, which can lead to very long calculation times.\n",
    "\n",
    "In this activity, we will explore how to configure MsSpec to reduce this calculation time to compute the signal from a deep emitter in Si(001).\n",
    "\n",
    "\n",
    "\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a2f7c01-3a7e-46d4-90aa-6ba2eada6337",
   "metadata": {},
   "source": [
    "## The number of scattering paths\n",
    "\n",
    "To fix the idea, we will first evaluate how many scattering paths we need to compute for an emitter in the subsurface (2{sup}`nd` plane) or in the bulk (7{sup}`th` plane) of a Si(001) cluster."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f78af28-335e-4f6b-9b98-929f9e6965f8",
   "metadata": {},
   "source": [
    "::::{tab-set}\n",
    "\n",
    ":::{tab-item} <i class=\"fa-solid fa-circle-question\"></i> Quiz\n",
    "Create a cluster of Si(001) with the emitter in the subsurface, 40 Å diameter with 2 planes (since atoms below the emitter can be ignored at high kinetic energies).\n",
    "\n",
    "1. For an emitter in the subsurface, we can use single scattering (see {ref}`forward-scattering`). How many paths would be generated for this calculation ?\n",
    "\n",
    "2. Same question for an emitter in the 7{sup}`th` plane. If we were able to treat each scattering path within only 1 µs. How long would be such calculation ?\n",
    "\n",
    "```{note}\n",
    "Remember that \n",
    "1. for an emitter in plane $p$, the scattering order has to be at least the number of planes `above` the emitter\n",
    "2. The number of scattering paths of order $n$ corresponds to the number of possibilities of arranging up to $n$ atoms (taking order into account).\n",
    "```\n",
    "\n",
    "\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a83ee1b8-dc25-4db9-a3bd-c5ba8443f758",
   "metadata": {},
   "source": [
    ":::{toggle}\n",
    "\n",
    "To get the total number of paths generated by a cluster of $N$ atoms up to order $M$, use the following formula:\n",
    "\n",
    "```{math}\n",
    ":label: eq-nbpaths\n",
    "\\sum_{i=0}^{i=M} (N-1)^i\n",
    "```\n",
    "\n",
    ":::{figure-md} nbpaths-fig\n",
    "<img src=\"fig1.jpg\" alt=\"path filtering\" width=\"600px\" align=\"center\">\n",
    "\n",
    "The time for computing all scattering path for increasing cluster size and scattering order (up to 6{sup}`th` order with 739 atoms. (One path is assumed to be calculated within 1 µs)\n",
    ":::\n",
    "\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5de975f5-8f3d-432e-bfcc-8455bc50a862",
   "metadata": {},
   "source": [
    "## Paths filtering in MsSpec\n",
    "\n",
    "As you may expect, not all paths contribute significantly to the total intensity. This is why we can filter out some scattering paths and drastically reduce the computation time. MsSpec offers several filters for this. The 3 most common filters are:\n",
    "1. the `forward_scattering` filter which allows all paths where each scattering angle is within a cone of defined aperture\n",
    "2. the `backward_scattering` filter which is similar to the previous one but for backscattering direction\n",
    "3. the `distance` filter which rejects all paths longer than a threshold distance\n",
    "\n",
    "The following figure illustrate the effect of theses filters on scattering paths\n",
    "\n",
    ":::{figure-md} filters-fig\n",
    "<img src=\"filters.jpg\" alt=\"path filtering\" width=\"600px\" align=\"center\">\n",
    "\n",
    "Some examples of scattering paths with `forward_scattering`, `backward_scattering` and `distance` filters selected. The accepted forward angle is 45°, the accepted backscattering angle is 20° and the threshold distance is $6a_0$ where $a_0$ is the lattice parameter. Note that the yellow path is rejected but if the `off_cone_events` option is set to a value > 1, then it could have been accepted.\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28aae2f7-2af9-4630-b89d-ab634725ad79",
   "metadata": {},
   "source": [
    "## Application to a deep plane in a Si(001) sample\n",
    "\n",
    "The following script will compute the contribution of a Si(2p) atom in the 4{sup}`th` plane of a Si(001) cluster at scattering order 3.\n",
    "\n",
    "Taking into account all scattering paths took 15 minutes to compute.\n",
    "\n",
    "(msd-paper)=\n",
    ":::{seealso}\n",
    "based on this paper from S. Tricot *et al.*\n",
    "[J. Electron. Spectrosc. Relat. Phenom. **256** 147176 (2022)](https://doi.org/10.1016/j.elspec.2022.147176)\n",
    ":::\n",
    "\n",
    "\n",
    "::::{tab-set}\n",
    "\n",
    ":::{tab-item} <i class=\"fa-solid fa-circle-question\"></i> Quiz\n",
    "\n",
    "The following script is almost completed, try to define path filtering options (no backscattering, accept all paths with forward angles < 40° and reject paths longer than the diameter of the cluster).\n",
    "\n",
    "```{literalinclude} Si001.py\n",
    ":lineno-match:\n",
    ":emphasize-lines: 37-41\n",
    "```\n",
    "\n",
    "1. How long was your calculation ?\n",
    "2. How does it compare to the calculation with **all** scattering paths up to order 3 ?\n",
    "3. What is the proportion of scattering paths of order 3 that were actually taken into account ?\n",
    "\n",
    ":::\n",
    "\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19fbd486-b0c1-450c-a00d-79984945aefd",
   "metadata": {},
   "source": [
    "```{toggle}\n",
    "The calculation took few seconds and the result is very close to the calculation with all scattering paths.\n",
    "\n",
    "Only 0.01% of 3{sup}`rd` order paths were actually taken into account\n",
    "\n",
    ":::{figure-md} si-fig\n",
    "<img src=\"results.png\" alt=\"Si polar scan\" width=\"600px\" align=\"center\">\n",
    "\n",
    "Si(2p) polar scan (contribution of an emitter in the 4{sup}`th` plane with all 7 114 945 scattering paths taken into account (orange curve), and for only 1525 filtered paths (blue curve).\n",
    "\n",
    ":::\n",
    "\n",
    ":::{literalinclude} Si001_completed.py\n",
    ":lineno-match:\n",
    ":emphasize-lines: 37-41\n",
    ":::\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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
