0% found this document useful (0 votes)
15 views29 pages

Handbook

The document is a manual for the Parratt32 program, which calculates the optical reflectivity of neutrons or x-rays from flat surfaces using Parratt's recursion scheme. It covers the program's graphical user interface, data loading, model setup, reflectivity calculations, and source code details. The manual serves as a reference for users to understand and troubleshoot the software while providing insights into its internal workings.

Uploaded by

rajarshi960
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views29 pages

Handbook

The document is a manual for the Parratt32 program, which calculates the optical reflectivity of neutrons or x-rays from flat surfaces using Parratt's recursion scheme. It covers the program's graphical user interface, data loading, model setup, reflectivity calculations, and source code details. The manual serves as a reference for users to understand and troubleshoot the software while providing insights into its internal workings.

Uploaded by

rajarshi960
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

The Reflectivity Tool

The Manual
c 1997–99 Christian Braun, HMI Berlin
current address: ETH Zürich
Laboratorium für Atmosphärenphysik
(LAPETH)
Hönggerberg HPP
8093 Zürich
Schweiz
e–mail: [email protected]

This document was prepared using pdfTEX

Parratt32: The Manual 2


Contents

Contents
1 Introduction 4

2 The Programme 4
2.1 The Graphical User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 Working with the Programme 6


3.1 Loading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Setting up a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 Calculating the Reflectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Calculating SLD Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.5 Setting Various Calculation Parameters . . . . . . . . . . . . . . . . . . . . . . 9
3.6 Fitting Models to Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.7 Saving the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.8 Copying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.9 Printing the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4 Some Internals 16
4.1 The SLD Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2 Hidden Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3 Trouble is... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5 The Source Code 18


5.1 The Code Itself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.2 Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

6 Appendix 28

Parratt32: The Manual 3


1 Introduction
This manual briefly explains the use of Parratt32. Most aspects are covered also by the
on–line help accessible from within the programme. This document’s purpose is to be printed
out and to be read separately while or before working with the programme. It also serves as a
source of reference in cases where users get stuck with some sort of problem. The second part
of the manual describes the vital parts of the source code and gives a brief summary of how
to compile it to get the executable. On the screen some of the graphics in this documentation
might look ugly, but once printed out the quality increases.

2 The Programme
Parratt32 is a programme to calculate the optical reflectivity of neutrons or x-rays from flat
surfaces. The calculation is based on Parratt’s recursion scheme for stratified media [Parratt54].
Parratt32 is a Microsoft Windows application (either Windows 95, Windows 98 or Windows
NT 4.0 — the i386 edition works fine, other platforms, such as FX32 on DEC Alpha could not
be tested). The 32 bit extensions for 16bit-Windows (versions 3.1x, also known as Win32s)
are not sufficient!

2.1 The Graphical User Interface

Parratt32’s graphical user interface (GUI for short, or main window) is shown in figure 2.1.
The main elements, depicted by the arrows are described in the next sections.

Parratt32: The Manual 4


2.1 The Graphical User Interface

2.1.1 Menu Bar


In the menu bar the different menu entries can be se- The Menu Bar.
lected with the mouse or with key strokes. Most of the
functionality that Parratt32 delivers can be found
here.

2.1.2 Tool Bar


Some of the frequently used items from the menu have The Tool Bar.
short–cuts placed in the tool bar as little buttons. This
makes accessing these items with the pointing device
(i.e. the mouse) very handy.

2.1.3 Reflectivity Window


This window is for displaying the reflectivity (R vs.
Qz ) calculated from the given sample. Measured sets
of data that were loaded into the programme are dis-
played here, too.

2.1.4 Profile Window


From the data given in the model window a scattering
length density profile can be calculated. The resulting
% vs. z is displayed in this window.

2.1.5 Model Window


The model window is the place where the most rele-
vant information of the model sample is stored. Mainly
this is a description of how the scattering length den-
sity varies with the different layers that make up the
sample.

2.1.6 Status Bar


Important programme information is displayed here The Status Bar.
from time to time, i. e. moving the mouse pointer in
one of the graphics windows will display the respective
coordinates.

2.1.7 Copyright Notification Area


This tool bar is just a reminder of where you got the Copyright Notification Area.
software from and who paid for it...

Parratt32: The Manual 5


3 Working with the Programme
3.1 Loading Data
The only kind of data Parratt32 can handle is ASCII
files made up of three or more columns separated by a
TAB character (ASCII 9). The first column are the Qz
values, the second column are the R(Qz ) values (where
R(0) ≡ 1) and the third column are δR(Qz ). These
can be set to zero. Additional columns will be ig-
nored. Since the reflectivity plot is logarithmic, Par-
ratt32 will ignore negative values in the second col-
umn. There is some error handling routine, that lets
Parratt32 ignore certain lines in the file: i. e. header
lines, negative R’s or other kind of NANs. Once fin-
ished loading the data, it will be displayed as small
green circles in the reflectivity window.

3.2 Setting up a Model


On start–up Parratt32 loads a standard set of pa-
rameters. Some of these parameters will be stored in
the Window’s registry when you end working with the
programme and will be reloaded the next time you
start Parratt32. In the model window the basic
sample start–up parameters are always Si bulk mate-
rial with a 25 Å SiO2 layer on top of it (values for
4.66 Å neutrons).
Prior to fitting a model for the layer system has to
be set up. There are three basically different sample
types in Parratt32 which will be described in the
next sections. For every layer in the model at least
two values have to be entered, four more can be en-
tered. The two essential values are: the layer thickness
in Ångstrøm (d/Å) and the real part of the scattering
length density (rho/Å^-2). The four optional param-
eters are the imaginary part of the scattering length
density (Im(rho/Å^-2)), the roughness of the layer
(sigma/Å), and the magnetic contribution to the scat-
tering length density (R(mag), I(mag), same units as
rho and Im(rho)).

Parratt32: The Manual 6


3.2 Setting up a Model

3.2.1 Simple Models


A simple model is made up of a varying number of es-
sentially different layers. By selecting the radio button
labeled ”independent layers” the model window rear-
ranges to its most simple form: there is only one edit
field for the number of layers and below it is shown a
table where one wants to type in the respective prop-
erties for the layers. By changing the number of layers
in the edit field one can change the number of rows in
independent layer mode
the table. Up to four hundred layers can make up
such a simple sample, but who wants to type in these
values...

3.2.2 Models With Functions


Sometimes it might be useful to describe the scattering
length density profile of interfaces by analytical func-
tions. By pressing the ”edit function” button a dia-
logue shows up where a function can either be chosen
from a list of predefined ones or typed as an expres-
sion. The latter, so called user–functions, can also be
saved to file and reloaded later on. Parratt32 comes
with four built–in functions: exponential decay, Liu–
Fisher decay [Liu89], oscillating, and oscillating with
decaying amplitude.
These function are hard–coded, so the evaluation of the
scattering length density is fast, and so is the calcula- profile function mode
tion of the reflectivity, well, at least faster than for the
user–functions, that run through the function parser
each time a value is requested. Apart from choosing
the function there are other parameters to be set in
the model. Firstly, the layers between which the func-
tional expression is inserted has to be specified. This
can be done in the edit field labeled ”append func-
tion to layer No.”. Append means when you choose
1, the function will be appended to layer one, which
is between layer one and two! Secondly, the range of The function edit dialogue
the function with respect to the z–coordinate must be
given. This is done with the two edit fields ”thick-
ness...” and ”number of steps”, which, by multiplying
give the total depth of the region between the chosen
layers. For convenience, there is a button labeled ”I
want to test my function” which calculates the reflec-

Parratt32: The Manual 7


3.3 Calculating the Reflectivity

tivity and the profile.

3.2.3 Multi Layer Models


Often samples come with regularly arranged stacks
of layers, so called multi–layered systems. To han-
dle these more conveniently the model window can be
switch to multi–layer mode. Here a second table shows
up: the multi–layer stack. This stack, which behaves
in the same way as does the basic sample, can be in-
serted into the basic sample at an arbitrary position
(edit field ”append multi layer...”). This means one
can have layers of the basic sample above the multi–
layer stack and below it. The multi–layer stack it-
self consists of at least two different materials (edit
multi–layer mode
field ”number of layers in the stack”), which can be
repeated several times (edit field ”number of repeti-
tions”). The total thickness of the sample is then cal-
culated from the thickness of the basic sample plus
the thickness of the multi–layer stack multiplied by
the number of repetitions.

3.3 Calculating the Reflectivity


The reflectivity R(Qz ) of the model can be displayed
by either selecting ”Calculate–Reflectivity” from the
main menu or by pressing the calculator button in
the tool bar. Note: the tool bar shows so called tool
tips if the mouse pointer rests over he images for a
little while. After a short while — depending of the
speed of the computer and the complexity of the model
— the calculation will be finished and the reflectivity
curve will be displayed in the reflectivity window. The
scaling of the graph is done automatically.

Parratt32: The Manual 8


3.4 Calculating SLD Profiles

3.4 Calculating SLD Profiles


The scattering length density profile %(z) of the sam-
ple can be calculated by either selecting ”Calculate–
Profile” from the main menu or by pressing the profile
button in the tool bar. The progress of the cal-
culation is displayed in the programme’s status bar.
After the calculation is done the resulting scattering
length density profile is displayed in the profile win-
dow, which also rescales automatically.
By simultaneously pressing the shift key, the left mouse
button, and dragging the mouse a rectangle can be
drawn inside the graph windows. This rectangle then
defines the new minimal and maximal coordinates of
the diagram. For automatic rescaling simply hold shift
and press the left mouse button somewhere inside the
window, hopefully everything will return to normal.

3.5 Setting Various Calculation


Parameters
Most calculation relevant settings are accessible via
the ”Settings Dialogue”. It will be shown when select-
ing ”Calculate–Settings” from the main menu. The
dialogue box comes with a lot of pages which will be
explained in the next sections. On the bottom of the
box there are four buttons three of which have the
standard meaning (”Ok”, ”Cancel”, and ”Help”) but
the fourth calculates either the reflectivity or the
profile depending on what page is selected in the dia-
logue.

3.5.1 Reflectivity Calculation Settings


On the first page of the dialogue box the settings for
reflectivity calculation can be specified. The wave-
length edit field is only relevant, when inserting val-
ues from the scattering length density database in the
model window.
When the chackbox labeled ”user supplied Q–range”
is checked the reflectivity will be calculated according
to the values given in the edit fields. Unchecking the
box will disable these fields and either set the values
to the default ones or to the values found in the data
file.

Parratt32: The Manual 9


3.5 Setting Various Calculation Parameters

When the checkbox labeled ”include magnetization”


is checked, the reflectivity will be calculated for three
different models:
a) R(Qz ) from the ordinary values in the model file
(rho and Im(rho)),
b) R+ (Qz ) from rho+ R(mag) and Im(rho)+I(mag),
c) R− (Qz ) from rho-R(mag) and Im(rho)- I(mag).
The three calculations can be distinguished by their
different colour in the reflectivity window:
R is plotted black
R+ is plotted red
R− is plotted blue .

3.5.2 Transmission Calculation Settings


For free samples, i. e. where the substrate is not in-
finitely thick on a macroscopic scale, the intensity of
the transmitted radiation can be calculated. The cal-
culation is mainly based on the length of the path the
radiation travels through the substrate. Therefore the
substrate’s thickness has to be specified (in millime-
ters). Also, the wavelength of the incident radiation
has to be specified (this is done on the ”Reflectiv-
ity Settings”–page) since the absorption cross section
of the substrate’s material is corrected for the wave-
length.

3.5.3 Profile Calculation Settings


The range of the calculation of the scattering length
density profile can be set by checking the box marked
”user supplied z–range”. This will enable the edit
fields, so that values may be entered for the lower and
upper boundary as well as for the number of steps on
the z–Axis. Unchecking this box will result in an au-
tomatic recalculation of the minimum and maximum
setting for the z–range depending on the thicknesses
given in the model window. For a single air/bulk in-
terface the default values are -25 Å to +25 Å for sam-
ples with Nlayer ≥ 1 the calculation will range from
-25% to +25% of the overall sample thickness. The
overall sample thickness is calculated from all layers
of the given model including the functional region of
the model or the including the multi layer stack. In

Parratt32: The Manual 10


3.5 Setting Various Calculation Parameters

the case of multi layer or function models the default


number of points where the scattering length is calcu-
lated (one hundred) can get too small to virtually see
all features of the profile. In this case one is advised
to manually choose the calculation region.

3.5.4 Background Settings


Working with real data often involves reflectivities mea-
sured down to a certain noise limit, which is also known
as instrumental background. In the case of uncorrected
data, which is, no background substracted from the
raw data, this background can be added to the cal-
culated reflectivity. Parratt32 offers different types
of background noise to be added to the data: a) the
typical noise threshold of the reflectivity instrument
(constant y0 ), b) linear increasing or decreasing back-
ground characterized by a constant y0 and a slope m,
c) and d) peaking background (either of gaussian or
lorentzian shape) on a baseline that is characterized
by five values: y0 and m for the baseline and A, w,
and xc for the area, the width, and the center position
of the peak.

3.5.5 Resolution Settings


Real reflectometers all have a finite angular resolution.
This means that measured R(Qz )–values are virtually
R(Qz ±δQz ) values. Parratt32 can approximate this
resolution by calculating the reflectivity at a position
Qz plus gaussian weighted reflectivities at eight addi-
tional positions Qz ± δQz , Qz ± 3δQz /4, Qz ± δQz /2,
and Qz ± δQz /4. Due to a lack of incoming radia-
tion reflectivity curves are sometimes measured with
different resolutions in different Qz regimes, i. e. differ-
ent settings of the collimation system. Therefore up to
eight regions with different resolutions can be specified
in the dialogue’s resolution table. The first column is
the starting value of the Qz –region — which obviously
should be zero in the first row —, the second column
is the δQz . Some neutron reflectometers use a velocity
selector instead of a crystalline monochromator. The
wavelength distribution (∆λ/λ)of such a device is con-
stant, hence the resolution decreases with increasing

Parratt32: The Manual 11


3.6 Fitting Models to Data

in Qz . Clicking the velocity selector checkbox allows


you to enter the appropriate values.

3.5.6 The SLD Database Settings


There is a handy way to fill in the values for the dif-
ferent layers in the model window. By clicking with
the right mouse button on the first column of a row
in the model window a small pop–up menu will ap-
pear. Now, element’s or compound’s properties can
be selected to be put into the respective row’s rho
and Im(rho) column. These values are taken from a
database that Parratt32 loads at programme start–
up. To correctly insert the values into the model win-
dow, the radiation type (neutrons or x–rays) has to be
specified. For details on the database see below.

3.6 Fitting Models to Data


Once data is loaded and an appropriate model is set
up, this model can then be fitted to the experimental
data. By either selecting ”Calculate–fit data” or by
pressing the fit–button from the tool bar the FitIt
dialogue box will show up. This dialogue box comes
with two pages.
On page one three main elements can be found: the
list of parameters, the χ2 label and the fitting control
center. Selecting the parameters is simply done by
clicking on the checkbox next to it. All values of the
parameters that are marked will be varied during the
fitting procedure. The χ2 label shows the momentary
goodness–of–fit during the procedure. In the fitting
control center one can either select a single χ2 evalu- Page 1 of the FitIt dialogue box
ation with the values of parameters given in the list or
a complete minimization run with the selected param-
eters. The tolerance value given in the edit field is read
out every time a new χ2 was calculated. Convergence
of a single parameter is reached when the following
expression is satisfied: |χ2old − χ2new |≤tolerance.
Once the ”do it”–button is pressed, the calculation
is under way. The progress of the calculation can be
monitored in Parratt32’s status bar. The first num-
ber given there is the number of the parameter which
is currently varied, the second number is the current

Parratt32: The Manual 12


3.6 Fitting Models to Data

iteration step. The fitting procedure can be stopped


by pressing the button labeled ”stop”.
Some fitting related settings can be made on page two
of the FitIt dialogue box. At first the weighting of the
χ2 calculation has to be chosen. Additionally, one can
include the background and resolution settings from
the ”Settings Dialogue” into the fit procedure.

3.6.1 Defining a Fitting Region


A fitting region can be defined in the reflectivity win-
dow by the following procedure: Data has to be loaded,
mark the left border of the region (low Q) by holding
down the control key (Ctrl) and press the left mouse Page 2 of the FitIt dialogue box
button when the mouse pointer is in the vicinity of the
leftmost datapoint of the desired region. The corre-
sponding data point will turn black! Now hold down
Ctrl again and press the right mouse button on the
rightmost datapoint. A message will pop up to tell
about the region in Qz that was selected. Once fitting
is done the region is discarded and the reflectivity is
calculated for the whole dataset again.

Parratt32: The Manual 13


3.7 Saving the Results

3.7 Saving the Results


Some examples for the different file formats
The data of all three windows can be saved to file. of calculated reflectivity data:
In the file menu there are entries for saving the data a) bare reflectivity:
Qz R(Qz )
of each window. One can also use the save button 0.000e00 1.000e00 0
2.500e-03 1.000e00 0
from the tool bar which then saves the contents of 5.000e-03 1.000e00 0
7.500e-03 1.000e00 0
the currently active window (the one, which has the 1.000e-02 9.999e-01 0
. . .
highlighted title bar). .
.
.
.
.
.
The data from the model window will be saved in b) reflectivity and transmission:
Qz R(Qz ) T (Qz )
a special Parratt32 format, which contains all the 0.000e00 1.000e00 0 2.220e-16
2.500e-03 1.000e00 0 4.357e-06
layer information plus the background and resolution 5.000e-03 1.000e00 0 9.922e-06
7.500e-03 1.000e00 0 2.008e-05
settings from the settings dialogue. 1.000e-02 9.999e-01 0 1.030e-04
. . . .
The data from the profile window will be saved as two– .
.
.
.
.
.
.
.
column ASCII, with the first column as z/Å and the c) reflectivity including magnetization:
R+ (Qz ) R− (Qz )
second column as %/Å−2 . Qz
0.000e00
R(Qz )
1.000e00 0 1.000e00 1.000e00
The data of the reflectivity window depends on the 2.500e-03
5.000e-03
1.000e00
1.000e00
0
0
1.000e00
1.000e00
1.000e00
1.000e00
magnetization and transmission settings. If none of 7.500e-03
1.000e-02
1.000e00
9.999e-01
0
0
1.000e00
1.000e00
4.614e-01
4.795e-02
these is selected the data will be saved as three TAB– .
.
.
.
.
.
.
.
.
.
. . . . .
delimited column ASCII files: column one are the d) reflectivity including magnetization and
Qz –values, column two are the calculated reflectivity transmission: + −
Qz R(Qz ) R (Qz ) R (Qz ) T (Qz )
values and column three are zeros. If ”include mag- 0.000e00 1.000e00 0 1.000e00 1.000e00 2.220e-16
2.500e-03 1.000e00 0 1.000e00 1.000e00 4.357e-06
netization” is checked in the ”settings dialogue box” 5.000e-03 1.000e00 0 1.000e00 1.000e00 9.922e-06
7.500e-03 1.000e00 0 1.000e00 4.614e-01 2.008e-05
then the file will be expanded to five columns, the 1.000e-02 9.999e-01 0 1.000e00 4.795e-02 1.030e-04

last two containing R+ (Qz ) and R− (Qz ). If ”calcu- .


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
late transmission” is checked in the ”settings dialogue
box” then the file will expand to four columns, the
fourth column containing the T (Qz ) values. If both
are checked, then there will be six columns in the file:
Qz , R, 0, R+ , R− , T .

3.8 Copying Data


Since Parratt32’s graphical features are limited data
from both the reflectivity window and the profile win-
dow can be copied to the Window’s clipboard and
pasted to other Windows applications, such as spread-
sheet or scientific graphic applications (i. e. Excel or
Origin).
Copying the data from the profile window results in a
two column dataset: z and %(z).
Copying the data from the reflectivity window leaves
you with at least two columns in the clipboard: Q
and R(Q). If you selected to include the calcula-
tion of the transmission of the sample, then there

Parratt32: The Manual 14


3.9 Printing the Results

will be three columns: Q, R(Q), and T . If magne-


tization is included, then there will be four columns:
Q, R(Q), R+ (Q), and R− (Q). And finally, if transmis-
sion and magnetization are included then there are five
columns: Q, R(Q), R+ (Q), R− (Q), and T .

3.9 Printing the Results


For a quick–and–dirty overview of the results from a
fitting session one can print out a summary sheet. Ei-
ther selecting ”File-Print...” or clicking the printer
button from the tool bar will launch Windows’
standard printing dialogue to select the printing de-
vice. Best results are achieved on A4 sized paper, but
letter size will also work.

Parratt32: The Manual 15


4 Some Internals
4.1 The SLD Database
Parratt32 comes with a user expandable material’s properties database. By default the
relevant files are located in <installdir>/database/. The main file (Parratt32.db) is a Para-
dox7 table. This table can be edited via ”Database–edit” from the main menu. The index
files — files that provide the sorting order of the table — are automatically regenerated when
new entries are send to the database. Elements are sorted according to the periodic system,
compounds are sorted by name. From the values given in the database table Parratt32
calculates the values rho and Im(rho) according to the following formulae:

3 M [g/mol]
neutrons: V [Å ] = 3 −1
· 1024
ρmass [g/cm ] · NA [mol ]
−2 bcoh [fm] −5
ρ[Å ] = 3 · 10
V [Å ]
−2 abs xs[barn] −8
Im(ρ)[Å ] = 3 · 10
1.789[Å] · 2 · V [Å ]
−2 2πδ
x–rays: ρ[Å ] =
1.5412
−2 2πβ
Im(ρ)[Å ] =
1.541 · λ
Note: 1.789 Å (2200m/s) is the wavelength for which the absorption cross section values are
tabulated, and 1.541 Å (Cu–Kα , 8047 eV) is the wavelength for which the δ and β values are
supplied in the table.

4.2 Hidden Features


4.2.1 Command Line Parameters
For faster loading Parratt32 can be started without the database file being loaded: simply
type ”parratt nodatabase” in the programme’s directory, without the quotes, of course. The
database file can then be loaded later on by selecting ”Database-load from disk” from the
main menu. Also, if you don’t like the welcome screen simply type ”parratt nosplash”. Both
of these parameters can be specified in the properties dialog of the shortcut placed in the Start
menu by the installation procedure, see the Windows help for details...

4.3 Trouble is...


4.3.1 Installing
Here is the story why you get error messages installing Parratt32: Since Inprise (formerly
known as Borland) changed their database engine (BDE) to version 5.0 and gave it away for
free, I thought it might be a good idea to have it on my system. It also had the advantage
of being the english language version which was not available to me in version 4.52. But, the

Parratt32: The Manual 16


4.3 Trouble is...

Installshield Express Delphi Edition that came with Delphi 3.0 (which was the development tool
for Parratt32) was not working correctly any more with this edition of the BDE. The support
site of ’Installshield.com’ had a little work–around for about two weeks in the late summer of
1998. It didn’t work error free (as you see) but at least is does not prevent the installation
of the software, completely. Then they stopped supporting this version of their software and
removed the patch from their server. In the meanwhile Inprise came out with Delphi 4 which
was bundled with a) the BDE 5.0 and b) a new version of Installshield Express Delphi Edition.
But this also means that people like me, who do not own a Delphi 4 or an InstallShieldExpress
2.03 will never be able to make correctly working sets of installation media.
All this only concerns the installation of Parratt32. Once installed there are no known
problems with BDE 5.0!

4.3.2 The Decimal Separator


What is a decimal separator most people will ask, especially when they are from english
speaking countries. However, in Germany for instance the decimals of a real number are
separated from the integer part by a comma. Microsoft Windows handles this with a setting in
the control panel (”International” or ”Systemsteuerung–Ländereinstellungen” in the german
version). When you have installed Parratt32 and switch the decimal separator setting later
on in either direction you will get some error messages concerning not–valid–floating–point–
values: don’t be surprised. Parratt32 tries to translate all these numbers depending on the
settings you chose on your system with these exceptions: a) Values that were stored in the
edit fields of some of the dialogue boxes (i.e. the ’settings’ or the ’FitIt’ dialogue) cannot be
converted on-the-fly since you typed them in! when Parratt32 complains about these values
try to look them up and correct them manually. b) Values are stored in the Windows’ registry
according to the current decimal separator setting, so after changing the separator setting
while Parratt32 is not running these values are not longer valid and the programme will
load default values instead of the ones found in the registry.
Always remember what decimal separator setting you have on your system and type in numbers
according to this setting. Parratt32 is no wizard knowing that you meant ”1.541” but
Windows said you should have typed ”1,541”!

Parratt32: The Manual 17


5 The Source Code
Parratt32’s language is Pascal. Since Pascal on a PC means Borland Turbo Pascal and
programming graphical user interfaces for PCs means Microsoft Windows the right tool is
called Delphi. Borland stopped calling its Pascal compiler Pascal after Borland Pascal 7.0, then
came Delphi 1 (BP 8.0), Delphi 2 (BP 9.0) and Delphi 3. The command-line compiler says
Delphi for Win32 version 10.0...
You do not have to know anything about Windows programming to understand the source code
of Parratt32 and the use of Delphi — there is nothing like int APIENTRY WinMain(hinstance,
hinstPrev, lpCmdLine, nCmdShow) like in C++ — , but a little knowledge of Pascal is recom-
mended.

5.1 The Code Itself


5.1.1 Naming conventions
I tried to keep a reasonable naming scheme for the objects in the programme, variables,
functions and procedures as well as for classes. The visual components all have a prefix to
determine of what type they are:
prefix component type Delphi type
lbl label TLabel
mnu menu item TMenuItem
btn button TButton, TSpeedButton
edt edit field TEdit, TRealEdit, TCardinalEdit, TSpinEdit
cb checkbox TCheckbox
rb radio button TRadioButton

5.1.2 Globals
This is the file called globals.pas where all the global constants, types and variables are stored.
It’s not too much I guess...
1 unit Globals;
2 {************************************************************************}
3 { some constants and some types that are used globally }
4 {************************************************************************}
5 interface
6
7 const { some upper array boundaries and misc. constants }
8 maxLayers = 499;
9 maxQValues = 999;
10 map = maxLayers*4+10;
11 { different values for variable ’sampletype’ }
12 simplesample = 1;
13 functionsample = 2;
14 multisample = 3;
15 { different function types }
16 ft_builtin = 0;
17 ft_user = 1;
18 { Window IDs}
19 wnd_Plot = 1;
20 wnd_Model = 2;
21 wnd_Profile = 3;
22 { dataset IDs}
23 ds_data = 0;
24 ds_calc = 1;
25 ds_prof = 0;
26 ds_minus = 2;
27 ds_plus = 3;
28 ds_trans = 4;

Parratt32: The Manual 18


5.1 The Code Itself

29 ds_fitwindow = 9;
30 { Help IDs}
31 hcIndex = 10;
32 hcfrmPlot = 80;
33 hcfrmModel = 40;
34 hcfrmProfile = 70;
35 fittingdialog = 350;
36 settingsdialog = 330;
37 functiondialog = 340;
38 DatabaseEditor = 375;
39
40 RegFilename = ’\SOFTWARE\HMI\Parratt32’;
41
42 type float = double;
43 flarray = array[-5..maxLayers*4+5] of float;
44 Pflarray = ^flarray;
45 flarray1 = array[0..maxQValues] of float;
46 PFlarray1 = ^flarray1;
47 IntegerArrayMFIT = ARRAY [1..map] OF integer;
48 FitParameter = array[1..maxLayers*4+10] of float;
49
50 var a: Pflarray; { layer parameters for fitting }
51 q, refl, errors: Pflarray1; { loaded data }
52 resltn, bg: Pflarray1; { resolution and background }
53 ndata: integer; { number of datapoints in loaded data }
54 nlparms: integer; { number of layer parameters }
55 c_MinZ, c_MaxZ: integer;
56 dataavail, hasbeenfitted, expert,
57 splashing, databaseing: Boolean;
58 sampletype, functiontype: Byte;
59 fitwindow: array[0..1] of float;
60 www_hmi, www_bensc, author_e_mail: String;
61 datafilename, fn_databasename: String;
62
63 implementation
64
65 {************************************************************************}
66
67
68 end.

5.1.3 Fequently Used Functions And Procedures


The file rtfuncs.pas contains most of the frequently used things, which are not directly related
to events produced by the user interface, such as mouse clicks or menu messages. The following
code shows the definition or interface part of this file
1 unit RTFuncs;
2 { here are some functions that are used by all modules }
3 interface
4
5 uses Forms, Windows, SysUtils, Controls, Graphics, Dialogs, Printers, Classes,
6 Menus, db, dbtables, xyGraph,
7 Globals;
8
9 procedure LoadReflData;
10 procedure SaveReflData;
11 procedure LoadModelData;
12 procedure SaveModelData;
13 procedure SaveProfileData;
14 procedure SaveUserFunction;
15 procedure LoadUserFunction;
16 procedure LoadDatabase;
17 procedure PlotData;
18 procedure PrintResults;
19 function EvalFunc(z, rhostart, rhoend, p_A, p_B, p_C, p_D, p_E: double): double;
20 procedure GetParameters(magnsign: Integer);
21 procedure PutParameters;
22
23 implementation
24 .
25 .
26 .
27 end.

In brief these functions and procedures perform the following tasks:

Parratt32: The Manual 19


5.1 The Code Itself

LoadReflData loads a data file from disk


SaveReflData saves the calculated reflectivity to a file
LoadModelData loads a model file from disk
SaveModelData save the current model to disk
SaveProfileData saves the calculated SLD profile to disk
SaveUserFunction saves the user function to disk
LoadUserFunction loads a user function from disk
LoadDatabase loads the element/compound database from
disk
PlotData plots the calculated reflectivity
PrintResults prints a summary sheet of the programme
output
EvalFunc(...) calculates the SLD at a given coordinate z
GetParameters(...) puts data from model window in fitting vector a
PutParameters put data from the fitting vector a into the model
window

5.1.4 The Calculation of the Reflectivity


The Parratt formalism is coded in the file uparratt.pas according to the following formula:
q
wave vector inside layer n: 2 − 4πρ
kz,n = kz,0 n

kz,n − kz,n+1 −2σn+1


2 kz,n kz,n+1
reflectivity from layer n: rn,n+1 = ·e
kz,n + kz,n+1
reflectivity of the system: R = |R0 |2
RN +1 =0
RN = rN,N +1
rn,n+1 + Rn+1 e2idn+1 kz,n+1
Rn =
1 + rn,n+1 Rn+1 e2idn+1 kz,n+1

NB: ρn is complex: ρn = %n + iIm(%n ), where %n and Im(%n ) are the values given in the model
window.
The source is somewhat documented. Only a few remarks: procedures kadd, ksub, kmul, kdiv,
kexp, and ksqrt all work on complex numbers (type kompl) and are defined in cxmath.pas. Since
Pascal knowns no way of passing back user defined types in functions these all had to be made
procedures that pass back the result in the last argument. For this reason there is heavy use of
temporary variables in the code. The bare reflectivity is calculated by cParratt(...), whereas
the plotting routine calls CalcParratt(...), where things like background and resolution are
handled.
1 unit Uparratt;
2 {************************************************************************}
3 { This Unit calculates the reflectivity of a n-layer system at given Qz }
4 { R(Qz) := cParratt(Qz, resolution, nlayers, parameters) }
5 { }
6 { }

Parratt32: The Manual 20


5.1 The Code Itself

7 { input is as follows: }
8 { double Qz: z-component of the scattering vector }
9 { double Resolution: Qz-resolution }
10 { integer nlayers: number of layers (no bulk, no vacuum) }
11 { pointer parameters: points to an array of the following parameters: }
12 { parameters[1] := rho(air) }
13 { [2] := Imrho(air) }
14 { [3] := d(1st layer) }
15 { [4] := rho(1st layer) }
16 { [5] := Imrho(1st layer) }
17 { [6] := sigma(1st layer) }
18 { ... }
19 { [4*nlayer+3] := d(nth layer) }
20 { ... }
21 { [4*nlayer+6] := sigma(nth layer) }
22 { [4*nlayer+7] := rho(bulk) }
23 { [4*nlayer+8] := Imrho(bulk) }
24 { [4*nlayer+9] := sigma(bulk) }
25 { example: }
26 { for a single interface there will be 5 parameters: }
27 { rho(air), Imrho(air), rho(bulk), Imrho(bulk), sigma(bulk) }
28 { }
29 { d is in Angstroms (AA) }
30 { rho is in AA^-2: rho_el * r_0 for x-rays, }
31 { Nb for neutrons }
32 { Imrho is in AA^-2: calculated from beta for xrays, }
33 { calculated from abs. cross section for neutrons }
34 { sigma is in AA }
35 {************************************************************************}
36
37 interface
38
39 uses Globals, cxmath;
40
41 function calcParratt(qz, rsltn, bg: float;
42 nlayers: integer; parameters: Pflarray;
43 UseRsltn, UseBG: Boolean): float;
44
45 implementation
46
47 var rrn: array[0..maxLayers+1] of kompl;
48
49 function cParratt(qz: float; nlayers: integer; parameters: Pflarray): float;
50 var
51 d, rho, imrho, sigma: array[0..maxLayers] of float;
52 rcc, rnn1, arg, temp1, temp2, num, den: kompl;
53 n : integer;
54 e2idk, rme: kompl;
55 kz0: float;
56
57 procedure kzn(kz0: float; ni: integer; var res: kompl);
58 var radix: kompl;
59 begin
60 radix[1] := kz0*kz0-4*pi*rho[ni];
61 radix[2] := 4*pi*imrho[ni]; // changed 08-02-98, cb
62 ksqrt(radix, res);
63 end;
64
65 procedure rn(kz0: float; ni: integer; var res: kompl);
66 var kz_n, kz_np1, rnum, rden, e2kks, rfres, arg1, arg2: kompl;
67 begin
68 kzn(kz0, ni, kz_n);
69 kzn(kz0, ni+1, kz_np1);
70 ksub(kz_n, kz_np1, rnum);
71 kadd(kz_n, kz_np1, rden);
72 kdiv(rnum, rden, rfres);
73 { include roughness according to Nevot & Croce }
74 kmul(kz_n, kz_np1, arg1);
75 rmul(-2*sqr(sigma[ni+1]), arg1, arg2);
76 kexp(arg2, e2kks);
77 kmul(rfres, e2kks, res);
78 end;
79
80 begin
81 { map parameters^[] to d[], rho[], my[], sigma[] }
82 rho[0]:= parameters^[1];
83 imrho[0]:= parameters^[2];
84 { calculate wavevector kz0’: 1. vacuum, air -> Qz/2 }
85 { 2. n0 <> 0 -> sqrt(Qz0^2/4+4*pi*rho0+4*pi*mu0)}
86 temp1[1] := qz*qz/4+4*pi*rho[0];
87 temp1[2] := 4*pi*imrho[0];
88 ksqrt(temp1, temp2);
89 kz0 := kabs(temp2);
90 for n := 1 to nlayers do

Parratt32: The Manual 21


5.1 The Code Itself

91 begin
92 d[n] := abs(parameters^[(n-1)*4+3]); // abs() added 22-01-98
93 rho[n] := parameters^[(n-1)*4+4];
94 imrho[n] := parameters^[(n-1)*4+5];
95 sigma[n] := parameters^[(n-1)*4+6];
96 end;
97 rho[nlayers+1] := parameters^[nlayers*4+3];
98 imrho[nlayers+1] := parameters^[nlayers*4+4];
99 sigma[nlayers+1] := parameters^[nlayers*4+5];
100 { done with remapping }
101 rrn[nlayers+1, 1] := 0;
102 rrn[nlayers+1, 2] := 0;
103 { calculate rrn[nlayers]: bulk interface }
104 rn(kz0, nlayers, rrn[nlayers]);
105 { calculate the rrn[...] for the remaining layers }
106 for n := nlayers-1 downto 0 do
107 begin
108 rn(kz0, n, rnn1); { r_n,n+1 }
109 kzn(kz0, n+1, temp1); { k_z,n+1 }
110 arg[1] := 0;
111 arg[2] := 2*d[n+1];
112 kmul(temp1, arg, temp2); { 2 * i * k_z,n+1 * z_n+1 }
113 kexp(temp2, e2idk);
114 kmul(e2idk, rrn[n+1], rme); { X_n+1 * exp(2 i k_z,n+1 z_n+1) }
115 kadd(rnn1, rme, num); { r_n,n+1 + X_n+1 * exp(---) }
116 kmul(rnn1, rme, den); { r_n,n+1 * X_n+1 * exp(---) }
117 den[1] := den[1] + 1;
118 kdiv(num, den, rrn[n]);
119 end;
120 rcc[1] := rrn[0,1]; rcc[2] := -1*rrn[0,2];
121 kmul(rrn[0], rcc, temp1);
122 cParratt := temp1[1];
123 end;
124
125 function calcParratt(qz, rsltn, bg: float;
126 nlayers: integer; parameters: Pflarray;
127 Usersltn, UseBG: Boolean): float;
128 var refl: float;
129 begin
130 refl := cParratt(qz, nlayers, parameters);
131 if UseRsltn then
132 begin
133 { approximate Gaussian beam profile by 8 pivot points }
134 refl := refl + 0.135*cParratt(qz-rsltn, nlayers, parameters);
135 refl := refl + 0.135*cParratt(qz+rsltn, nlayers, parameters);
136 refl := refl + 0.325*cParratt(qz-rsltn*3/4, nlayers, parameters);
137 refl := refl + 0.325*cParratt(qz+rsltn*3/4, nlayers, parameters);
138 refl := refl + 0.605*cParratt(qz-rsltn/2, nlayers, parameters);
139 refl := refl + 0.605*cParratt(qz+rsltn/2, nlayers, parameters);
140 refl := refl + 0.88*cParratt(qz-rsltn/4, nlayers, parameters);
141 refl := refl + 0.88*cParratt(qz+rsltn/4, nlayers, parameters);
142 refl := refl/4.89;
143 end;
144 if UseBG then refl := refl + bg;
145 calcParratt := refl;
146 end;
147 end.

5.1.5 The Calculation of the Transmission


Calculation of the transmission of ”free samples” is done according to the following formula:

T (Qz ) = (1 − R(Qz ))Tsubst (Qz )

where Tsubst (Qz ) is the transmission through the substrate, which is calculated by the following
code:
1 function calcTrans(qz: float; nlayers: integer; parameters: Pflarray;
2 wavelength, substratethickness: float): float;
3 var pathlength: float;
4 d, rho, imrho: float;
5 temp1, temp2, temp3: kompl;
6 n: integer;
7 currentk: kompl;
8 begin
9 pathlength := 0;
10 for n := 1 to nlayers do
11 begin

Parratt32: The Manual 22


5.1 The Code Itself

12 d := abs(parameters^[(n-1)*4+3]);
13 rho := parameters^[(n-1)*4+4];
14 imrho := parameters^[(n-1)*4+5];
15 temp1[1] := d*2*pi/wavelength;
16 temp1[2] := 0;
17 temp2[1] := qz*qz/4-4*pi*rho;
18 temp2[2] := 4*pi*imrho;
19 ksqrt(temp2, currentk);
20 kdiv(temp1, currentk, temp3);
21 pathlength := pathlength + temp3[1];
22 end;
23 rho := parameters^[nlayers*4+3];
24 imrho := parameters^[nlayers*4+4];
25 d := substratethickness*1e7; // thickness is in millimeters, we need AA
26 temp1[1] := d*2*pi/wavelength;
27 temp1[2] := 0;
28 temp2[1] := qz*qz/4-4*pi*rho;
29 temp2[2] := 4*pi*imrho;
30 ksqrt(temp2, currentk);
31 kdiv(temp1, currentk, temp3);
32 pathlength := pathlength + temp3[1];
33 if pathlength > 0 then result := exp(-2*wavelength*imrho*pathlength)
34 else result := 0;
35 end;

5.1.6 Calculation of the Scattering Length Density Profile


The calculation of the scattering length density profile from the given layers in the model is
done by evaluating the following formula:
N   
X %i − %i+1 z − zi
%(z) = 1 + erf √ ,
2 2σi
i=1

where N is the overall number of layers, %i is the scattering length density of the ith layer at
position zi and with a gaussian roughness σi . The error function erf(x) is approximated by a
5th order polynomial given by Press et al.[Press88].
1 procedure TfrmProfile.ShowProfile;
2 var i, j, s_index, r_index, d_index,
3 lastlayer, zstart, zend, temp: integer;
4 z, zinc, sum,
5 deltarho, sigma, zi: float;
6
7 function erfcc(x: float): float; { according to ’NumRecip’ }
8 var
9 t,z,ans: extended;
10 begin
11 z := abs(x);
12 t := 1.0/(1.0+0.5*z);
13 try
14 ans := t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418
15 +t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587
16 +t*(-0.82215223+t*0.17087277)))))))));
17 except
18 ans := 0;
19 end;
20 if x >= 0.0 then erfcc := ans
21 else erfcc := 2.0-ans
22 end;
23
24 begin
25 Screen.Cursor := crHourGlass;
26 GetParameters(0);
27 xyGraph1[ds_prof].Free;
28 xyGraph1.Plotting := False;
29 with xyGraph1[ds_prof] do
30 begin
31 DrawPoints := False;
32 LineColor := clBlack;
33 end;
34 if SettingsDlg.cbUserZ.Checked then
35 with SettingsDlg do
36 begin
37 zstart := edtMinZ.Value;

Parratt32: The Manual 23


5.1 The Code Itself

38 zend := edtMaxZ.Value;
39 if zstart > zend then
40 begin
41 temp := zstart;
42 zstart := zend;
43 zend := temp;
44 end;
45 end
46 else CalcZBounds(zstart, zend);
47 z := zstart;
48 zinc := (zend-zstart)/SettingsDlg.edtNumPointsZ.Value;
49 while z <= zend do
50 begin
51 lastlayer := 0;
52 case sampletype of
53 simplesample:
54 lastlayer := frmModel.edtLayers.Value;
55 functionsample:
56 lastlayer := frmModel.edtLayers.Value+frmModel.edtNumCalcLayers.Value;
57 multisample:
58 lastlayer := frmModel.edtLayers.Value
59 +frmModel.edtRepts.Value*frmModel.edtStackedLayers.Value;
60 end;
61 sum := a^[1];
62 for i:= 1 to lastlayer+1 do
63 begin
64 if i = lastlayer+1 then
65 begin
66 r_index := 4*i-1; { rho_bulk }
67 s_index := r_index+2; { sigma_bulk }
68 if lastlayer = 0
69 then deltarho := a^[r_index] - a^[r_index-2]
70 else deltarho := a^[r_index] - a^[r_index-3];
71 end else
72 begin
73 s_index := 4*i+2;
74 r_index := 4*i;
75 if i = 1
76 then deltarho := a^[r_index]-a^[r_index-3]
77 else deltarho := a^[r_index]-a^[r_index-4];
78 end;
79 zi := 0;
80 j := 1;
81 if i>1 then while j<i do
82 begin
83 d_index := 4*(j-1)+3;
84 zi := zi + a^[d_index];
85 inc(j);
86 end;
87 if a^[s_index] = 0 then sigma := 1e-3
88 else sigma := a^[s_index];
89 sum := sum + deltarho/2*(2-erfcc((z-zi)*sqrt(2)/2/sigma)); // changed on 24-NOV-99
90 end;
91 xyGraph1[ds_prof][z] := sum;
92 Form1.Statusbar1.Panels[0].Text :=
93 Format(’calculating rho(z), z = %f’, [z]);
94 Form1.Statusbar1.Refresh;
95 z := z + zinc;
96 end;
97 Screen.Cursor := crDefault;
98 xyGraph1.Dimensions.YAxisTitleOffset := 20;
99 xyGraph1.Plotting := True;
100 Form1.Statusbar1.Panels[0].Text := ’’;
101 end;

5.1.7 Fitting of A Model to Reflectivity Data


The code for the optimization of the layers’s parameters is a little too complex to be described
in just one formula. The minimization is done by least squares fitting:
M
!2
2
X RQcalc
z,i
− RQ
meas
z,i
χ = ,
weighting
i=1
meas meas
where weighting is either 1 (”no weighting”) or RQz,i
(”statistical weighting”) or δRQz,i
(”er-
ror weighting”), see function CalcChisq(weight: integer) for details, and M is the num-

Parratt32: The Manual 24


5.1 The Code Itself

ber of datapoints. The parameter minimization is done in the procedure Newton(parindex:


integer), which is a simple Newton method for finding minima in parametric functions. It
in(de)creases the parameter value until the difference in χ2 between two successive steps falls
below a given threshold (edtTolerance.Value). The rest of the code deals with chosing of
parameters, fitting regions in Qz etc.
1 procedure TFitDlg.btn1iterClick(Sender: TObject);
2 var i, j, nvarpar, RB: integer;
3 lista, listadone: IntegerArrayMFIT;
4 chisq, chiold, chidiff: real;
5 finished: Boolean;
6 iter: longint;
7 nol, weightingmethod : integer;
8 lastvarpar: integer;
9 FitndataStart, FitndataEnd: integer;
10
11 function CalcChisq(weight: integer): float;
12 var cref, cs: float;
13 i: integer;
14 begin
15 cs := 0;
16 if cbDisplayGraph.Checked then
17 begin
18 frmPlot.xyGraph1.Plotting := False;
19 frmPlot.xyGraph1[ds_calc].Free;
20 with frmPlot.xyGraph1[ds_calc] do
21 begin
22 DrawPoints := false;
23 LineColor := clBlack;
24 end;
25 end;
26 for i := fitndataStart to fitndataEnd do
27 begin
28 cref := calcParratt(q^[i], resltn^[i], bg^[i],
29 nol, a, cbRsltn.Checked, cbBackground.Checked);
30 case weight of
31 1: cs := cs + sqr((refl^[i]-cref)/errors^[i]);
32 2: cs := cs + sqr((refl^[i]-cref)/refl^[i]);
33 3: cs := cs + sqr(refl^[i]-cref);
34 end;
35 if cbDisplayGraph.Checked then frmPlot.xyGraph1[ds_calc][q^[i]] := cref;
36 end;
37 CalcChisq := cs/(fitndataEnd-fitndataStart);
38 if cbDisplayGraph.Checked then
39 begin
40 frmPlot.xyGraph1.Plotting := True;
41 lblChisq.Caption := FormatFloat(’0.0000E+00’,
42 cs/(fitndataEnd-fitndataStart));
43 end;
44 end;
45
46 procedure Newton(parindex: integer);
47 var incr, fac, newchisq: float;
48 begin
49 chisq := CalcChisq(weightingmethod);
50 if P[parindex]=0 then incr := 1e-7 else incr := P[parindex] * 0.1;
51 fac := 1;
52 iter := 0;
53 repeat
54 repeat
55 inc(iter);
56 Form1.StatusBar1.Panels[0].Text := IntToStr(parindex)+’ : ’
57 +IntToStr(iter);
58 Form1.Statusbar1.Refresh;
59 P[parindex] := P[parindex]+incr*fac;
60 CalcAfromP;
61 if cbDisplayGraph.Checked then ListBoxUpdate;
62 newchisq := CalcChisq(weightingmethod);
63 chidiff := chisq-newchisq;
64 if chidiff<0 then
65 begin
66 fac := -fac;
67 P[parindex] := P[parindex]+incr*fac;
68 end else
69 begin
70 chisq := newchisq;
71 fac := fac*2;
72 end;
73 Application.ProcessMessages;
74 until (chidiff<0) or (iter>99);

Parratt32: The Manual 25


5.1 The Code Itself

75 fac := fac/2;
76 Application.Processmessages;
77 if ModalResult = mrCancel then stopfitting := True;
78 until ((abs(chidiff)<=edtTolerance.Value) and (iter>2)) or stopfitting;
79 end;
80
81 begin
82 stopfitting := False;
83 converged := False;
84 lblconverged.Visible := False;
85 btnStop.Enabled := True;
86 Screen.Cursor := crHourGlass;
87 { set weighting method }
88 if RBNone.Checked then weightingmethod := 3;
89 if RBStat.Checked then weightingmethod := 2;
90 if RBErrors.Checked then weightingmethod := 1;
91 { get variable parameters from CheckListBox1 }
92 nvarpar := 0;
93 i := 0;
94 while i <= CheckListBox1.Items.Count-1 do
95 begin
96 if CheckListBox1.Checked[i] then
97 begin
98 lista[nvarpar+1] := i+1;
99 inc(nvarpar);
100 end;
101 inc(i);
102 end;
103 { set overall number of layers }
104 nol := frmModel.edtLayers.Value;
105 case sampletype of
106 functionsample: nol := nol + frmModel.edtNumCalcLayers.Value;
107 multisample: nol := nol + frmModel.edtStackedLayers.Value
108 *frmModel.edtRepts.Value;
109 end;
110 { get fitting region }
111 if (fitwindow[0] = -1) and (fitwindow[1]=-1) then
112 begin { no user region specified }
113 fitndataStart := 1;
114 fitndataEnd := ndata;
115 end else
116 begin { user has specified a region }
117 i := 0;
118 repeat
119 inc(i);
120 until (i > ndata) or (q^[i] > fitwindow[0]);
121 fitndataStart := i-1;
122 i := 0;
123 repeat
124 inc(i);
125 until (i > ndata) or (q^[i] > fitwindow[1]);
126 fitndataEnd := i-1;
127 end;
128 { check if user wants to fit or only the chisq }
129 RB := 0;
130 if RBchisq.Checked then RB := 1;
131 if RBfulliter.Checked then RB := 2;
132 case RB of
133 1: lblChisq.Caption := FormatFloat(’0.0000E+00’, CalcChisq(weightingmethod));
134 2: begin
135 { ’make’ all parameters are still unminimized }
136 for i := 1 to nvarpar do listadone[i] := 1;
137 finished := False;
138 lastvarpar := -1;
139 repeat
140 if nvarpar > 1 then
141 repeat
142 j := Trunc(Random*nvarpar)+1;
143 until j <> lastvarpar
144 else j := 1;
145 lastvarpar := j;
146 if listadone[j]=1 then
147 begin
148 chiold := CalcChisq(weightingmethod);
149 Newton(lista[j]);
150 if chisq<chiold then for i:=1 to nvarpar do listadone[i]:=1
151 else listadone[j] := 0;
152 end;
153 lblChisq.Caption := FormatFloat(’0.0000E+00’, chisq);
154 { replot the graph }
155 frmPlot.xyGraph1.Plotting := False;
156 frmPlot.xyGraph1[ds_calc].Free;
157 with frmPlot.xyGraph1[ds_calc] do
158 begin

Parratt32: The Manual 26


5.2 Compiling

159 DrawPoints := false;


160 LineColor := clBlack;
161 end;
162 for i := fitndataStart to FitndataEnd do
163 frmPlot.xyGraph1[ds_calc][q^[i]] :=
164 calcParratt(q^[i], resltn^[i], bg^[i], nol, a,
165 cbRsltn.Checked, cbBackground.Checked);
166 frmPlot.xyGraph1.Plotting := True;
167 { update the parameters in CheckListBox1 }
168 ListBoxUpdate;
169 { check for ’un-minimized’ parameters }
170 j := 0;
171 for i := 1 to nvarpar do j := j+listadone[i];
172 if j=0 then converged := True;
173 if (ModalResult = mrCancel) or stopfitting then finished := True;
174 until (chisq<edtTolerance.Value) or finished or converged;
175 if converged then lblConverged.Visible := True;
176 CalcAfromP;
177 Form1.StatusBar1.Panels[0].Text := ’’;
178 end;
179 end;
180 btnStop.Enabled := False;
181 Screen.Cursor := crDefault;
182 MessageBeep(0);
183 Application.ProcessMessages;
184 end;
185

5.2 Compiling
5.2.1 What you need...
1. The sources. These can be obtained from Christian Braun by e–mail:
[email protected] or ask Roland Steitz ([email protected]) for my whereabouts.

2. A complete installation of Delphi. Version 3 professional is recommended, version 2 could


work, version 4 does work.

3. The following non–standard Delphi components:

a) TXYGraph: a component for xy-diagrams from Grahame Grieve at Kestral Com-


puting
b) RZNEdit: an advanced edit field component from Robert Kratz
c) TParser 10.1: a function parsing component from Stefan Hoffmeister based on pre-
vious work of Renate Schaaf and Alin Flaider
d) TGotoWeb: a browser launching component by Sylvain Cresto
e) TVersionInfo: a non-visual component that reads the version information of a file,
by an unknown author

All of them are freeware and can be found on the Delphi Super Page.

Parratt32: The Manual 27


6 Appendix
Acknowledgements
Holger Rhan ([email protected]) for ymath62.pas
The Technische Universität Berlin for the hardware (IBM 486DX2/66 Mhz boosted to 80 Mhz)
and software (Windows NT 4.0, Borland Delphi 3)
The Hahn–Meitner–Institut Berlin (HMI) for funding
and to all those who complained about missing features, buggy procedures, and gave lots of
helpful hints.

References
[Parratt54] L. G. Parratt, Phys. Rev, 95(2), 359-369, 1954

[Nevot80] L. Névot, P. Croce, Revue de physique appliquée, 15 (1980), 761

[Press88] W. H. Press, B. P. Flannery, S. A. Teukolsky, and W. T. Vetterling: ”Numerical


Recipes”, Cambridge University Press, 1988

[Liu89] A. J. Liu, M. E. Fisher, Phys. Rev. A, 40 (1989), 7202

Parratt32: The Manual 28


Index
background noise, 11 printing, 15

data reflectivity
ASCII files, 6 calculation of, 7, 8, 20
copying, 14 resolution, 11
errors in experimental, 6 angular, 11
experimental, 6
raw, 11 sample
database, 12 free, 10
scattering length, 12 scattering length density
dialogue calculation of, 7, 9, 23
model function, 7 settings
settings, 9 background, 11
database, 12
error function, 23 profile calculation, 10
reflectivity calculation, 9
fitting, 12, 24 resolution, 11
χ2 , 24 transmission calculation, 10
least squares, 24 source code, 4
subset of data, 13 fitting procedure, 24
discard, 13 reflectivity calculation, 20
function SLD profile calculation, 23
built in, 7 transmission calculation, 22
user, 7 status bar, 5
graphical user interface, 4 thickness
of substrate, 10
instrument tool bar, 5
collimation system, 11 transmission
noise, 11 calculation of, 10, 22
velocity selector, 11
user function, 7
layer
multiple, 8 wavelength, 9, 10
number of, 7 distribution, 11
thickness, 10 window
graph
magnetization rescaling, 9
including, 10 main, 4
menu bar, 5 model, 5, 6
model profile, 5, 9
independent layers, 7 reflectivity, 5, 6, 8
multi layer, 8
setting up, 6, 12
simple, 7
with function, 7

noise, 11

Parratt32: The Manual 29

You might also like