{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "287e0e45", "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: LBCO+Si, McStas\n", "\n", "This example demonstrates a Rietveld refinement of La0.5Ba0.5CoO3 crystal\n", "structure with a small amount of Si phase using time-of-flight neutron powder\n", "diffraction data simulated with McStas." ] }, { "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 Models\n", "\n", "This section shows how to add sample models and modify their parameters.\n", "\n", "### Create Sample Model 1: LBCO" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "model_1 = SampleModel('lbco')" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "#### Set Space Group" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "model_1.space_group.name_h_m = 'P m -3 m'\n", "model_1.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_1.cell.length_a = 3.8909" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "#### Set Atom Sites" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "model_1.atom_sites.add(\n", " 'La',\n", " 'La',\n", " 0,\n", " 0,\n", " 0,\n", " wyckoff_letter='a',\n", " b_iso=0.2,\n", " occupancy=0.5,\n", ")\n", "model_1.atom_sites.add(\n", " 'Ba',\n", " 'Ba',\n", " 0,\n", " 0,\n", " 0,\n", " wyckoff_letter='a',\n", " b_iso=0.2,\n", " occupancy=0.5,\n", ")\n", "model_1.atom_sites.add(\n", " 'Co',\n", " 'Co',\n", " 0.5,\n", " 0.5,\n", " 0.5,\n", " wyckoff_letter='b',\n", " b_iso=0.2567,\n", ")\n", "model_1.atom_sites.add(\n", " 'O',\n", " 'O',\n", " 0,\n", " 0.5,\n", " 0.5,\n", " wyckoff_letter='c',\n", " b_iso=1.4041,\n", ")" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "### Create Sample Model 2: Si" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "model_2 = SampleModel('si')" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "#### Set Space Group" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "model_2.space_group.name_h_m = 'F d -3 m'\n", "model_2.space_group.it_coordinate_system_code = '2'" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "#### Set Unit Cell" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "model_2.cell.length_a = 5.43146" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "#### Set Atom Sites" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "model_2.atom_sites.add(\n", " 'Si',\n", " 'Si',\n", " 0.0,\n", " 0.0,\n", " 0.0,\n", " wyckoff_letter='a',\n", " b_iso=0.0,\n", ")" ] }, { "cell_type": "markdown", "id": "19", "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 Data" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "download_from_repository('mcstas_lbco-si.xye', destination='data')" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "#### Create Experiment" ] }, { "cell_type": "code", "execution_count": null, "id": "22", "metadata": {}, "outputs": [], "source": [ "experiment = Experiment(\n", " 'mcstas',\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", " scattering_type='bragg',\n", " data_path='data/mcstas_lbco-si.xye',\n", ")" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "#### Set Instrument" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "experiment.instrument.setup_twotheta_bank = 94.90931761529106\n", "experiment.instrument.calib_d_to_tof_offset = 0.0\n", "experiment.instrument.calib_d_to_tof_linear = 58724.76869981215\n", "experiment.instrument.calib_d_to_tof_quad = -0.00001" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "#### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "# experiment.peak_profile_type = 'pseudo-voigt * ikeda-carpenter'\n", "experiment.peak.broad_gauss_sigma_0 = 45137\n", "experiment.peak.broad_gauss_sigma_1 = -52394\n", "experiment.peak.broad_gauss_sigma_2 = 22998\n", "experiment.peak.broad_mix_beta_0 = 0.0055\n", "experiment.peak.broad_mix_beta_1 = 0.0041\n", "experiment.peak.asym_alpha_0 = 0\n", "experiment.peak.asym_alpha_1 = 0.0097" ] }, { "cell_type": "markdown", "id": "27", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "Select the background type." ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "experiment.background_type = 'line-segment'" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "Add background points." ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [], "source": [ "experiment.background.add(x=45000, y=0.2)\n", "experiment.background.add(x=50000, y=0.2)\n", "experiment.background.add(x=55000, y=0.2)\n", "experiment.background.add(x=65000, y=0.2)\n", "experiment.background.add(x=70000, y=0.2)\n", "experiment.background.add(x=75000, y=0.2)\n", "experiment.background.add(x=80000, y=0.2)\n", "experiment.background.add(x=85000, y=0.2)\n", "experiment.background.add(x=90000, y=0.2)\n", "experiment.background.add(x=95000, y=0.2)\n", "experiment.background.add(x=100000, y=0.2)\n", "experiment.background.add(x=105000, y=0.2)\n", "experiment.background.add(x=110000, y=0.2)" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ "#### Set Linked Phases" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "experiment.linked_phases.add('lbco', scale=4.0)\n", "experiment.linked_phases.add('si', scale=0.2)" ] }, { "cell_type": "markdown", "id": "34", "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": "35", "metadata": {}, "outputs": [], "source": [ "project = Project()" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "#### Set Plotting Engine" ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "project.plotter.engine = 'plotly'" ] }, { "cell_type": "markdown", "id": "38", "metadata": {}, "source": [ "#### Add Sample Models" ] }, { "cell_type": "code", "execution_count": null, "id": "39", "metadata": {}, "outputs": [], "source": [ "project.sample_models.add(model_1)\n", "project.sample_models.add(model_2)" ] }, { "cell_type": "markdown", "id": "40", "metadata": {}, "source": [ "#### Show Sample Models" ] }, { "cell_type": "code", "execution_count": null, "id": "41", "metadata": {}, "outputs": [], "source": [ "project.sample_models.show_names()" ] }, { "cell_type": "markdown", "id": "42", "metadata": {}, "source": [ "#### Add Experiments" ] }, { "cell_type": "code", "execution_count": null, "id": "43", "metadata": {}, "outputs": [], "source": [ "project.experiments.add(experiment)" ] }, { "cell_type": "markdown", "id": "44", "metadata": {}, "source": [ "#### Set Excluded Regions\n", "\n", "Show measured data as loaded from the file." ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "project.plot_meas(expt_name='mcstas')" ] }, { "cell_type": "markdown", "id": "46", "metadata": {}, "source": [ "Add excluded regions." ] }, { "cell_type": "code", "execution_count": null, "id": "47", "metadata": {}, "outputs": [], "source": [ "experiment.excluded_regions.add(minimum=0, maximum=40000)\n", "experiment.excluded_regions.add(minimum=108000, maximum=200000)" ] }, { "cell_type": "markdown", "id": "48", "metadata": {}, "source": [ "Show excluded regions." ] }, { "cell_type": "code", "execution_count": null, "id": "49", "metadata": {}, "outputs": [], "source": [ "experiment.excluded_regions.show()" ] }, { "cell_type": "markdown", "id": "50", "metadata": {}, "source": [ "Show measured data after adding excluded regions." ] }, { "cell_type": "code", "execution_count": null, "id": "51", "metadata": {}, "outputs": [], "source": [ "project.plot_meas(expt_name='mcstas')" ] }, { "cell_type": "markdown", "id": "52", "metadata": {}, "source": [ "Show experiment as CIF." ] }, { "cell_type": "code", "execution_count": null, "id": "53", "metadata": {}, "outputs": [], "source": [ "project.experiments['mcstas'].show_as_cif()" ] }, { "cell_type": "markdown", "id": "54", "metadata": {}, "source": [ "## Perform Analysis\n", "\n", "This section outlines the analysis process, including how to configure\n", "calculation and fitting engines.\n", "\n", "#### Set Calculator" ] }, { "cell_type": "code", "execution_count": null, "id": "55", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_calculator = 'cryspy'" ] }, { "cell_type": "markdown", "id": "56", "metadata": {}, "source": [ "#### Set Minimizer" ] }, { "cell_type": "code", "execution_count": null, "id": "57", "metadata": {}, "outputs": [], "source": [ "project.analysis.current_minimizer = 'lmfit (leastsq)'" ] }, { "cell_type": "markdown", "id": "58", "metadata": {}, "source": [ "#### Set Fitting Parameters\n", "\n", "Set sample model parameters to be optimized." ] }, { "cell_type": "code", "execution_count": null, "id": "59", "metadata": {}, "outputs": [], "source": [ "model_1.cell.length_a.free = True\n", "model_1.atom_sites['Co'].b_iso.free = True\n", "model_1.atom_sites['O'].b_iso.free = True\n", "\n", "model_2.cell.length_a.free = True" ] }, { "cell_type": "markdown", "id": "60", "metadata": {}, "source": [ "Set experiment parameters to be optimized." ] }, { "cell_type": "code", "execution_count": null, "id": "61", "metadata": {}, "outputs": [], "source": [ "experiment.linked_phases['lbco'].scale.free = True\n", "experiment.linked_phases['si'].scale.free = True\n", "\n", "experiment.peak.broad_gauss_sigma_0.free = True\n", "experiment.peak.broad_gauss_sigma_1.free = True\n", "experiment.peak.broad_gauss_sigma_2.free = True\n", "\n", "experiment.peak.asym_alpha_1.free = True\n", "experiment.peak.broad_mix_beta_0.free = True\n", "experiment.peak.broad_mix_beta_1.free = True\n", "\n", "for point in experiment.background:\n", " point.y.free = True" ] }, { "cell_type": "markdown", "id": "62", "metadata": {}, "source": [ "#### Perform Fit" ] }, { "cell_type": "code", "execution_count": null, "id": "63", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "64", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "65", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='mcstas')" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }