{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:33:59.640945Z", "iopub.status.busy": "2026-06-30T22:33:59.640739Z", "iopub.status.idle": "2026-06-30T22:33:59.645401Z", "shell.execute_reply": "2026-06-30T22:33:59.644512Z" }, "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": [ "# NaCl - powder X-ray CW 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:33:59.647031Z", "iopub.status.busy": "2026-06-30T22:33:59.646883Z", "iopub.status.idle": "2026-06-30T22:34:02.548249Z", "shell.execute_reply": "2026-06-30T22:34:02.547184Z" } }, "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:34:02.550314Z", "iopub.status.busy": "2026-06-30T22:34:02.549881Z", "iopub.status.idle": "2026-06-30T22:34:02.763924Z", "shell.execute_reply": "2026-06-30T22:34:02.763125Z" } }, "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:34:02.766272Z", "iopub.status.busy": "2026-06-30T22:34:02.766079Z", "iopub.status.idle": "2026-06-30T22:34:02.771946Z", "shell.execute_reply": "2026-06-30T22:34:02.771243Z" } }, "outputs": [], "source": [ "project.structures.create(name='nacl')\n", "structure = project.structures['nacl']\n", "\n", "structure.space_group.name_h_m = 'F m -3 m'\n", "structure.space_group.coord_system_code = '1'\n", "structure.cell.length_a = 5.6018\n", "\n", "structure.atom_sites.create(\n", " id='Na',\n", " type_symbol='Na',\n", " fract_x=0.0,\n", " fract_y=0.0,\n", " fract_z=0.0,\n", " adp_type='Biso',\n", " adp_iso=1.1053,\n", ")\n", "structure.atom_sites.create(\n", " id='Cl',\n", " type_symbol='Cl',\n", " fract_x=0.5,\n", " fract_y=0.5,\n", " fract_z=0.5,\n", " adp_type='Biso',\n", " adp_iso=0.5708,\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:34:02.774215Z", "iopub.status.busy": "2026-06-30T22:34:02.773968Z", "iopub.status.idle": "2026-06-30T22:34:02.835135Z", "shell.execute_reply": "2026-06-30T22:34:02.834278Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mPeak profile type for experiment \u001b[0m\u001b[32m'nacl_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='nacl_pdf',\n", " sample_form='powder',\n", " beam_mode='constant wavelength',\n", " radiation_probe='xray',\n", " scattering_type='total',\n", ")\n", "\n", "R_GRID = np.arange(1.0, 30.05, 0.05)\n", "PDF_SCALE = 0.4254\n", "PDF_QDAMP = 0.0606\n", "PDF_QBROAD = 0.0\n", "PDF_QMAX = 21.0\n", "PDF_DELTA_1 = 0.0\n", "PDF_DELTA_2 = 3.5041\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='nacl', 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:34:02.836716Z", "iopub.status.busy": "2026-06-30T22:34:02.836550Z", "iopub.status.idle": "2026-06-30T22:34:02.878763Z", "shell.execute_reply": "2026-06-30T22:34:02.878160Z" } }, "outputs": [], "source": [ "B_TO_U_FACTOR = 8.0 * np.pi**2\n", "NACL_CIF_FOR_PDFFIT2 = f\"\"\"\n", "data_nacl\n", "_space_group_name_H-M_alt 'F m -3 m'\n", "_space_group_IT_coordinate_system_code 1\n", "_cell_length_a 5.6018\n", "_cell_length_b 5.6018\n", "_cell_length_c 5.6018\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", "Na Na 0.0 0.0 0.0 {1.1053 / B_TO_U_FACTOR:.12f}\n", "Cl Cl 0.5 0.5 0.5 {0.5708 / B_TO_U_FACTOR:.12f}\n", "\"\"\"\n", "\n", "pdffit2 = PdfFit()\n", "pdffit2.add_structure(pdffit_cif_parser().parse(NACL_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='X',\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:34:02.880785Z", "iopub.status.busy": "2026-06-30T22:34:02.880434Z", "iopub.status.idle": "2026-06-30T22:34:03.723045Z", "shell.execute_reply": "2026-06-30T22:34:03.722435Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mCalculator for experiment \u001b[0m\u001b[32m'nacl_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", " 'nacl_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:34:03.724558Z", "iopub.status.busy": "2026-06-30T22:34:03.724402Z", "iopub.status.idle": "2026-06-30T22:34:03.732126Z", "shell.execute_reply": "2026-06-30T22:34:03.731501Z" } }, "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", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [] } ], "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 }