Skip to content

Commit cbf69ed

Browse files
author
Wojciech Potrzebowski
authored
Merge pull request #1974 from SasView/scattering-calculator-docs
Scattering calculator docs
2 parents aede4f9 + ec0cbfc commit cbf69ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+876487
-79
lines changed
2.04 MB
Loading
-4.74 KB
Binary file not shown.
-9.46 KB
Loading
-3.36 KB
Binary file not shown.

src/sas/qtgui/Calculators/media/gensas_orientational_average.ipynb

Lines changed: 252 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.. gsc_ex_default_data.rst
2+
3+
.. _gsc_ex_default_data:
4+
5+
Example 1: Default Calculator Data
6+
==================================
7+
8+
In this example we will use the default data in the generic scattering calculator to find
9+
the scattering intensity of a 60x60x60\ |Ang| cube with constant
10+
scattering length density of 6.97x10\ :sup:`-6`\ |Ang|:sup:`-2`. We begin by
11+
selecting Tool>Generic Scattering Calculator from the top menu.
12+
13+
Upon loading the calculator we are shown the following interface:
14+
15+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_1.png
16+
17+
The default data within the generic scattering calculator (highlighted in red) describes a rectangular grid of 10x10x10 pixels, with
18+
each pixel being 6x6x6\ |Ang|. Each pixel has a constant nuclear SLD of 6.97x10\ :sup:`-6`\ |Ang|:sup:`-2`
19+
and no magnetic SLD.
20+
21+
To calculate the scattering pattern we press the `compute` button, and the following image appears in the main window:
22+
23+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_2.png
24+
25+
This shows the scattering intensity on a 30x30 grid of pixels, up to a maximum value of 0.3\ |Ang|:sup:`-1` in each axis, as
26+
specified in the Q Range settings in the calculator. In order to see a higher resolution image we can adjust the number of Qx (Qy) bins. For a smooth image here we will set
27+
this value to 100.
28+
29+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_3.png
30+
31+
The orange highlight of the textbox informs us that this value is potentially too high. More precisely it is informing us that with the given spacing of
32+
pixels in the realspace data the fourier transform cannot resolve peaks at such fine resolution in Q space. While using more Q bins can produce
33+
nicer images - which more clearly show the overall pattern - we should be aware of this limitation. Pressing compute again gives us a new output:
34+
35+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_4.png
36+
37+
Given that for purely nuclear data the scattering pattern corresponds to a fourier transform, it should not be a suprise that for a cube oriented with
38+
the axes, a sinc pattern appears.
39+
40+
It is often far more useful to know the orientational average at each magnitude $\|\mathbf{Q}\|$ than the result for a single oriented cube. Because the default data
41+
is positioned on a regular grid, and contains only nuclear scattering length densities, we can use the Debye full average option, which uses the Debye formula to
42+
calculate the orientational average:
43+
44+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_5.png
45+
46+
Pressing compute again gives:
47+
48+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_6.png
49+
50+
For such a simple structure as a cube, the analytical result is well known, and we can compare the calculator's simulation to the fitting tool. In the main window we select the most
51+
recent dataset (the Debye full average) and select `Send data to` with `Fitting` mode selected in the adjacent drop down. We can now select the `Parallelepiped` model category and then
52+
the `Parallelepiped` model in the fitting window. We also adjust all the paramters in the window to match those of the default data in the scattering calculator.
53+
54+
- scale: 1
55+
- background: 0
56+
- sld: 6.97 ($\times 10^{-6}$ in units)
57+
- sld_solvent: 0
58+
- length_a: 60
59+
- length_b: 60
60+
- length_c: 60
61+
62+
If we now press `Compute/Plot` at the bottom of the fitting window we see a plot of our data and the analytical result.
63+
64+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_7.png
65+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_7b.png
66+
67+
As we would expect, at higher Q values the calculator result deviates from the analytical result, due to the discretisation of the data in the calculator. With a 10x10x10 cube of pixels
68+
we obtain $\chi^2 = 1.96$.
69+
70+
If we adjust the calculator settings to use a 20x20x20 grid of pixels to describe the same sample, we expect to see a much better match. We adjust the settings as follows:
71+
72+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_8.png
73+
74+
Recomputing the data and comparing it to the fitting calculator as before gives a substantially better fit of $\chi^2 = 0.12$. It is visibly obvious from the graph as well
75+
that this fit is much closer to the analytical result.
76+
77+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_9.png
78+
.. figure:: gsc_ex_default_data_assets/gsc_ex_default_data_9b.png
79+
80+
*Document History*
81+
82+
| 2021-09-14 Robert Bourne
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
.. gsc_ex_magnetic_cylinder.rst
2+
3+
.. _gsc_ex_magnetic_cylinder:
4+
5+
Example 2: A Magnetic Cylinder
6+
==================================
7+
8+
In this example we will generate an SLD file describing a single solid
9+
cylinder, with both nuclear and magnetic scattering length densities (SLDs).
10+
We will then use the calculator to create scattering intensity patterns for
11+
both a non-magnetised and magnetised cylinder.
12+
13+
Our cylinder will have a radius of 20\ |Ang| and a
14+
length of 40\ |Ang|, with its axis
15+
at a 60° polar angle to the $z$-axis. The cylinder will have equal nuclear and
16+
magnetic SLDs, with the magnetic SLD along the cylinder's axis.
17+
We will write first an SLD file for an cylinder aligned along the $z$ axis -
18+
and perform the rotation within the calculator.
19+
20+
The file should describe a cylinder as below, with a constant nuclear
21+
scattering length density of
22+
1x10\ :sup:`-6`\ |Ang|:sup:`-2` and
23+
a constant magnetic scattering length density of
24+
(0, 0, 1x10\ :sup:`-6`)\ |Ang|:sup:`-2`:
25+
26+
.. figure:: gsc_ex_magnetic_cylinder_assets/cylinder_graphic.png
27+
28+
The following code generates a SLD file describing such a sample, using the SLD
29+
file format as given in the documentation for the generic scattering
30+
calculator::
31+
32+
import numpy as np
33+
34+
size = np.linspace(-35.0, 35.0, 50) # describe each axis of the grid
35+
# create a full 3D grid
36+
xs, ys, zs = np.meshgrid(size, size, size)
37+
xs = xs.flatten()
38+
ys = ys.flatten()
39+
zs = zs.flatten()
40+
# create arrays to hold the SLDs
41+
N = np.zeros_like(xs)
42+
Mx = np.zeros_like(xs)
43+
My = np.zeros_like(xs)
44+
Mz = np.zeros_like(xs)
45+
# fill in the values of the non-zero SLDs within the cylinder
46+
inside_cylinder = np.bitwise_and(np.float_power(xs, 2) + np.float_power(ys, 2) <= 20**2, np.abs(zs) <= 20)
47+
N[inside_cylinder] = 1e-6
48+
Mz[inside_cylinder] = 1e-6
49+
# save the output to an sld file
50+
output = np.column_stack((xs, ys, zs, N, Mx, My, Mz))
51+
np.savetxt("mag_cylinder.sld", output, header="x y z N mx my mz")
52+
53+
We do not need to worry that the large number of pixels with zero valued SLD
54+
around the cylinder slows down the computation, as such pixels are
55+
stripped before the calculation begins.
56+
57+
Structural Scattering
58+
^^^^^^^^^^^^^^^^^^^^^
59+
60+
First we consider the scattering pattern of a non-magnetic cylinder. We open
61+
the scattering calculator and use the nuclear datafile `load` button to load
62+
the nuclear SLD for this sample into the calculator.
63+
64+
65+
.. figure:: gsc_ex_magnetic_cylinder_assets/gsc_tool_1.png
66+
67+
By pressing the `draw` button we can see a view of the pixels describing the
68+
sample. Pixels with 0 SLD are coloured in yellow, all others are given a colour
69+
related
70+
to their SLD, which in this case is a constant.
71+
72+
.. figure:: gsc_ex_magnetic_cylinder_assets/cylinder_draw_output.png
73+
74+
In order to set the cylinder at an angle of 60° to the $z$ axis we use the
75+
sample coordinates highlighted in red below. We also want a 100x100 pixel
76+
binning in $Q$.
77+
Unlike the default data in example 1, this value is not given a warning orange
78+
background, due to the higher discretisation of the real space data for the
79+
cylinder.
80+
81+
.. figure:: gsc_ex_magnetic_cylinder_assets/gsc_tool_2.png
82+
83+
Pressing compute gives us the following output in the main window:
84+
85+
.. figure:: gsc_ex_magnetic_cylinder_assets/nuclear_output.png
86+
87+
This relatively simple system can be compared with the analytical model in the
88+
fitting calculator to test the correctness of our results.
89+
We send the output of the scattering calculator to the fitting panel as in
90+
example 1 :ref:`gsc_ex_default_data` and choose the `cylinder` category and `cylinder` model.
91+
We then set the following settings to match the fitting calculator to the
92+
scattering calculator settings:
93+
94+
- *scale*: 1.0
95+
- *background*: 0.0
96+
- *sld*: 1 ($\times 10^{-6}$ in units)
97+
- *sld_solvent*: 0
98+
- *radius*: 20
99+
- *length*: 40
100+
- *theta*: 60
101+
- *phi*: 0
102+
103+
Computing this gives us the model and residual plots:
104+
105+
.. figure:: gsc_ex_magnetic_cylinder_assets/main_window_3.png
106+
107+
The value of $\chi^2 = 5.65\times 10^{-6}$ demonstrates that the calculator has
108+
produced very accurate results.
109+
110+
For a better comparison of the results, we can adjust the colour scales by
111+
right-clicking on each of the scattering intensity plots and selecting `2D
112+
Color Map` to set the maximum and minimum ranges of the plots:
113+
114+
.. figure:: gsc_ex_magnetic_cylinder_assets/color_map_4.png
115+
116+
We also need to adjust the scale for the residuals plot. Since the residuals
117+
for this fit include negative values we need to change from a log to a linear
118+
scale
119+
by right clicking the plot and selecting `Toggle Linear/Log Scale`. We can then
120+
adjust the range of the color map as before - in this case to the range from
121+
-0.01 to 0.01.
122+
123+
.. figure:: gsc_ex_magnetic_cylinder_assets/main_window_5.png
124+
125+
Magnetic Scattering
126+
^^^^^^^^^^^^^^^^^^^^^
127+
128+
We will now add the magnetic SLD to the cylinder. We load our magnetic cylinder
129+
SLD file into the magnetic datafile slot, and alter the magnetic beamline
130+
settings
131+
to put the polarisation direction along the $U$ axis (the horizontal direction)
132+
and to record the ++ cross-section ("+" state as defined in Moon, Riste, and
133+
Koehler, 1969 [#MRK1969]_ corresponds to 0 in the textbox for up_frac).
134+
135+
.. figure:: gsc_ex_magnetic_cylinder_assets/gsc_tool_5b.png
136+
137+
Running the calculation gives us the following output in the main window:
138+
139+
.. figure:: gsc_ex_magnetic_cylinder_assets/magnetic_output.png
140+
141+
Additional to the structural scattering pattern now an angular anisotropy due
142+
to the magnetisation occurs.
143+
144+
Again we can compare our result to the analytic result of the fitting
145+
calculator. We set the same settings as before for the cylinder model but also
146+
check the
147+
`Magnetism` checkbox in the fitting window. We then navigate to the `Magnetism`
148+
tab and set the following settings to match with the scattering calculator:
149+
150+
- *up_frac_i*: 0
151+
- *up_frac_f*: 0
152+
- *up_angle*: 90 (corresponds to up_theta in the calculator)
153+
- *up_phi*: 0
154+
- *sld_M0*: 1 (corresponds to sample magnetic SLD)
155+
- *sld_mtheta*: 60 (gives the direction of the magnetic SLD in polar angles)
156+
- *sld_mphi*: 0
157+
- *sld_solvent_M0*: 0 (the magnetic SLD of the solvent)
158+
- *sld_solvent_mtheta*: 0
159+
- *sld_solvent_mphi*: 0
160+
161+
.. figure:: gsc_ex_magnetic_cylinder_assets/magnetism_fitting.png
162+
163+
Carrying out the fitting gives the following results (after adjusting scales to
164+
match):
165+
166+
.. figure:: gsc_ex_magnetic_cylinder_assets/main_window_6.png
167+
168+
Again the value of $\chi^2 = 1.92\times 10^{-7}$ shows an excellent fit.
169+
170+
References
171+
----------
172+
173+
.. [#MRK1969] Polarization Analysis of Thermal-Neutron Scattering
174+
(1969) R. M. Moon, T. Riste, and W. C. Koehler Phys. Rev. 181, 920
175+
`DOI <https://doi.org/10.1103/PhysRev.181.920>`__
176+
177+
*Document History*
178+
179+
| 2021-09-14 Robert Bourne
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)