{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "cba8ffa4", "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": [ "# Structure Refinement: PbSO4, NPD + XRD\n", "\n", "This example demonstrates a more advanced use of the EasyDiffraction library\n", "by explicitly creating and configuring sample models and experiments\n", "before adding them to a project. It could be more suitable for users who are\n", "interested in creating custom workflows. This tutorial provides minimal\n", "explanation and is intended for users already familiar with EasyDiffraction.\n", "\n", "The tutorial covers a Rietveld refinement of PbSO4 crystal structure based\n", "on the joint fit of both X-ray and neutron diffraction data." ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## Import Library" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "from easydiffraction import Experiment\n", "from easydiffraction import Project\n", "from easydiffraction import SampleModel\n", "from easydiffraction import download_from_repository" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Define Sample Model\n", "\n", "This section shows how to add sample models and modify their parameters.\n", "\n", "#### Create Sample Model" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "model = SampleModel('pbso4')" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "#### Set Space Group" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "model.space_group.name_h_m = 'P n m a'" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "#### Set Unit Cell" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "model.cell.length_a = 8.47\n", "model.cell.length_b = 5.39\n", "model.cell.length_c = 6.95" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "#### Set Atom Sites" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "model.atom_sites.add('Pb', 'Pb', 0.1876, 0.25, 0.167, b_iso=1.37)\n", "model.atom_sites.add('S', 'S', 0.0654, 0.25, 0.684, b_iso=0.3777)\n", "model.atom_sites.add('O1', 'O', 0.9082, 0.25, 0.5954, b_iso=1.9764)\n", "model.atom_sites.add('O2', 'O', 0.1935, 0.25, 0.5432, b_iso=1.4456)\n", "model.atom_sites.add('O3', 'O', 0.0811, 0.0272, 0.8086, b_iso=1.2822)" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "## Define Experiments\n", "\n", "This section shows how to add experiments, configure their parameters, and\n", "link the sample models defined in the previous step.\n", "\n", "### Experiment 1: npd\n", "\n", "#### Download Data" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "download_from_repository('d1a_pbso4.dat', destination='data')" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "#### Create Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "expt1 = Experiment('npd', radiation_probe='neutron', data_path='data/d1a_pbso4.dat')" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "#### Set Instrument" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "expt1.instrument.setup_wavelength = 1.91\n", "expt1.instrument.calib_twotheta_offset = -0.1406" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "#### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "expt1.peak.broad_gauss_u = 0.139\n", "expt1.peak.broad_gauss_v = -0.412\n", "expt1.peak.broad_gauss_w = 0.386\n", "expt1.peak.broad_lorentz_x = 0\n", "expt1.peak.broad_lorentz_y = 0.088" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "Select the background type." ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "expt1.background_type = 'line-segment'" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "Add background points." ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "for x, y in [\n", " (11.0, 206.1624),\n", " (15.0, 194.75),\n", " (20.0, 194.505),\n", " (30.0, 188.4375),\n", " (50.0, 207.7633),\n", " (70.0, 201.7002),\n", " (120.0, 244.4525),\n", " (153.0, 226.0595),\n", "]:\n", " expt1.background.add(x, y)" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "#### Set Linked Phases" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "expt1.linked_phases.add('pbso4', scale=1.5)" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "### Experiment 2: xrd\n", "\n", "#### Download Data" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "download_from_repository('lab_pbso4.dat', destination='data')" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "#### Create Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "expt2 = Experiment('xrd', radiation_probe='xray', data_path='data/lab_pbso4.dat')" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "#### Set Instrument" ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [], "source": [ "expt2.instrument.setup_wavelength = 1.540567\n", "expt2.instrument.calib_twotheta_offset = -0.05181" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ "#### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "expt2.peak.broad_gauss_u = 0.304138\n", "expt2.peak.broad_gauss_v = -0.112622\n", "expt2.peak.broad_gauss_w = 0.021272\n", "expt2.peak.broad_lorentz_x = 0\n", "expt2.peak.broad_lorentz_y = 0.057691" ] }, { "cell_type": "markdown", "id": "34", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "markdown", "id": "35", "metadata": {}, "source": [ "Select background type." ] }, { "cell_type": "code", "execution_count": null, "id": "36", "metadata": {}, "outputs": [], "source": [ "expt2.background_type = 'chebyshev polynomial'" ] }, { "cell_type": "markdown", "id": "37", "metadata": {}, "source": [ "Add background points." ] }, { "cell_type": "code", "execution_count": null, "id": "38", "metadata": {}, "outputs": [], "source": [ "for x, y in [\n", " (0, 119.195),\n", " (1, 6.221),\n", " (2, -45.725),\n", " (3, 8.119),\n", " (4, 54.552),\n", " (5, -20.661),\n", "]:\n", " expt2.background.add(x, y)" ] }, { "cell_type": "markdown", "id": "39", "metadata": {}, "source": [ "#### Set Linked Phases" ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "expt2.linked_phases.add('pbso4', scale=0.001)" ] }, { "cell_type": "markdown", "id": "41", "metadata": {}, "source": [ "## Define Project\n", "\n", "The project object is used to manage sample models, experiments, and analysis.\n", "\n", "#### Create Project" ] }, { "cell_type": "code", "execution_count": null, "id": "42", "metadata": {}, "outputs": [], "source": [ "project = Project()" ] }, { "cell_type": "markdown", "id": "43", "metadata": {}, "source": [ "#### Add Sample Model" ] }, { "cell_type": "code", "execution_count": null, "id": "44", "metadata": {}, "outputs": [], "source": [ "project.sample_models.add(model)" ] }, { "cell_type": "markdown", "id": "45", "metadata": {}, "source": [ "#### Add Experiments" ] }, { "cell_type": "code", "execution_count": null, "id": "46", "metadata": {}, "outputs": [], "source": [ "project.experiments.add(expt1)\n", "project.experiments.add(expt2)" ] }, { "cell_type": "markdown", "id": "47", "metadata": {}, "source": [ "## Perform Analysis\n", "\n", "This section outlines the analysis process, including how to configure calculation and fitting engines.\n", "\n", "#### Set Calculator" ] }, { "cell_type": "code", "execution_count": null, "id": "48", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_calculator = 'cryspy'" ] }, { "cell_type": "markdown", "id": "49", "metadata": {}, "source": [ "#### Set Fit Mode" ] }, { "cell_type": "code", "execution_count": null, "id": "50", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit_mode = 'joint'" ] }, { "cell_type": "markdown", "id": "51", "metadata": {}, "source": [ "#### Set Minimizer" ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_minimizer = 'lmfit (leastsq)'" ] }, { "cell_type": "markdown", "id": "53", "metadata": {}, "source": [ "#### Set Fitting Parameters\n", "\n", "Set sample model parameters to be optimized." ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "model.cell.length_a.free = True\n", "model.cell.length_b.free = True\n", "model.cell.length_c.free = True" ] }, { "cell_type": "markdown", "id": "55", "metadata": {}, "source": [ "Set experiment parameters to be optimized." ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "expt1.linked_phases['pbso4'].scale.free = True\n", "\n", "expt1.instrument.calib_twotheta_offset.free = True\n", "\n", "expt1.peak.broad_gauss_u.free = True\n", "expt1.peak.broad_gauss_v.free = True\n", "expt1.peak.broad_gauss_w.free = True\n", "expt1.peak.broad_lorentz_y.free = True" ] }, { "cell_type": "code", "execution_count": null, "id": "57", "metadata": {}, "outputs": [], "source": [ "expt2.linked_phases['pbso4'].scale.free = True\n", "\n", "expt2.instrument.calib_twotheta_offset.free = True\n", "\n", "expt2.peak.broad_gauss_u.free = True\n", "expt2.peak.broad_gauss_v.free = True\n", "expt2.peak.broad_gauss_w.free = True\n", "expt2.peak.broad_lorentz_y.free = True\n", "\n", "for term in expt2.background:\n", " term.coef.free = True" ] }, { "cell_type": "markdown", "id": "58", "metadata": {}, "source": [ "#### Perform Fit" ] }, { "cell_type": "code", "execution_count": null, "id": "59", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "60", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "61", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='npd', x_min=35.5, x_max=38.3, show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "62", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='xrd', x_min=29.0, x_max=30.4, show_residual=True)" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }