==========================
Simulating an XAS spectrum
==========================

First we must create a core hole setup.  This can be done with the
:program:`gpaw-setup` command::

    gpaw-setup -f PBE N --name hch1s --core-hole=1s,0.5

or you can write a small script to do it:

.. literalinclude:: setups.py

Set the location of setups as described here:
:ref:`installation of paw datasets`.


Spectrum calculation using unoccupied states
============================================

1s Transition potential
-----------------------

We do a "ground state" calculation with a core hole. Use a lot of
unoccupied states.

.. literalinclude:: run.py

To get the absolute energy scale we do a Delta Kohn-Sham calculation
where we compute the total energy difference between the ground state
and the first core excited state. The excited state should be spin
polarized and to fix the occupation to a spin up core hole and an
electron in the lowest unoccupied spin up state (singlet) we must set
the magnetic moment to one on the atom with the hole and fix the total
magnetic moment with ``occupations=FermiDirac(0.0, fixmagmom=True)``:

.. literalinclude:: dks.py


Plot the spectrum:

.. literalinclude:: plot.py


.. figure:: xas_h2o_spectrum.png
   :width: 400 px

2p Transition potential
-----------------------
The 2p transitions are calculated similar to the 1s
spectrum, involving a ground state calculation that includes a core hole.
The dipole matrix elements can be written out, allowing
to restart the XAS calculation without using the calculator object.
These files are much smaller than `.gpw` files.

.. literalinclude:: run_2p.py

The 2p calculations require to take spin-orbit
splitting into account. This is due to the non-zero angular momentum
of the initial state. The spin-orbit splitting can be determined
experimentally or calculated theoretically; we have utilized
the experimental value here.

.. literalinclude:: plot_2p.py

.. figure:: xas_h2s_spectrum.png
   :width: 400 px

There are some limitations to using this method for 2p transitions, as 
𝑙≠0, and it is based on the single-particle picture.
This is particularly relevant for transition metals as Ti [Joh25]_.

Haydock recursion method
========================

For systems in the condensed phase it is much more efficient to use the Haydock
recursion method to calculate the spectrum, thus avoiding to determine
many unoccupied states. First we do a core hole calculation with
enough k-points to converge the ground state density. Then we compute
the recursion coefficients with a denser k-point mesh to converge the
uncoccupied DOS. A Delta Kohn-Sham calculation can be done for the
gamma point, and the shift is made so that the first unoccupied
eigenvalue at the gamma point ends up at the computed total energy
difference.


.. literalinclude:: diamond1.py

Compute recursion coefficients:

.. literalinclude:: diamond2.py

Compute the spectrum with the get_spectra method. delta is the HWHM
(should we change it to FWHM???) width of the lorentzian broadening,
and fwhm is the FWHM of the Gaussian broadening.

::

  sys.setrecursionlimit(10000)

  name='diamond333_hch_600_1400a.rec'
  x_start=-20
  x_end=100
  dx=0.01
  x_rec = x_start + npy.arange(0, x_end - x_start ,dx)

  r = RecursionMethod(filename=name)
  y = r.get_spectra(x_rec, delta=0.4, fwhm=0.4 )
  y2 = sum(y)

  p.plot(x_rec + 273.44,y2)
  p.show()


Below the calculated spectrum of Diamond with half and full core holes
are shown along with the experimental spectrum.

.. figure:: h2o_xas_3.png
   :width: 400 px

.. figure:: h2o_xas_4.png
   :width: 400 px

XES
===

To compute XES, first do a ground state calcualtion with an 0.0 core
hole (an 'xes1s' setup as created above ). The system will not be
charged so the setups can be placed on all atoms one wants to
calculate XES for. Since XES probes the occupied states no unoccupied
states need to be determined. Calculate the spectrum with

::

  xas = XAS(calc, mode='xes', center=n)

Where n is the number of the atom in the atoms object, the center
keyword is only necessary if there are more than one xes setup. The spectrum
can be shifted by putting the first transition to the fermi level, or to
compute the total energy diffrence between the core hole state and the state
with a valence hole in HOMO.


Further considerations
======================

For XAS: Gridspacing can be set to the default value. The shape of the
spectrum is quite insensitive to the functional used, the DKS shifts
are more sensitive. The absolute energy position can be shifted so
that the calculated XPS energy matches the expreimental value
[Leetmaa2006]. Use a large box, see convergence with box size for a
water molecule below:

.. literalinclude:: h2o_xas_box1.py

and plot it

.. literalinclude:: h2o_xas_box2.py

.. figure:: h2o_xas_box.png
        :width: 550 px


.. [Nil04] *Chemical bonding on surfaces probed by X-ray emission
   spectroscopy and density functional theory*, A. Nilsson and
   L. G. M. Pettersson, Surf. Sci. Rep. 55 (2004) 49-167

.. [Joh25] *Explicit core-hole single-particle methods for L- and M- edge X-ray
   absorption and electron energy-loss spectra*
   E. A. B. Johnsen,  N. Horiuchi, T. Susi, and M. Walter (2025).  arXiv. https://arxiv.org/abs/2504.08458
