Fitting Powder Diffraction data#
This notebook guides you through the Rietveld refinement of crystal structures using simulated powder diffraction data. It consists of two parts:
Introduction: A simple reference fit using silicon (Si) crystal structure.
Exercise: A more complex fit using Laβ.β Baβ.β CoOβ (LBCO) crystal structure.
π οΈ Import Library#
We start by importing the necessary library for the analysis. In this notebook, we use the EasyDiffraction library. As mentioned in the introduction to EasyScience, EasyDiffraction is built on that framework and offers a high-level interface focused specifically for diffraction analysis.
This notebook is self-contained and designed for hands-on learning. However, if youβre interested in exploring more advanced features or learning about additional capabilities of the EasyDiffraction library, please refer to the official documentation: https://docs.easydiffraction.org/lib
Depending on your requirements, you may choose to import only specific classes. However, for the sake of simplicity in this notebook, we will import the entire library.
π See documentation for more details about importing the EasyDiffraction library and its components.
import easydiffraction as ed
β
'cryspy' calculation engine is successfully imported.
β
'pdffit' calculation engine is successfully imported.
from jupyterquiz import display_quiz
π Introduction: Simple Reference Fit β Si#
Before diving into the more complex fitting exercise with the Laβ.β Baβ.β CoOβ (LBCO) crystal structure, letβs start with a simpler example using the silicon (Si) crystal structure. This will help us understand the basic concepts and steps involved in fitting a crystal structure using powder diffraction data.
For this part of the notebook, we will use the powder diffraction data previously simulated using the Si crystal structure.
π¦ Create a Project β βreferenceβ#
In EasyDiffraction, a project serves as a container for all information related to the analysis of a specific experiment or set of experiments. It enables you to organize your data, experiments, sample models, and fitting parameters in a structured manner. You can think of it as a folder containing all the essential details about your analysis. The project also allows us to visualize both the measured and calculated diffraction patterns, among other things.
π See documentation for more details about creating a project and its purpose in the analysis workflow.
project_1 = ed.Project(name='reference')
You can set the title and description of the project to provide context and information about the analysis being performed. This is useful for documentation purposes and helps others (or yourself in the future) understand the purpose of the project at a glance.
project_1.info.title = 'Reference Silicon Fit'
project_1.info.description = 'Fitting simulated powder diffraction pattern of Si.'
π¬ Create an Experiment#
An experiment represents a specific diffraction measurement performed on a specific sample using a particular instrument. It contains details about the measured data, instrument parameters, and other relevant information.
π See documentation for more details about experiments and their purpose in the analysis workflow.
si_xye_path = '../4-reduction/reduced_Si.xye'
Now we can create the experiment and load the measured data. In this case, the experiment is defined as a powder diffraction measurement using time-of-flight neutrons. The measured data is loaded from a file containing the reduced diffraction pattern of Si from the data reduction notebook.
π See documentation for more details about different types of experiments.
project_1.experiments.add(
name='sim_si',
sample_form='powder',
beam_mode='time-of-flight',
radiation_probe='neutron',
data_path=si_xye_path,
)
Loading measured data from ASCII file
/home/runner/work/dmsc-school/dmsc-school/4-reduction/reduced_Si.xye
Data loaded successfully
Experiment π¬ 'sim_si'. Number of data points: 900
Inspect Measured Data#
After creating the experiment, we can examine the measured data. The measured data consists of a diffraction pattern having time-of-flight (TOF) values and corresponding intensities. The TOF values are given in microseconds (ΞΌs), and the intensities are in arbitrary units.
The data is stored in XYE format, a simple text format containing three columns: TOF, intensity, and intensity error (if available).
π See documentation for more details about the measured data and its format.
To visualize the measured data, we can use the plot_meas
method of the
project. Before plotting, we need to set the plotting engine to βplotlyβ,
which provides interactive visualizations.
π See documentation for more details about setting the plotting engine.
project_1.plotter.engine = 'plotly'
Current plotter changed to
plotly
project_1.plot_meas(expt_name='sim_si')
If you zoom in on the highest TOF peak (around 120,000 ΞΌs), you will notice that it has a broad and unusual shape. This distortion, along with additional effects on the low TOF peaks, is most likely an artifact related to the simplifications made during the simulation and/or reduction process and is currently under investigation. However, this is outside the scope of this school. Therefore, we will simply exclude both the low and high TOF regions from the analysis by adding an excluded regions to the experiment.
In real experiments, it is often necessary to exclude certain regions from the measured data. For example, the direct beam can significantly increase the background at very low angles, making those parts of the diffractogram unreliable. Additionally, sample environment components may introduce unwanted peaks. In such cases, excluding specific regions is often simpler and more effective than modeling them with an additional sample phase.
π See documentation for more details about excluding regions from the measured data.
project_1.experiments['sim_si'].excluded_regions.add(start=0, end=55000)
project_1.experiments['sim_si'].excluded_regions.add(start=105500, end=200000)
To visualize the effect of excluding the high TOF region, we can plot the measured data again. The excluded region will be omitted from the plot and is not used in the fitting process.
project_1.plot_meas(expt_name='sim_si')
Set Instrument Parameters#
After the experiment is created and measured data is loaded, we need to set the instrument parameters.
In this type of experiment, the instrument parameters define how the
measured data is converted between d-spacing and time-of-flight (TOF)
during the data reduction process as well as the angular position of the
detector. So, we put values based on those from the reduction. These
values can be found in the header of the corresponding .XYE file. Their
names are two_theta
and DIFC
, which stand for the two-theta angle
and the linear conversion factor from d-spacing to TOF, respectively.
You can set them manually, but it is more convenient to use the
get_value_from_xye_header
function from the EasyDiffraction library.
π See documentation for more details about the instrument parameters.
project_1.experiments['sim_si'].instrument.setup_twotheta_bank = ed.get_value_from_xye_header(si_xye_path, 'two_theta')
project_1.experiments['sim_si'].instrument.calib_d_to_tof_linear = ed.get_value_from_xye_header(si_xye_path, 'DIFC')
Before proceeding, letβs take a quick look at the concept of parameters in EasyDiffraction, which is similar to the parameter concept in EasyScience. The current version of EasyDiffraction is transitioning to reuse the parameter system from EasyScience.
That is, every parameter is an object, which has different attributes,
such as value
, units
, etc. To display the parameter of interest, you can
simply print the parameter object.
For example, to display the linear conversion factor from d-spacing to TOF,
which is the calib_d_to_tof_linear
parameter, you can do the following:
project_1.experiments['sim_si'].instrument.calib_d_to_tof_linear
Parameter: sim_si.instr.d_to_tof_linear = 61441.67446228409 Β΅s/β«
The value
attribute represents the current value of the parameter as a
float. You can access it directly by using the value
attribute of the
parameter. This is useful when you want to use the parameter value in
calculations or when you want to assign it to another parameter. For example,
to get only the value of the same parameter as floating point number, but not
the whole object, you can do the following:
project_1.experiments['sim_si'].instrument.calib_d_to_tof_linear.value
61441.67446228409
Note that to set the value of the parameter, you can simply assign a new
value to the parameter object without using the value
attribute, as we did
above.
π See documentation for more details about parameters in EasyDiffraction and their attributes.
Set Peak Profile Parameters#
The next set of parameters is needed to define the peak profile used in the fitting process. The peak profile describes the shape of the diffraction peaks. They include parameters for the broadening and asymmetry of the peaks.
There are several commonly used peak profile functions:
Gaussian: Describes peaks with a symmetric bell-shaped curve, often used when instrumental broadening dominates. Click for more details.
Lorentzian: Produces narrower central peaks with longer tails, frequently used to model size broadening effects. Click for more details.
Pseudo-Voigt: A linear combination of Gaussian and Lorentzian components, providing flexibility to represent real diffraction peaks. Click for more details.
Pseudo-Voigt convoluted with Ikeda-Carpenter: Incorporates the asymmetry introduced by the neutron pulse shape in time-of-flight instruments. This is a common choice for TOF neutron powder diffraction data. Click for more details.
Here, we use a pseudo-Voigt peak profile function with Ikeda-Carpenter asymmetry.
The parameter values are typically determined experimentally on the same instrument and under the same configuration as the data being analyzed, using measurements of a standard sample. In our case, the Si sample serves as this standard reference. We will refine the peak profile parameters here, and these refined values will be used as starting points for the more complex fit in the next part of the notebook. For this initial fit, we will provide reasonable physical guesses as starting values.
π See documentation for more details about the peak profile types.
project_1.experiments['sim_si'].peak_profile_type = 'pseudo-voigt * ikeda-carpenter'
project_1.experiments['sim_si'].peak.broad_gauss_sigma_0 = 69498
project_1.experiments['sim_si'].peak.broad_gauss_sigma_1 = -55578
project_1.experiments['sim_si'].peak.broad_gauss_sigma_2 = 14560
project_1.experiments['sim_si'].peak.broad_mix_beta_0 = 0.0019
project_1.experiments['sim_si'].peak.broad_mix_beta_1 = 0.0137
project_1.experiments['sim_si'].peak.asym_alpha_0 = -0.0055
project_1.experiments['sim_si'].peak.asym_alpha_1 = 0.0147
Peak profile type for experiment 'sim_si' changed to
pseudo-voigt * ikeda-carpenter
Set Background#
The background of the diffraction pattern represents the portion of the pattern that is not related to the crystal structure of the sample. Itβs rather represents noise and other sources of scattering that can affect the measured intensities. This includes contributions from the instrument, the sample holder, the sample environment, and other sources of incoherent scattering.
The background can be modeled in various ways. In this example, we will use a simple line segment background, which is a common approach for powder diffraction data. The background intensity at any point is defined by linear interpolation between neighboring points. The background points are selected to span the range of the diffraction pattern while avoiding the peaks.
We will add several background points at specific TOF values (in ΞΌs) and corresponding intensity values. These points are chosen to represent the background level in the diffraction pattern free from any peaks.
The background points are added using the add
method of the background
object. The x
parameter represents the TOF value, and the y
parameter
represents the intensity value at that TOF.
Letβs set all the background points at a constant value of 0.01, which can be roughly estimated by the eye, and we will refine them later during the fitting process.
π See documentation for more details about the background and its types.
project_1.experiments['sim_si'].background_type = 'line-segment'
project_1.experiments['sim_si'].background.add(x=50000, y=0.01)
project_1.experiments['sim_si'].background.add(x=60000, y=0.01)
project_1.experiments['sim_si'].background.add(x=70000, y=0.01)
project_1.experiments['sim_si'].background.add(x=80000, y=0.01)
project_1.experiments['sim_si'].background.add(x=90000, y=0.01)
project_1.experiments['sim_si'].background.add(x=100000, y=0.01)
project_1.experiments['sim_si'].background.add(x=110000, y=0.01)
Background type for experiment 'sim_si' changed to
line-segment
π§© Create a Sample Model β Si#
After setting up the experiment, we need to create a sample model that describes the crystal structure of the sample being analyzed.
In this case, we will create a sample model for silicon (Si) with a cubic crystal structure. The sample model contains information about the space group, lattice parameters, atomic positions of the atoms in the unit cell, atom types, occupancies and atomic displacement parameters. The sample model is essential for the fitting process, as it is used to calculate the expected diffraction pattern.
EasyDiffraction refines the crystal structure of the sample, but does not solve it. Therefore, we need a good starting point with reasonable structural parameters.
Here, we define the Si structure as a cubic structure. As this is a cubic structure, we only need to define the single lattice parameter, which is the length of the unit cell edge. The Si crystal structure has a single atom in the unit cell, which is located at the origin (0, 0, 0) of the unit cell. The symmetry of this site is defined by the Wyckoff letter βaβ. The atomic displacement parameter defines the thermal vibrations of the atoms in the unit cell and is presented as an isotropic parameter (B_iso).
Sometimes, the initial crystal structure parameters can be obtained from one of the crystallographic databases, like for example the Crystallography Open Database (COD). In this case, we use the COD entry for silicon as a reference for the initial crystal structure model: https://www.crystallography.net/cod/4507226.html
Usually, the crystal structure parameters are provided in a CIF file format, which is a standard format for crystallographic data. An example of a CIF file for silicon is shown below. The CIF file contains the space group information, unit cell parameters, and atomic positions.
π See documentation for more details about the CIF format and its use in EasyDiffraction.
data_si
_space_group.name_H-M_alt "F d -3 m"
_space_group.IT_coordinate_system_code 2
_cell.length_a 5.43
_cell.length_b 5.43
_cell.length_c 5.43
_cell.angle_alpha 90.0
_cell.angle_beta 90.0
_cell.angle_gamma 90.0
loop_
_atom_site.label
_atom_site.type_symbol
_atom_site.fract_x
_atom_site.fract_y
_atom_site.fract_z
_atom_site.wyckoff_letter
_atom_site.occupancy
_atom_site.ADP_type
_atom_site.B_iso_or_equiv
Si Si 0 0 0 a 1.0 Biso 0.89
As with adding the experiment in the previous step, we will create a default sample model and then modify its parameters to match the Si structure.
π See documentation for more details about sample models and their purpose in the data analysis workflow.
Add Sample Model#
project_1.sample_models.add(name='si')
Set Space Group#
π See documentation for more details about the space group.
project_1.sample_models['si'].space_group.name_h_m = 'F d -3 m'
project_1.sample_models['si'].space_group.it_coordinate_system_code = '2'
Set Lattice Parameters#
π See documentation for more details about the unit cell parameters.
project_1.sample_models['si'].cell.length_a = 5.43
Set Atom Sites#
π See documentation for more details about the atom sites category.
project_1.sample_models['si'].atom_sites.add(
label='Si',
type_symbol='Si',
fract_x=0,
fract_y=0,
fract_z=0,
wyckoff_letter='a',
b_iso=0.89,
)
π Assign Sample Model to Experiment#
Now we need to assign, or link, this sample model to the experiment created above. This linked crystallographic phase will be used to calculate the expected diffraction pattern based on the crystal structure defined in the sample model.
π See documentation for more details about linking a sample model to an experiment.
project_1.experiments['sim_si'].linked_phases.add(id='si', scale=1.0)
π Analyze and Fit the Data#
After setting up the experiment and sample model, we can now analyze the measured diffraction pattern and perform the fit. Building on the analogies from the EasyScience library and the previous notebooks, we can say that all the parameters we introduced earlier β those defining the sample model (crystal structure parameters) and the experiment (instrument, background, and peak profile parameters) β together form the complete set of parameters that can be refined during the fitting process.
Unlike in the previous analysis notebooks, we will not create a math_model object here. The mathematical model used to calculate the expected diffraction pattern is already defined in the library and will be applied automatically during the fitting process.
Reminder:
The fitting process involves comparing the measured diffraction pattern with the calculated diffraction pattern based on the sample model and instrument parameters. The goal is to adjust the parameters of the sample model and the experiment to minimize the difference between the measured and calculated diffraction patterns. This is done by refining the parameters of the sample model and the instrument settings to achieve a better fit.
π See documentation for more details about the fitting process in EasyDiffraction.
Set Fit Parameters#
To perform the fit, we need to specify the refinement parameters. These
are the parameters that will be adjusted during the fitting process to
minimize the difference between the measured and calculated diffraction
patterns. This is done by setting the free
attribute of the
corresponding parameters to True
.
Note: setting param.free = True
is equivalent to using
param.fixed = False
in the EasyScience library.
We will refine the scale factor of the Si phase, the intensities of the background points as well as the peak profile parameters. The structure parameters of the Si phase will not be refined, as this sample is considered a reference sample with known parameters.
project_1.experiments['sim_si'].linked_phases['si'].scale.free = True
for line_segment in project_1.experiments['sim_si'].background:
line_segment.y.free = True
project_1.experiments['sim_si'].peak.broad_gauss_sigma_0.free = True
project_1.experiments['sim_si'].peak.broad_gauss_sigma_1.free = True
project_1.experiments['sim_si'].peak.broad_gauss_sigma_2.free = True
project_1.experiments['sim_si'].peak.broad_mix_beta_0.free = True
project_1.experiments['sim_si'].peak.broad_mix_beta_1.free = True
project_1.experiments['sim_si'].peak.asym_alpha_0.free = True
project_1.experiments['sim_si'].peak.asym_alpha_1.free = True
Show Free Parameters#
We can check which parameters are free to be refined by calling the
show_free_params
method of the analysis
object of the project.
π See documentation for more details on how to
show all parameters of the project,
show all fittable parameters, and
show only free parameters of the project.
project_1.analysis.show_free_params()
Free parameters for both sample models (π§© data blocks) and experiments (π¬ data blocks)
datablock | category | entry | parameter | value | uncertainty | min | max | units | |
---|---|---|---|---|---|---|---|---|---|
1 | sim_si |
background |
50000 |
y |
0.0100 |
||||
2 | sim_si |
background |
60000 |
y |
0.0100 |
||||
3 | sim_si |
background |
70000 |
y |
0.0100 |
||||
4 | sim_si |
background |
80000 |
y |
0.0100 |
||||
5 | sim_si |
background |
90000 |
y |
0.0100 |
||||
6 | sim_si |
background |
100000 |
y |
0.0100 |
||||
7 | sim_si |
background |
110000 |
y |
0.0100 |
||||
8 | sim_si |
linked_phases |
si |
scale |
1.0000 |
||||
9 | sim_si |
peak |
asym_alpha_0 |
-0.0055 |
|||||
10 | sim_si |
peak |
asym_alpha_1 |
0.0147 |
|||||
11 | sim_si |
peak |
gauss_sigma_0 |
69498.0000 |
Β΅sΒ² |
||||
12 | sim_si |
peak |
gauss_sigma_1 |
-55578.0000 |
Β΅s/β« |
||||
13 | sim_si |
peak |
gauss_sigma_2 |
14560.0000 |
Β΅sΒ²/β«Β² |
||||
14 | sim_si |
peak |
mix_beta_0 |
0.0019 |
deg |
||||
15 | sim_si |
peak |
mix_beta_1 |
0.0137 |
deg |
Visualize Diffraction Patterns#
Before performing the fit, we can visually compare the measured
diffraction pattern with the calculated diffraction pattern based on the
initial parameters of the sample model and the instrument.
This provides an indication of how well the initial parameters
match the measured data. The plot_meas_vs_calc
method of the project
allows this comparison.
project_1.plot_meas_vs_calc(expt_name='sim_si')
Run Fitting#
We can now perform the fit using the fit
method of the analysis
object of the project.
π See documentation for more details about the fitting process.
project_1.analysis.fit()
Using experiment π¬ 'sim_si' for 'single' fitting
π Starting fit process with 'lmfit (leastsq)'...
π Goodness-of-fit (reduced ΟΒ²) change:
iteration | ΟΒ² | improvement [%] |
---|---|---|
1 |
32.61 |
|
19 |
4.19 |
87.2% β |
35 |
2.43 |
42.0% β |
100 |
2.41 |
π Best goodness-of-fit (reduced ΟΒ²) is 2.41 at iteration 99
β
Fitting complete.
Fit results
β
Success: True
β±οΈ Fitting time: 1.00 seconds
π Goodness-of-fit (reduced ΟΒ²): 2.41
π R-factor (Rf): 6.84%
π R-factor squared (RfΒ²): 5.53%
π Weighted R-factor (wR): 5.24%
π Fitted parameters:
datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
---|---|---|---|---|---|---|---|---|---|
1 | sim_si |
background |
50000 |
y |
0.0100 |
-0.0319 |
0.0102 |
419.24 % β |
|
2 | sim_si |
background |
60000 |
y |
0.0100 |
0.0122 |
0.0024 |
21.63 % β |
|
3 | sim_si |
background |
70000 |
y |
0.0100 |
0.0186 |
0.0016 |
86.04 % β |
|
4 | sim_si |
background |
80000 |
y |
0.0100 |
0.0204 |
0.0013 |
103.75 % β |
|
5 | sim_si |
background |
90000 |
y |
0.0100 |
0.0247 |
0.0011 |
147.23 % β |
|
6 | sim_si |
background |
100000 |
y |
0.0100 |
0.0206 |
0.0017 |
106.22 % β |
|
7 | sim_si |
background |
110000 |
y |
0.0100 |
0.0213 |
0.0040 |
112.91 % β |
|
8 | sim_si |
linked_phases |
si |
scale |
1.0000 |
1.4482 |
0.0072 |
44.82 % β |
|
9 | sim_si |
peak |
asym_alpha_0 |
-0.0055 |
-0.0053 |
0.0004 |
3.17 % β |
||
10 | sim_si |
peak |
asym_alpha_1 |
0.0147 |
0.0138 |
0.0007 |
6.17 % β |
||
11 | sim_si |
peak |
gauss_sigma_0 |
69498.0000 |
89792.2067 |
6980.3379 |
Β΅sΒ² |
29.20 % β |
|
12 | sim_si |
peak |
gauss_sigma_1 |
-55578.0000 |
-74344.5178 |
8513.8041 |
Β΅s/β« |
33.77 % β |
|
13 | sim_si |
peak |
gauss_sigma_2 |
14560.0000 |
18715.7903 |
2384.5043 |
Β΅sΒ²/β«Β² |
28.54 % β |
|
14 | sim_si |
peak |
mix_beta_0 |
0.0019 |
0.0011 |
0.0001 |
deg |
39.57 % β |
|
15 | sim_si |
peak |
mix_beta_1 |
0.0137 |
0.0164 |
0.0008 |
deg |
19.52 % β |
Check Fit Results#
You can see that the agreement between the measured and calculated diffraction patterns is now much improved and that the intensities of the calculated peaks align much better with the measured peaks. To check the quality of the fit numerically, we can look at the goodness-of-fit ΟΒ² value and the reliability R-factors. The ΟΒ² value is a measure of how well the calculated diffraction pattern matches the measured pattern, and it is calculated as the sum of the squared differences between the measured and calculated intensities, divided by the number of data points. Ideally, the ΟΒ² value should be close to 1, indicating a good fit.
Visualize Fit Results#
After the fit is completed, we can plot the comparison between the measured and calculated diffraction patterns again to see how well the fit improved the agreement between the two. The calculated diffraction pattern is now based on the refined parameters.
project_1.plot_meas_vs_calc(expt_name='sim_si')
TOF vs d-spacing#
The diffraction pattern is typically analyzed and plotted in the time-of-flight (TOF) axis, which represents the time it takes for neutrons to travel from the sample to the detector. However, it is sometimes more convenient to visualize the diffraction pattern in the d-spacing axis, which represents the distance between planes in the crystal lattice.
The conversion from d-spacing to TOF was already introduced in the data reduction notebook. As a reminder, the two are related through the instrument calibration parameters according to the equation:
where offset
, linear
, and quad
are calibration parameters.
In our case, only the linear
term is used (the calib_d_to_tof_linear
parameter we set earlier). The offset
and quad
terms were not part of
the data reduction and are therefore set to 0 by default.
The plot_meas_vs_calc
method of the project allows us to
plot the measured and calculated diffraction patterns in the d-spacing axis
by setting the d_spacing
parameter to True
.
project_1.plot_meas_vs_calc(expt_name='sim_si', d_spacing=True)
As you can see, the calculated diffraction pattern now matches the measured pattern much more closely. Typically, additional experimental parameters are included in the refinement process to further improve the fit. In this example, the structural parameters are not refined because the Si crystal structure is a well-known standard reference used to calibrate both the instrument and the experimental setup. The refined experimental parameters obtained here will then be applied when fitting the crystal structures of other materials.
In the next part of the notebook, we will move to a more advanced case and fit a more complex crystal structure: Laβ.β Baβ.β CoOβ (LBCO).
Save Project#
Before moving on, we can save the project to disk for later use. This will
preserve the entire project structure, including experiments, sample models,
and fitting results. The project is saved into a directory specified by the
dir_path
attribute of the project object.
project_1.save_as(dir_path='powder_diffraction_Si')
Saving project π¦ 'reference' to
/home/runner/work/dmsc-school/dmsc-school/5-analysis/powder_diffraction_Si
β
project.cif
β
sample_models/si.cif
β
experiments/sim_si.cif
β
analysis.cif
β
summary.cif
πͺ Exercise: Complex Fit β LBCO#
Now that you have a basic understanding of the fitting process, we will undertake a more complex fit of the Laβ.β Baβ.β CoOβ (LBCO) crystal structure using simulated powder diffraction data from the data reduction notebook.
You can use the same approach as in the previous part of the notebook, but this time we will refine a more complex crystal structure LBCO with multiple atoms in the unit cell.
π¦ Exercise 1: Create a Project#
Create a new project for the LBCO fit.
Hint:
You can use the same approach as in the previous part of the notebook, but this time we will create a new project for the LBCO fit.
Solution:
π¬ Exercise 2: Define an Experiment#
Exercise 2.1: Create an Experiment#
Create an experiment within the new project and load the reduced diffraction pattern for LBCO.
lbco_xye_path = '../4-reduction/reduced_LBCO.xye'
Hint:
You can use the same approach as in the previous part of the notebook, but this time you need to use the data file for LBCO.
Solution:
Loading measured data from ASCII file
/home/runner/work/dmsc-school/dmsc-school/4-reduction/reduced_LBCO.xye
Data loaded successfully
Experiment π¬ 'sim_lbco'. Number of data points: 900
Exercise 2.1: Inspect Measured Data#
Check the measured data of the LBCO experiment. Are there any peaks with the shape similar to those excluded in the Si fit? If so, exclude them from this analysis as well.
Hint:
You can use the plot_meas
method of the project to visualize the
measured diffraction pattern. You can also use the excluded_regions
attribute of the experiment to exclude specific regions from the analysis
as we did in the previous part of the notebook.
Solution:
Current plotter changed to
plotly
Exercise 2.2: Set Instrument Parameters#
Set the instrument parameters for the LBCO experiment.
Hint:
Use the values from the data reduction process for the LBCO and follow the same approach as in the previous part of the notebook.
Solution:
Exercise 2.3: Set Peak Profile Parameters#
Set the peak profile parameters for the LBCO experiment.
Hint:
Use the values from the previous part of the notebook. You can
either manually copy the values from the Si fit or use the value
attribute
of the parameters from the Si experiment to set the initial values for the
LBCO experiment. This will help us to have a good starting point for the fit.
Solution:
Exercise 2.4: Set Background#
Set the background points for the LBCO experiment. What would you suggest as the initial intensity value for the background points?
Hint:
Use the same approach as in the previous part of the notebook, but this time you need to set the background points for the LBCO experiment. You can zoom in on the measured diffraction pattern to determine the approximate background level.
Solution:
Background type for experiment 'sim_lbco' changed to
line-segment
π§© Exercise 3: Define a Sample Model β LBCO#
The LBSO structure is not as simple as the Si model, as it contains multiple atoms in the unit cell. It is not in COD, so we give you the structural parameters in CIF format to create the sample model.
Note that those parameters are not necessarily the most accurate ones, but they are a good starting point for the fit. The aim of the study is to refine the LBCO lattice parameters.
data_lbco
_space_group.name_H-M_alt "P m -3 m"
_space_group.IT_coordinate_system_code 1
_cell.length_a 3.89
_cell.length_b 3.89
_cell.length_c 3.89
_cell.angle_alpha 90.0
_cell.angle_beta 90.0
_cell.angle_gamma 90.0
loop_
_atom_site.label
_atom_site.type_symbol
_atom_site.fract_x
_atom_site.fract_y
_atom_site.fract_z
_atom_site.wyckoff_letter
_atom_site.occupancy
_atom_site.ADP_type
_atom_site.B_iso_or_equiv
La La 0.0 0.0 0.0 a 0.5 Biso 0.95
Ba Ba 0.0 0.0 0.0 a 0.5 Biso 0.95
Co Co 0.5 0.5 0.5 b 1.0 Biso 0.80
O O 0.0 0.5 0.5 c 1.0 Biso 1.66
Note that the occupancy
of the La and Ba atoms is 0.5 and those atoms
are located in the same position (0, 0, 0) in the unit cell. This means that
an extra attribute occupancy
needs to be set for those atoms later in the
sample model.
We model the La/Ba site using the virtual crystal approximation. In this approach, the scattering is taken as a weighted average of La and Ba. This reproduces the average diffraction pattern well but does not capture certain real-world effects.
The edge cases are:
Random distribution. La and Ba atoms are placed randomly. The Bragg peaks still match the average structure, but the pattern also shows extra background (diffuse scattering) between the peaks, but this is usually neglected in the analysis.
Perfect ordering. La and Ba arrange themselves in a regular pattern, creating a larger repeating unit. This gives rise to extra peaks (βsuperlattice reflectionsβ) and changes the intensity of some existing peaks.
Virtual crystal approximation (our model). We replace the site with a single βvirtual atomβ that averages La and Ba. This gives the correct average Bragg peaks but leaves out the extra background of the random case and the extra peaks of the ordered case.
Exercise 3.1: Create Sample Model#
Add a sample model for LBCO to the project. The sample model parameters will be set in the next exercises.
Hint:
You can use the same approach as in the previous part of the notebook, but this time you need to use the model name corresponding to the LBCO structure, e.g. βlbcoβ.
Solution:
Exercise 3.2: Set Space Group#
Set the space group for the LBCO sample model.
Hint:
Use the space group name and IT coordinate system code from the CIF data.
Solution:
Exercise 3.3: Set Lattice Parameters#
Set the lattice parameters for the LBCO sample model.
Hint:
Use the lattice parameters from the CIF data.
Solution:
Exercise 3.4: Set Atom Sites#
Set the atom sites for the LBCO sample model.
Hint:
Use the atom sites from the CIF data. You can use the add
method of
the atom_sites
attribute of the sample model to add the atom sites.
Solution:
π Exercise 4: Assign Sample Model to Experiment#
Now assign the LBCO sample model to the experiment created above.
Hint:
Use the linked_phases
attribute of the experiment to link the sample model.
Solution:
π Exercise 5: Analyze and Fit the Data#
Exercise 5.1: Set Fit Parameters#
Select the initial set of parameters to be refined during the fitting process.
Hint:
You can start with the scale factor and the background points, as in the Si fit.
Solution:
Exercise 5.2: Run Fitting#
Visualize the measured and calculated diffraction patterns before fitting and then run the fitting process.
Hint:
Use the plot_meas_vs_calc
method of the project to visualize the
measured and calculated diffraction patterns before fitting. Then, use the
fit
method of the analysis
object of the project to perform the fitting
process.
Solution:
Using experiment π¬ 'sim_lbco' for 'single' fitting
π Starting fit process with 'lmfit (leastsq)'...
π Goodness-of-fit (reduced ΟΒ²) change:
iteration | ΟΒ² | improvement [%] |
---|---|---|
1 |
118.34 |
|
12 |
68.92 |
41.8% β |
22 |
68.92 |
π Best goodness-of-fit (reduced ΟΒ²) is 68.92 at iteration 21
β
Fitting complete.
Fit results
β
Success: True
β±οΈ Fitting time: 0.19 seconds
π Goodness-of-fit (reduced ΟΒ²): 68.92
π R-factor (Rf): 38.24%
π R-factor squared (RfΒ²): 63.71%
π Weighted R-factor (wR): 65.03%
π Fitted parameters:
datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
---|---|---|---|---|---|---|---|---|---|
1 | sim_lbco |
background |
50000 |
y |
0.2000 |
0.2463 |
0.1748 |
23.13 % β |
|
2 | sim_lbco |
background |
60000 |
y |
0.2000 |
0.2770 |
0.0381 |
38.50 % β |
|
3 | sim_lbco |
background |
70000 |
y |
0.2000 |
0.2762 |
0.0250 |
38.12 % β |
|
4 | sim_lbco |
background |
80000 |
y |
0.2000 |
0.2436 |
0.0194 |
21.78 % β |
|
5 | sim_lbco |
background |
90000 |
y |
0.2000 |
0.2625 |
0.0138 |
31.27 % β |
|
6 | sim_lbco |
background |
100000 |
y |
0.2000 |
0.2578 |
0.0161 |
28.90 % β |
|
7 | sim_lbco |
background |
110000 |
y |
0.2000 |
0.1931 |
0.0566 |
3.47 % β |
|
8 | sim_lbco |
linked_phases |
lbco |
scale |
1.0000 |
2.7232 |
0.1379 |
172.32 % β |
Exercise 5.3: Find the Misfit in the Fit#
Visualize the measured and calculated diffraction patterns after the fit. As you can see, the fit shows noticeable discrepancies. If you zoom in on different regions of the pattern, you will observe that all the calculated peaks are shifted to the left.
Exercise 5.4: Refine the LBCO Lattice Parameter#
To improve the fit, refine the lattice parameters of the LBCO phase.
Hint:
To achieve this, we will set the free
attribute of the length_a
parameter of the LBCO cell to True
.
LBCO has a cubic crystal structure (space group P m -3 m
), which means
that length_b
and length_c
are constrained to be equal to length_a
.
Therefore, only length_a
needs to be refined; the other two will be
updated automatically. All cell angles are fixed at 90Β°, so they do not
require refinement.
Solution:
Using experiment π¬ 'sim_lbco' for 'single' fitting
π Starting fit process with 'lmfit (leastsq)'...
π Goodness-of-fit (reduced ΟΒ²) change:
iteration | ΟΒ² | improvement [%] |
---|---|---|
1 |
69.08 |
|
13 |
10.82 |
84.3% β |
23 |
5.07 |
53.2% β |
33 |
4.88 |
3.6% β |
65 |
4.88 |
π Best goodness-of-fit (reduced ΟΒ²) is 4.88 at iteration 53
β
Fitting complete.
Fit results
β
Success: True
β±οΈ Fitting time: 0.56 seconds
π Goodness-of-fit (reduced ΟΒ²): 4.88
π R-factor (Rf): 7.08%
π R-factor squared (RfΒ²): 6.94%
π Weighted R-factor (wR): 6.24%
π Fitted parameters:
datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
---|---|---|---|---|---|---|---|---|---|
1 | lbco |
cell |
length_a |
3.8800 |
3.8919 |
0.0001 |
Γ
|
0.31 % β |
|
2 | sim_lbco |
background |
50000 |
y |
0.2463 |
0.2191 |
0.0465 |
11.02 % β |
|
3 | sim_lbco |
background |
60000 |
y |
0.2770 |
0.2665 |
0.0101 |
3.79 % β |
|
4 | sim_lbco |
background |
70000 |
y |
0.2762 |
0.2553 |
0.0067 |
7.57 % β |
|
5 | sim_lbco |
background |
80000 |
y |
0.2436 |
0.2469 |
0.0052 |
1.36 % β |
|
6 | sim_lbco |
background |
90000 |
y |
0.2625 |
0.2443 |
0.0037 |
6.94 % β |
|
7 | sim_lbco |
background |
100000 |
y |
0.2578 |
0.2444 |
0.0043 |
5.20 % β |
|
8 | sim_lbco |
background |
110000 |
y |
0.1931 |
0.1925 |
0.0151 |
0.27 % β |
|
9 | sim_lbco |
linked_phases |
lbco |
scale |
2.7232 |
4.9379 |
0.0470 |
81.33 % β |
One of the main goals of this study was to refine the lattice parameter of the LBCO phase. As shown in the updated fit results, the overall fit has improved significantly, even though the change in cell length is less than 1% of the initial value. This demonstrates how even a small adjustment to the lattice parameter can have a substantial impact on the quality of the fit.
Exercise 5.5: Visualize the Fit Results in d-spacing#
Plot measured vs calculated diffraction patterns in d-spacing instead of TOF.
Hint:
Use the plot_meas_vs_calc
method of the project and set the
d_spacing
parameter to True
.
Solution:
Exercise 5.6: Refine the Peak Profile Parameters#
As you can see, the fit is now relatively good and the peak positions are much closer to the measured data.
The peak profile parameters were not refined, and their starting values were set based on the previous fit of the Si standard sample. Although these starting values are reasonable and provide a good starting point for the fit, they are not necessarily optimal for the LBCO phase. This can be seen while inspecting the individual peaks in the diffraction pattern. For example, the calculated curve does not perfectly describe the peak at about 1.38 Γ , as can be seen below:
project_2.plot_meas_vs_calc(expt_name='sim_lbco', d_spacing=True, x_min=1.35, x_max=1.40)
The peak profile parameters are determined based on both the instrument and the sample characteristics, so they can vary when analyzing different samples on the same instrument. Therefore, it is better to refine them as well.
Select the peak profile parameters to be refined during the fitting process.
Hint:
You can set the free
attribute of the peak profile parameters to True
to allow the fitting process to adjust them. You can use the same approach as
in the previous part of the notebook, but this time you will refine the peak
profile parameters of the LBCO phase.
Solution:
Using experiment π¬ 'sim_lbco' for 'single' fitting
π Starting fit process with 'lmfit (leastsq)'...
π Goodness-of-fit (reduced ΟΒ²) change:
iteration | ΟΒ² | improvement [%] |
---|---|---|
1 |
4.97 |
|
20 |
4.56 |
8.1% β |
344 |
4.54 |
π Best goodness-of-fit (reduced ΟΒ²) is 4.54 at iteration 343
β
Fitting complete.
Fit results
β
Success: True
β±οΈ Fitting time: 2.88 seconds
π Goodness-of-fit (reduced ΟΒ²): 4.54
π R-factor (Rf): 5.79%
π R-factor squared (RfΒ²): 5.14%
π Weighted R-factor (wR): 4.42%
π Fitted parameters:
datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
---|---|---|---|---|---|---|---|---|---|
1 | lbco |
cell |
length_a |
3.8919 |
3.8914 |
0.0003 |
Γ
|
0.01 % β |
|
2 | sim_lbco |
background |
50000 |
y |
0.2191 |
0.2172 |
0.0449 |
0.89 % β |
|
3 | sim_lbco |
background |
60000 |
y |
0.2665 |
0.2689 |
0.0098 |
0.89 % β |
|
4 | sim_lbco |
background |
70000 |
y |
0.2553 |
0.2539 |
0.0066 |
0.56 % β |
|
5 | sim_lbco |
background |
80000 |
y |
0.2469 |
0.2514 |
0.0051 |
1.84 % β |
|
6 | sim_lbco |
background |
90000 |
y |
0.2443 |
0.2441 |
0.0035 |
0.10 % β |
|
7 | sim_lbco |
background |
100000 |
y |
0.2444 |
0.2453 |
0.0041 |
0.38 % β |
|
8 | sim_lbco |
background |
110000 |
y |
0.1925 |
0.1918 |
0.0145 |
0.39 % β |
|
9 | sim_lbco |
linked_phases |
lbco |
scale |
4.9379 |
4.9027 |
0.0471 |
0.71 % β |
|
10 | sim_lbco |
peak |
asym_alpha_0 |
-0.0053 |
-0.0035 |
0.0016 |
34.80 % β |
||
11 | sim_lbco |
peak |
asym_alpha_1 |
0.0138 |
0.0117 |
0.0022 |
15.15 % β |
||
12 | sim_lbco |
peak |
gauss_sigma_0 |
89792.2067 |
102631.5703 |
16020.3984 |
Β΅sΒ² |
14.30 % β |
|
13 | sim_lbco |
peak |
gauss_sigma_1 |
-74344.5178 |
-102992.2701 |
19532.3657 |
Β΅s/β« |
38.53 % β |
|
14 | sim_lbco |
peak |
gauss_sigma_2 |
18715.7903 |
29440.7280 |
6039.3993 |
Β΅sΒ²/β«Β² |
57.30 % β |
|
15 | sim_lbco |
peak |
mix_beta_0 |
0.0011 |
0.0030 |
0.0005 |
deg |
159.07 % β |
|
16 | sim_lbco |
peak |
mix_beta_1 |
0.0164 |
0.0097 |
0.0019 |
deg |
40.76 % β |
Exercise 5.7: Find Undefined Features#
After refining the lattice parameter and the peak profile parameters, the fit is significantly improved, but inspect the diffraction pattern again. Are you noticing anything undefined?
Hint:
While the fit is now significantly better, there are still some unexplained peaks in the diffraction pattern. These peaks are not accounted for by the LBCO phase. For example, if you zoom in on the region around 1.6 Γ (or 95,000 ΞΌs), you will notice that the rightmost peak is not explained by the LBCO phase at all.
Solution:
Exercise 5.8: Identify the Cause of the Unexplained Peaks#
Analyze the residual peaks that remain after refining the LBCO phase and the peak-profile parameters. Based on their positions and characteristics, decide:
Exercise 5.9: Identify the impurity phase#
Use the positions of the unexplained peaks to identify the most likely secondary phase present in the sample.
Hint:
Check the positions of the unexplained peaks in the diffraction pattern. Compare them with the known diffraction patterns in the previous part of the notebook.
Solution:
The unexplained peaks are likely due to the presence of a small amount of Si in the LBCO sample. In real experiments, it might happen, e.g., because the sample holder was not cleaned properly after the Si experiment.
You can visalize both the patterns of the Si and LBCO phases to confirm this hypothesis.
Exercise 5.10: Create a Second Sample Model β Si as Impurity#
Create a second sample model for the Si phase, which is the impurity phase identified in the previous step. Link this sample model to the LBCO experiment.
Hint:
You can use the same approach as in the previous part of the notebook, but this time you need to create a sample model for Si and link it to the LBCO experiment.
Solution:
Exercise 5.11: Refine the Scale of the Si Phase#
Visualize the measured diffraction pattern and the calculated diffraction pattern. Check if the Si phase is contributing to the calculated diffraction pattern. Refine the scale factor of the Si phase to improve the fit.
Hint:
You can use the plot_meas_vs_calc
method of the project to
visualize the patterns. Then, set the free
attribute of the scale
parameter of the Si phase to True
to allow the fitting process to adjust
the scale factor.
Solution:
Using experiment π¬ 'sim_lbco' for 'single' fitting
π Starting fit process with 'lmfit (leastsq)'...
π Goodness-of-fit (reduced ΟΒ²) change:
iteration | ΟΒ² | improvement [%] |
---|---|---|
1 |
2280.02 |
|
21 |
1.57 |
99.9% β |
39 |
1.55 |
1.4% β |
616 |
1.54 |
π Best goodness-of-fit (reduced ΟΒ²) is 1.54 at iteration 615
β
Fitting complete.
Fit results
β
Success: True
β±οΈ Fitting time: 8.72 seconds
π Goodness-of-fit (reduced ΟΒ²): 1.54
π R-factor (Rf): 4.67%
π R-factor squared (RfΒ²): 4.38%
π Weighted R-factor (wR): 3.94%
π Fitted parameters:
datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
---|---|---|---|---|---|---|---|---|---|
1 | lbco |
cell |
length_a |
3.8914 |
3.8912 |
0.0002 |
Γ
|
0.00 % β |
|
2 | sim_lbco |
background |
50000 |
y |
0.2172 |
0.2198 |
0.0262 |
1.23 % β |
|
3 | sim_lbco |
background |
60000 |
y |
0.2689 |
0.2609 |
0.0057 |
2.96 % β |
|
4 | sim_lbco |
background |
70000 |
y |
0.2539 |
0.2475 |
0.0038 |
2.52 % β |
|
5 | sim_lbco |
background |
80000 |
y |
0.2514 |
0.2493 |
0.0030 |
0.86 % β |
|
6 | sim_lbco |
background |
90000 |
y |
0.2441 |
0.2407 |
0.0021 |
1.40 % β |
|
7 | sim_lbco |
background |
100000 |
y |
0.2453 |
0.2276 |
0.0025 |
7.21 % β |
|
8 | sim_lbco |
background |
110000 |
y |
0.1918 |
0.2235 |
0.0086 |
16.54 % β |
|
9 | sim_lbco |
linked_phases |
lbco |
scale |
4.9027 |
4.9262 |
0.0277 |
0.48 % β |
|
10 | sim_lbco |
linked_phases |
si |
scale |
1.0000 |
0.0375 |
0.0014 |
96.25 % β |
|
11 | sim_lbco |
peak |
asym_alpha_0 |
-0.0035 |
-0.0063 |
0.0009 |
82.33 % β |
||
12 | sim_lbco |
peak |
asym_alpha_1 |
0.0117 |
0.0159 |
0.0013 |
36.22 % β |
||
13 | sim_lbco |
peak |
gauss_sigma_0 |
102631.5703 |
97208.5646 |
9305.8036 |
Β΅sΒ² |
5.28 % β |
|
14 | sim_lbco |
peak |
gauss_sigma_1 |
-102992.2701 |
-86734.0911 |
11694.3419 |
Β΅s/β« |
15.79 % β |
|
15 | sim_lbco |
peak |
gauss_sigma_2 |
29440.7280 |
22668.4275 |
3608.0504 |
Β΅sΒ²/β«Β² |
23.00 % β |
|
16 | sim_lbco |
peak |
mix_beta_0 |
0.0030 |
0.0019 |
0.0003 |
deg |
36.93 % β |
|
17 | sim_lbco |
peak |
mix_beta_1 |
0.0097 |
0.0138 |
0.0012 |
deg |
42.20 % β |
All previously unexplained peaks are now accounted for in the pattern, and the fit is improved. Some discrepancies in the peak intensities remain, but further improvements would require more advanced data reduction and analysis, which are beyond the scope of this school.
To review the analysis results, you can generate and print a summary
report using the show_report()
method, as demonstrated in the cell below.
The report includes parameters related to the sample model and the
experiment, such as the refined unit cell parameter a
of LBCO.
Information about the crystal or magnetic structure, along with experimental details, fitting quality, and other relevant data, is often submitted to crystallographic journals as part of a scientific publication. It can also be deposited in crystallographic databases when relevant.
project_2.summary.show_report()
*** PROJECT INFO ***
Title
La0.5Ba0.5CoO3 Fit
Description
Fitting simulated powder diffraction pattern of
La0.5Ba0.5CoO3.
*** CRYSTALLOGRAPHIC DATA ***
Phase datablock
π§© lbco
Space group
P m -3 m
Cell parameters
alpha |
90.00000 |
beta |
90.00000 |
gamma |
90.00000 |
a |
3.89122 |
b |
3.89122 |
c |
3.89122 |
Atom sites
Label | Type | fract_x | fract_y | fract_z | Occupancy | B_iso |
---|---|---|---|---|---|---|
La |
La |
0.00000 |
0.00000 |
0.00000 |
0.50000 |
0.95000 |
Ba |
Ba |
0.00000 |
0.00000 |
0.00000 |
0.50000 |
0.95000 |
Co |
Co |
0.50000 |
0.50000 |
0.50000 |
1.00000 |
0.80000 |
O |
O |
0.00000 |
0.50000 |
0.50000 |
1.00000 |
1.66000 |
Phase datablock
π§© si
Space group
F d -3 m
Cell parameters
alpha |
90.00000 |
beta |
90.00000 |
gamma |
90.00000 |
a |
5.43000 |
b |
5.43000 |
c |
5.43000 |
Atom sites
Label | Type | fract_x | fract_y | fract_z | Occupancy | B_iso |
---|---|---|---|---|---|---|
Si |
Si |
0.12500 |
0.12500 |
0.12500 |
1.00000 |
0.89000 |
*** EXPERIMENTS ***
Experiment datablock
π¬ sim_lbco
Experiment type
powder, neutron, time-of-flight
Profile type
pseudo-voigt * ikeda-carpenter
*** FITTING ***
Calculation engine
cryspy
Minimization engine
lmfit (leastsq)
Fit quality
Goodness-of-fit (reduced ΟΒ²) |
1.54 |
Finally, we save the project to disk to preserve the current state of the analysis.
project_2.save_as(dir_path='powder_diffraction_LBCO_Si')
Saving project π¦ 'main' to
/home/runner/work/dmsc-school/dmsc-school/5-analysis/powder_diffraction_LBCO_Si
β
project.cif
β
sample_models/lbco.cif
β
sample_models/si.cif
β
experiments/sim_lbco.cif
β
analysis.cif
β
summary.cif
Final Remarks#
In this part of the notebook, you learned how to use EasyDiffraction to refine lattice parameters of a more complex crystal structure, Laβ.β Baβ.β CoOβ (LBCO). In real experiments, you might also refine additional parameters, such as atomic positions, occupancies, and atomic displacement factors, to achieve an even better fit. For our purposes, weβll stop here, as the goal was to give you a starting point for analyzing more complex crystal structures with EasyDiffraction.
π Bonus#
Congratulations β youβve now completed the diffraction data analysis part of the DMSC Summer School!
If youβd like to keep exploring, the EasyDiffraction library offers many additional tutorials and examples on the official documentation site: π https://docs.easydiffraction.org/lib/tutorials/
Besides the Python package, EasyDiffraction also comes with a graphical user interface (GUI) that lets you perform similar analyses without writing code. To be fair, itβs not quite feature-complete compared to the Python library yet β but weβre working on it! π§
If you prefer a point-and-click interface over coding, the GUI provides a user-friendly way to analyze diffraction data. You can download it as a standalone application here: π https://easydiffraction.org
Weβd love to hear your feedback on EasyDiffraction β both the library and the GUI! π¬