{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:23.408291Z", "iopub.status.busy": "2026-08-18T18:36:23.408132Z", "iopub.status.idle": "2026-08-18T18:36:23.411896Z", "shell.execute_reply": "2026-08-18T18:36:23.411266Z" }, "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: Si, TOF\n", "\n", "This example shows how to **calculate and plot a time-of-flight\n", "diffraction pattern without any measured data**. As with the\n", "constant-wavelength example, the structure, instrument, peak profile,\n", "and background are defined in code, and the pattern is computed over a\n", "calculation range.\n", "\n", "For a time-of-flight instrument an absolute time window is meaningless\n", "without a calibration, so the default `data_range` is derived from the\n", "instrument's TOF calibration. No data file is downloaded or loaded.\n", "\n", "For this example, a time-of-flight neutron powder experiment for Si is\n", "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:36:23.413243Z", "iopub.status.busy": "2026-08-18T18:36:23.413081Z", "iopub.status.idle": "2026-08-18T18:36:26.064637Z", "shell.execute_reply": "2026-08-18T18:36:26.063869Z" } }, "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:36:26.066681Z", "iopub.status.busy": "2026-08-18T18:36:26.066412Z", "iopub.status.idle": "2026-08-18T18:36:26.348621Z", "shell.execute_reply": "2026-08-18T18:36:26.347941Z" } }, "outputs": [], "source": [ "project = edi.Project(name='si_simulation')" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "## 🧩 Define Structure" ] }, { "cell_type": "code", "execution_count": 4, "id": "7", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.350783Z", "iopub.status.busy": "2026-08-18T18:36:26.350600Z", "iopub.status.idle": "2026-08-18T18:36:26.353849Z", "shell.execute_reply": "2026-08-18T18:36:26.353229Z" } }, "outputs": [], "source": [ "project.structures.create(name='si')" ] }, { "cell_type": "code", "execution_count": 5, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.355372Z", "iopub.status.busy": "2026-08-18T18:36:26.355213Z", "iopub.status.idle": "2026-08-18T18:36:26.357689Z", "shell.execute_reply": "2026-08-18T18:36:26.357002Z" } }, "outputs": [], "source": [ "structure = project.structures['si']" ] }, { "cell_type": "code", "execution_count": 6, "id": "9", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.359041Z", "iopub.status.busy": "2026-08-18T18:36:26.358866Z", "iopub.status.idle": "2026-08-18T18:36:26.361830Z", "shell.execute_reply": "2026-08-18T18:36:26.361114Z" } }, "outputs": [], "source": [ "structure.space_group.name_h_m = 'F d -3 m'\n", "structure.space_group.coord_system_code = '2'" ] }, { "cell_type": "code", "execution_count": 7, "id": "10", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.363163Z", "iopub.status.busy": "2026-08-18T18:36:26.362995Z", "iopub.status.idle": "2026-08-18T18:36:26.365597Z", "shell.execute_reply": "2026-08-18T18:36:26.364987Z" } }, "outputs": [], "source": [ "structure.cell.length_a = 5.431" ] }, { "cell_type": "code", "execution_count": 8, "id": "11", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.366987Z", "iopub.status.busy": "2026-08-18T18:36:26.366815Z", "iopub.status.idle": "2026-08-18T18:36:26.370251Z", "shell.execute_reply": "2026-08-18T18:36:26.369624Z" } }, "outputs": [], "source": [ "structure.atom_sites.create(\n", " id='Si',\n", " type_symbol='Si',\n", " fract_x=0.125,\n", " fract_y=0.125,\n", " fract_z=0.125,\n", " adp_iso=0.5,\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "id": "12", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.371775Z", "iopub.status.busy": "2026-08-18T18:36:26.371518Z", "iopub.status.idle": "2026-08-18T18:36:26.634496Z", "shell.execute_reply": "2026-08-18T18:36:26.633716Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mStructure 🧩 \u001b[0m\u001b[32m'si'\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='si')" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "## πŸ”¬ Define Experiment\n", "\n", "### Create Experiment Without Data" ] }, { "cell_type": "code", "execution_count": 10, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.636096Z", "iopub.status.busy": "2026-08-18T18:36:26.635907Z", "iopub.status.idle": "2026-08-18T18:36:26.664361Z", "shell.execute_reply": "2026-08-18T18:36:26.663823Z" } }, "outputs": [], "source": [ "project.experiments.create(\n", " name='sim',\n", " sample_form='powder',\n", " beam_mode='time-of-flight',\n", " radiation_probe='neutron',\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "id": "15", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.666004Z", "iopub.status.busy": "2026-08-18T18:36:26.665838Z", "iopub.status.idle": "2026-08-18T18:36:26.668388Z", "shell.execute_reply": "2026-08-18T18:36:26.667806Z" } }, "outputs": [], "source": [ "experiment = project.experiments['sim']" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "### Set Instrument\n", "\n", "The TOF calibration (offset, linear, and quadratic d-to-TOF terms) is\n", "what converts the default d-spacing window into a time window." ] }, { "cell_type": "code", "execution_count": 12, "id": "17", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.669613Z", "iopub.status.busy": "2026-08-18T18:36:26.669437Z", "iopub.status.idle": "2026-08-18T18:36:26.672549Z", "shell.execute_reply": "2026-08-18T18:36:26.671961Z" } }, "outputs": [], "source": [ "experiment.instrument.setup_twotheta_bank = 144.845\n", "experiment.instrument.calib_d_to_tof_offset = 0.0\n", "experiment.instrument.calib_d_to_tof_linear = 7476.91\n", "experiment.instrument.calib_d_to_tof_quadratic = -1.54" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": 13, "id": "19", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.673878Z", "iopub.status.busy": "2026-08-18T18:36:26.673562Z", "iopub.status.idle": "2026-08-18T18:36:26.677430Z", "shell.execute_reply": "2026-08-18T18:36:26.676871Z" } }, "outputs": [], "source": [ "experiment.peak.broad_gauss_sigma_0 = 3.0\n", "experiment.peak.broad_gauss_sigma_1 = 40.0\n", "experiment.peak.broad_gauss_sigma_2 = 2.0\n", "experiment.peak.decay_beta_0 = 0.04221\n", "experiment.peak.decay_beta_1 = 0.00946\n", "experiment.peak.rise_alpha_0 = 0.0\n", "experiment.peak.rise_alpha_1 = 0.5971" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "### Set Background" ] }, { "cell_type": "code", "execution_count": 14, "id": "21", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.678677Z", "iopub.status.busy": "2026-08-18T18:36:26.678521Z", "iopub.status.idle": "2026-08-18T18:36:26.683864Z", "shell.execute_reply": "2026-08-18T18:36:26.683290Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mBackground type for experiment \u001b[0m\u001b[32m'sim'\u001b[0m\u001b[1;36m already set to\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "line-segment\n" ] } ], "source": [ "experiment.background.type = 'line-segment'\n", "experiment.background.create(id='1', position=10000, intensity=500)\n", "experiment.background.create(id='2', position=40000, intensity=500)" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "### Set Calculation Range" ] }, { "cell_type": "code", "execution_count": 15, "id": "23", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.685050Z", "iopub.status.busy": "2026-08-18T18:36:26.684900Z", "iopub.status.idle": "2026-08-18T18:36:26.687818Z", "shell.execute_reply": "2026-08-18T18:36:26.687253Z" } }, "outputs": [], "source": [ "experiment.data_range.time_of_flight_min = 5000.0\n", "experiment.data_range.time_of_flight_max = 15000.0\n", "experiment.data_range.time_of_flight_inc = 2.0" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "### Set Linked Structures" ] }, { "cell_type": "code", "execution_count": 16, "id": "25", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.689223Z", "iopub.status.busy": "2026-08-18T18:36:26.689057Z", "iopub.status.idle": "2026-08-18T18:36:26.692803Z", "shell.execute_reply": "2026-08-18T18:36:26.691888Z" } }, "outputs": [], "source": [ "experiment.linked_structures.create(structure_id='si', scale=10.0)" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "## πŸš€ Perform Calculation\n", "\n", "### Display Pattern" ] }, { "cell_type": "code", "execution_count": 17, "id": "27", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:26.694182Z", "iopub.status.busy": "2026-08-18T18:36:26.694020Z", "iopub.status.idle": "2026-08-18T18:36:27.824613Z", "shell.execute_reply": "2026-08-18T18:36:27.823790Z" } }, "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": 18, "id": "28", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:27.827120Z", "iopub.status.busy": "2026-08-18T18:36:27.826956Z", "iopub.status.idle": "2026-08-18T18:36:27.867955Z", "shell.execute_reply": "2026-08-18T18:36:27.867218Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='sim', x_min=5000, x_max=6000)" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "### Inspect as Text" ] }, { "cell_type": "code", "execution_count": 19, "id": "30", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:27.869353Z", "iopub.status.busy": "2026-08-18T18:36:27.869204Z", "iopub.status.idle": "2026-08-18T18:36:27.882020Z", "shell.execute_reply": "2026-08-18T18:36:27.881254Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mExperiment πŸ”¬ \u001b[0m\u001b[32m'sim'\u001b[0m\u001b[1;36m as text\u001b[0m\n" ] }, { "data": { "text/html": [ "
Edi
1data_sim
2
3_experiment_type.sample_form powder
4_experiment_type.beam_mode time-of-flight
5_experiment_type.radiation_probe neutron
6_experiment_type.scattering_type bragg
7
8_diffrn.ambient_temperature ?
9_diffrn.ambient_pressure ?
10_diffrn.ambient_magnetic_field ?
11_diffrn.ambient_electric_field ?
12
13_calculator.type cryspy
14
15_data_range.time_of_flight_min 5000.
16_data_range.time_of_flight_max 15000.
17_data_range.time_of_flight_inc 2.
18
19_peak.rise_alpha_0 0.
20_peak.rise_alpha_1 0.5971
21_peak.decay_beta_0 0.04221
22_peak.decay_beta_1 0.00946
23_peak.broad_gauss_sigma_0 3.
24_peak.broad_gauss_sigma_1 40.
25_peak.broad_gauss_sigma_2 2.
26_peak.broad_gauss_size 0.
27_peak.broad_gauss_strain 0.
28_peak.cutoff_fwhm 0.
29_peak.type tof-jorgensen
30
31_instrument.setup_twotheta_bank 144.845
32_instrument.calib_d_to_tof_offset 0.
33_instrument.calib_d_to_tof_linear 7476.91
34_instrument.calib_d_to_tof_quadratic -1.54
35_instrument.calib_d_to_tof_reciprocal 0.
36
37_absorption.type none
38
39loop_
40_linked_structure.structure_id
41_linked_structure.scale
42si 10.
43
44_background.type line-segment
45
46loop_
47_background.id
48_background.position
49_background.intensity
501 10000 500
512 40000 500
52
53loop_
54_refln.id
55_refln.structure_id
56_refln.d_spacing
57_refln.sin_theta_over_lambda
58_refln.index_h
59_refln.index_k
60_refln.index_l
61_refln.f_calc
62_refln.f_squared_calc
63_refln.time_of_flight
641 si 1.92014846 0.26039653 2 2 0 3.20863212 10.29532007 14351.09932031
652 si 1.63750811 0.305342 3 1 1 2.24018272 5.01841861 12239.37137416
663 si 1.56779466 0.31891932 2 2 2 0. 0. 11718.47425195
674 si 1.35775 0.36825631 4 0 0 3.10167267 9.62037338 10148.9355855
685 si 1.24595685 0.40129801 3 3 1 2.16550644 4.68941815 9313.51652715
696 si 1.21440852 0.41172307 4 2 0 0. 0. 9077.75202306
707 si 1.10859823 0.45102002 4 2 2 2.99827871 8.98967524 8286.9965678
718 si 1.04519644 0.47837897 5 1 1 2.09331949 4.3819865 7813.15734337
729 si 1.04519644 0.47837897 3 3 3 2.09331949 4.3819865 7813.15734337
7310 si 0.96007423 0.52079306 4 4 0 2.89833138 8.40032478 7176.96914365
74...
7515 si 0.82821974 0.60370452 5 3 3 1.95608441 3.82626623 6191.46806257
7616 si 0.81875406 0.610684 6 2 2 0. 0. 6120.71803871
7717 si 0.78389733 0.63783863 4 4 4 2.70832085 7.33500184 5860.18344831
7818 si 0.76049211 0.65746901 5 5 1 1.89087853 3.5754216 5685.24043305
7919 si 0.76049211 0.65746901 7 1 1 1.89087853 3.5754216 5685.24043305
8020 si 0.75314419 0.6638835 6 4 0 0. 0. 5630.317808
8121 si 0.7257479 0.68894446 6 4 2 2.61803924 6.85412944 5425.5406156
8222 si 0.70705598 0.70715759 5 5 3 1.82784627 3.34102199 5285.8240047
8323 si 0.70705598 0.70715759 7 3 1 1.82784627 3.34102199 5285.8240047
8424 si 0.678875 0.73651261 8 0 0 2.53076715 6.40478236 5075.1775345
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.experiments['sim'].show_as_text()" ] }, { "cell_type": "markdown", "id": "31", "metadata": {}, "source": [ "## πŸ’Ύ Save Project" ] }, { "cell_type": "code", "execution_count": 20, "id": "32", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:36:27.883335Z", "iopub.status.busy": "2026-08-18T18:36:27.883189Z", "iopub.status.idle": "2026-08-18T18:36:28.042315Z", "shell.execute_reply": "2026-08-18T18:36:28.041514Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSaving project πŸ“¦ \u001b[0m\u001b[32m'si_simulation'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/simulate-si-tof'\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "β”œβ”€β”€ πŸ“„ project.edi\n", "β”œβ”€β”€ πŸ“ structures/\n", "β”‚ └── πŸ“„ si.edi\n", "β”œβ”€β”€ πŸ“ experiments/\n", "β”‚ └── πŸ“„ sim.edi\n", "β”œβ”€β”€ πŸ“ analysis/\n", "β”‚ └── πŸ“„ analysis.edi\n", "└── πŸ“ reports/\n", " └── πŸ“„ si_simulation.html\n" ] } ], "source": [ "project.save_as(dir_path='projects/simulate-si-tof')" ] } ], "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 }