{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:36.705018Z", "iopub.status.busy": "2026-06-30T22:38:36.704869Z", "iopub.status.idle": "2026-06-30T22:38:36.708855Z", "shell.execute_reply": "2026-06-30T22:38:36.708121Z" }, "tags": [ "hide-in-docs" ] }, "outputs": [], "source": [ "# Check whether easydiffraction is installed; install it if needed.\n", "# Required for remote environments such as Google Colab.\n", "import importlib.util\n", "\n", "if importlib.util.find_spec('easydiffraction') is None:\n", " %pip install easydiffraction==0.19.1" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "# Si - powder neutron TOF total - gaussian-damped sinc\n", "\n", "Verifies the total-scattering gaussian-damped sinc profile by comparing\n", "EasyDiffraction's pdffit calculator with a direct diffpy.pdffit2\n", "calculation on the same synthetic r-grid.\n", "\n", "**Refinement:** none — every parameter is taken from the direct\n", "diffpy.pdffit2 reference; only the calculated patterns are compared." ] }, { "cell_type": "code", "execution_count": 2, "id": "2", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:36.710333Z", "iopub.status.busy": "2026-06-30T22:38:36.710174Z", "iopub.status.idle": "2026-06-30T22:38:40.054642Z", "shell.execute_reply": "2026-06-30T22:38:40.053698Z" } }, "outputs": [], "source": [ "from importlib import metadata\n", "\n", "import numpy as np\n", "from diffpy.pdffit2 import PdfFit\n", "from diffpy.structure.parsers.p_cif import P_cif as pdffit_cif_parser\n", "\n", "import easydiffraction as edi\n", "from easydiffraction import ExperimentFactory\n", "from easydiffraction.analysis import verification as verify" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "## Build the project" ] }, { "cell_type": "code", "execution_count": 3, "id": "4", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:40.056494Z", "iopub.status.busy": "2026-06-30T22:38:40.056200Z", "iopub.status.idle": "2026-06-30T22:38:40.270182Z", "shell.execute_reply": "2026-06-30T22:38:40.269366Z" } }, "outputs": [], "source": [ "project = edi.Project()" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Define the structure" ] }, { "cell_type": "code", "execution_count": 4, "id": "6", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:40.272071Z", "iopub.status.busy": "2026-06-30T22:38:40.271782Z", "iopub.status.idle": "2026-06-30T22:38:40.278541Z", "shell.execute_reply": "2026-06-30T22:38:40.277920Z" } }, "outputs": [], "source": [ "project.structures.create(name='si')\n", "structure = project.structures['si']\n", "\n", "structure.space_group.name_h_m = 'F d -3 m'\n", "structure.space_group.coord_system_code = '1'\n", "structure.cell.length_a = 5.4306\n", "\n", "structure.atom_sites.create(\n", " id='Si',\n", " type_symbol='Si',\n", " fract_x=0.0,\n", " fract_y=0.0,\n", " fract_z=0.0,\n", " wyckoff_letter='a',\n", " adp_type='Biso',\n", " adp_iso=0.717,\n", ")" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "## Create the experiment" ] }, { "cell_type": "code", "execution_count": 5, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:40.280732Z", "iopub.status.busy": "2026-06-30T22:38:40.280499Z", "iopub.status.idle": "2026-06-30T22:38:40.342068Z", "shell.execute_reply": "2026-06-30T22:38:40.341188Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mPeak profile type for experiment \u001b[0m\u001b[32m'si_pdf'\u001b[0m\u001b[1;36m changed to\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "gaussian-damped-sinc\n" ] } ], "source": [ "experiment = ExperimentFactory.from_scratch(\n", " name='si_pdf',\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", " scattering_type='total',\n", ")\n", "\n", "R_GRID = np.arange(1.0, 30.05, 0.05)\n", "PDF_SCALE = 1.2728\n", "PDF_QDAMP = 0.0251\n", "PDF_QBROAD = 0.0183\n", "PDF_QMAX = 35.0\n", "PDF_DELTA_1 = 2.54\n", "PDF_DELTA_2 = -1.7525\n", "PDF_PARTICLE_DIAMETER = 0.0\n", "\n", "experiment.data._create_items_set_xcoord_and_id(R_GRID)\n", "experiment.data._set_g_r_meas(np.zeros_like(R_GRID))\n", "experiment.data._set_g_r_meas_su(np.ones_like(R_GRID))\n", "\n", "experiment.peak.type = 'gaussian-damped-sinc'\n", "experiment.peak.damp_q = PDF_QDAMP\n", "experiment.peak.broad_q = PDF_QBROAD\n", "experiment.peak.cutoff_q = PDF_QMAX\n", "experiment.peak.sharp_delta_1 = PDF_DELTA_1\n", "experiment.peak.sharp_delta_2 = PDF_DELTA_2\n", "experiment.peak.damp_particle_diameter = PDF_PARTICLE_DIAMETER\n", "\n", "experiment.linked_structures.create(structure_id='si', scale=PDF_SCALE)\n", "\n", "project.experiments.add(experiment)" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "## Direct pdffit2 reference" ] }, { "cell_type": "code", "execution_count": 6, "id": "10", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:40.343799Z", "iopub.status.busy": "2026-06-30T22:38:40.343574Z", "iopub.status.idle": "2026-06-30T22:38:40.384502Z", "shell.execute_reply": "2026-06-30T22:38:40.383836Z" } }, "outputs": [], "source": [ "B_TO_U_FACTOR = 8.0 * np.pi**2\n", "SI_CIF_FOR_PDFFIT2 = f\"\"\"\n", "data_si\n", "_space_group_name_H-M_alt 'F d -3 m'\n", "_space_group_IT_coordinate_system_code 1\n", "_cell_length_a 5.4306\n", "_cell_length_b 5.4306\n", "_cell_length_c 5.4306\n", "_cell_angle_alpha 90\n", "_cell_angle_beta 90\n", "_cell_angle_gamma 90\n", "loop_\n", "_atom_site_label\n", "_atom_site_type_symbol\n", "_atom_site_fract_x\n", "_atom_site_fract_y\n", "_atom_site_fract_z\n", "_atom_site_U_iso_or_equiv\n", "Si Si 0.0 0.0 0.0 {0.717 / B_TO_U_FACTOR:.12f}\n", "\"\"\"\n", "\n", "pdffit2 = PdfFit()\n", "pdffit2.add_structure(pdffit_cif_parser().parse(SI_CIF_FOR_PDFFIT2))\n", "pdffit2.setvar('pscale', PDF_SCALE)\n", "pdffit2.setvar('delta1', PDF_DELTA_1)\n", "pdffit2.setvar('delta2', PDF_DELTA_2)\n", "pdffit2.setvar('spdiameter', PDF_PARTICLE_DIAMETER)\n", "pdffit2.read_data_lists(\n", " stype='N',\n", " qmax=PDF_QMAX,\n", " qdamp=PDF_QDAMP,\n", " r_data=list(R_GRID),\n", " Gr_data=list(np.zeros_like(R_GRID)),\n", ")\n", "pdffit2.setvar('qbroad', PDF_QBROAD)\n", "pdffit2.calc()\n", "\n", "calc_direct_pdffit2 = np.array(pdffit2.getpdf_fit())\n", "LABEL_DIRECT_PDFFIT2 = f'diffpy.pdffit2 {metadata.version(\"diffpy.pdffit2\")}'" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "## edi-pdffit VS direct pdffit2" ] }, { "cell_type": "code", "execution_count": 7, "id": "12", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:40.389612Z", "iopub.status.busy": "2026-06-30T22:38:40.389399Z", "iopub.status.idle": "2026-06-30T22:38:40.520978Z", "shell.execute_reply": "2026-06-30T22:38:40.520430Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mCalculator for experiment \u001b[0m\u001b[32m'si_pdf'\u001b[0m\u001b[1;36m already set to\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "pdffit\n" ] }, { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "experiment.calculator.type = 'pdffit'\n", "\n", "project.analysis.calculate()\n", "calc_ed_pdffit = experiment.data.intensity_calc\n", "LABEL_ED_PDFFIT = verify.engine_label('pdffit')\n", "\n", "project.display.pattern_comparison(\n", " 'si_pdf',\n", " reference=calc_direct_pdffit2,\n", " candidate=calc_ed_pdffit,\n", " reference_label=LABEL_DIRECT_PDFFIT2,\n", " candidate_label=LABEL_ED_PDFFIT,\n", ")" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "## Agreement check" ] }, { "cell_type": "code", "execution_count": 8, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:40.522874Z", "iopub.status.busy": "2026-06-30T22:38:40.522718Z", "iopub.status.idle": "2026-06-30T22:38:40.531396Z", "shell.execute_reply": "2026-06-30T22:38:40.530803Z" } }, "outputs": [ { "data": { "text/html": [ "
ComparisonMetricExpectedActualOK
1edi 0.19.1 (pdffit 1.6.0) vs diffpy.pdffit2 1.6.0Profile diff (%)< 2.50.00
2Max deviation (%)< 60.00
3Area ratio0.99 to 1.011.0000
4Shape correlation> 0.9991.0000
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "verify.assert_patterns_agree(\n", " [\n", " (\n", " f'{LABEL_ED_PDFFIT} vs {LABEL_DIRECT_PDFFIT2}',\n", " calc_direct_pdffit2,\n", " calc_ed_pdffit,\n", " ),\n", " ],\n", ")" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.5" } }, "nbformat": 4, "nbformat_minor": 5 }