{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7e31b322-df55-44ed-9f29-6f6efa71eafe",
   "metadata": {},
   "source": [
    "# Activity 1: Getting started\n",
    "\n",
    "MsSpec is a Fortran code with two components: Phagen (Written by R. Natoli) and Spec (written by D. Sébilleau). Phagen computes the phase shifts of the electronic wave propagating in the matter on a spherical harmonics basis. Spec uses those phase shifts to compute the multiple scattering process and simulate the intensity of different electronic spectroscopies.\n",
    "\n",
    "In the most recent version of MsSpec, the program is interfaced with python (https://msspec.cnrs.fr/), allowing for much more flexibility and interplay with other simulation techniques.\n",
    "\n",
    "## Building atomic systems\n",
    "\n",
    "MsSpec works in the *cluster* approach. Building such a cluster for a calculation is a fundamental step.\n",
    "We use the [python Atomic Simulation Environment (ASE)](https://wiki.fysik.dtu.dk/ase/) for this.\n",
    "\n",
    "ASE is a set of tools and Python modules for setting up, manipulating, running, visualizing and analyzing atomistic simulations.\n",
    "Building atomic systems, structures... is pretty straightforward:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "07a04c8c-a268-481a-8f1b-14878ed771b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\n",
       "    <head>\n",
       "        <title>ASE atomic visualization</title>\n",
       "        <link rel=\"stylesheet\" type=\"text/css\"             href=\"https://www.x3dom.org/release/x3dom.css\"></link>\n",
       "        <script type=\"text/javascript\"             src=\"https://www.x3dom.org/release/x3dom.js\"></script>\n",
       "    </head>\n",
       "    <body>\n",
       "        <X3D width=\"400px\"; height=\"300px\";>\n",
       "\n",
       "<!--Inserting Generated X3D Scene-->\n",
       "<scene>\n",
       "  <viewpoint position=\"0 0 3.052956\">\n",
       "    <group/>\n",
       "  </viewpoint>\n",
       "  <transform translation=\"-0.0 -0.0 -0.0\">\n",
       "    <group>\n",
       "      <group>\n",
       "        <transform translation=\"0 0 0\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"0 0 0\"/>\n",
       "            </appearance>\n",
       "            <lineset vertexCount=\"5\">\n",
       "              <coordinate point=\"0 0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0\"/>\n",
       "            </lineset>\n",
       "          </shape>\n",
       "        </transform>\n",
       "        <transform translation=\"0.0 0.0 0.0\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"0 0 0\"/>\n",
       "            </appearance>\n",
       "            <lineset vertexCount=\"5\">\n",
       "              <coordinate point=\"0 0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0\"/>\n",
       "            </lineset>\n",
       "          </shape>\n",
       "        </transform>\n",
       "        <transform translation=\"0 0 0\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"0 0 0\"/>\n",
       "            </appearance>\n",
       "            <lineset vertexCount=\"5\">\n",
       "              <coordinate point=\"0 0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0\"/>\n",
       "            </lineset>\n",
       "          </shape>\n",
       "        </transform>\n",
       "        <transform translation=\"0.0 0.0 0.0\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"0 0 0\"/>\n",
       "            </appearance>\n",
       "            <lineset vertexCount=\"5\">\n",
       "              <coordinate point=\"0 0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0\"/>\n",
       "            </lineset>\n",
       "          </shape>\n",
       "        </transform>\n",
       "      </group>\n",
       "      <group>\n",
       "        <transform translation=\"0.0 0.0 0.119262\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"1.0 0.051 0.051\"/>\n",
       "            </appearance>\n",
       "            <sphere radius=\"0.66\"/>\n",
       "          </shape>\n",
       "        </transform>\n",
       "        <transform translation=\"0.0 0.763239 -0.477047\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"1.0 1.0 1.0\"/>\n",
       "            </appearance>\n",
       "            <sphere radius=\"0.31\"/>\n",
       "          </shape>\n",
       "        </transform>\n",
       "        <transform translation=\"0.0 -0.763239 -0.477047\">\n",
       "          <shape>\n",
       "            <appearance>\n",
       "              <material diffuseColor=\"1.0 1.0 1.0\"/>\n",
       "            </appearance>\n",
       "            <sphere radius=\"0.31\"/>\n",
       "          </shape>\n",
       "        </transform>\n",
       "      </group>\n",
       "    </group>\n",
       "  </transform>\n",
       "</scene>\n",
       "<!--End of Inserted Scene-->\n",
       "\n",
       "        </X3D>\n",
       "    </body>\n",
       "</html>\n",
       "\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# To build a molecule with ASE\n",
    "from ase.build import molecule\n",
    "# To view\n",
    "from ase.visualize import view\n",
    "\n",
    "# Only for jupyter\n",
    "import nglview\n",
    "from functools import partial\n",
    "view = partial(view, viewer='x3d')\n",
    "\n",
    "# Create a water molecule\n",
    "water = molecule('H2O')\n",
    "# Display it\n",
    "view(water)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94b690a2-52f0-43e4-953d-6e7519ac4e9c",
   "metadata": {},
   "source": [
    "## Barebone script for MsSpec\n",
    "\n",
    "MsSpec can simulate different electronic spectroscopies like PED, AED, LEED, EXAFS, APECS and more will be included in the forthcoming version. However, it is really well-suited for PhotoElectron Diffraction simulation, and the python interface is only fully available for it at the moment. Since PED covers all the MsSpec features and concepts, we will focus on this technique.\n",
    "\n",
    "There are typically 3 steps to follow to get a result with MsSpec:\n",
    "\n",
    "1. Create a *cluster*\n",
    "2. Create an ASE *calculator*\n",
    "3. Run the simulation\n",
    "\n",
    "### PED polar scan for Cu(001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6ddd72a9-8f32-484f-9a3e-9ab3a85945a4",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " _________________________________________________________________\n",
      "\n",
      "                               PHAGEN\n",
      " _________________________________________________________________\n",
      "\n",
      " -----------------------------------------------------------------\n",
      "  parameters for this xpd calculation:\n",
      " -----------------------------------------------------------------\n",
      "  edge= l3\n",
      "  potype= hedin     norman= stdcrm    absorber=  1\n",
      "  coor= angs        emin=   23.25 Ry  emax=   23.25 Ry\n",
      "  delta=  0.300 Ry  gamma=  0.00  Ry  eftri=  0.000  Ry\n",
      "  cip=    0.00  Ry  lmaxt= 19         charelx: ex\n",
      "  ionization state : neutral\n",
      "  relativistic corrections of type: nr\n",
      "  final state potential generated internally\n",
      "\n",
      "\n",
      "  Computes the T-matrix and radial matrix elements                              \n",
      "\n",
      "\n",
      "  coordinates in angstroms                         Radii\n",
      " -----------------------------------------------------------------\n",
      "  Cu    29    7.2000    7.2000    2.7000    0.0000 0.0000\n",
      "  Cu    29    3.6000    7.2000    2.7000    0.0000 0.0000\n",
      "  Cu    29    3.6000    7.2000    6.3000    0.0000 0.0000\n",
      "  Cu    29    5.4000    5.4000    2.7000    0.0000 0.0000\n",
      "  Cu    29    7.2000    3.6000    2.7000    0.0000 0.0000\n",
      "  Cu    29    7.2000    5.4000    0.9000    0.0000 0.0000\n",
      "  Cu    29    5.4000    5.4000    6.3000    0.0000 0.0000\n",
      "  Cu    29    7.2000    3.6000    6.3000    0.0000 0.0000\n",
      "  Cu    29    7.2000    5.4000    4.5000    0.0000 0.0000\n",
      "  Cu    29    5.4000    7.2000    0.9000    0.0000 0.0000\n",
      "  Cu    29    5.4000    9.0000    2.7000    0.0000 0.0000\n",
      "  Cu    29    7.2000    9.0000    0.9000    0.0000 0.0000\n",
      "  Cu    29    5.4000    7.2000    4.5000    0.0000 0.0000\n",
      "  Cu    29    5.4000    9.0000    6.3000    0.0000 0.0000\n",
      "  Cu    29    7.2000    7.2000    6.3000    0.0000 0.0000\n",
      "  Cu    29    7.2000    9.0000    4.5000    0.0000 0.0000\n",
      "  Cu    29    7.2000   10.8000    2.7000    0.0000 0.0000\n",
      "  Cu    29    7.2000   10.8000    6.3000    0.0000 0.0000\n",
      "  Cu    29    9.0000    5.4000    2.7000    0.0000 0.0000\n",
      "  Cu    29    9.0000    5.4000    6.3000    0.0000 0.0000\n",
      "  Cu    29    9.0000    7.2000    0.9000    0.0000 0.0000\n",
      "  Cu    29    9.0000    9.0000    2.7000    0.0000 0.0000\n",
      "  Cu    29   10.8000    7.2000    2.7000    0.0000 0.0000\n",
      "  Cu    29    9.0000    7.2000    4.5000    0.0000 0.0000\n",
      "  Cu    29    9.0000    9.0000    6.3000    0.0000 0.0000\n",
      "  Cu    29   10.8000    7.2000    6.3000    0.0000 0.0000\n",
      " -----------------------------------------------------------------\n",
      "\n",
      "\n",
      "  ** enter calphas **\n",
      "                        ---\n",
      "  total energy for atom in ground state \n",
      "  total energy for atom with a hole in l3 edge\n",
      "  calculated ionization energy for edge l3 =    946.40898981930718       eV\n",
      "  energy distance between edges l2 and l3  =     20.447898479699866      eV\n",
      "                        ---\n",
      "  calculated ionization potential (ryd)    =   69.588894358327252     \n",
      "                        ---\n",
      "           \n",
      "           \n",
      "  symmetrizing coordinates... \n",
      "\n",
      "\n",
      "               symmetrized atomic coordinates of cluster  \n",
      "\n",
      "                                  position\n",
      "            atom    no.    x         y         z        eq\n",
      "\n",
      "           1  osph     0    0.0000    0.0000    0.0000     0\n",
      "           2  Cu      29    0.0000    0.0000   -2.3549     0\n",
      "           3  Cu      29   -6.8030    0.0000   -2.3549     0\n",
      "           4  Cu      29   -6.8030    0.0000    4.4481     0\n",
      "           5  Cu      29   -3.4015   -3.4015   -2.3549     0\n",
      "           6  Cu      29    0.0000   -6.8030   -2.3549     0\n",
      "           7  Cu      29    0.0000   -3.4015   -5.7564     0\n",
      "           8  Cu      29   -3.4015   -3.4015    4.4481     0\n",
      "           9  Cu      29    0.0000   -6.8030    4.4481     0\n",
      "          10  Cu      29    0.0000   -3.4015    1.0466     0\n",
      "          11  Cu      29   -3.4015    0.0000   -5.7564     0\n",
      "          12  Cu      29   -3.4015    3.4015   -2.3549     0\n",
      "          13  Cu      29    0.0000    3.4015   -5.7564     0\n",
      "          14  Cu      29   -3.4015    0.0000    1.0466     0\n",
      "          15  Cu      29   -3.4015    3.4015    4.4481     0\n",
      "          16  Cu      29    0.0000    0.0000    4.4481     0\n",
      "          17  Cu      29    0.0000    3.4015    1.0466     0\n",
      "          18  Cu      29    0.0000    6.8030   -2.3549     0\n",
      "          19  Cu      29    0.0000    6.8030    4.4481     0\n",
      "          20  Cu      29    3.4015   -3.4015   -2.3549     0\n",
      "          21  Cu      29    3.4015   -3.4015    4.4481     0\n",
      "          22  Cu      29    3.4015    0.0000   -5.7564     0\n",
      "          23  Cu      29    3.4015    3.4015   -2.3549     0\n",
      "          24  Cu      29    6.8030    0.0000   -2.3549     0\n",
      "          25  Cu      29    3.4015    0.0000    1.0466     0\n",
      "          26  Cu      29    3.4015    3.4015    4.4481     0\n",
      "          27  Cu      29    6.8030    0.0000    4.4481     0\n",
      "\n",
      "  computing muffin tin potential and phase shifts\n",
      "  generating core state wavefunction \n",
      "  generating final potential (complex hedin-lundqvist exchange)    \n",
      "  MT radii for Hydrogen atoms determined by stdcrm unless other options are specified\n",
      "\n",
      " -----------------------------------------------------------------\n",
      "  i    rs(i)   i=1,natoms      \n",
      "    1   10.61    2    2.39    3    2.48    4    2.48    5    2.33    6    2.48    7    2.42    8    2.33\n",
      "    9    2.48   10    2.34   11    2.42   12    2.43   13    2.41   14    2.34   15    2.33   16    2.33\n",
      "   17    2.41   18    2.48   19    2.48   20    2.43   21    2.33   22    2.41   23    2.43   24    2.48\n",
      "   25    2.41   26    2.48   27    2.48\n",
      "  N.B.: Order of atoms as reshuffled by symmetry routines \n",
      " -----------------------------------------------------------------\n",
      "\n",
      "  input value for coulomb interst. potential = -0.69999999999999996     \n",
      "  and interstitial rs =   3.0000000000000000     \n",
      "  lower bound for coulomb interst. potential = -0.42096854155426744     \n",
      "  and for interst. rs =   2.2601548755994800     \n",
      "\n",
      "  lmax assignment based on l_max = r_mt * k_e + 2\n",
      "  where e is the running energy\n",
      "  optimal lmax chosen according to the running energy e for each atom\n",
      "\n",
      "\n",
      "  number of centers=   26\n",
      "\n",
      "  starting potentials and/or charge densities written to file 13\n",
      "  symmetry information generated internally\n",
      "  symmetry information  written  to file 14\n",
      "\n",
      "\n",
      "  core initial state of type: 2p3/2\n",
      "\n",
      "  fermi level =  -0.18577\n",
      "\n",
      "\n",
      "  generating t_l (for030) and atomic cross section (for050)\n",
      " corewf: fnisx =   0.99897282652190389     \n",
      " writing atomic orbital energies\n",
      "  orbital energy  (Ryd   eV)  1s     -665.31473820425299       -9051.6067087119445     \n",
      "  orbital energy  (Ryd   eV)  2s     -83.344250979299673       -1133.8984964214017     \n",
      "  orbital energy  (Ryd   eV)  2p1/2  -72.640262391310984       -988.27073658171264     \n",
      "  orbital energy  (Ryd   eV)  2p3/2  -71.111924241975288       -967.47769675961820     \n",
      "  orbital energy  (Ryd   eV)  3s     -10.313080620773222       -140.30945712466604     \n",
      "  orbital energy  (Ryd   eV)  3p1/2  -6.8622342038346620       -93.360693201889049     \n",
      "  orbital energy  (Ryd   eV)  3p3/2  -6.6575123914750813       -90.575453038451158     \n",
      "  orbital energy  (Ryd   eV)  3d3/2 -0.97575422779613052       -13.275135822501518     \n",
      "  orbital energy  (Ryd   eV)  3d5/2 -0.94859035000087122       -12.905571277531651     \n",
      "  orbital energy  (Ryd   eV)  4s    -0.48955130473863673       -6.6603452768703502     \n",
      "\n",
      " using overlapped potential to search for core states of photoabsorber\n",
      "\n",
      "  calculating non relativistic core states\n",
      " ------------------------------\n",
      "  energy of core state =   -647.40974057864071       for orbital =1s   \n",
      "  n. of zeros found:           0  expected:            0\n",
      " ------------------------------\n",
      "  energy of core state =   -79.554273906250828       for orbital =2s   \n",
      "  n. of zeros found:           1  expected:            1\n",
      " ------------------------------\n",
      "  energy of core state =   -70.720428814458870       for orbital =2p1/2\n",
      "  n. of zeros found:           0  expected:            0\n",
      " ------------------------------\n",
      "  energy of core state =   -70.720428814476662       for orbital =2p3/2\n",
      "  n. of zeros found:           0  expected:            0\n",
      " ------------------------------\n",
      "  energy of core state =   -8.8984913351029302       for orbital =3s   \n",
      "  n. of zeros found:           2  expected:            2\n",
      " ------------------------------\n",
      "  energy of core state =   -6.0089160418603047       for orbital =3p1/2\n",
      "  n. of zeros found:           1  expected:            1\n",
      " ------------------------------\n",
      "  energy of core state =   -6.0089159791922135       for orbital =3p3/2\n",
      "  n. of zeros found:           1  expected:            1\n",
      "\n",
      "  calculating relativistic core states\n",
      "  energy of core state =   -656.39378974400631       for orb =1s   \n",
      "  n. of zeros found:           0  expected:            0\n",
      "  energy of core state =   -81.632060147320317       for orb =2s   \n",
      "  n. of zeros found:           1  expected:            1\n",
      "  energy of core state =   -72.635764622230667       for orb =2p1/2\n",
      "  n. of zeros found:           0  expected:            0\n",
      "  energy of core state =   -71.074395040994631       for orb =2p3/2\n",
      "  n. of zeros found:           0  expected:            0\n",
      "  energy of core state =   -9.2498851019868340       for orb =3s   \n",
      "  n. of zeros found:           2  expected:            2\n",
      "  energy of core state =   -6.2923189144601208       for orb =3p1/2\n",
      "  n. of zeros found:           1  expected:            1\n",
      "  energy of core state =   -6.0867535547385900       for orb =3p3/2\n",
      "  n. of zeros found:           1  expected:            1\n",
      " -------------------------------\n",
      "  density of the valence charge (au^{-3}   5.7214295960466316E-002\n",
      "  rs_v corresponding to valence density (au)   1.6099125416630322     \n",
      "  valence plasmon energy (in eV)    20.414212418262935     \n",
      "\n",
      "  gamma =  0.000000   rsint =  3.578556\n",
      "\n",
      "  check in subroutine cont\n",
      "  order of neighb. -- symb. -- dist. from absorber\n",
      "  \n",
      "          22 Cu         4.8104573684277954     \n",
      "          11 Cu         4.8104573684277971     \n",
      "          12 Cu         4.8104573684277971     \n",
      "          16 Cu         4.8104573684277971     \n",
      "          19 Cu         4.8104573684277971     \n",
      "          21 Cu         4.8104573684277971     \n",
      "          24 Cu         4.8104573684277971     \n",
      "           9 Cu         4.8104573684277989     \n",
      "          13 Cu         4.8104573684277989     \n",
      "           4 Cu         4.8104573684277998     \n",
      "          10 Cu         4.8104573684277998     \n",
      "           6 Cu         4.8104573684277998     \n",
      "          17 Cu         6.8030140516481801     \n",
      "          23 Cu         6.8030140516481801     \n",
      "           5 Cu         6.8030140516481818     \n",
      "           2 Cu         6.8030140516481818     \n",
      "          15 Cu         6.8030140516481818     \n",
      "          20 Cu         8.3319565697610241     \n",
      "          14 Cu         8.3319565697610258     \n",
      "          25 Cu         8.3319565697610241     \n",
      "           7 Cu         8.3319565697610258     \n",
      "          18 Cu         9.6209147368555961     \n",
      "          26 Cu         9.6209147368555961     \n",
      "           3 Cu         9.6209147368555978     \n",
      "           8 Cu         9.6209147368555978     \n",
      "  -----------------------------------------------------------------\n",
      "        1            Cu            0.000000\n",
      "        4            Cu            4.810457\n",
      "        6            Cu            4.810457\n",
      "        9            Cu            4.810457\n",
      "       10            Cu            4.810457\n",
      "       11            Cu            4.810457\n",
      "       12            Cu            4.810457\n",
      "       13            Cu            4.810457\n",
      "       16            Cu            4.810457\n",
      "       19            Cu            4.810457\n",
      "       21            Cu            4.810457\n",
      "       22            Cu            4.810457\n",
      "       24            Cu            4.810457\n",
      "        2            Cu            6.803014\n",
      "        5            Cu            6.803014\n",
      "       15            Cu            6.803014\n",
      "       17            Cu            6.803014\n",
      "       23            Cu            6.803014\n",
      "        7            Cu            8.331957\n",
      "       14            Cu            8.331957\n",
      "       20            Cu            8.331957\n",
      "       25            Cu            8.331957\n",
      "        3            Cu            9.620915\n",
      "        8            Cu            9.620915\n",
      "       18            Cu            9.620915\n",
      "       26            Cu            9.620915\n",
      "        1            Cu            0.000000\n",
      "       24            Cu            4.810457\n",
      "       23            Cu            6.803014\n",
      "       25            Cu            8.331957\n",
      "       26            Cu            9.620915\n",
      "   \n",
      "  irho =           2  entering vxc to calculate energy dependent exchange\n",
      "  energy dependent vcon =             (-0.19406005312524771,0.14609730856378345)  at energy   23.254999999999999     \n",
      "  check ionization potential:   69.588894358327252     \n",
      "   \n",
      "   \n",
      "  value of the mean free path:\n",
      " -----------------------------------------------------------------\n",
      "  average mean free path in the cluster     : mfp =  11.19104  angstrom at energy   23.25500\n",
      "\n",
      " -----------------------------------------------------------------\n",
      "   \n",
      "  calculating atomic t-matrix elements atm(n)\n",
      "  check orthogonality between core and continuum state\n",
      "  scalar product between core and continuum state =            (-0.56419733810139194,0.62474878945367607)\n",
      "  --- sqrt(xe/pi) =        (1.2415322757386615,-1.93379098468138516E-003)\n",
      "\n",
      "\n",
      "                ** phagen terminated normally ** \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from ase.io import read\n",
    "from msspec.calculator import MSSPEC\n",
    "\n",
    "\n",
    "cluster = read('copper.cif')\n",
    "# view the cluster\n",
    "cluster.edit()\n",
    "\n",
    "# The \"emitter\" atom is located in the middle of the 3rd plane\n",
    "cluster.emitter = 10\n",
    "\n",
    "# Create a \"calculator\"\n",
    "calc = MSSPEC(spectroscopy='PED', algorithm='inversion', txt='msspec.log')\n",
    "calc.set_atoms(cluster)\n",
    "\n",
    "data = calc.get_theta_scan(level='2p3/2')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "385f2556-852c-415c-8350-6fe1cfc897c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the result with the interactive GUI\n",
    "data.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ef1be6d9-091e-4be1-90b6-d57e58a3950a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 63528 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot using matplotlib directly\n",
    "data[0].views[0].plot();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85c7dcac-9f0e-4a56-8077-c96f89865dcb",
   "metadata": {},
   "source": [
    "## Shaping a cluster\n",
    "\n",
    "Based on the previous *.cif file, create a new cluster without the deepest plane and run the same calculation for the same emitter\n",
    "\n",
    "> <i class=\"fa fa-info-circle\"></i> **Note**:\n",
    "> As the cluster will contain fewer atoms, the emitter index will be different\n",
    "\n",
    "```{note}\n",
    "This is a note\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "What do you conclude ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ca106e8e-cc03-4bd3-a67a-9f2a7b0025ec",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "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"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 63084 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Unfold to see the answer\n",
    "# Edit the previous cluster\n",
    "# cluster = read('./UK_School/copper_3planes.cif')\n",
    "# cluster.edit()\n",
    "# Emitter index is now n°8\n",
    "\n",
    "cluster = read('./UK_School/copper_3planes.cif')\n",
    "cluster.emitter = 8\n",
    "\n",
    "calc = MSSPEC(spectroscopy='PED', algorithm='inversion', txt='msspec.log')\n",
    "calc.set_atoms(cluster)\n",
    "\n",
    "data = calc.get_theta_scan(level='2p3/2')\n",
    "data[0].views[0].plot();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc21cc1b-384f-4df0-861a-eddb5d8a7b49",
   "metadata": {},
   "source": [
    "The number of atoms used for the calculation greatly impact the calculation time and memory. Most of the time, a cluster is shaped as an hemisphere to minimize the number of atoms to take into account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f7ecc1d0-097d-4199-822a-c564e9e94337",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "461829b650494b99acf1efd720761abf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(NGLWidget(), VBox(children=(Dropdown(description='Show', options=('All', 'Cu'), value='All'), D…"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from msspec.utils import hemispherical_cluster, get_atom_index\n",
    "from ase.build import bulk\n",
    "\n",
    "copper = bulk('Cu', cubic=True)\n",
    "cluster = hemispherical_cluster(copper, planes=3, emitter_plane=2)\n",
    "cluster.emitter = get_atom_index(cluster, 0,0,0)\n",
    "\n",
    "view(cluster)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1f83a3b5-1248-482d-9686-488acbc70021",
   "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"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 60568 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "calc = MSSPEC(spectroscopy='PED', algorithm='inversion', txt='msspec.log')\n",
    "calc.set_atoms(cluster)\n",
    "\n",
    "data = calc.get_theta_scan(level='2p3/2');\n",
    "data[0].views[0].plot();"
   ]
  }
 ],
 "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
}
