### Fitting reflectometry data

In this example we will fit synthetic reflectometry data generated with GenX.

The only fitting parameter of the simulation considered here is the thickness of the Ti layers. The reference data was obtained under the following assumptions:

• All Ti layers have the same thickness
• Thickness value was $3 , nm$

The fit view produced by running the fitting script is shown in the picture. The right-hand part of the view contains information about the current iteration of the fitting process, the maximum relative difference $d_{r, max}$ between the reference and the simulated data, and the current values of the fitting parameters.

One should note that in the current example the BornAgain built-in fitting engine and default minimizer (namely, Minuit) was used to fit the data.

The minimizer can be selected by the setMinimizer command:

minimizer = ba.Minimizer()
minimizer.setMinimizer("Genetic", "", "MaxIterations=30")


This code snippet replaces the default Minuit minimizer with the Genetic one, which is recommended to use for complicated multi-dimensional fitting tasks.

### Further topics

A much more sophisticated example of fitting experimental reflectometry data with BornAgain and an external minimizer can be found in Examples/fit/specular/RealLifeReflectometryFitting.py in the BornAgain directory.

### Complete script and data

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132  #!/usr/bin/env python3 """ Example demonstrates how to fit specular data. Our sample represents twenty interchanging layers of Ti and Ni. We will fit thicknesses of all Ti layers, assuming them being equal. Reference data was generated with GENX for ti layers' thicknesses equal to 3 nm """ import os import numpy as np from matplotlib import pyplot as plt import bornagain as ba from bornagain import ba_fitmonitor def get_sample(params): """ Creates a sample and returns it :param params: a dictionary of optimization parameters :return: the sample defined """ # substrate (Si) si_sld_real = 2.0704e-06 # \AA^{-2} # layers' parameters n_repetitions = 10 # Ni ni_sld_real = 9.4245e-06 # \AA^{-2} ni_thickness = 70*ba.angstrom # Ti ti_sld_real = -1.9493e-06 # \AA^{-2} ti_thickness = params["ti_thickness"] # defining materials m_vacuum = ba.MaterialBySLD() m_ni = ba.MaterialBySLD("Ni", ni_sld_real, 0) m_ti = ba.MaterialBySLD("Ti", ti_sld_real, 0) m_substrate = ba.MaterialBySLD("SiSubstrate", si_sld_real, 0) # vacuum layer and substrate form multi layer vacuum_layer = ba.Layer(m_vacuum) ni_layer = ba.Layer(m_ni, ni_thickness) ti_layer = ba.Layer(m_ti, ti_thickness) substrate_layer = ba.Layer(m_substrate) sample = ba.MultiLayer() sample.addLayer(vacuum_layer) for _ in range(n_repetitions): sample.addLayer(ti_layer) sample.addLayer(ni_layer) sample.addLayer(substrate_layer) return sample def get_real_data(filename): """ Loading data from genx_interchanging_layers.dat Returns a Nx2 array (N - the number of experimental data entries) with first column being coordinates, second one being values. """ real_data = np.loadtxt(filename, usecols=(0, 1), skiprows=3) # translating axis values from double incident angle (degs) # to incident angle (radians) real_data[:, 0] *= np.pi/360 global expdata expdata = real_data.copy() def get_real_data_axis(): """ Get axis coordinates of the experimental data :return: 1D array with axis coordinates """ return expdata[:, 0] def get_real_data_values(): """ Get experimental data values as a 1D array :return: 1D array with experimental data values """ return expdata[:, 1] def get_simulation(params): """ Create and return specular simulation with its instrument defined """ wavelength = 1.54*ba.angstrom # beam wavelength scan = ba.AlphaScan(wavelength, get_real_data_axis()) sample = get_sample(params) return ba.SpecularSimulation(scan, sample) def run_fitting(): """ Setup simulation and fit """ real_data = get_real_data_values() fit_objective = ba.FitObjective() fit_objective.addSimulationAndData(get_simulation, real_data, 1) plot_observer = ba_fitmonitor.PlotterSpecular() fit_objective.initPrint(10) fit_objective.initPlot(10, plot_observer) params = ba.Parameters() params.add("ti_thickness", 50*ba.angstrom, min=10*ba.angstrom, max=60*ba.angstrom) minimizer = ba.Minimizer() result = minimizer.minimize(fit_objective.evaluate, params) fit_objective.finalize(result) if __name__ == '__main__': datadir = os.getenv('BA_EXAMPLE_DATA_DIR', '') data_fname = os.path.join(datadir, "genx_interchanging_layers.dat.gz") get_real_data(data_fname) run_fitting() plt.show() 
Examples/fit/specular/FitSpecularBasics.py

Data to be fitted: Examples/data/genx_interchanging_layers.dat.gz