{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "c166156b", "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: HS, HRPT\n", "\n", "This example demonstrates a Rietveld refinement of HS crystal structure\n", "using constant wavelength neutron powder diffraction data from HRPT at PSI." ] }, { "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('hs')" ] }, { "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 = 'R -3 m'\n", "model.space_group.it_coordinate_system_code = 'h'" ] }, { "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 = 6.9\n", "model.cell.length_c = 14.1" ] }, { "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('Zn', 'Zn', 0, 0, 0.5, wyckoff_letter='b', b_iso=0.5)\n", "model.atom_sites.add('Cu', 'Cu', 0.5, 0, 0, wyckoff_letter='e', b_iso=0.5)\n", "model.atom_sites.add('O', 'O', 0.21, -0.21, 0.06, wyckoff_letter='h', b_iso=0.5)\n", "model.atom_sites.add('Cl', 'Cl', 0, 0, 0.197, wyckoff_letter='c', b_iso=0.5)\n", "model.atom_sites.add('H', '2H', 0.13, -0.13, 0.08, wyckoff_letter='h', b_iso=0.5)" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "#### Symmetry constraints\n", "\n", "Show CIF output before applying symmetry constraints." ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "model.show_as_cif()" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "Apply symmetry constraints." ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "model.apply_symmetry_constraints()" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "Show CIF output after applying symmetry constraints." ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "model.show_as_cif()" ] }, { "cell_type": "markdown", "id": "17", "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": "18", "metadata": {}, "outputs": [], "source": [ "download_from_repository('hrpt_hs.xye', destination='data')" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "#### Create Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "expt = Experiment(name='hrpt', data_path='data/hrpt_hs.xye')" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "#### Set Instrument" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "expt.instrument.setup_wavelength = 1.89\n", "expt.instrument.calib_twotheta_offset = 0.0" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "#### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "expt.peak.broad_gauss_u = 0.1\n", "expt.peak.broad_gauss_v = -0.2\n", "expt.peak.broad_gauss_w = 0.2\n", "expt.peak.broad_lorentz_x = 0.0\n", "expt.peak.broad_lorentz_y = 0" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "expt.background.add(x=4.4196, y=500)\n", "expt.background.add(x=6.6207, y=500)\n", "expt.background.add(x=10.4918, y=500)\n", "expt.background.add(x=15.4634, y=500)\n", "expt.background.add(x=45.6041, y=500)\n", "expt.background.add(x=74.6844, y=500)\n", "expt.background.add(x=103.4187, y=500)\n", "expt.background.add(x=121.6311, y=500)\n", "expt.background.add(x=159.4116, y=500)" ] }, { "cell_type": "markdown", "id": "27", "metadata": {}, "source": [ "#### Set Linked Phases" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "expt.linked_phases.add('hs', scale=0.5)" ] }, { "cell_type": "markdown", "id": "29", "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": "30", "metadata": {}, "outputs": [], "source": [ "project = Project()" ] }, { "cell_type": "markdown", "id": "31", "metadata": {}, "source": [ "#### Set Plotting Engine" ] }, { "cell_type": "code", "execution_count": null, "id": "32", "metadata": {}, "outputs": [], "source": [ "project.plotter.engine = 'plotly'" ] }, { "cell_type": "markdown", "id": "33", "metadata": {}, "source": [ "#### Add Sample Model" ] }, { "cell_type": "code", "execution_count": null, "id": "34", "metadata": {}, "outputs": [], "source": [ "project.sample_models.add(model)" ] }, { "cell_type": "markdown", "id": "35", "metadata": {}, "source": [ "#### Add Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "36", "metadata": {}, "outputs": [], "source": [ "project.experiments.add(expt)" ] }, { "cell_type": "markdown", "id": "37", "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": "38", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_calculator = 'cryspy'" ] }, { "cell_type": "markdown", "id": "39", "metadata": {}, "source": [ "#### Set Minimizer" ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_minimizer = 'lmfit (leastsq)'" ] }, { "cell_type": "markdown", "id": "41", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "42", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "43", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', x_min=48, x_max=51, show_residual=True)" ] }, { "cell_type": "markdown", "id": "44", "metadata": {}, "source": [ "### Perform Fit 1/5\n", "\n", "Set parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "model.cell.length_a.free = True\n", "model.cell.length_c.free = True\n", "\n", "expt.linked_phases['hs'].scale.free = True\n", "expt.instrument.calib_twotheta_offset.free = True" ] }, { "cell_type": "markdown", "id": "46", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "47", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "48", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "49", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "50", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "51", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', x_min=48, x_max=51, show_residual=True)" ] }, { "cell_type": "markdown", "id": "53", "metadata": {}, "source": [ "### Perform Fit 2/5\n", "\n", "Set more parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "expt.peak.broad_gauss_u.free = True\n", "expt.peak.broad_gauss_v.free = True\n", "expt.peak.broad_gauss_w.free = True\n", "expt.peak.broad_lorentz_x.free = True\n", "\n", "for point in expt.background:\n", " point.y.free = True" ] }, { "cell_type": "markdown", "id": "55", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "57", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "58", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "59", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "60", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "61", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', x_min=48, x_max=51, show_residual=True)" ] }, { "cell_type": "markdown", "id": "62", "metadata": {}, "source": [ "### Perform Fit 3/5\n", "\n", "Set more parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "63", "metadata": {}, "outputs": [], "source": [ "model.atom_sites['O'].fract_x.free = True\n", "model.atom_sites['O'].fract_z.free = True\n", "model.atom_sites['Cl'].fract_z.free = True\n", "model.atom_sites['H'].fract_x.free = True\n", "model.atom_sites['H'].fract_z.free = True" ] }, { "cell_type": "markdown", "id": "64", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "65", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "66", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "67", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "68", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "69", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "70", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', x_min=48, x_max=51, show_residual=True)" ] }, { "cell_type": "markdown", "id": "71", "metadata": {}, "source": [ "### Perform Fit 4/5\n", "\n", "Set more parameters to be refined." ] }, { "cell_type": "code", "execution_count": null, "id": "72", "metadata": {}, "outputs": [], "source": [ "model.atom_sites['Zn'].b_iso.free = True\n", "model.atom_sites['Cu'].b_iso.free = True\n", "model.atom_sites['O'].b_iso.free = True\n", "model.atom_sites['Cl'].b_iso.free = True\n", "model.atom_sites['H'].b_iso.free = True" ] }, { "cell_type": "markdown", "id": "73", "metadata": {}, "source": [ "Show free parameters after selection." ] }, { "cell_type": "code", "execution_count": null, "id": "74", "metadata": {}, "outputs": [], "source": [ "project.analysis.show_free_params()" ] }, { "cell_type": "markdown", "id": "75", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "76", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "77", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "78", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "79", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='hrpt', x_min=48, x_max=51, show_residual=True)" ] }, { "cell_type": "markdown", "id": "80", "metadata": {}, "source": [ "## Summary\n", "\n", "This final section shows how to review the results of the analysis." ] }, { "cell_type": "markdown", "id": "81", "metadata": {}, "source": [ "#### Show Project Summary" ] }, { "cell_type": "code", "execution_count": null, "id": "82", "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 }