{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:53.662778Z", "iopub.status.busy": "2026-08-18T18:16:53.662588Z", "iopub.status.idle": "2026-08-18T18:16:53.667193Z", "shell.execute_reply": "2026-08-18T18:16:53.666331Z" }, "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.20.1" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "# Calculation Without Data: LBCO, CWL\n", "\n", "This example shows how to **calculate and plot a diffraction pattern\n", "without any measured data**. Everything the calculation needs β€” the\n", "crystal structure, the instrument, the peak profile, and the\n", "background β€” is defined in code, and the pattern is computed over a\n", "calculation range instead of over loaded data points.\n", "\n", "This is useful to preview what a candidate structure should look like,\n", "to teach, or to generate a synthetic pattern before any measurement\n", "exists. No data file is downloaded or loaded.\n", "\n", "For this example, a constant-wavelength neutron powder experiment for\n", "La0.5Ba0.5CoO3 is used." ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## πŸ› οΈ Import Library" ] }, { "cell_type": "code", "execution_count": 2, "id": "3", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:53.668850Z", "iopub.status.busy": "2026-08-18T18:16:53.668612Z", "iopub.status.idle": "2026-08-18T18:16:56.420784Z", "shell.execute_reply": "2026-08-18T18:16:56.419945Z" } }, "outputs": [], "source": [ "import easydiffraction as edi" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## πŸ“¦ Define Project" ] }, { "cell_type": "code", "execution_count": 3, "id": "5", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.423170Z", "iopub.status.busy": "2026-08-18T18:16:56.422889Z", "iopub.status.idle": "2026-08-18T18:16:56.716290Z", "shell.execute_reply": "2026-08-18T18:16:56.715420Z" } }, "outputs": [], "source": [ "project = edi.Project(name='lbco_simulation')" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## 🧩 Define Structure" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "### Download CIF file" ] }, { "cell_type": "code", "execution_count": 4, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.718050Z", "iopub.status.busy": "2026-08-18T18:16:56.717853Z", "iopub.status.idle": "2026-08-18T18:16:56.836269Z", "shell.execute_reply": "2026-08-18T18:16:56.835751Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mGetting data\u001b[0m\u001b[1;36m...\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Data \u001b[32m'struct-lbco'\u001b[0m: La0.5Ba0.5CoO3 \u001b[1m(\u001b[0mcrystal structure\u001b[1m)\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "βœ… Data \u001b[32m'struct-lbco'\u001b[0m downloaded to \u001b[32m'../../../data/struct-lbco.cif'\u001b[0m\n" ] } ], "source": [ "structure_path = edi.download_data('struct-lbco', destination='data')" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "### Add Structure from CIF" ] }, { "cell_type": "code", "execution_count": 5, "id": "10", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.838245Z", "iopub.status.busy": "2026-08-18T18:16:56.837952Z", "iopub.status.idle": "2026-08-18T18:16:56.850890Z", "shell.execute_reply": "2026-08-18T18:16:56.850196Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mDefined structures 🧩\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m[\u001b[0m\u001b[32m'lbco'\u001b[0m\u001b[1m]\u001b[0m\n" ] } ], "source": [ "project.structures.add_from_cif_path(structure_path)\n", "project.structures.show_names()\n", "\n", "structure = project.structures['lbco']" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "### Plot Structure" ] }, { "cell_type": "code", "execution_count": 6, "id": "12", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.852649Z", "iopub.status.busy": "2026-08-18T18:16:56.852449Z", "iopub.status.idle": "2026-08-18T18:16:56.897925Z", "shell.execute_reply": "2026-08-18T18:16:56.897117Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mStructure 🧩 \u001b[0m\u001b[32m'lbco'\u001b[0m\u001b[1;36m \u001b[0m\u001b[1;36m(\u001b[0m\u001b[1;36mAtom view type: \u001b[0m\u001b[32m'covalent'\u001b[0m\u001b[1;36m)\u001b[0m\n" ] }, { "data": { "text/html": [ "
\n", "
\n", "
Loading plot…
\n", "
\n", "
\n", "
\n", "
drag = rotate
wheel = zoom
right-drag = pan
\n", "
\n", "
\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.structure(struct_name='lbco')" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "## πŸ”¬ Define Experiment\n", "\n", "### Create Experiment Without Data\n", "\n", "Instead of loading a measured data file, the 'virtual' experiment is created\n", "directly from its type. With no measured scan present, the pattern is\n", "later computed over the `data_range` defined below." ] }, { "cell_type": "code", "execution_count": 7, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.899647Z", "iopub.status.busy": "2026-08-18T18:16:56.899377Z", "iopub.status.idle": "2026-08-18T18:16:56.937920Z", "shell.execute_reply": "2026-08-18T18:16:56.935506Z" } }, "outputs": [], "source": [ "project.experiments.create(\n", " name='sim',\n", " sample_form='powder',\n", " beam_mode='constant wavelength',\n", " radiation_probe='neutron',\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "15", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.943619Z", "iopub.status.busy": "2026-08-18T18:16:56.940916Z", "iopub.status.idle": "2026-08-18T18:16:56.948703Z", "shell.execute_reply": "2026-08-18T18:16:56.945615Z" } }, "outputs": [], "source": [ "experiment = project.experiments['sim']" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "### Set Instrument" ] }, { "cell_type": "code", "execution_count": 9, "id": "17", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.950366Z", "iopub.status.busy": "2026-08-18T18:16:56.950172Z", "iopub.status.idle": "2026-08-18T18:16:56.955496Z", "shell.execute_reply": "2026-08-18T18:16:56.952675Z" } }, "outputs": [], "source": [ "experiment.instrument.setup_wavelength = 1.494" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": 10, "id": "19", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.957290Z", "iopub.status.busy": "2026-08-18T18:16:56.957097Z", "iopub.status.idle": "2026-08-18T18:16:56.963058Z", "shell.execute_reply": "2026-08-18T18:16:56.962351Z" } }, "outputs": [], "source": [ "experiment.peak.broad_gauss_u = 0.1\n", "experiment.peak.broad_gauss_v = -0.1\n", "experiment.peak.broad_gauss_w = 0.1\n", "experiment.peak.broad_lorentz_y = 0.1" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "### Set Background" ] }, { "cell_type": "code", "execution_count": 11, "id": "21", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.967945Z", "iopub.status.busy": "2026-08-18T18:16:56.967755Z", "iopub.status.idle": "2026-08-18T18:16:56.977642Z", "shell.execute_reply": "2026-08-18T18:16:56.974695Z" } }, "outputs": [], "source": [ "experiment.background.create(id='1', position=10, intensity=20)\n", "experiment.background.create(id='2', position=160, intensity=20)" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "### Set Calculation Range\n", "\n", "With no measured data, the x-grid to calculate on comes from the\n", "`data_range` category. It already holds a sensible default window\n", "derived from the instrument, so the experiment is calculable without\n", "any setup. Here we change the default window as an example." ] }, { "cell_type": "code", "execution_count": 12, "id": "23", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.980293Z", "iopub.status.busy": "2026-08-18T18:16:56.980103Z", "iopub.status.idle": "2026-08-18T18:16:56.986029Z", "shell.execute_reply": "2026-08-18T18:16:56.985349Z" } }, "outputs": [], "source": [ "experiment.data_range.two_theta_min = 10.0\n", "experiment.data_range.two_theta_max = 160.0\n", "experiment.data_range.two_theta_inc = 0.05" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "### Set Linked Structures" ] }, { "cell_type": "code", "execution_count": 13, "id": "25", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:56.990265Z", "iopub.status.busy": "2026-08-18T18:16:56.990083Z", "iopub.status.idle": "2026-08-18T18:16:56.996258Z", "shell.execute_reply": "2026-08-18T18:16:56.995362Z" } }, "outputs": [], "source": [ "experiment.linked_structures.create(structure_id='lbco', scale=10.0)" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "## πŸš€ Perform Calculation\n", "\n", "### Display Pattern\n", "\n", "Plotting the pattern computes the calculated curve over the\n", "`data_range` grid and shows a two-panel view: the calculated curve\n", "with its background on the main panel, plus a Bragg-peaks row. There\n", "is no measured curve or residual, because there is no measurement." ] }, { "cell_type": "code", "execution_count": 14, "id": "27", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:57.000033Z", "iopub.status.busy": "2026-08-18T18:16:56.999842Z", "iopub.status.idle": "2026-08-18T18:16:57.754064Z", "shell.execute_reply": "2026-08-18T18:16:57.753245Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='sim')" ] }, { "cell_type": "code", "execution_count": 15, "id": "28", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:57.755549Z", "iopub.status.busy": "2026-08-18T18:16:57.755364Z", "iopub.status.idle": "2026-08-18T18:16:57.800170Z", "shell.execute_reply": "2026-08-18T18:16:57.799299Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='sim', x_min=30, x_max=60)" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "### Modify Parameters and Recalculate" ] }, { "cell_type": "code", "execution_count": 16, "id": "30", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:57.801881Z", "iopub.status.busy": "2026-08-18T18:16:57.801688Z", "iopub.status.idle": "2026-08-18T18:16:57.805038Z", "shell.execute_reply": "2026-08-18T18:16:57.804344Z" } }, "outputs": [], "source": [ "structure.cell.length_a = 3.6\n", "structure.atom_sites['O'].adp_iso = 1.2\n", "experiment.peak.broad_lorentz_y = 0.9" ] }, { "cell_type": "code", "execution_count": 17, "id": "31", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:57.806858Z", "iopub.status.busy": "2026-08-18T18:16:57.806664Z", "iopub.status.idle": "2026-08-18T18:16:58.082883Z", "shell.execute_reply": "2026-08-18T18:16:58.081872Z" } }, "outputs": [], "source": [ "project.analysis.calculate()" ] }, { "cell_type": "code", "execution_count": 18, "id": "32", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:58.084813Z", "iopub.status.busy": "2026-08-18T18:16:58.084629Z", "iopub.status.idle": "2026-08-18T18:16:58.125703Z", "shell.execute_reply": "2026-08-18T18:16:58.124814Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='sim', x_min=30, x_max=60)" ] }, { "cell_type": "markdown", "id": "33", "metadata": {}, "source": [ "## πŸ’Ύ Save Project" ] }, { "cell_type": "code", "execution_count": 19, "id": "34", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:16:58.127379Z", "iopub.status.busy": "2026-08-18T18:16:58.127200Z", "iopub.status.idle": "2026-08-18T18:16:58.296558Z", "shell.execute_reply": "2026-08-18T18:16:58.295753Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSaving project πŸ“¦ \u001b[0m\u001b[32m'lbco_simulation'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/simulate-lbco-cwl'\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“„ project.edi\n", "β”œβ”€β”€ πŸ“ structures/\n", "β”‚ └── πŸ“„ lbco.edi\n", "β”œβ”€β”€ πŸ“ experiments/\n", "β”‚ └── πŸ“„ sim.edi\n", "β”œβ”€β”€ πŸ“ analysis/\n", "β”‚ └── πŸ“„ analysis.edi\n", "└── πŸ“ reports/\n", " └── πŸ“„ lbco_simulation.html\n" ] } ], "source": [ "project.save_as(dir_path='projects/simulate-lbco-cwl')" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }