{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "88b65284-bdd1-4140-af28-526e77b9b4b6",
   "metadata": {},
   "source": [
    "# Activity 2: Setting up the \"experiment\"\n",
    "\n",
    "To model a spectroscopy experiment, some parameters need to be correctly defined. For example the source direction with respect to the sample surface is important even if the light is not polarized. You can access those parameters in the \"source_parameters\" attribute of your calculator object.\n",
    "\n",
    "Other parameters are material specific. For example the inner potential. It is usually between 10 and 20 eV for most materials. This potential will add to the photoelectron kinetic energy inside the material. When the photoelectron escapes the sample, this internal potential is missing and this will create an energy step that will act as a refraction for the photoelectron intensity. The effect will be significant for large polar angles and for small kinetic energy of the photoelectron.\n",
    "\n",
    "## Sb-induced smooth growth of Ag on Ag(111) example\n",
    "Let's look at the effect of those parameters on the following example. This example is based on ().\n",
    "The idea is to use low energy photoelectron diffraction to see the substitution of Ag by Sb atoms on the surface plane.\n",
    "\n",
    "Let's start by building the cluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0b0a420f-7074-443b-8cb4-1f609f5b123e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6fc0bcb7f39e4206b1c24637f021003f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(NGLWidget(), VBox(children=(Dropdown(description='Show', options=('All', 'Sb', 'Ag'), value='Al…"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from ase.build import bulk\n",
    "from ase.visualize import view\n",
    "\n",
    "from msspec.calculator import MSSPEC\n",
    "from msspec.utils import hemispherical_cluster, get_atom_index, cut_plane\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "# Only for jupyter\n",
    "import nglview\n",
    "from functools import partial\n",
    "view = partial(view, viewer='ngl')\n",
    "\n",
    "# Create the silver cell\n",
    "Ag = bulk('Ag', cubic=True)\n",
    "# Orientate the cell in the [111] direction\n",
    "Ag.rotate((1,1,1), (0,0,1), rotate_cell=True)\n",
    "# Align the azimuth to match experimental reference\n",
    "Ag.rotate(15, 'z', rotate_cell=True)\n",
    "\n",
    "# Create a cluster\n",
    "cluster = hemispherical_cluster(Ag, diameter=20, emitter_plane=0)\n",
    "cluster = cut_plane(cluster, z=-4.8)\n",
    "cluster.emitter = get_atom_index(cluster, 0,0,0)\n",
    "cluster[cluster.emitter].symbol = 'Sb'\n",
    "\n",
    "view(cluster)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae74eedc-5ea4-4782-a49a-dcc2feac6c7c",
   "metadata": {},
   "source": [
    "Now create a calculator and configure experimental parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "242638d5-aea4-46cb-8d26-061c695cbb3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "'NoneType' object has no attribute 'upper'\n",
      "'integrals' is ignored since the 'spinpol' global parameter is set to False. Enable spin polarization in the constructor of your Calculator if you want to use this option.\n",
      "The sample temperature was set, but will be ignored since 'use_debye_model' parameter is False.\n",
      "The sample Debye temperature was set, but will be ignored since 'use_debye_model' parameter is False.\n"
     ]
    }
   ],
   "source": [
    "calc = MSSPEC(spectroscopy='PED', algorithm='inversion', txt='msspec.log')\n",
    "calc.set_atoms(cluster)\n",
    "\n",
    "calc.source_parameters.theta = 0\n",
    "calc.source_parameters.phi = 0\n",
    "\n",
    "calc.detector_parameters.angular_acceptance = 1\n",
    "calc.detector_parameters.average_sampling = 'low'\n",
    "\n",
    "calc.muffintin_parameters.interstitial_potential = 0\n",
    "data = calc.get_phi_scan(level='4d', theta=40, phi=np.linspace(0,240,121), kinetic_energy=45)\n",
    "\n",
    "# normalize data between [0,1]\n",
    "dset = data[0]\n",
    "dset.cross_section -= dset.cross_section.min()\n",
    "dset.cross_section /= dset.cross_section.max()\n",
    "\n",
    "# Add experimental data points in the dataset\n",
    "x, y = np.loadtxt('data.txt').T\n",
    "dset.add_columns(experiment=y)\n",
    "\n",
    "# Add points to view\n",
    "view = dset.views[0]\n",
    "view.select('phi', 'experiment', legend='Exp. data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bfedff51-12cc-43c0-b6ab-8e17038d271e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 87060 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data[0].views[0].plot();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0332a072-90b0-4d3e-8ee9-06cd16bb2608",
   "metadata": {},
   "source": [
    "Try to change the source direction and the inner potential of Ag to better match the experiment...\n",
    "\n",
    "> <i class=\"fa fa-info-circle\"></i> **Note**:\n",
    "> The cluster is smaller than it should for size convergence, but the calculation would take too much memory for this example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9dc38bd9-8788-40fc-b0d3-448b341a0d51",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "# Unfold to see the answer\n",
    "\n",
    "# value hidden in refernce [] of the paper...\n",
    "# The former SA73 Beamline in LURE\n",
    "calc.source_parameters.theta = 22.5\n",
    "\n",
    "# Inner potentials are usually between 10 and 20 V. For Ag\n",
    "calc.muffintin_parameters.interstitial_potential = 10.2"
   ]
  }
 ],
 "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
}
