{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "7c151045", "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: Si, SEPD\n", "\n", "This example demonstrates a Rietveld refinement of Si crystal structure using\n", "time-of-flight neutron powder diffraction data from SEPD at Argonne." ] }, { "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('si')" ] }, { "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 = 'F d -3 m'\n", "model.space_group.it_coordinate_system_code = '2'" ] }, { "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 = 5.431" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "#### Set Atom Sites" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "model.atom_sites.add('Si', 'Si', 0.125, 0.125, 0.125, b_iso=0.5)" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "## Define Experiment\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", "#### Download Measured Data" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "download_from_repository('sepd_si.xye', destination='data')" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "#### Create Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "expt = Experiment('sepd', beam_mode='time-of-flight', data_path='data/sepd_si.xye')" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "#### Set Instrument" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "expt.instrument.setup_twotheta_bank = 144.845\n", "expt.instrument.calib_d_to_tof_offset = 0.0\n", "expt.instrument.calib_d_to_tof_linear = 7476.91\n", "expt.instrument.calib_d_to_tof_quad = -1.54" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "#### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "expt.peak_profile_type = 'pseudo-voigt * ikeda-carpenter'\n", "expt.peak.broad_gauss_sigma_0 = 3.0\n", "expt.peak.broad_gauss_sigma_1 = 40.0\n", "expt.peak.broad_gauss_sigma_2 = 2.0\n", "expt.peak.broad_mix_beta_0 = 0.04221\n", "expt.peak.broad_mix_beta_1 = 0.00946" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "#### Set Peak Asymmetry" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "expt.peak.asym_alpha_0 = 0.0\n", "expt.peak.asym_alpha_1 = 0.5971" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "expt.background_type = 'line-segment'\n", "for x in range(0, 35000, 5000):\n", " expt.background.add(x=x, y=200)" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "#### Set Linked Phases" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "expt.linked_phases.add('si', scale=10.0)" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "## Define Project\n", "\n", "The project object is used to manage the sample model, experiment, and\n", "analysis.\n", "\n", "#### Create Project" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "project = Project()" ] }, { "cell_type": "markdown", "id": "27", "metadata": {}, "source": [ "#### Set Plotting Engine" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "project.plotter.engine = 'plotly'" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "#### Add Sample Model" ] }, { "cell_type": "code", "execution_count": null, "id": "30", "metadata": {}, "outputs": [], "source": [ "project.sample_models.add(model)" ] }, { "cell_type": "markdown", "id": "31", "metadata": {}, "source": [ "#### Add Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "32", "metadata": {}, "outputs": [], "source": [ "project.experiments.add(expt)" ] }, { "cell_type": "markdown", "id": "33", "metadata": {}, "source": [ "## Perform Analysis\n", "\n", "This section shows the analysis process, including how to set up\n", "calculation and fitting engines.\n", "\n", "#### Set Calculator" ] }, { "cell_type": "code", "execution_count": null, "id": "34", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_calculator = 'cryspy'" ] }, { "cell_type": "markdown", "id": "35", "metadata": {}, "source": [ "#### Set Minimizer" ] }, { "cell_type": "code", "execution_count": null, "id": "36", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_minimizer = 'lmfit (leastsq)'" ] }, { "cell_type": "markdown", "id": "37", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "38", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', show_residual=True)\n", "project.plot_meas_vs_calc(expt_name='sepd', x_min=23200, x_max=23700, show_residual=True)" ] }, { "cell_type": "markdown", "id": "39", "metadata": {}, "source": [ "### Perform Fit 1/5\n", "\n", "Set parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "model.cell.length_a.free = True\n", "\n", "expt.linked_phases['si'].scale.free = True\n", "expt.instrument.calib_d_to_tof_offset.free = True" ] }, { "cell_type": "markdown", "id": "41", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "42", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "43", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "44", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "45", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "46", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "47", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', x_min=23200, x_max=23700, show_residual=True)" ] }, { "cell_type": "markdown", "id": "48", "metadata": {}, "source": [ "### Perform Fit 2/5\n", "\n", "Set more parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "49", "metadata": {}, "outputs": [], "source": [ "for point in expt.background:\n", " point.y.free = True" ] }, { "cell_type": "markdown", "id": "50", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "51", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "52", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "53", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "54", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "55", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', x_min=23200, x_max=23700, show_residual=True)" ] }, { "cell_type": "markdown", "id": "57", "metadata": {}, "source": [ "### Perform Fit 3/5\n", "\n", "Fix background points." ] }, { "cell_type": "code", "execution_count": null, "id": "58", "metadata": {}, "outputs": [], "source": [ "for point in expt.background:\n", " point.y.free = False" ] }, { "cell_type": "markdown", "id": "59", "metadata": {}, "source": [ "Set more parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "60", "metadata": {}, "outputs": [], "source": [ "expt.peak.broad_gauss_sigma_0.free = True\n", "expt.peak.broad_gauss_sigma_1.free = True\n", "expt.peak.broad_gauss_sigma_2.free = True" ] }, { "cell_type": "markdown", "id": "61", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "62", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "63", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "64", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "65", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "66", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "67", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', x_min=23200, x_max=23700, show_residual=True)" ] }, { "cell_type": "markdown", "id": "68", "metadata": {}, "source": [ "### Perform Fit 4/5\n", "\n", "Set more parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "69", "metadata": {}, "outputs": [], "source": [ "model.atom_sites['Si'].b_iso.free = True" ] }, { "cell_type": "markdown", "id": "70", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "71", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "72", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "73", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "74", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "75", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "76", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='sepd', x_min=23200, x_max=23700, show_residual=True)" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }