Fitting a simple slab model#
In order to show one of the simplest analyses that easyreflectometry
can perform, we will use the great example from the refnx documentation. This involves the analysis of a single neutron reflectometry dataset from a hydrated polymer film system. Before we start on any analysis, we will import the necessary packages and functions.
First configure matplotlib to place figures in notebook and import needed modules
[1]:
%matplotlib inline
import pooch
import refnx
import easyreflectometry
from easyreflectometry.data import load
from easyreflectometry.sample import Layer
from easyreflectometry.sample import Sample
from easyreflectometry.sample import Material
from easyreflectometry.sample import Multilayer
from easyreflectometry.experiment import Model
from easyreflectometry.experiment import PercentageFhwm
from easyreflectometry.calculators import CalculatorFactory
from easyreflectometry.fitting import Fitter
from easyreflectometry.plot import plot
One of benefits of using a Jupyter Notebook for our analysis is improved reproducibility, to ensure this, below we share the version of the software packages being used.
[2]:
print(f'easyreflectometry: {easyreflectometry.__version__}')
print(f'refnx: {refnx.__version__}')
easyreflectometry: 1.1.1
refnx: 0.1.48
Reading in experimental data#
easyreflectometry
has support for the .ort
file format, a standard file format for reduced reflectivity data developed by the Open Reflectometry Standards Organisation. To load in a dataset, we use the load
function. We use pooch
to fetch the file from the repository.
[3]:
file_path = pooch.retrieve(
# URL to one of Pooch's test files
url="https://raw.githubusercontent.com/EasyScience/EasyReflectometryLib/master/docs/src/tutorials/fitting/example.ort",
known_hash="82d0c95c069092279a799a8131ad3710335f601d9f1080754b387f42e407dfab",
)
data = load(file_path)
Downloading data from 'https://raw.githubusercontent.com/EasyScience/EasyReflectometryLib/master/docs/src/tutorials/fitting/example.ort' to file '/home/runner/.cache/pooch/ec8fae13fd7789fd387981c8373238f0-example.ort'.
The function about will load the file into a scipp Dataset object. This offers some nice visualisations of the data, including the HTML view.
[4]:
data
[4]:
- datadict(){'R_0': <scipp.Variable> (Qz_0: 408) float64 [dimensionless] [0.709581, 0.8...
- coordsdict(){'Qz_0': <scipp.Variable> (Qz_0: 408) float64 [1/Å] [0.00806022, 0...
- attrsdict(){'R_0': {'orso_header': <scipp.Variable> () PyObject <no unit> {'data_...
easyreflectometry
also includes a custom plotting function for the data.
[5]:
plot(data)
Building our model#
Now that we have read in the experimental data that we want to analyse, it is necessary that we construct some model that describes what we think the system looks like. The construction of this models is discussed in detail in the model-dependent analysis and reflectometry slab models sections of the ISIS Virtual Reflectometry Training Course on neutron reflectometry fitting.
The system that we are investigating consists of four layers (with the top and bottom as semi-finite super- and sub-phases). The super-phase (where the neutrons are incident first) is a silicon (Si) wafer and as a process of the sample preparation there is anticipated to by a layer of silicon dioxide (SiO2) on this material. Then a polymer film has been attached to the silicon dioxide by some chemical method and this polymer film is solvated in a heavy water (D2O) which also makes up the sub-phase of the system. This is shown pictorially below, as a slab model.
A slab model description of the polymer film system (note that the layers are not to scale), showing the four layers of silicon, silicon dioxide, the polymer film and the heavy water subphase.
In order to constuct this model in EasyReflecotmetry
, first we must construct objects for each of the materials that will compose the layers. These objects should be of type Material
, when constructed from_pars
the arguments are the real and imaginary components of the scattering length density (in units of 10-6Å-2) and some name for the material.
[6]:
si = Material(sld=2.07, isld=0, name='Si')
sio2 = Material(sld=3.47, isld=0, name='SiO2')
film = Material(sld=2.0, isld=0, name='Film')
d2o = Material(sld=6.36, isld=0, name='D2O')
We can investigate the properties of one of these objects as follows.
[7]:
film
[7]:
Film:
sld: 2.000e-6 1/Å^2
isld: 0.000e-6 1/Å^2
Next we will produce layers from each of these materials, of type Layer
. The from_pars
constructor for these take the material, a thickness and a interfacial roughness (on the top of the layer). The thickness and roughness values are both in Å.
[8]:
si_layer = Layer(material=si, thickness=0, roughness=0, name='Si layer')
sio2_layer = Layer(material=sio2, thickness=30, roughness=3, name='SiO2 layer')
film_layer = Layer(material=film, thickness=250, roughness=3, name='Film Layer')
subphase = Layer(material=d2o, thickness=0, roughness=3, name='D2O Subphase')
Again, we can probe the properties of the layer as such.
[9]:
film_layer
[9]:
Film Layer:
material:
Film:
sld: 2.000e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 250.000 Å
roughness: 3.000 Å
Given that the silicon and silicon dioxide layer both compose the solid subphase, it can be helpful to combine these as a Multilayer
assembly type in our code.
[10]:
superphase = Multilayer([si_layer, sio2_layer], name='Si/SiO2 Superphase')
These objects are then combined as a Sample
, where the constructor takes a series of layers (or some more complex easyreflectometry
assemblies) and, optionally, some name for the sample.
[11]:
sample = Sample(superphase, film_layer, subphase, name='Film Structure')
This sample can be investigated from the string representation like the other objects.
[12]:
sample
[12]:
Film Structure:
- Si/SiO2 Superphase:
Si layer/SiO2 layer:
- Si layer:
material:
Si:
sld: 2.070e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 0.000 Å
roughness: 0.000 Å
- SiO2 layer:
material:
SiO2:
sld: 3.470e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 30.000 Å
roughness: 3.000 Å
- Film Layer:
material:
Film:
sld: 2.000e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 250.000 Å
roughness: 3.000 Å
- D2O Subphase:
material:
D2O:
sld: 6.360e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 0.000 Å
roughness: 3.000 Å
Constructing the model#
The structure of the system under investigation is just part of the analysis story. It is also necessary to describe the instrumental parameters, namely the background level, the resolution and some option to scale the data in the y-axis.
Note
Currently, only constant with resolution is supported. We are working to include more complex resolution in future.
the Model
constructor takes our smple, a scale factor, a uniform background level and a resolution function.
[13]:
resolution_function = PercentageFhwm(0.02)
model = Model(
sample=sample,
scale=1,
background=1e-6,
resolution_function=resolution_function,
name='Film Model'
)
From this object, we can investigate all of the parameters of our model.
[14]:
model
[14]:
Film Model:
scale: 1.0
background: 1.0e-06
resolution: 0.02 %
sample:
Film Structure:
- Si/SiO2 Superphase:
Si layer/SiO2 layer:
- Si layer:
material:
Si:
sld: 2.070e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 0.000 Å
roughness: 0.000 Å
- SiO2 layer:
material:
SiO2:
sld: 3.470e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 30.000 Å
roughness: 3.000 Å
- Film Layer:
material:
Film:
sld: 2.000e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 250.000 Å
roughness: 3.000 Å
- D2O Subphase:
material:
D2O:
sld: 6.360e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 0.000 Å
roughness: 3.000 Å
Setting varying parameters#
Now that the model is fully constructed, we can select the parameters in our model that should be varied. Below we set the thickness of the SiO2 and film layers to vary along with the real scattering length density of the film and all of the roughnesses.
[15]:
# Thicknesses
sio2_layer.thickness.bounds = (15, 50)
film_layer.thickness.bounds = (200, 300)
# Roughnesses
sio2_layer.roughness.bounds = (1, 15)
film_layer.roughness.bounds = (1, 15)
subphase.roughness.bounds = (1, 15)
# Scattering length density
film_layer.material.sld.bounds = (0.1, 3)
In addition to these variables of the structure, we will also vary the background level and scale factor.
[16]:
# Background
model.background.bounds = (1e-8, 1e-5)
# Scale
model.scale.bounds = (0.5, 1.5)
Choosing our calculation engine#
The easyreflectometry
package enables the calculation of the reflectometry profile using either refnx or Refl1D. For this tutorial, we will stick to the current default, which is refnx. The calculator must be created and associated with the model that we are to fit.
[17]:
interface = CalculatorFactory()
model.interface = interface
We can check the calculation engine currently in use as follows.
[18]:
print(interface.current_interface.name)
refnx
Performing an optimisation#
The optimisation of our model is achieved with a Fitter
, which takes our model and calculator.
[19]:
fitter = Fitter(model)
To actually perform the optimisation, we must pass our data
object created from the experimental data. This will return a new sc.Dataset
with the result of out analysis, and the model will be updated in place.
[20]:
analysed = fitter.fit(data)
[21]:
analysed
[21]:
- datadict(){'R_0': <scipp.Variable> (Qz_0: 408) float64 [dimensionless] [0.709581, 0.8...
- coordsdict(){'Qz_0': <scipp.Variable> (Qz_0: 408) float64 [1/Å] [0.00806022, 0...
- attrsdict(){'R_0': {'orso_header': <scipp.Variable> () PyObject <no unit> {'data_...
- R_0_modelscippVariable(Qz_0: 408)float64𝟙0.874, 0.874, ..., 3.925e-07, 3.922e-07
- SLD_0scippVariable(z_0: 500)float641/Å^22.070e-06, 2.070e-06, ..., 6.360e-06, 6.360e-06
The same plot
function that was used on the raw data can be used for this analysed
object and will show the best fit simulated data and the associated scattering length density profile.
[22]:
plot(analysed)
Finally, from the string representation of the parameters we can obtain information about the optimised values.
[23]:
model
[23]:
Film Model:
scale: 0.8739516938032471
background: 3.8894958011557427e-07
resolution: 0.02 %
sample:
Film Structure:
- Si/SiO2 Superphase:
Si layer/SiO2 layer:
- Si layer:
material:
Si:
sld: 2.070e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 0.000 Å
roughness: 0.000 Å
- SiO2 layer:
material:
SiO2:
sld: 3.470e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 38.727 Å
roughness: 8.076 Å
- Film Layer:
material:
Film:
sld: 2.360e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 258.510 Å
roughness: 10.343 Å
- D2O Subphase:
material:
D2O:
sld: 6.360e-6 1/Å^2
isld: 0.000e-6 1/Å^2
thickness: 0.000 Å
roughness: 3.511 Å
We note here that the results obtained are very similar to those from the refnx tutorial, which is hardly surprising given that we have used the refnx engine in this example.