A multilayer fitting model#

One of the main tools in EasyReflectometry is the assemblies library. This allows the user to define their model, using specific parameters for their system of interest (if it is included in the assemblies library). These assemblies will impose necessary constraints and computational efficiencies based on the assembly that is used.

In this tutorial, we will look at one of these assemblies, that of a RepeatingMultilayer (documented here). This tutorial is based on an example from the BornAgain documentation looking at specular reflectivity analysis. Before performing analysis, we should import the packages that we need.

First configure matplotlib to place figures in notebook and import needed modules

[1]:
%matplotlib inline

import numpy as np
import scipp as sc
import EasyReflectometry
import refl1d
from EasyReflectometry.data import load
from EasyReflectometry.sample import Layer
from EasyReflectometry.sample import Sample
from EasyReflectometry.sample import Material
from EasyReflectometry.sample import RepeatingMultilayer
from EasyReflectometry.experiment.model import Model
from EasyReflectometry.calculators import CalculatorFactory
from EasyReflectometry.fitting import Fitter
from EasyReflectometry.plot import plot

As mentioned in the previous tutorial, we share the version of the software packages we will use.

[2]:
print(f'numpy: {np.__version__}')
print(f'scipp: {sc.__version__}')
print(f'EasyReflectometry: {EasyReflectometry.__version__}')
print(f'Refl1D: {refl1d.__version__}')
numpy: 1.26.4
scipp: 24.02.0
EasyReflectometry: 0.0.0
Refl1D: 0.8.16

Reading in experimental data#

The data that we will investigate in this tutorial was generated with GenX and is stored in an .ort format file.

[3]:
data = load('_static/repeating_layers.ort')
data
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/orsopy/fileio/base.py:287: ORSOSchemaWarning: Has to be one of ('neutron', 'x-ray') got x-rays
  warnings.warn(
[3]:
  • data
    dict
    ()
    {'R_0': <scipp.Variable> (Qz_0: 400) float64 [dimensionless] [1, 1, ..., 8....
  • coords
    dict
    ()
    {'Qz_0': <scipp.Variable> (Qz_0: 400) float64 [1/Å] [0.000712093, ...
  • attrs
    dict
    ()
    {'R_0': {'orso_header': <scipp.Variable> () PyObject <no unit> {'data_...

This data is very featureful, with many fringes present (arising from the multilayer structure)

[4]:
plot(data)
../_images/tutorials_repeating_8_0.png

Building our model#

The system that was used to produce the data shown above is based on a silicon subphase, with a repeating multilayer of nickel and titanium grown upon it. Typcially, under experimental conditions, the producer of the sample will know how many repeats there will be of the multilayer system (as these are grown using some vapour disposition or sputtering method that the producer controls). We show the model that will be used graphically below.

A slab model description of the repeating multilayer system.

A slab model description of the repeating multilayer, showing the four layers of vacuum, titanium, nickel and silicon, with the titanium/nickel layers being repeated 10 times.

To construct such a layer structure, first we create each of the materials and associated layers

[5]:
vacuum = Material.from_pars(0, 0, 'Vacuum')
ti = Material.from_pars(-1.9493, 0, 'Ti')
ni = Material.from_pars(9.4245, 0, 'Ni')
si = Material.from_pars(2.0704, 0, 'Si')
[6]:
superphase = Layer.from_pars(vacuum, 0, 0, 'Vacuum Superphase')
ti_layer = Layer.from_pars(ti, 40, 0, 'Ti Layer')
ni_layer = Layer.from_pars(ni, 70, 0, 'Ni Layer')
subphase = Layer.from_pars(si, 0, 0, 'Si Subphase')

Then, to produce the repeating multilayer, we use the RepeatingMultilayer assembly type. This can be constructed in a range of different ways, however here we pass a list of Layer type objects and a number of repetitions.

[7]:
rep_multilayer = RepeatingMultilayer.from_pars([ti_layer, ni_layer], repetitions=10, name='NiTi Multilayer')
rep_multilayer
[7]:
NiTi Multilayer:
  Ti Layer/Ni Layer:
  - Ti Layer:
      material:
        Ti:
          sld: -1.949e-6 1 / angstrom ** 2
          isld: 0.000e-6 1 / angstrom ** 2
      thickness: 40.000 angstrom
      roughness: 0.000 angstrom
  - Ni Layer:
      material:
        Ni:
          sld: 9.425e-6 1 / angstrom ** 2
          isld: 0.000e-6 1 / angstrom ** 2
      thickness: 70.000 angstrom
      roughness: 0.000 angstrom
  repetitions: 10.0

From these objects, we can construct our structure and combine this with a scaling, background and resolution (since this data is simulated there is no background or resolution smearing).

[8]:
sample = Sample.from_pars(superphase, rep_multilayer, subphase, name='Multilayer Structure')
model = Model.from_pars(sample, 1, 0, 0, 'Multilayer Model')

In the analysis, we will only vary a single parameter, the thickness of titanium layer.

[9]:
ti_layer.thickness.bounds = (10, 60)

Choosing our calculation engine#

In the previous tutorial, we used the default refnx engine for our analysis. Here, we will change our engine to be Refl1D. This is achieved with the interface.switch('refl1d') method below.

[10]:
interface = CalculatorFactory()
interface.switch('refl1d')
model.interface = interface
print(interface.current_interface.name)
refl1d

Performing an optimisation#

The easyScience framework allows us to access a broad range of optimisation methods. Below, we have selected the differential evolution method from lmfit.

[11]:
fitter = Fitter(model)
analysed = fitter.fit(data, method='differential_evolution')
analysed
[11]:
  • data
    dict
    ()
    {'R_0': <scipp.Variable> (Qz_0: 400) float64 [dimensionless] [1, 1, ..., 8....
  • coords
    dict
    ()
    {'Qz_0': <scipp.Variable> (Qz_0: 400) float64 [1/Å] [0.000712093, ...
  • attrs
    dict
    ()
    {'R_0': {'orso_header': <scipp.Variable> () PyObject <no unit> {'data_...
  • R_0_model
    scipp
    Variable
    (Qz_0: 400)
    float64
    𝟙
    1.000, 1.000, ..., 9.474e-08, 4.953e-09
  • SLD_0
    scipp
    Variable
    (z_0: 10200)
    float64
    1/Å^2
    8.882e-22, 8.882e-22, ..., 2.070e-06, 2.070e-06

We can visualise the analysed model and SLD profile with the plot function.

[12]:
plot(analysed)
../_images/tutorials_repeating_23_0.png

The value of the titanium layer thickness that gives this best fit can be found from the relavant object. Note that the uncertainty of 0 is due to the use of the lmfit differential evolution algorithm, which does not include uncertainty analysis.

[13]:
ti_layer.thickness
[13]:
<Parameter 'thickness': 29.987946086297534+/-0 Å, bounds=[10:60]>

This result of a thickness of 30 Å is the same as that which is used to produce the data.