{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "be511061", "metadata": { "tags": [ "hide-in-docs" ] }, "outputs": [], "source": [ "# Check if the easydiffraction library is installed.\n", "# If not, install it including the 'visualization' extras.\n", "# This is needed, e.g., when running this as a notebook via Google Colab or\n", "# Jupyter Notebook in an environment where the library is not pre-installed.\n", "import builtins\n", "import importlib.util\n", "\n", "if hasattr(builtins, '__IPYTHON__'):\n", " if importlib.util.find_spec('easydiffraction') is None:\n", " !pip install 'easydiffraction[visualization]'\n" ] }, { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Fitting Powder Diffraction data\n", "\n", "This tutorial guides you through the Rietveld refinement of crystal\n", "structures using simulated powder diffraction data. It consists of two parts:\n", "- Introduction: A simple reference fit using silicon (Si) crystal structure.\n", "- Exercise: A more complex fit using La₀.₅Ba₀.₅CoO₃ (LBCO) crystal structure.\n", "\n", "## 🛠️ Import Library\n", "\n", "We start by importing the necessary library for the analysis. In this\n", "tutorial, we use the EasyDiffraction library, which offers tools for\n", "analyzing and refining powder diffraction data.\n", "\n", "This tutorial is self-contained and designed for hands-on learning.\n", "However, if you're interested in exploring more advanced features or learning\n", "about additional capabilities of the EasyDiffraction library, please refer to\n", "the official documentation: https://docs.easydiffraction.org/lib/tutorials/\n", "\n", "Depending on your requirements, you may choose to import only specific\n", "classes. However, for the sake of simplicity in this tutorial, we will import\n", "the entire library." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import easydiffraction as ed" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## 📘 Introduction: Simple Reference Fit – Si\n", "\n", "Before diving into the more complex fitting exercise with the La₀.₅Ba₀.₅CoO₃\n", "(LBCO) crystal structure, let's start with a simpler example using the\n", "silicon (Si) crystal structure. This will help us understand the basic\n", "concepts and steps involved in fitting a crystal structure using powder\n", "diffraction data.\n", "\n", "For this part of the tutorial, we will use the powder diffraction data\n", "from the previous tutorial, simulated using the Si crystal structure.\n", "\n", "### 📦 Create a Project – 'reference'\n", "\n", "In EasyDiffraction, a project serves as a container for all information\n", "related to the analysis of a specific experiment or set of experiments. It\n", "enables you to organize your data, experiments, sample models, and fitting\n", "parameters in a structured manner. You can think of it as a folder containing\n", "all the essential details about your analysis. The project also allows\n", "us to visualize both the measured and calculated diffraction patterns, among\n", "other things." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "project_1 = ed.Project(name='reference')" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "\n", "You can set the title and description of the project to provide context\n", "and information about the analysis being performed. This is useful for\n", "documentation purposes and helps others (or yourself in the future)\n", "understand the purpose of the project at a glance." ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "project_1.info.title = 'Reference Silicon Fit'\n", "project_1.info.description = 'Fitting simulated powder diffraction pattern of Si.'" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "### 🔬 Create an Experiment\n", "\n", "Now we will create an experiment within the project. An experiment\n", "represents a specific diffraction measurement performed on a specific sample\n", "using a particular instrument. It contains details about the measured data,\n", "instrument parameters, and other relevant information.\n", "\n", "In this case, the experiment is defined as a powder diffraction measurement\n", "using time-of-flight neutrons. The measured data is loaded from a file\n", "containing the reduced diffraction pattern of Si from the data reduction\n", "tutorial." ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "dir_path = 'data'\n", "file_name = 'reduced_Si.xye'\n", "si_xye_path = f'{dir_path}/{file_name}'" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "Uncomment the following cell if your data reduction failed and the reduced data\n", "file is missing. In this case, you can download our pre-generated reduced\n", "data file from the EasyDiffraction repository.\n", "The `download_from_repository` function will not overwrite an existing file\n", "unless you set `overwrite=True`, so it's safe to run even if the file is\n", "already present." ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "ed.download_from_repository(file_name, destination=dir_path)" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "Now we can create the experiment and load the measured data." ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "project_1.experiments.add(\n", " name='sim_si',\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", " data_path=si_xye_path,\n", ")" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "#### Inspect Measured Data\n", "\n", "After creating the experiment, we can examine the measured data. The measured\n", "data consists of a diffraction pattern having time-of-flight (TOF) values\n", "and corresponding intensities. The TOF values are given in microseconds (μs),\n", "and the intensities are in arbitrary units.\n", "\n", "The data is stored in XYE format, a simple text format containing three\n", "columns: TOF, intensity, and intensity error (if available).\n", "\n", "The `plot_meas` method of the project enables us to visualize the measured\n", "diffraction pattern.\n", "\n", "Before plotting, we set the plotting engine to 'plotly', which provides\n", "interactive visualizations." ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "project_1.plotter.engine = 'plotly'" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "project_1.plot_meas(expt_name='sim_si')" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "If you zoom in on the highest TOF peak (around 120,000 μs), you will notice\n", "that it has a broad and unusual shape. This distortion, along with some more\n", "effects on the low TOF peaks, is a result of the simplified data reduction\n", "process. Obtaining a more accurate diffraction pattern would require a more\n", "advanced data reduction, which is beyond the scope of this tutorial.\n", "Therefore, we will simply exclude both the low and high TOF regions from the\n", "analysis by adding an excluded regions to the experiment." ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].excluded_regions.add(minimum=0, maximum=55000)\n", "project_1.experiments['sim_si'].excluded_regions.add(minimum=105500, maximum=200000)" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "To visualize the effect of excluding the high TOF region, we can plot\n", "the measured data again. The excluded region will be omitted from the plot\n", "and is not used in the fitting process." ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "project_1.plot_meas(expt_name='sim_si')" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "#### Set Instrument Parameters\n", "\n", "After the experiment is created and measured data are loaded, we need\n", "to set the instrument parameters.\n", "\n", "In this type of experiment, the instrument parameters define how the\n", "measured data is converted between d-spacing and time-of-flight (TOF)\n", "during the data reduction process as well as the angular position of the\n", "detector. So, we put values based on those from the reduction. These\n", "values can be found in the header of the corresponding .XYE file. Their\n", "names are `two_theta` and `DIFC`, which stand for the two-theta angle\n", "and the linear conversion factor from d-spacing to TOF, respectively.\n", "\n", "You can set them manually, but it is more convenient to use the\n", "`get_value_from_xye_header` function from the EasyDiffraction library." ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].instrument.setup_twotheta_bank = ed.get_value_from_xye_header(si_xye_path, 'two_theta')\n", "project_1.experiments['sim_si'].instrument.calib_d_to_tof_linear = ed.get_value_from_xye_header(si_xye_path, 'DIFC')" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "Every parameters is an object, which has different attributes, such as\n", "`value`, `free`, etc. To display the parameter of interest, you can simply\n", "print the parameter object. For example, to display the linear conversion\n", "factor from d-spacing to TOF, which is the `calib_d_to_tof_linear` parameter,\n", "you can do the following:" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].instrument.calib_d_to_tof_linear" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "The `value` attribute represents the current value of the parameter as a float.\n", "You can access it directly by using the `value` attribute of the parameter.\n", "This is useful when you want to use the parameter value in calculations or\n", "when you want to assign it to another parameter. For example, to get only the\n", "value of the same parameter as floating point number, but not the whole object,\n", "you can do the following:" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].instrument.calib_d_to_tof_linear.value" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "Note that to set the value of the parameter, you can simply assign a new value\n", "to the parameter object without using the `value` attribute, as we did above." ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "#### Set Peak Profile Parameters\n", "\n", "The next set of parameters is needed to define the peak profile used in the\n", "fitting process. The peak profile describes the shape of the diffraction peaks.\n", "They include parameters for the broadening and asymmetry of the peaks.\n", "\n", "Here, we use a pseudo-Voigt peak profile function with Ikeda-Carpenter\n", "asymmetry, which is a common choice for neutron powder diffraction data.\n", "\n", "The values are typically determined experimentally on the same instrument and\n", "under the same configuration as the data being analyzed based on measurements\n", "of a standard sample. We consider this Si sample as a standard reference.\n", "Therefore, we will set the initial values of the peak profile parameters based\n", "on the values obtained from another simulation and refine them during the\n", "fitting process. The refined parameters will be used as a starting point for\n", "the more complex fit in the next part of the tutorial." ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].peak_profile_type = 'pseudo-voigt * ikeda-carpenter'\n", "project_1.experiments['sim_si'].peak.broad_gauss_sigma_0 = 69498\n", "project_1.experiments['sim_si'].peak.broad_gauss_sigma_1 = -55578\n", "project_1.experiments['sim_si'].peak.broad_gauss_sigma_2 = 14560\n", "project_1.experiments['sim_si'].peak.broad_mix_beta_0 = 0.0019\n", "project_1.experiments['sim_si'].peak.broad_mix_beta_1 = 0.0137\n", "project_1.experiments['sim_si'].peak.asym_alpha_0 = -0.0055\n", "project_1.experiments['sim_si'].peak.asym_alpha_1 = 0.0147" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "#### Set Background\n", "\n", "The background of the diffraction pattern represents the portion of the\n", "pattern that is not related to the crystal structure of the sample. It's\n", "rather representing the noise and other sources of scattering that can affect\n", "the measured intensities. This includes contributions from the instrument,\n", "the sample holder, the sample environment, and other sources of incoherent\n", "scattering.\n", "\n", "The background can be modeled in various ways. In this example, we will use\n", "a simple line segment background, which is a common approach for powder\n", "diffraction data. The background intensity at any point is defined by linear\n", "interpolation between neighboring points. The background points are selected\n", "to span the range of the diffraction pattern while avoiding the peaks.\n", "\n", "We will add several background points at specific TOF values (in μs) and\n", "corresponding intensity values. These points are chosen to represent the\n", "background level in the diffraction pattern free from any peaks.\n", "\n", "The background points are added using the `add` method of the `background`\n", "object. The `x` parameter represents the TOF value, and the `y` parameter\n", "represents the intensity value at that TOF.\n", "\n", "Let's set all the background points at a constant value of 0.01, which can be\n", "roughly determined by the eye, and we will refine them later during the fitting\n", "process." ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].background_type = 'line-segment'\n", "project_1.experiments['sim_si'].background.add(x=50000, y=0.01)\n", "project_1.experiments['sim_si'].background.add(x=60000, y=0.01)\n", "project_1.experiments['sim_si'].background.add(x=70000, y=0.01)\n", "project_1.experiments['sim_si'].background.add(x=80000, y=0.01)\n", "project_1.experiments['sim_si'].background.add(x=90000, y=0.01)\n", "project_1.experiments['sim_si'].background.add(x=100000, y=0.01)\n", "project_1.experiments['sim_si'].background.add(x=110000, y=0.01)" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "### 🧩 Create a Sample Model – Si\n", "\n", "After setting up the experiment, we need to create a sample model that\n", "describes the crystal structure of the sample being analyzed.\n", "\n", "In this case, we will create a sample model for silicon (Si) with a\n", "cubic crystal structure. The sample model contains information about the\n", "space group, lattice parameters, atomic positions of the atoms in the\n", "unit cell, atom types, occupancies and atomic displacement parameters.\n", "The sample model is essential for the fitting process, as it\n", "is used to calculate the expected diffraction pattern.\n", "\n", "EasyDiffraction refines the crystal structure of the sample,\n", "but does not solve it. Therefore, we need a good starting point with\n", "reasonable structural parameters.\n", "\n", "Here, we define the Si structure as a\n", "cubic structure. As this is a cubic structure, we only need to define\n", "the single lattice parameter, which\n", "is the length of the unit cell edge. The Si crystal structure has a\n", "single atom in the unit cell, which is located at the origin (0, 0, 0) of\n", "the unit cell. The symmetry of this site is defined by the Wyckoff letter 'a'.\n", "The atomic displacement parameter defines the thermal vibrations of the\n", "atoms in the unit cell and is presented as an isotropic parameter (B_iso).\n", "\n", "Sometimes, the initial crystal structure parameters can be obtained\n", "from one of the crystallographic databases, like for example\n", "the Crystallography Open Database (COD). In this case, we use the COD\n", "entry for silicon as a reference for the initial crystal structure model:\n", "https://www.crystallography.net/cod/4507226.html\n", "\n", "Usually, the crystal structure parameters are provided in a CIF file\n", "format, which is a standard format for crystallographic data. An example\n", "of a CIF file for silicon is shown below. The CIF file contains the\n", "space group information, unit cell parameters, and atomic positions." ] }, { "cell_type": "markdown", "id": "31", "metadata": {}, "source": [ "```\n", "data_si\n", "\n", "_space_group.name_H-M_alt \"F d -3 m\"\n", "_space_group.IT_coordinate_system_code 2\n", "\n", "_cell.length_a 5.43\n", "_cell.length_b 5.43\n", "_cell.length_c 5.43\n", "_cell.angle_alpha 90.0\n", "_cell.angle_beta 90.0\n", "_cell.angle_gamma 90.0\n", "\n", "loop_\n", "_atom_site.label\n", "_atom_site.type_symbol\n", "_atom_site.fract_x\n", "_atom_site.fract_y\n", "_atom_site.fract_z\n", "_atom_site.wyckoff_letter\n", "_atom_site.occupancy\n", "_atom_site.ADP_type\n", "_atom_site.B_iso_or_equiv\n", "Si Si 0 0 0 a 1.0 Biso 0.89\n", "```" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ "\n", "As with adding the experiment in the\n", "previous step, we will create a default sample model\n", "and then modify its parameters to match the Si structure.\n", "\n", "#### Add Sample Model" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "project_1.sample_models.add(name='si')" ] }, { "cell_type": "markdown", "id": "34", "metadata": {}, "source": [ "#### Set Space Group" ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "project_1.sample_models['si'].space_group.name_h_m = 'F d -3 m'\n", "project_1.sample_models['si'].space_group.it_coordinate_system_code = '2'" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "#### Set Lattice Parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "project_1.sample_models['si'].cell.length_a = 5.43" ] }, { "cell_type": "markdown", "id": "38", "metadata": {}, "source": [ "#### Set Atom Sites" ] }, { "cell_type": "code", "execution_count": null, "id": "39", "metadata": {}, "outputs": [], "source": [ "project_1.sample_models['si'].atom_sites.add(\n", " label='Si',\n", " type_symbol='Si',\n", " fract_x=0,\n", " fract_y=0,\n", " fract_z=0,\n", " wyckoff_letter='a',\n", " b_iso=0.89,\n", ")" ] }, { "cell_type": "markdown", "id": "40", "metadata": {}, "source": [ "### 🔗 Assign Sample Model to Experiment\n", "\n", "Now we need to assign, or link, this sample model to the experiment created\n", "above. This linked crystallographic phase will be used to calculate the\n", "expected diffraction pattern based on the crystal structure defined in the\n", "sample model." ] }, { "cell_type": "code", "execution_count": null, "id": "41", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].linked_phases.add(id='si', scale=1.0)" ] }, { "cell_type": "markdown", "id": "42", "metadata": {}, "source": [ "### 🚀 Analyze and Fit the Data\n", "\n", "After setting up the experiment and sample model, we can now analyze the\n", "measured diffraction pattern and perform the fit.\n", "\n", "The fitting process involves comparing the measured diffraction pattern with\n", "the calculated diffraction pattern based on the sample model and instrument\n", "parameters. The goal is to adjust the parameters of the sample model and\n", "the experiment to minimize the difference between the measured and calculated\n", "diffraction patterns. This is done by refining the parameters of the sample\n", "model and the instrument settings to achieve a better fit.\n", "\n", "#### Set Fit Parameters\n", "\n", "To perform the fit, we need to specify the refinement parameters. These\n", "are the parameters that will be adjusted during the fitting process to\n", "minimize the difference between the measured and calculated diffraction\n", "patterns. This is done by setting the `free` attribute of the\n", "corresponding parameters to `True`.\n", "\n", "We will refine the scale factor of the Si phase, the intensities of the\n", "background points as well as the peak profile parameters. The structure\n", "parameters of the Si phase will not be refined, as this sample is\n", "considered a reference sample with known parameters." ] }, { "cell_type": "code", "execution_count": null, "id": "43", "metadata": {}, "outputs": [], "source": [ "project_1.experiments['sim_si'].linked_phases['si'].scale.free = True\n", "\n", "for line_segment in project_1.experiments['sim_si'].background:\n", " line_segment.y.free = True\n", "\n", "project_1.experiments['sim_si'].peak.broad_gauss_sigma_0.free = True\n", "project_1.experiments['sim_si'].peak.broad_gauss_sigma_1.free = True\n", "project_1.experiments['sim_si'].peak.broad_gauss_sigma_2.free = True\n", "project_1.experiments['sim_si'].peak.broad_mix_beta_0.free = True\n", "project_1.experiments['sim_si'].peak.broad_mix_beta_1.free = True\n", "project_1.experiments['sim_si'].peak.asym_alpha_0.free = True\n", "project_1.experiments['sim_si'].peak.asym_alpha_1.free = True" ] }, { "cell_type": "markdown", "id": "44", "metadata": {}, "source": [ "#### Show Free Parameters\n", "\n", "We can check which parameters are free to be refined by calling the\n", "`show_free_params` method of the `analysis` object of the project." ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "project_1.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "46", "metadata": {}, "source": [ "#### Visualize Diffraction Patterns\n", "\n", "Before performing the fit, we can visually compare the measured\n", "diffraction pattern with the calculated diffraction pattern based on the\n", "initial parameters of the sample model and the instrument.\n", "This provides an indication of how well the initial parameters\n", "match the measured data. The `plot_meas_vs_calc` method of the project\n", "allows this comparison." ] }, { "cell_type": "code", "execution_count": null, "id": "47", "metadata": {}, "outputs": [], "source": [ "project_1.plot_meas_vs_calc(expt_name='sim_si')" ] }, { "cell_type": "markdown", "id": "48", "metadata": {}, "source": [ "#### Run Fitting\n", "\n", "We can now perform the fit using the `fit` method of the `analysis`\n", "object of the project." ] }, { "cell_type": "code", "execution_count": null, "id": "49", "metadata": {}, "outputs": [], "source": [ "project_1.analysis.fit()" ] }, { "cell_type": "markdown", "id": "50", "metadata": {}, "source": [ "#### Check Fit Results\n", "You can\n", "see that the agreement between the measured and calculated diffraction patterns\n", "is now much improved and that the intensities of the\n", "calculated peaks align much better with the measured peaks. To check the\n", "quality of the fit numerically, we can look at the goodness-of-fit\n", "χ² value and the reliability R-factors. The χ² value is a measure of how well\n", "the calculated diffraction pattern matches the measured pattern, and it is\n", "calculated as the sum of the squared differences between the measured and\n", "calculated intensities, divided by the number of data points. Ideally, the\n", "χ² value should be close to 1, indicating a good fit." ] }, { "cell_type": "markdown", "id": "51", "metadata": {}, "source": [ "#### Visualize Fit Results\n", "\n", "After the fit is completed, we can plot the comparison between the\n", "measured and calculated diffraction patterns again to see how well the fit\n", "improved the agreement between the two. The calculated diffraction pattern\n", "is now based on the refined parameters." ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "project_1.plot_meas_vs_calc(expt_name='sim_si')" ] }, { "cell_type": "markdown", "id": "53", "metadata": {}, "source": [ "#### TOF vs d-spacing\n", "\n", "The diffraction pattern is typically analyzed and plotted in the\n", "time-of-flight (TOF) axis, which represents the time it takes for neutrons\n", "to travel from the sample to the detector. However, it is sometimes more\n", "convenient to visualize the diffraction pattern in the d-spacing axis,\n", "which represents the distance between planes in the crystal lattice. The\n", "d-spacing can be calculated from the TOF values using the instrument\n", "parameters. The `plot_meas_vs_calc` method of the project allows us to\n", "plot the measured and calculated diffraction patterns in the d-spacing axis\n", "by setting the `d_spacing` parameter to `True`." ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "project_1.plot_meas_vs_calc(expt_name='sim_si', d_spacing=True)" ] }, { "cell_type": "markdown", "id": "55", "metadata": {}, "source": [ "As you can see, the calculated diffraction pattern now matches the measured\n", "pattern much more closely. Typically, additional parameters are included in the\n", "refinement process to further improve the fit. However, we will stop here,\n", "as the goal of this part of the tutorial is to demonstrate that the data reduction\n", "and fitting process function correctly. The fit is not perfect, but it is\n", "sufficient to show that the fitting process works and that the parameters\n", "are being adjusted appropriately. The next part of the tutorial will be more advanced\n", "and will involve fitting a more complex crystal structure: La₀.₅Ba₀.₅CoO₃ (LBCO).\n", "\n", "## 💪 Exercise: Complex Fit – LBCO\n", "\n", "Now that you have a basic understanding of the fitting process, we will\n", "undertake a more complex fit of the La₀.₅Ba₀.₅CoO₃ (LBCO) crystal structure\n", "using simulated powder diffraction data from the previous tutorial.\n", "\n", "You can use the same approach as in the previous part of the tutorial, but\n", "this time we will refine a more complex crystal structure LBCO with multiple atoms\n", "in the unit cell.\n", "\n", "### 📦 Exercise 1: Create a Project\n", "\n", "Create a new project for the LBCO fit." ] }, { "cell_type": "markdown", "id": "56", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "57", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can use the same approach as in the previous part of the\n", "tutorial, but this time we will create a new project for the LBCO fit." ] }, { "cell_type": "markdown", "id": "58", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "59", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2 = ed.Project(name='main')\n", "project_2.info.title = 'La0.5Ba0.5CoO3 Fit'\n", "project_2.info.description = 'Fitting simulated powder diffraction pattern of La0.5Ba0.5CoO3.'" ] }, { "cell_type": "markdown", "id": "60", "metadata": {}, "source": [ "### 🔬 Exercise 2: Define an Experiment\n", "\n", "#### Exercise 2.1: Create an Experiment\n", "\n", "Create an experiment within the new project and load the reduced diffraction\n", "pattern for LBCO." ] }, { "cell_type": "markdown", "id": "61", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "62", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can use the same approach as in the previous part of the\n", "tutorial, but this time you need to use the data file for LBCO." ] }, { "cell_type": "markdown", "id": "63", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "64", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "dir_path = 'data'\n", "file_name = 'reduced_LBCO.xye'\n", "lbco_xye_path = f'{dir_path}/{file_name}'\n", "\n", "# Uncomment the following line if your data reduction failed and the reduced data file is missing.\n", "ed.download_from_repository(file_name, destination=dir_path)\n", "\n", "project_2.experiments.add(\n", " name='sim_lbco',\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", " data_path=lbco_xye_path,\n", ")" ] }, { "cell_type": "markdown", "id": "65", "metadata": {}, "source": [ "#### Exercise 2.1: Inspect Measured Data\n", "\n", "Check the measured data of the LBCO experiment. Are there any\n", "peaks with the shape similar to those excluded in the Si fit?\n", "If so, exclude them from this analysis as well." ] }, { "cell_type": "markdown", "id": "66", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "67", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can use the `plot_meas` method of the project to visualize the\n", "measured diffraction pattern. You can also use the `excluded_regions` attribute\n", "of the experiment to exclude specific regions from the analysis as we did\n", "in the previous part of the tutorial." ] }, { "cell_type": "markdown", "id": "68", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "69", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.plotter.engine = 'plotly'\n", "project_2.plot_meas(expt_name='sim_lbco')\n", "\n", "project_2.experiments['sim_lbco'].excluded_regions.add(minimum=0, maximum=55000)\n", "project_2.experiments['sim_lbco'].excluded_regions.add(minimum=105500, maximum=200000)\n", "\n", "project_2.plot_meas(expt_name='sim_lbco')" ] }, { "cell_type": "markdown", "id": "70", "metadata": {}, "source": [ "#### Exercise 2.2: Set Instrument Parameters\n", "\n", "Set the instrument parameters for the LBCO experiment." ] }, { "cell_type": "markdown", "id": "71", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "72", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the values from the data reduction process for the LBCO and\n", "follow the same approach as in the previous part of the tutorial." ] }, { "cell_type": "markdown", "id": "73", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "74", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.experiments['sim_lbco'].instrument.setup_twotheta_bank = ed.get_value_from_xye_header(lbco_xye_path, 'two_theta')\n", "project_2.experiments['sim_lbco'].instrument.calib_d_to_tof_linear = ed.get_value_from_xye_header(lbco_xye_path, 'DIFC')" ] }, { "cell_type": "markdown", "id": "75", "metadata": {}, "source": [ "#### Exercise 2.3: Set Peak Profile Parameters\n", "\n", "Set the peak profile parameters for the LBCO experiment." ] }, { "cell_type": "markdown", "id": "76", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "77", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the values from the previous part of the tutorial. You can\n", "either manually copy the values from the Si fit or use the `value` attribute of\n", "the parameters from the Si experiment to set the initial values for the LBCO\n", "experiment. This will help us to have a good starting point for the fit." ] }, { "cell_type": "markdown", "id": "78", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "79", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.peak_profile_type = 'pseudo-voigt * ikeda-carpenter'\n", "project_2.experiments['sim_lbco'].peak.broad_gauss_sigma_0 = project_1.experiments['sim_si'].peak.broad_gauss_sigma_0.value\n", "project_2.experiments['sim_lbco'].peak.broad_gauss_sigma_1 = project_1.experiments['sim_si'].peak.broad_gauss_sigma_1.value\n", "project_2.experiments['sim_lbco'].peak.broad_gauss_sigma_2 = project_1.experiments['sim_si'].peak.broad_gauss_sigma_2.value\n", "project_2.experiments['sim_lbco'].peak.broad_mix_beta_0 = project_1.experiments['sim_si'].peak.broad_mix_beta_0.value\n", "project_2.experiments['sim_lbco'].peak.broad_mix_beta_1 = project_1.experiments['sim_si'].peak.broad_mix_beta_1.value\n", "project_2.experiments['sim_lbco'].peak.asym_alpha_0 = project_1.experiments['sim_si'].peak.asym_alpha_0.value\n", "project_2.experiments['sim_lbco'].peak.asym_alpha_1 = project_1.experiments['sim_si'].peak.asym_alpha_1.value" ] }, { "cell_type": "markdown", "id": "80", "metadata": {}, "source": [ "#### Exercise 2.4: Set Background\n", "\n", "Set the background points for the LBCO experiment. What would you suggest as\n", "the initial intensity value for the background points?" ] }, { "cell_type": "markdown", "id": "81", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "82", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the same approach as in the previous part of the tutorial, but\n", "this time you need to set the background points for the LBCO experiment. You can\n", "zoom in on the measured diffraction pattern to determine the approximate\n", "background level." ] }, { "cell_type": "markdown", "id": "83", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "84", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.experiments['sim_lbco'].background_type = 'line-segment'\n", "project_2.experiments['sim_lbco'].background.add(x=50000, y=0.2)\n", "project_2.experiments['sim_lbco'].background.add(x=60000, y=0.2)\n", "project_2.experiments['sim_lbco'].background.add(x=70000, y=0.2)\n", "project_2.experiments['sim_lbco'].background.add(x=80000, y=0.2)\n", "project_2.experiments['sim_lbco'].background.add(x=90000, y=0.2)\n", "project_2.experiments['sim_lbco'].background.add(x=100000, y=0.2)\n", "project_2.experiments['sim_lbco'].background.add(x=110000, y=0.2)" ] }, { "cell_type": "markdown", "id": "85", "metadata": {}, "source": [ "### 🧩 Exercise 3: Define a Sample Model – LBCO\n", "\n", "The LBSO structure is not as simple as the Si model, as it contains multiple\n", "atoms in the unit cell. It is not in COD, so we give you the structural\n", "parameters in CIF format to create the sample model.\n", "\n", "Note that those parameters are not necessarily the most accurate ones, but they\n", "are a good starting point for the fit. The aim of the study is to refine the\n", "LBCO lattice parameters." ] }, { "cell_type": "markdown", "id": "86", "metadata": {}, "source": [ "```\n", "data_lbco\n", "\n", "_space_group.name_H-M_alt \"P m -3 m\"\n", "_space_group.IT_coordinate_system_code 1\n", "\n", "_cell.length_a 3.89\n", "_cell.length_b 3.89\n", "_cell.length_c 3.89\n", "_cell.angle_alpha 90.0\n", "_cell.angle_beta 90.0\n", "_cell.angle_gamma 90.0\n", "\n", "loop_\n", "_atom_site.label\n", "_atom_site.type_symbol\n", "_atom_site.fract_x\n", "_atom_site.fract_y\n", "_atom_site.fract_z\n", "_atom_site.wyckoff_letter\n", "_atom_site.occupancy\n", "_atom_site.ADP_type\n", "_atom_site.B_iso_or_equiv\n", "La La 0.0 0.0 0.0 a 0.5 Biso 0.95\n", "Ba Ba 0.0 0.0 0.0 a 0.5 Biso 0.95\n", "Co Co 0.5 0.5 0.5 b 1.0 Biso 0.80\n", "O O 0.0 0.5 0.5 c 1.0 Biso 1.66\n", "```" ] }, { "cell_type": "markdown", "id": "87", "metadata": {}, "source": [ "#### Exercise 3.1: Create Sample Model\n", "\n", "Add a sample model for LBCO to the project. The sample model parameters\n", "will be set in the next exercises." ] }, { "cell_type": "markdown", "id": "88", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "89", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can use the same approach as in the previous part of the\n", "tutorial, but this time you need to use the model name corresponding to the\n", "LBCO structure, e.g. 'lbco'." ] }, { "cell_type": "markdown", "id": "90", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "91", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.sample_models.add(name='lbco')" ] }, { "cell_type": "markdown", "id": "92", "metadata": {}, "source": [ "#### Exercise 3.2: Set Space Group\n", "\n", "Set the space group for the LBCO sample model." ] }, { "cell_type": "markdown", "id": "93", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "94", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the space group name and IT coordinate system code from the CIF data." ] }, { "cell_type": "markdown", "id": "95", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "96", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.sample_models['lbco'].space_group.name_h_m = 'P m -3 m'\n", "project_2.sample_models['lbco'].space_group.it_coordinate_system_code = '1'" ] }, { "cell_type": "markdown", "id": "97", "metadata": {}, "source": [ "#### Exercise 3.3: Set Lattice Parameters\n", "\n", "Set the lattice parameters for the LBCO sample model." ] }, { "cell_type": "markdown", "id": "98", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "99", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the lattice parameters from the CIF data." ] }, { "cell_type": "markdown", "id": "100", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "101", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.sample_models['lbco'].cell.length_a = 3.88" ] }, { "cell_type": "markdown", "id": "102", "metadata": {}, "source": [ "#### Exercise 3.4: Set Atom Sites\n", "\n", "Set the atom sites for the LBCO sample model." ] }, { "cell_type": "markdown", "id": "103", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "104", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the atom sites from the CIF data. You can use the `add` method of\n", "the `atom_sites` attribute of the sample model to add the atom sites.\n", "Note that the `occupancy` of the La and Ba atoms is 0.5 and those atoms\n", "are located in the same position (0, 0, 0) in the unit cell. This means that\n", "an extra attribute `occupancy` needs to be set for those atoms." ] }, { "cell_type": "markdown", "id": "105", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "106", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.sample_models['lbco'].atom_sites.add(\n", " label='La',\n", " type_symbol='La',\n", " fract_x=0,\n", " fract_y=0,\n", " fract_z=0,\n", " wyckoff_letter='a',\n", " b_iso=0.95,\n", " occupancy=0.5,\n", ")\n", "project_2.sample_models['lbco'].atom_sites.add(\n", " label='Ba',\n", " type_symbol='Ba',\n", " fract_x=0,\n", " fract_y=0,\n", " fract_z=0,\n", " wyckoff_letter='a',\n", " b_iso=0.95,\n", " occupancy=0.5,\n", ")\n", "project_2.sample_models['lbco'].atom_sites.add(\n", " label='Co',\n", " type_symbol='Co',\n", " fract_x=0.5,\n", " fract_y=0.5,\n", " fract_z=0.5,\n", " wyckoff_letter='b',\n", " b_iso=0.80,\n", ")\n", "project_2.sample_models['lbco'].atom_sites.add(\n", " label='O',\n", " type_symbol='O',\n", " fract_x=0,\n", " fract_y=0.5,\n", " fract_z=0.5,\n", " wyckoff_letter='c',\n", " b_iso=1.66,\n", ")" ] }, { "cell_type": "markdown", "id": "107", "metadata": {}, "source": [ "### 🔗 Exercise 4: Assign Sample Model to Experiment\n", "\n", "Now assign the LBCO sample model to the experiment created above." ] }, { "cell_type": "markdown", "id": "108", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "109", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the `linked_phases` attribute of the experiment to link the sample model." ] }, { "cell_type": "markdown", "id": "110", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "111", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.experiments['sim_lbco'].linked_phases.add(id='lbco', scale=1.0)" ] }, { "cell_type": "markdown", "id": "112", "metadata": {}, "source": [ "### 🚀 Exercise 5: Analyze and Fit the Data\n", "\n", "#### Exercise 5.1: Set Fit Parameters\n", "\n", "Select the initial set of parameters to be refined during the fitting\n", "process." ] }, { "cell_type": "markdown", "id": "113", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "114", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can start with the scale factor and the background\n", "points, as in the Si fit, but this time you will refine the LBCO\n", "phase related parameters." ] }, { "cell_type": "markdown", "id": "115", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "116", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.experiments['sim_lbco'].linked_phases['lbco'].scale.free = True\n", "\n", "for line_segment in project_2.experiments['sim_lbco'].background:\n", " line_segment.y.free = True" ] }, { "cell_type": "markdown", "id": "117", "metadata": {}, "source": [ "#### Exercise 5.2: Run Fitting\n", "\n", "Visualize the measured and calculated diffraction patterns before fitting and\n", "then run the fitting process." ] }, { "cell_type": "markdown", "id": "118", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "119", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the `plot_meas_vs_calc` method of the project to visualize the\n", "measured and calculated diffraction patterns before fitting. Then, use the\n", "`fit` method of the `analysis` object of the project to perform the fitting\n", "process." ] }, { "cell_type": "markdown", "id": "120", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "121", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.plot_meas_vs_calc(expt_name='sim_lbco')\n", "\n", "project_2.analysis.fit()" ] }, { "cell_type": "markdown", "id": "122", "metadata": {}, "source": [ "#### Exercise 5.3: Find the Misfit in the Fit\n", "\n", "Visualize the measured and calculated diffraction patterns after the fit. As\n", "you can see, the fit shows noticeable discrepancies. If you zoom in on different regions of the pattern,\n", "you will observe that all the calculated peaks are shifted to the left.\n", "\n", "What could be the reason for the misfit?" ] }, { "cell_type": "markdown", "id": "123", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "124", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Consider the following options:\n", "1. The conversion parameters from TOF to d-spacing are not correct.\n", "2. The lattice parameters of the LBCO phase are not correct.\n", "3. The peak profile parameters are not correct.\n", "4. The background points are not correct." ] }, { "cell_type": "markdown", "id": "125", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "markdown", "id": "126", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "1. ❌ The conversion parameters from TOF to d-spacing were set based on the\n", "data reduction step. While they are specific to each dataset and thus differ\n", "from those used for the Si data, the full reduction workflow has already been\n", "validated with the Si fit. Therefore, they are not the cause of the misfit in\n", "this case.\n", "2. ✅ The lattice parameters of the LBCO phase were set based on the CIF data,\n", "which is a good starting point, but they are not necessarily as accurate as\n", "needed for the fit. The lattice parameters may need to be refined.\n", "3. ❌ The peak profile parameters do not change the position of the peaks,\n", "but rather their shape.\n", "4. ❌ The background points affect the background level, but not the peak\n", "positions." ] }, { "cell_type": "code", "execution_count": null, "id": "127", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.plot_meas_vs_calc(expt_name='sim_lbco')" ] }, { "cell_type": "markdown", "id": "128", "metadata": {}, "source": [ "#### Exercise 5.4: Refine the LBCO Lattice Parameter\n", "\n", "To improve the fit, refine the lattice parameter of the LBCO phase." ] }, { "cell_type": "markdown", "id": "129", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "130", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "To achieve this, we will set the `free` attribute of the `length_a`\n", "parameter of the LBCO cell to `True`." ] }, { "cell_type": "markdown", "id": "131", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "132", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.sample_models['lbco'].cell.length_a.free = True\n", "\n", "project_2.analysis.fit()\n", "\n", "project_2.plot_meas_vs_calc(expt_name='sim_lbco')" ] }, { "cell_type": "markdown", "id": "133", "metadata": {}, "source": [ "One of the main goals of this study was to refine the lattice parameter of\n", "the LBCO phase. As shown in the updated fit results, the overall fit has\n", "improved significantly, even though the change in cell length is less than\n", "1% of the initial value. This demonstrates how even a small adjustment to the\n", "lattice parameter can have a substantial impact on the quality of the fit." ] }, { "cell_type": "markdown", "id": "134", "metadata": {}, "source": [ "#### Exercise 5.5: Visualize the Fit Results in d-spacing\n", "\n", "Plot measured vs calculated diffraction patterns in d-spacing instead of TOF." ] }, { "cell_type": "markdown", "id": "135", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "136", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Use the `plot_meas_vs_calc` method of the project and set the\n", "`d_spacing` parameter to `True`." ] }, { "cell_type": "markdown", "id": "137", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "138", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.plot_meas_vs_calc(expt_name='sim_lbco', d_spacing=True)" ] }, { "cell_type": "markdown", "id": "139", "metadata": {}, "source": [ "#### Exercise 5.6: Refine the Peak Profile Parameters\n", "\n", "As you can see, the fit is now relatively good and the peak positions are much\n", "closer to the measured data.\n", "\n", "The peak profile parameters were not refined, and their starting values were\n", "set based on the previous fit of the Si standard sample. Although these starting\n", "values are reasonable and provide a good starting point for the fit, they are\n", "not necessarily optimal for the LBCO phase. This can be seen while inspecting\n", "the individual peaks in the diffraction pattern. For example, the calculated curve\n", "does not perfectly describe the peak at about 1.38 Å, as can be seen below:" ] }, { "cell_type": "code", "execution_count": null, "id": "140", "metadata": {}, "outputs": [], "source": [ "project_2.plot_meas_vs_calc(expt_name='sim_lbco', d_spacing=True, x_min=1.35, x_max=1.40)" ] }, { "cell_type": "markdown", "id": "141", "metadata": {}, "source": [ "The peak profile parameters are determined based on both the instrument\n", "and the sample characteristics, so they can vary when analyzing different\n", "samples on the same instrument. Therefore, it is better to refine them as well.\n", "\n", "Select the peak profile parameters to be refined during the fitting process." ] }, { "cell_type": "markdown", "id": "142", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "143", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can set the `free` attribute of the peak profile parameters to `True`\n", "to allow the fitting process to adjust them. You can use the same approach as in\n", "the previous part of the tutorial, but this time you will refine the peak profile\n", "parameters of the LBCO phase." ] }, { "cell_type": "markdown", "id": "144", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "145", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.experiments['sim_lbco'].peak.broad_gauss_sigma_0.free = True\n", "project_2.experiments['sim_lbco'].peak.broad_gauss_sigma_1.free = True\n", "project_2.experiments['sim_lbco'].peak.broad_gauss_sigma_2.free = True\n", "project_2.experiments['sim_lbco'].peak.broad_mix_beta_0.free = True\n", "project_2.experiments['sim_lbco'].peak.broad_mix_beta_1.free = True\n", "project_2.experiments['sim_lbco'].peak.asym_alpha_0.free = True\n", "project_2.experiments['sim_lbco'].peak.asym_alpha_1.free = True\n", "\n", "project_2.analysis.fit()\n", "\n", "project_2.plot_meas_vs_calc(expt_name='sim_lbco', d_spacing=True, x_min=1.35, x_max=1.40)" ] }, { "cell_type": "markdown", "id": "146", "metadata": {}, "source": [ "#### Exercise 5.7: Find Undefined Features\n", "\n", "After refining the lattice parameter and the peak profile parameters, the fit is\n", "significantly improved, but inspect the diffraction pattern again. Are you noticing\n", "anything undefined?" ] }, { "cell_type": "markdown", "id": "147", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "148", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "While the fit is now significantly better, there are still some\n", "unexplained peaks in the diffraction pattern. These peaks are not accounted\n", "for by the LBCO phase. For example, if you zoom in on the region around\n", "1.6 Å (or 95,000 μs), you will notice that the rightmost peak is not\n", "explained by the LBCO phase at all." ] }, { "cell_type": "markdown", "id": "149", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "150", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_2.plot_meas_vs_calc(expt_name='sim_lbco', x_min=1.53, x_max=1.7, d_spacing=True)" ] }, { "cell_type": "markdown", "id": "151", "metadata": {}, "source": [ "#### Exercise 5.8: Identify the Cause of the Unexplained Peaks\n", "\n", "Analyze the residual peaks that remain after refining the LBCO phase and the\n", "peak-profile parameters. Based on their positions and characteristics, decide\n", "which potential cause best explains the misfit." ] }, { "cell_type": "markdown", "id": "152", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "153", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Consider the following options:\n", "1. The LBCO phase is not correctly modeled.\n", "2. The LBCO phase is not the only phase present in the sample.\n", "3. The data reduction process introduced artifacts.\n", "4. The studied sample is not LBCO, but rather a different phase." ] }, { "cell_type": "markdown", "id": "154", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "markdown", "id": "155", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "1. ❌ In principle, this could be the case, as sometimes the presence of\n", "extra peaks in the diffraction pattern can indicate lower symmetry\n", "than the one used in the model, or that the model is not complete. However,\n", "in this case, the LBCO phase is correctly modeled based on the CIF data.\n", "2. ✅ The unexplained peaks are due to the presence of an impurity phase\n", "in the sample, which is not included in the current model.\n", "3. ❌ The data reduction process is not likely to introduce such specific\n", "peaks, as it is tested and verified in the previous part of the tutorial.\n", "4. ❌ This could also be the case in real experiments, but in this case,\n", "we know that the sample is LBCO, as it was simulated based on the CIF data." ] }, { "cell_type": "markdown", "id": "156", "metadata": {}, "source": [ "#### Exercise 5.9: Identify the impurity phase\n", "\n", "Use the positions of the unexplained peaks to identify the most likely\n", "secondary phase present in the sample." ] }, { "cell_type": "markdown", "id": "157", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "158", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "Check the positions of the unexplained peaks in the diffraction pattern.\n", "Compare them with the known diffraction patterns in the introduction section\n", "of the tutorial." ] }, { "cell_type": "markdown", "id": "159", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "markdown", "id": "160", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "The unexplained peaks are likely due to the presence of a small amount of\n", "Si in the LBCO sample. In real experiments, it might happen, e.g., because the\n", "sample holder was not cleaned properly after the Si experiment.\n", "\n", "You can visalize both the patterns of the Si and LBCO phases to confirm this hypothesis." ] }, { "cell_type": "code", "execution_count": null, "id": "161", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "project_1.plot_meas_vs_calc(expt_name='sim_si', x_min=1, x_max=1.7, d_spacing=True)\n", "project_2.plot_meas_vs_calc(expt_name='sim_lbco', x_min=1, x_max=1.7, d_spacing=True)" ] }, { "cell_type": "markdown", "id": "162", "metadata": {}, "source": [ "#### Exercise 5.10: Create a Second Sample Model – Si as Impurity\n", "\n", "Create a second sample model for the Si phase, which is the impurity phase\n", "identified in the previous step. Link this sample model to the LBCO\n", "experiment." ] }, { "cell_type": "markdown", "id": "163", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "164", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can use the same approach as in the previous part of the\n", "tutorial, but this time you need to create a sample model for Si and link it\n", "to the LBCO experiment." ] }, { "cell_type": "markdown", "id": "165", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "166", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "# Set Space Group\n", "project_2.sample_models.add(name='si')\n", "project_2.sample_models['si'].space_group.name_h_m = 'F d -3 m'\n", "project_2.sample_models['si'].space_group.it_coordinate_system_code = '2'\n", "\n", "# Set Lattice Parameters\n", "project_2.sample_models['si'].cell.length_a = 5.43\n", "\n", "# Set Atom Sites\n", "project_2.sample_models['si'].atom_sites.add(\n", " label='Si',\n", " type_symbol='Si',\n", " fract_x=0,\n", " fract_y=0,\n", " fract_z=0,\n", " wyckoff_letter='a',\n", " b_iso=0.89,\n", ")\n", "\n", "# Assign Sample Model to Experiment\n", "project_2.experiments['sim_lbco'].linked_phases.add(id='si', scale=1.0)" ] }, { "cell_type": "markdown", "id": "167", "metadata": {}, "source": [ "#### Exercise 5.11: Refine the Scale of the Si Phase\n", "\n", "Visualize the measured diffraction pattern and the calculated diffraction\n", "pattern. Check if the Si phase is contributing to the calculated diffraction\n", "pattern. Refine the scale factor of the Si phase to improve the fit." ] }, { "cell_type": "markdown", "id": "168", "metadata": {}, "source": [ "**Hint:**" ] }, { "cell_type": "markdown", "id": "169", "metadata": { "tags": [ "dmsc-school-hint" ] }, "source": [ "You can use the `plot_meas_vs_calc` method of the project to\n", "visualize the patterns. Then, set the `free` attribute of the `scale`\n", "parameter of the Si phase to `True` to allow the fitting process to adjust\n", "the scale factor." ] }, { "cell_type": "markdown", "id": "170", "metadata": {}, "source": [ "**Solution:**" ] }, { "cell_type": "code", "execution_count": null, "id": "171", "metadata": { "tags": [ "solution", "hide-input" ] }, "outputs": [], "source": [ "# Before optimizing the parameters, we can visualize the measured\n", "# diffraction pattern and the calculated diffraction pattern based on the\n", "# two phases: LBCO and Si.\n", "project_2.plot_meas_vs_calc(expt_name='sim_lbco')\n", "\n", "# As you can see, the calculated pattern is now the sum of both phases,\n", "# and Si peaks are visible in the calculated pattern. However, their intensities\n", "# are much too high. Therefore, we need to refine the scale factor of the Si phase.\n", "project_2.experiments['sim_lbco'].linked_phases['si'].scale.free = True\n", "\n", "# Now we can perform the fit with both phases included.\n", "project_2.analysis.fit()\n", "\n", "# Let's plot the measured diffraction pattern and the calculated diffraction\n", "# pattern both for the full range and for a zoomed-in region around the previously\n", "# unexplained peak near 95,000 μs. The calculated pattern will be the sum of\n", "# the two phases.\n", "project_2.plot_meas_vs_calc(expt_name='sim_lbco')\n", "project_2.plot_meas_vs_calc(expt_name='sim_lbco', x_min=88000, x_max=101000)" ] }, { "cell_type": "markdown", "id": "172", "metadata": {}, "source": [ "All previously unexplained peaks are now accounted for in the pattern, and the\n", "fit is improved. Some discrepancies in the peak intensities remain, but\n", "further improvements would require more advanced data reduction and analysis,\n", "which are beyond the scope of this tutorial.\n", "\n", "#### Final Remarks\n", "\n", "In this part of the tutorial, you learned how to use EasyDiffraction\n", "to refine lattice parameters of a more complex crystal structure,\n", "La₀.₅Ba₀.₅CoO₃ (LBCO).\n", "In real experiments, you might also refine additional parameters,\n", "such as atomic positions, occupancies, and atomic displacement factors,\n", "to achieve an even better fit.\n", "For our purposes, we'll stop here, as the goal was to give you a\n", "starting point for analyzing more complex crystal structures\n", "with EasyDiffraction." ] }, { "cell_type": "markdown", "id": "173", "metadata": {}, "source": [ "## 🎁 Bonus\n", "\n", "You've now completed the diffraction data analysis part of the\n", "DMSC Summer School.\n", "To keep learning and exploring more features of the EasyDiffraction library,\n", "visit the official tutorials page, where you'll find more examples:\n", "https://docs.easydiffraction.org/lib/tutorials/" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "tags,-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }