<jupyter_start><jupyter_text>Tutorial: Small Angle X-ray Scattering Small Angle X-ray Scattering (SAXS) is a reciprocal space technique that can be used to investigate magnetic structures on mesoscopic length scales. It uses the effect of magnetic circular dichroism, in which left and right circularly polarised light are absorbed differently in the presence of a magnetic field parallel to the propagation direction of the light. Due to this effect, only magnetisation parallel the beam direction is able to scatter X-rays. The wavelengths of the X-rays can be tuned to the absorption edges of the different elements present in the sample allowing for element-specific magnetisation to be measured. However, in calculating SAXS from micromagnetic simulations we can use the approximation that the cross sections are non element specific.The differential scattering cross section can be given by\begin{equation}\frac{d\Sigma}{d\Omega} \propto \left\vert \widetilde{M_z} \right\vert^2\end{equation}where $\widetilde{M_z}$ is the Fourier transform of the $z$ component of the magnetisation Ref. \[[<NAME>](https://onlinelibrary.wiley.com/iucr/doi/10.1107/S0909049598005019)\].  SAXS Reference frame For `mag2exp` the SAXS experimental reference frame is defined with the beam propagating in the $z$ direction.  The micromagnetic simulation A micromagnetic simulation can be set up using Ubermag to obtain a 3-dimensional magnetic structure with periodic boundary conditions in the $xy$ plane.<jupyter_code>%matplotlib inline
import oommfc as oc
import discretisedfield as df
import micromagneticmodel as mm
import numpy as np
import ubermagutil.units as uu
np.random.seed(1)

region = df.Region(p1=(-300e-9, -150e-9, 0), p2=(300e-9, 300e-9, 5e-9))
mesh = df.Mesh(region=region, cell=(5e-9, 5e-9, 5e-9), bc='xy')

system = mm.System(name='Box2')

system.energy = (mm.Exchange(A=1.6e-11)
                 + mm.DMI(D=4e-3, crystalclass='T') 
                 + mm.Zeeman(H=(0, 0, 2e5)))

Ms = 1.1e6

def m_fun(pos):
    return 2 * np.random.rand(3) - 1

# create system with above geometry and initial magnetisation
system.m = df.Field(mesh, dim=3, value=m_fun, norm=Ms)
system.m.plane('z').mpl()
# NBVAL_IGNORE_OUTPUT
# minimize the energy
md = oc.MinDriver()
md.drive(system)

# Plot relaxed configuration: vectors in z-plane
system.m.plane('z').mpl()<jupyter_output>Running OOMMF (DockerOOMMFRunner) [2021/09/09 11:52]... (145.2 s)<jupyter_text>Now we have a magnetisation texture we can compute the SAXS scattering cross sections.  Computing SAXS Cross-sections We can use the `mag2exp` package to calculate the cross section\begin{equation}\frac{d\Sigma}{d\Omega} \propto \left\vert \widetilde{M_z} \right\vert^2 .\end{equation}<jupyter_code>import mag2exp
cross_section = mag2exp.x_ray.saxs(system.m)<jupyter_output><empty_output><jupyter_text>As the cross section is a `discretisedfield` object the built-in plotting functions can be used to view them.NOTE: The values of the axis in Fourier space are frequency NOT angular frequency so DO NOT include a factor of $2\pi$. i.e. $|{\bf k}| = \frac{1}{\lambda} \neq \frac{2 \pi}{\lambda}$, where $\bf k$ is the wave vector and $\lambda$ is the wavelength.<jupyter_code>cross_section.mpl.scalar(cmap='gray', interpolation='spline16',
                         colorbar_label=r'Intensity')<jupyter_output><empty_output><jupyter_text>Due to the high intensity of some areas a linear colour map make some features more difficult to see. We can use `matplotlib.color` to change the colour bar to a logarithmic scale. This reveals the higher order low intensity diffraction peaks.<jupyter_code>import matplotlib.colors as colors
cross_section.mpl.scalar(cmap='gray',
                         colorbar_label=r'Intensity', norm=colors.LogNorm(vmin=1e40,vmax=cross_section.real.array.max()))<jupyter_output><empty_output><jupyter_text>It is also possible to just plot a selected region of the cross section by<jupyter_code>saxs_region = df.Region(p1=(-40e6, -40e6, 0), p2=(40e6, 40e6, 0.5))
cross_section[saxs_region].mpl.scalar(cmap='gray', interpolation='spline16',
                                      colorbar_label=r'Intensity')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09966698382492864, 'fraction_numerical': 0.028068506184586107, 'mean_word_length': 4.736698499317872, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '551223', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1268, 'n_words': 444, 'path': 'docs/SAXS.ipynb', 'repo_name': 'ubermag/exsim', 'size': '4210'}