Debian Codegraph @

hugo/content/ref/fit/ex/reflectometry-honeycomb.md

raw
1 +++
2 title = "Reflectometry: Fit honeycomb"
3 weight = 60
4 +++
5
6 ### Reflectometry: Fit honeycomb lattice
7
8
9 In this example, we want to demonstrate how to fit a more complex sample.
10 For this purpose, we utilize the reflectometry data of an
11 artificial magnetic honeycomb lattice published by A. Glavic et al., in
12 [this paper](https://doi.org/10.1002/advs.201700856)
13
14 The experiment was performed with polarized neutrons, but without polarization analysis.
15 Since the magnetization of the sample was parallel to the neutron spin, there is no spin flip
16 and we apply the scalar theory to this problem.
17 This is primarily done to speed up computations: when the polarized computational engine is utilized
18 the fitting procedure takes roughly three times as long.
19
20
21 #### Experimental data
22
23 The experimental data consists of four datasets that should be fitted simultaneously.
24 These datasets arise from the two polarization channels for up and down polarization of the incoming beam
25 and both of these channels are measured at two temperatures (300K and 150K).
26
27 All of this is measured on the same sample, so all parameters are assumed to be the same,
28 except the magnetization being temperature dependent.
29 Therefore, we introduce a scaling parameter for the magnetization as the ratio of the
30 magnetizations at 150K and 300K: $M_{s150} = M_{150K} / M_{300K}$.
31
32
33 #### Magnetization model
34
35 To model a magnetic material, one can assign a magnetization vector to any material, as is demonstrated
36 in the [magnetic material tutorial](/ref/sample/material/magnetization).
37 When a non-vanishing magnetization vector is specified for at least one layer in a sample,
38 BornAgain will automatically utilize the polarized computational engine.
39 This leads to lower performance as the computations are more invovled.
40
41 In this example, the magnetization is (anti)parallel to the neutron spin and hence we instead parametrize
42 the magnetic layers with an effective SLD that is the sum/difference of the nuclear and their magnetic SLD:
43
44 $$\rho_\pm = \rho_{\text{N}} \pm \rho_{\text{M}}$$
45
46 Here the $+$ is chosen for incoming neutrons with spin up and $-$ is chosen for spin down neutrons.
47
48
49 #### Computational model
50
51 We simulate this experiment by bulding a 6 layer model:
52 As usual the top layer is the vacuum and the bottom layer is a silicon substrate.
53 On top of the silicon substrate, we simulate a thin oxide layer, where we fit its roughness and thickness
54 The SLDs of these three layers are taken from the literature and kept constant.
55
56 Then we model the lattice structure with a three-layer model: two layers to account for density fluctuations
57 in $z$-direction and another oxide layer on top.
58 This lattice structure is assumed to be magnetic and we fit all of their SLDs, magnetic SLDs,
59 thicknesses and roughnesses.
60 The magnetic SLD depends on the temperature of the dataset, according to the scaling described above, where
61 the $M_{s150}$ parameter is fitted.
62
63
64 All layers are modeled without absorption, i.e. no imaginary part of the SLD.
65 Ferthermore, we apply a resolution correction as described in [this tutorial](/ref/instr/scan/tof-resolution)
66 with a fixed value for $\Delta Q / Q = 0.018$.
67 The experimental data is normalized to unity, but we still fit the intensity, as is necessary due to the resolution correction.
68
69
70 #### Running a computation
71
72 In order to run a computation, we define some functions to utilize a common simulation function
73 for the two spin channels and both temperatures:
74
75 ```python
76 def run_Simulation_300_p( qzs, params ):
77 return run_simulation(qzs, params, sign=1)
78
79 def run_Simulation_300_m( qzs, params ):
80 return run_simulation(qzs, params, sign=-1)
81
82 def run_Simulation_150_p( qzs, params ):
83 return run_simulation(qzs, params, sign=1, ms150=True)
84
85 def run_Simulation_150_m( qzs, params ):
86 return run_simulation(qzs, params, sign=-1, ms150=True)
87 ```
88
89 Here, the given arguments specify whether we want to simulate a spin-up beam (`sign = 1`)
90 and whether the scaling of the magnetization should be applied (`ms150=True`).
91 For the latter, `true` means that a dataset at 150K is simulated while `false`
92 corresponds to 300K and the scaling parameter is set to unity.
93
94 All four reflectivity curves are then computed using:
95
96 ```python
97 q_300_p, r_300_p = qr( run_Simulation_300_p( qzs, paramsInitial ) )
98 q_300_m, r_300_m = qr( run_Simulation_300_m( qzs, paramsInitial ) )
99
100 q_150_p, r_150_p = qr( run_Simulation_150_p( qzs, paramsInitial ) )
101 q_150_m, r_150_m = qr( run_Simulation_150_m( qzs, paramsInitial ) )
102 ```
103
104 We choose some sensible initial parameters and these yield the following
105 simulation result
106
107 {{< galleryscg >}}
108 {{< figscg src="/img/draw/Honeycomb_reflectivity_initial.png" width="350px" caption="Reflectivity with the initial parameters">}}
109 {{< figscg src="/img/draw/Honeycomb_sld_initial.png" width="350px" caption="SLD profile with the initial parameters">}}
110 {{< /galleryscg >}}
111
112 We have chosen the initial magnetization to be zero, hence there is only a single SLD curve for both spin directions.
113
114
115
116 #### Fitting
117
118 We fit this example by utilizing the differential evolution algorithm from [Scipy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html).
119 As a measure for the goodness of the fit, we use the relative difference:
120
121 $$\Delta = \sum_{j = 1}^4 \frac{1}{N_j} \sum_{i = 1}^N \left( \frac{d_{ji} - s_{ji}}{d_{ji} + s_{ji}} \right)^2$$
122
123 Here the sum over $i$ sums up the fitting error at every data point as usual and
124 the sum over $j$ adds the contributions from all four datasets.
125 This is implemented in the `FitObjective::__call__` function and
126 the `FitObjective` object holds all four datasets, and also performs the corresponding simulations.
127 In the function `run_fit_differential_evolution` this is dispatched to the differential evolution algorithm.
128
129 The given uncercainty of the experimental data is not taken into account.
130
131
132
133 #### Fit Result
134
135 As usual, the fit can be run with the following command:
136 ```python
137 python3 Honeycomb_fit.py fit
138 ```
139 On a four-core workstation, the fitting procedure takes roughly 45 minutes to complete and we obtain the following result:
140
141 {{< galleryscg >}}
142 {{< figscg src="/img/draw/Honeycomb_reflectivity_fit.png" width="350px" caption="Reflectivity with the fit result">}}
143 {{< figscg src="/img/draw/Honeycomb_sld_fit.png" width="350px" caption="SLD profile with the fit result">}}
144 {{< /galleryscg >}}
145
146
147 As can be seen from the plot of the SLDs, the magnetization is indeed larger for the measurement at lower temperature, exactly as expected.
148
149 {{< show-ex file="fit/specular/Honeycomb_fit.py" >}}
150
151 Data to be fitted: {{% ref-data-spec honeycomb150m.dat %}}, {{% ref-data-spec honeycomb150p.dat %}}, {{% ref-data-spec honeycomb300m.dat %}}, {{% ref-data-spec honeycomb300p.dat %}}