{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d91893e9", "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: NCAF, WISH\n", "\n", "This example demonstrates a Rietveld refinement of Na2Ca3Al2F14 crystal\n", "structure using time-of-flight neutron powder diffraction data from WISH at\n", "ISIS.\n", "\n", "Two datasets from detector banks 5+6 and 4+7 are used for joint fitting." ] }, { "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 covers 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('ncaf')" ] }, { "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 = 'I 21 3'\n", "model.space_group.it_coordinate_system_code = '1'" ] }, { "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 = 10.250256" ] }, { "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('Ca', 'Ca', 0.4663, 0.0, 0.25, wyckoff_letter='b', b_iso=0.92)\n", "model.atom_sites.add('Al', 'Al', 0.2521, 0.2521, 0.2521, wyckoff_letter='a', b_iso=0.73)\n", "model.atom_sites.add('Na', 'Na', 0.0851, 0.0851, 0.0851, wyckoff_letter='a', b_iso=2.08)\n", "model.atom_sites.add('F1', 'F', 0.1377, 0.3054, 0.1195, wyckoff_letter='c', b_iso=0.90)\n", "model.atom_sites.add('F2', 'F', 0.3625, 0.3633, 0.1867, wyckoff_letter='c', b_iso=1.37)\n", "model.atom_sites.add('F3', 'F', 0.4612, 0.4612, 0.4612, wyckoff_letter='a', b_iso=0.88)" ] }, { "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(\n", " 'wish_ncaf_5_6.xys',\n", " destination='data',\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "download_from_repository(\n", " 'wish_ncaf_4_7.xys',\n", " destination='data',\n", ")" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "#### Create Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "expt56 = Experiment(\n", " 'wish_5_6',\n", " beam_mode='time-of-flight',\n", " data_path='data/wish_ncaf_5_6.xys',\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "expt47 = Experiment(\n", " 'wish_4_7',\n", " beam_mode='time-of-flight',\n", " data_path='data/wish_ncaf_4_7.xys',\n", ")" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "#### Set Instrument" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "expt56.instrument.setup_twotheta_bank = 152.827\n", "expt56.instrument.calib_d_to_tof_offset = -13.5\n", "expt56.instrument.calib_d_to_tof_linear = 20773.0\n", "expt56.instrument.calib_d_to_tof_quad = -1.08308" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "expt47.instrument.setup_twotheta_bank = 121.660\n", "expt47.instrument.calib_d_to_tof_offset = -15.0\n", "expt47.instrument.calib_d_to_tof_linear = 18660.0\n", "expt47.instrument.calib_d_to_tof_quad = -0.47488" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "#### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "expt56.peak.broad_gauss_sigma_0 = 0.0\n", "expt56.peak.broad_gauss_sigma_1 = 0.0\n", "expt56.peak.broad_gauss_sigma_2 = 15.5\n", "expt56.peak.broad_mix_beta_0 = 0.007\n", "expt56.peak.broad_mix_beta_1 = 0.01\n", "expt56.peak.asym_alpha_0 = -0.0094\n", "expt56.peak.asym_alpha_1 = 0.1" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "expt47.peak.broad_gauss_sigma_0 = 0.0\n", "expt47.peak.broad_gauss_sigma_1 = 29.8\n", "expt47.peak.broad_gauss_sigma_2 = 18.0\n", "expt47.peak.broad_mix_beta_0 = 0.006\n", "expt47.peak.broad_mix_beta_1 = 0.015\n", "expt47.peak.asym_alpha_0 = -0.0115\n", "expt47.peak.asym_alpha_1 = 0.1" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "expt56.background_type = 'line-segment'\n", "for x, y in [\n", " (9162, 465),\n", " (11136, 593),\n", " (13313, 497),\n", " (14906, 546),\n", " (16454, 533),\n", " (17352, 496),\n", " (18743, 428),\n", " (20179, 452),\n", " (21368, 397),\n", " (22176, 468),\n", " (22827, 477),\n", " (24644, 380),\n", " (26439, 381),\n", " (28257, 378),\n", " (31196, 343),\n", " (34034, 328),\n", " (37265, 310),\n", " (41214, 323),\n", " (44827, 283),\n", " (49830, 273),\n", " (52905, 257),\n", " (58204, 260),\n", " (62916, 261),\n", " (70186, 262),\n", " (74204, 262),\n", " (82103, 268),\n", " (91958, 268),\n", " (102712, 262),\n", "]:\n", " expt56.background.add(x, y)" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "expt47.background_type = 'line-segment'\n", "for x, y in [\n", " (9090, 488),\n", " (10672, 566),\n", " (12287, 494),\n", " (14037, 559),\n", " (15451, 529),\n", " (16764, 445),\n", " (18076, 460),\n", " (19456, 413),\n", " (20466, 511),\n", " (21880, 396),\n", " (23798, 391),\n", " (25447, 385),\n", " (28073, 349),\n", " (30058, 332),\n", " (32583, 309),\n", " (34804, 355),\n", " (37160, 318),\n", " (40324, 290),\n", " (46895, 260),\n", " (50631, 256),\n", " (54602, 246),\n", " (58439, 264),\n", " (66520, 250),\n", " (75002, 258),\n", " (83649, 257),\n", " (92770, 255),\n", " (101524, 260),\n", "]:\n", " expt47.background.add(x, y)" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "#### Set Linked Phases" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "expt56.linked_phases.add('ncaf', scale=1.0)" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "expt47.linked_phases.add('ncaf', scale=2.0)" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "#### Set Excluded Regions" ] }, { "cell_type": "code", "execution_count": null, "id": "30", "metadata": {}, "outputs": [], "source": [ "expt56.excluded_regions.add(minimum=0, maximum=10010)\n", "expt56.excluded_regions.add(minimum=100010, maximum=200000)" ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [], "source": [ "expt47.excluded_regions.add(minimum=0, maximum=10006)\n", "expt47.excluded_regions.add(minimum=100004, maximum=200000)" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ "## Define Project\n", "\n", "The project object is used to manage the sample model, experiments, and\n", "analysis\n", "\n", "#### Create Project" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "project = Project()" ] }, { "cell_type": "markdown", "id": "34", "metadata": {}, "source": [ "#### Set Plotting Engine" ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "project.plotter.engine = 'plotly'" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "#### Add Sample Model" ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "project.sample_models.add(model)" ] }, { "cell_type": "markdown", "id": "38", "metadata": {}, "source": [ "#### Add Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "39", "metadata": {}, "outputs": [], "source": [ "project.experiments.add(expt56)\n", "project.experiments.add(expt47)" ] }, { "cell_type": "markdown", "id": "40", "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": "41", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_calculator = 'cryspy'" ] }, { "cell_type": "markdown", "id": "42", "metadata": {}, "source": [ "#### Set Minimizer" ] }, { "cell_type": "code", "execution_count": null, "id": "43", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_minimizer = 'lmfit (leastsq)'" ] }, { "cell_type": "markdown", "id": "44", "metadata": {}, "source": [ "#### Set Fit Mode" ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit_mode = 'joint'" ] }, { "cell_type": "markdown", "id": "46", "metadata": {}, "source": [ "#### Set Free Parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "47", "metadata": {}, "outputs": [], "source": [ "model.atom_sites['Ca'].b_iso.free = True\n", "model.atom_sites['Al'].b_iso.free = True\n", "model.atom_sites['Na'].b_iso.free = True\n", "model.atom_sites['F1'].b_iso.free = True\n", "model.atom_sites['F2'].b_iso.free = True\n", "model.atom_sites['F3'].b_iso.free = True" ] }, { "cell_type": "code", "execution_count": null, "id": "48", "metadata": {}, "outputs": [], "source": [ "expt56.linked_phases['ncaf'].scale.free = True\n", "expt56.instrument.calib_d_to_tof_offset.free = True\n", "expt56.instrument.calib_d_to_tof_linear.free = True\n", "expt56.peak.broad_gauss_sigma_2.free = True\n", "expt56.peak.broad_mix_beta_0.free = True\n", "expt56.peak.broad_mix_beta_1.free = True\n", "expt56.peak.asym_alpha_1.free = True\n", "\n", "expt47.linked_phases['ncaf'].scale.free = True\n", "expt47.instrument.calib_d_to_tof_linear.free = True\n", "expt47.instrument.calib_d_to_tof_offset.free = True\n", "expt47.peak.broad_gauss_sigma_2.free = True\n", "expt47.peak.broad_mix_beta_0.free = True\n", "expt47.peak.broad_mix_beta_1.free = True\n", "expt47.peak.asym_alpha_1.free = True" ] }, { "cell_type": "markdown", "id": "49", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "50", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='wish_5_6', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "51", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='wish_4_7', show_residual=True)" ] }, { "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='wish_5_6', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='wish_4_7', show_residual=True)" ] }, { "cell_type": "markdown", "id": "57", "metadata": {}, "source": [ "## Summary\n", "\n", "This final section shows how to review the results of the analysis." ] }, { "cell_type": "markdown", "id": "58", "metadata": {}, "source": [ "#### Show Project Summary" ] }, { "cell_type": "code", "execution_count": null, "id": "59", "metadata": {}, "outputs": [], "source": [ "project.summary.show_report()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }