{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "8a1bd9c8", "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: Co2SiO4, D20\n", "\n", "This example demonstrates a Rietveld refinement of Co2SiO4 crystal structure\n", "using constant wavelength neutron powder diffraction data from D20 at ILL." ] }, { "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('cosio')" ] }, { "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'\n", "model.space_group.it_coordinate_system_code = 'abc'" ] }, { "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.3\n", "model.cell.length_b = 6.0\n", "model.cell.length_c = 4.8" ] }, { "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('Co1', 'Co', 0, 0, 0, wyckoff_letter='a', b_iso=0.5)\n", "model.atom_sites.add('Co2', 'Co', 0.279, 0.25, 0.985, wyckoff_letter='c', b_iso=0.5)\n", "model.atom_sites.add('Si', 'Si', 0.094, 0.25, 0.429, wyckoff_letter='c', b_iso=0.5)\n", "model.atom_sites.add('O1', 'O', 0.091, 0.25, 0.771, wyckoff_letter='c', b_iso=0.5)\n", "model.atom_sites.add('O2', 'O', 0.448, 0.25, 0.217, wyckoff_letter='c', b_iso=0.5)\n", "model.atom_sites.add('O3', 'O', 0.164, 0.032, 0.28, wyckoff_letter='d', 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('co2sio4_d20.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('d20', data_path='data/co2sio4_d20.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.87\n", "expt.instrument.calib_twotheta_offset = 0.1" ] }, { "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.3\n", "expt.peak.broad_gauss_v = -0.5\n", "expt.peak.broad_gauss_w = 0.4" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "#### Set Background" ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "expt.background.add(x=8, y=500)\n", "expt.background.add(x=9, y=500)\n", "expt.background.add(x=10, y=500)\n", "expt.background.add(x=11, y=500)\n", "expt.background.add(x=12, y=500)\n", "expt.background.add(x=15, y=500)\n", "expt.background.add(x=25, y=500)\n", "expt.background.add(x=30, y=500)\n", "expt.background.add(x=50, y=500)\n", "expt.background.add(x=70, y=500)\n", "expt.background.add(x=90, y=500)\n", "expt.background.add(x=110, y=500)\n", "expt.background.add(x=130, y=500)\n", "expt.background.add(x=150, 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('cosio', scale=1.0)" ] }, { "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='d20', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "43", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='d20', x_min=41, x_max=54, show_residual=True)" ] }, { "cell_type": "markdown", "id": "44", "metadata": {}, "source": [ "#### Set Free Parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "model.cell.length_a.free = True\n", "model.cell.length_b.free = True\n", "model.cell.length_c.free = True\n", "\n", "model.atom_sites['Co2'].fract_x.free = True\n", "model.atom_sites['Co2'].fract_z.free = True\n", "model.atom_sites['Si'].fract_x.free = True\n", "model.atom_sites['Si'].fract_z.free = True\n", "model.atom_sites['O1'].fract_x.free = True\n", "model.atom_sites['O1'].fract_z.free = True\n", "model.atom_sites['O2'].fract_x.free = True\n", "model.atom_sites['O2'].fract_z.free = True\n", "model.atom_sites['O3'].fract_x.free = True\n", "model.atom_sites['O3'].fract_y.free = True\n", "model.atom_sites['O3'].fract_z.free = True\n", "\n", "model.atom_sites['Co1'].b_iso.free = True\n", "model.atom_sites['Co2'].b_iso.free = True\n", "model.atom_sites['Si'].b_iso.free = True\n", "model.atom_sites['O1'].b_iso.free = True\n", "model.atom_sites['O2'].b_iso.free = True\n", "model.atom_sites['O3'].b_iso.free = True" ] }, { "cell_type": "code", "execution_count": null, "id": "46", "metadata": {}, "outputs": [], "source": [ "expt.linked_phases['cosio'].scale.free = True\n", "\n", "expt.instrument.calib_twotheta_offset.free = True\n", "\n", "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_y.free = True\n", "\n", "for point in expt.background:\n", " point.y.free = True" ] }, { "cell_type": "markdown", "id": "47", "metadata": {}, "source": [ "#### Set Constraints\n", "\n", "Set aliases for parameters." ] }, { "cell_type": "code", "execution_count": null, "id": "48", "metadata": {}, "outputs": [], "source": [ "project.analysis.aliases.add(\n", " label='biso_Co1',\n", " param_uid=project.sample_models['cosio'].atom_sites['Co1'].b_iso.uid,\n", ")\n", "project.analysis.aliases.add(\n", " label='biso_Co2',\n", " param_uid=project.sample_models['cosio'].atom_sites['Co2'].b_iso.uid,\n", ")" ] }, { "cell_type": "markdown", "id": "49", "metadata": {}, "source": [ "Set constraints." ] }, { "cell_type": "code", "execution_count": null, "id": "50", "metadata": {}, "outputs": [], "source": [ "project.analysis.constraints.add(\n", " lhs_alias='biso_Co2',\n", " rhs_expr='biso_Co1',\n", ")" ] }, { "cell_type": "markdown", "id": "51", "metadata": {}, "source": [ "Apply constraints." ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "project.analysis.apply_constraints()" ] }, { "cell_type": "markdown", "id": "53", "metadata": {}, "source": [ "#### Run Fitting" ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "project.analysis.fit()" ] }, { "cell_type": "markdown", "id": "55", "metadata": {}, "source": [ "#### Plot Measured vs Calculated" ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='d20', show_residual=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "57", "metadata": {}, "outputs": [], "source": [ "project.plot_meas_vs_calc(expt_name='d20', x_min=41, x_max=54, show_residual=True)" ] }, { "cell_type": "markdown", "id": "58", "metadata": {}, "source": [ "## Summary\n", "\n", "This final section shows how to review the results of the analysis." ] }, { "cell_type": "markdown", "id": "59", "metadata": {}, "source": [ "#### Show Project Summary" ] }, { "cell_type": "code", "execution_count": null, "id": "60", "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 }