# Interactive, Web-Based Calculator of Neutron and X-ray Reflectivity.

1. Summary

For many users of the neutron and X-ray reflectometry instruments at NIST, these measurements represent a relatively small and specialized part of their research portfolio. As such, providing calculation and modeling tools that are as accessible and easy-to-use as possible is a high priority of the facility. In order to meet this need, a purely web-browser-based calculator for reflectivity modeling and rudimentary fitting has been developed and provided on a publicly accessible web server.

Going to https://www.ncnr.nist.gov/instruments/magik/calculators/reflectivity-calculator.html, will load a one-page web application into the browser. Any relatively modern browser with support for ECMA Script 5 will be able to load and run the application. A calculator for magnetic samples can be found at https://www.ncnr.nist.gov/instruments/magik/calculators/magnetic-reflectivity-calculator.html.

Key words: calculator; neutron; reflectivity; scattering.

2. Software Details

2.1 Reflectivity Calculations

The final output of a reflectometry measurement is the reflectivity R (defined as reflected intensity divided by incident intensity of the probe radiation) as a function of the scattering vector [??] (defined in terms of the change in the momentum [??] of the scattering radiation as [mathematical expression not reproducible]). After a measurement, users must be able to compare the measured R to a theoretical calculation based on a physical model of the sample being measured. The theoretical reflectivity is only well-defined for samples which are flat thin-films, in which the length and width are nearly infinite compared to the thickness. In that case, there is only one dimension of interest in the modeling which is the surface normal we will call [??], defined to be parallel to [??].

The inputs to the physical model are the scattering length density (SLD, real and imaginary components) of the sample under investigation as a function of depth along [??] into the material, where z [equivalent to] 0 is defined at the lower boundary of the sample. The reflectivity is calculated by solving the one-dimensional Schrodinger equation for the potential described by the SLD profile(s), as described in Refs. [1-3].

The roughness, defined as the width of a smooth transition (an error-function) between layers of constant SLD is handled in this application using the Nevot-Croce approximation [4] in which the reflectance of a given interface is reduced by an exponential factor depending on the roughness. This approximation has been shown to be accurate for roughnesses that do not approach the thicknesses of the layers on either side.

For magnetic samples, the magnetic interaction between the spin of polarized neutron and the internal sample magnetism must also be included in the calculation. This is determined by the magnitude and direction of the magnetization (compared with the spin-direction of the neutron) of the sample as a function of depth. As a result, there are four defined reflectivities instead of one, corresponding to the four possible combinations of spin-up and spin-down for the incoming and reflected neutrons (spin-flipping scattering events are possible and calculable). The calculation of these four magnetic scattering cross-sections is described in Ref. [5]. The effects of large applied magnetic fields on the scattering are implemented here as described in Ref. [6].

2.2 Layout of Application: Non-Magnetic

On the one-page application as seen in Fig. 1, on the left there are two plots one above the other: the upper is a plot of the calculated reflectivity R (unitless) as a function of Q (in units of inverse [Angstrom]). The lower plot is an interactive display of the scattering length density (SLD) of the sample under investigation (in units of [10.sup.-6] [[Angstrom].sup.-2]) as a function of depth into the material (in units of [Angstrom]).

At the top of this panel are controls for the range of momentum transfer vector Q desired for calculation, and a selector for log/linear plotting of R. Below this there is a table representation of the same profile represented in the lower left plot, organized as stacked layers with fixed SLD (real and imaginary, labeled "SLDn" and "iSLDn" respectively), roughness, and thickness; the first row in the table is the substrate, defined as the material below the first layer. As such the thickness of this table entry is not meaningful and is fixed to be zero; similarly the last table row defines the properties of the incident medium, through which the radiation travels to reach the surface of the sample.

The inputs for neutron or X-ray non-magnetic scattering are very similar except in magnitude: the absorption (imaginary SLD) is typically nearly zero for neutron scattering but not for X-rays, and the real part of the SLD is often an order of magnitude larger for X-rays as for neutrons.

2.3 Layout of Application: Magnetic

The version of the application [7] for magnetic samples has a just a few differences in the interface from the non-magnetic version, as seen in Fig. 2. The biggest difference is that there are four calculated reflectivities for the different spin cross-sections rather than one, and when loading data from an external file it can read in multiple cross-sections as well.

Note that the magnetic calculations are only appropriate for polarized neutron scattering; for polarized X-ray scattering the assumptions underlying the model here are not appropriate.

For the model inputs, in addition to real and imaginary SLD properties and roughness for each layer, the magnitude (labelled "SLDm", in the same units as the SLD) and direction of the magnetic potential vector for a neutron is controllable through the profile plot or the table. The units for the magnetic field in the material are chosen to be n radians, to put them on a similar scale as the SLD units.

The direction of the externally-applied magnetic field for the measurement is specified in the input labelled "AGUIDE", and is in degrees.

2.4 Interactivity, Import and Export

Both calculators allow immediate, responsive editing of the model scattering length density profile through mouse interaction with handles on the profile plot (click and drag), as well as allowing the user to enter numbers for layer parameters in a table at the right. Changes to the table are immediately reflected in the calculated curve as well as the profile plot, just as changes to the profile plot through mouse interaction are immediately reflected in the table and calculated curve. The only parameter that is not controllable from the plot interface is the roughness (layer interface width), which is only adjustable through the table.

Export of the table data in tabular text-file form is possible by pressing the "export table" button below the table; the file can be imported into a spreadsheet program for editing, and re-loading of a (possibly edited) set of profile data is done by pressing the "import table" button immediately to the right of the export button.

Tabular text-file representations of the calculated reflectivities and the profile (including interface roughness) can be exported by buttons below the appropriate plots for those, as well.

Either of the graphs on the left can be exported by itself by pressing the "export svg" button below them.

Data can be uploaded into the web application through a button in the top panel of the interface; the data must be in a text file with tab- or space-separated values with column-order [Q, R, [DELTA]R, ...] (lines separated by carriage return, all header lines commented with a "#" character), where Q and R are defined as above and AR represents and statistical error (one standard deviation) on the reflectivity.

For magnetic cross-section data the cross-section datasets are expected to be appended to a single file with a double carriage-return between the datasets for the different cross sections, with a header line before each cross-section of the form e.g.,

# "polarization": "++"

Once data is uploaded it is plotted together with the calculated reflectivity based on the interactive model, to allow manual adjustment of the model to try to match the measured reflectivity.

At any time the user may generate a (Python) problem definition file for the powerful Refl1d [8] fitting program by pressing a button in the application. This definition is based on the current snapshot of the model as displayed in the application, and will essentially replicate the structure defined there in a way that is understood by Refl1d. The problem file is highly decorated with documentation comments, including commented-out options for activating fitting parameters or advanced features in Refl1d.

2.5 Fitting

Users wishing to perform a rudimentary optimization of profile parameters can do so by selecting the "fit mode" button and then highlighting the parameters to optimize (by clicking on them) in the table. Optimization is accomplished with a Levenberg-Marquardt engine with an automatic parameter-initialization routine, from the open-source ALGLIB C++ math library [9]. The fit calculations take place entirely within the javascript execution environment of the user's browser, with no server involved.

When the fit is complete, the results are displayed in text form below the fit button, and the table, profile plot and calculation plot are updated based on the best fit results.

Specific constraints are applied to the fit: the thickness of any layer is constrained to be greater than or equal to zero, the magnetic moment of the first row (substrate) is zero, and the (meaningless) thickness parameter of the first and last rows of the table are fixed at zero.

3. Software Specifications
```NIST Operating Unit    NIST Center for Neutron Research, Science Group

Category               Reflectivity calculator (matrix formalism)

Targeted Users         Neutron and X-ray reflectometry NCNR facility
users and staff

Operating Systems      All (requires up-to-date web browser)

Programming Language   ECMAScript2015 (javascript) and HTML5 and
C++2011 and C99

Inputs/Outputs         Input layer structure, export calculations or
model file for fitting in Refl1d

Documentation          Help available through button in web interface

Accessibility          N/A

Disclaimer             https://www.nist.gov/director/licensing
```

4. Software Libraries

Multiple external, open-source software libraries were used to build this application, including the previously-mentioned ALGLIB [9] and RefllD [8] programs.

Almost all the graphical components on the page are built with the help of the D3 javascript library [10].

The fitting routines were compiled from the ALGLIB C++ source to a javascript runtime using Emscripten [11].

The reflectivity calculations are accomplished through wrapping of the C99 calculation kernels (polarized and unpolarized) in the Refl1d [8] fitting program, which were ported to javascript through Emscripten, after a slight modification to include Nevot-Croce roughness approximation in the magnetic calculations.

5. Methods for Validation

It is possible to immediately compare the output of this calculator to the result calculated by the Refl1d program by exporting a model file and then loading it into Refl1d, where a side-by-side comparison is then possible. By exporting the calculated curves from both the web application and Refl1d, a numerical comparison can also be quickly made. It should be noted that since the calculation kernels for the web application were transpiled from essentially unaltered copies of the kernel code for Refl1d, no difference in the outputs is expected. The outputs of Refl1d have previously been independently compared to other reflectometry fitting software in the past, making it a good reference point.

https://doi.org/10.6028/jres.122.034

Acknowledgments

The author would like to acknowledge Paul Kienzle of the NIST Center for Neutron Research, Gaithersburg, MD, USA for providing the reflectivity calculation kernels from Refl1d, and for invaluable discussions on the design of this application.

6. References

[1] Penfold J, Thomas R (1990) The application of the specular reflection of neutrons to the study of surfaces and interfaces. Journal of Physics: Condensed Matter 2(6):1369. https://doi.org/10.1088/0953-8984/2Z6/001

[2] Russell T (1990) X-ray and neutron reflectivity for the investigation of polymers. Materials Science Reports 5(4):171-271. https://doi.org/10.1016/S0920-2307(05)80002-7

[3] Majkrzak CF, Felcher GP (1990) Neutron scattering studies of surfaces and interfaces. MRS Bulletin 15(11):65-72. https://doi.org/10.1557/S0883769400058383

[4] Croce P, Nevot L (1974) Influence des interfaces rugueux sur la reflexion speculaire des rayons x. Journal of Applied Crystallography 7(2):125-130. https://doi.org/10.1107/S0021889874008946

[5] Felcher G (1993) Magnetic depth profiling studies by polarized neutron reflection. Physica B: Condensed Matter 192(1-2):137-149. https://doi.org/10.1016/0921-4526(93)90115-M

[6] Maranville BB, et al. (2016) Measurement and modeling of polarized specular neutron reflectivity in large magnetic fields. Journal of Applied Crystallography 49(4):1121-1129. https://doi.org/10.1107/S1600576716007135

[7] Maranville BB (2017) Online magnetic neutron reflectivity calculator, https://ncnr.nist.gov/instruments/magik/calculators/magnetic-reflectivity-calculator.html.

[8] Kirby B, et al. (2012) Phase-sensitive specular neutron reflectometry for imaging the nanometer scale composition depth profile of thin-film materials. Current Opinion in Colloid & Interface Science 17(1):44-53. https://doi.org/10.1016/j.cocis.2011.11.001

[9] Bochkanov S, Bystritsky V (1999-2017) ALGLIB, http://www.alglib.net/.

[10] Bostock M, Ogievetsky V, Heer J (2011) D3: Data-driven documents. IEEE Transactions on Visualization and Computer Graphics 17(12):2301-2309. https://doi.org/10.1109/TVCG.2011.185

[11] Zakai A (2011) Emscripten: An llvm-to-javascript compiler. Proceedings of the ACM International Conference Companion on Object Oriented Programming Systems Languages and Applications Companion OOPSLA '11 (ACM, New York, NY, USA), pp 301-312. https://doi.org/10.1145/2048147.2048224

About the authors: Brian B. Maranville is an instrument scientist for the MAGIK neutron reflectometer at the NIST Center for Neutron Research. The National Institute of Standards and Technology is an agency of the U.S. Department of Commerce.

National Institute of Standards and Technology, Gaithersburg, MD 20899, USA

brian.maranville@nist.gov

Accepted: June 30, 2017

Published: July 12, 2017

Software DOI: https://doi.org/10.18434/M3QG67

Software Version: 1.0

Caption: Fig. 1. Screenshot of non-magnetic reflectivity calculator.

Caption: Fig. 2. Screenshot of magnetic reflectivity calculator.
COPYRIGHT 2017 National Institute of Standards and Technology
No portion of this article can be reproduced without the express written permission from the copyright holder.