{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:16.324908Z", "iopub.status.busy": "2026-08-18T18:35:16.324734Z", "iopub.status.idle": "2026-08-18T18:35:16.329266Z", "shell.execute_reply": "2026-08-18T18:35:16.328390Z" }, "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: NaCl, X-ray\n", "\n", "This is the most minimal \"calculation without data\" example. It\n", "defines an X-ray powder experiment for NaCl, **accepts the default\n", "calculation range**, and plots the calculated pattern — no data file\n", "and no manual range setup.\n", "\n", "The default `data_range` is derived from the instrument (here the\n", "X-ray wavelength), so a structure-only experiment is calculable\n", "immediately." ] }, { "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:35:16.330925Z", "iopub.status.busy": "2026-08-18T18:35:16.330711Z", "iopub.status.idle": "2026-08-18T18:35:19.081379Z", "shell.execute_reply": "2026-08-18T18:35:19.080606Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "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:35:19.083626Z", "iopub.status.busy": "2026-08-18T18:35:19.083308Z", "iopub.status.idle": "2026-08-18T18:35:19.376229Z", "shell.execute_reply": "2026-08-18T18:35:19.375398Z" } }, "outputs": [], "source": [ "project = edi.Project(name='nacl_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:35:19.377928Z", "iopub.status.busy": "2026-08-18T18:35:19.377746Z", "iopub.status.idle": "2026-08-18T18:35:19.381072Z", "shell.execute_reply": "2026-08-18T18:35:19.380400Z" } }, "outputs": [], "source": [ "project.structures.create(name='nacl')" ] }, { "cell_type": "code", "execution_count": 5, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.382640Z", "iopub.status.busy": "2026-08-18T18:35:19.382440Z", "iopub.status.idle": "2026-08-18T18:35:19.385050Z", "shell.execute_reply": "2026-08-18T18:35:19.384378Z" } }, "outputs": [], "source": [ "structure = project.structures['nacl']" ] }, { "cell_type": "code", "execution_count": 6, "id": "9", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.386825Z", "iopub.status.busy": "2026-08-18T18:35:19.386658Z", "iopub.status.idle": "2026-08-18T18:35:19.389667Z", "shell.execute_reply": "2026-08-18T18:35:19.388975Z" } }, "outputs": [], "source": [ "structure.space_group.name_h_m = 'F m -3 m'\n", "structure.space_group.coord_system_code = '1'" ] }, { "cell_type": "code", "execution_count": 7, "id": "10", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.391333Z", "iopub.status.busy": "2026-08-18T18:35:19.391158Z", "iopub.status.idle": "2026-08-18T18:35:19.394356Z", "shell.execute_reply": "2026-08-18T18:35:19.393764Z" } }, "outputs": [], "source": [ "structure.cell.length_a = 5.62" ] }, { "cell_type": "code", "execution_count": 8, "id": "11", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.396129Z", "iopub.status.busy": "2026-08-18T18:35:19.395952Z", "iopub.status.idle": "2026-08-18T18:35:19.401259Z", "shell.execute_reply": "2026-08-18T18:35:19.400515Z" } }, "outputs": [], "source": [ "structure.atom_sites.create(\n", " id='Na',\n", " type_symbol='Na',\n", " fract_x=0,\n", " fract_y=0,\n", " fract_z=0,\n", " adp_iso=0.5,\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_iso=0.5,\n", ")" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "## 🔬 Define Experiment\n", "\n", "### Create Experiment Without Data" ] }, { "cell_type": "code", "execution_count": 9, "id": "13", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.402699Z", "iopub.status.busy": "2026-08-18T18:35:19.402528Z", "iopub.status.idle": "2026-08-18T18:35:19.449654Z", "shell.execute_reply": "2026-08-18T18:35:19.448201Z" } }, "outputs": [], "source": [ "project.experiments.create(\n", " name='sim',\n", " sample_form='powder',\n", " beam_mode='constant wavelength',\n", " radiation_probe='xray',\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "id": "14", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.452026Z", "iopub.status.busy": "2026-08-18T18:35:19.451848Z", "iopub.status.idle": "2026-08-18T18:35:19.456710Z", "shell.execute_reply": "2026-08-18T18:35:19.454211Z" } }, "outputs": [], "source": [ "experiment = project.experiments['sim']" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "### Set Instrument" ] }, { "cell_type": "code", "execution_count": 11, "id": "16", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.458079Z", "iopub.status.busy": "2026-08-18T18:35:19.457902Z", "iopub.status.idle": "2026-08-18T18:35:19.463484Z", "shell.execute_reply": "2026-08-18T18:35:19.459994Z" } }, "outputs": [], "source": [ "experiment.instrument.setup_wavelength = 1.5406" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "### Set Peak Profile" ] }, { "cell_type": "code", "execution_count": 12, "id": "18", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.467888Z", "iopub.status.busy": "2026-08-18T18:35:19.467713Z", "iopub.status.idle": "2026-08-18T18:35:19.471023Z", "shell.execute_reply": "2026-08-18T18:35:19.470246Z" } }, "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" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "### Set Linked Structures" ] }, { "cell_type": "code", "execution_count": 13, "id": "20", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.472557Z", "iopub.status.busy": "2026-08-18T18:35:19.472374Z", "iopub.status.idle": "2026-08-18T18:35:19.476944Z", "shell.execute_reply": "2026-08-18T18:35:19.476229Z" } }, "outputs": [], "source": [ "experiment.linked_structures.create(structure_id='nacl', scale=1.0)" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "### Inspect the Default Calculation Range\n", "\n", "No range is set explicitly: the default window (derived from the\n", "wavelength) is used as-is." ] }, { "cell_type": "code", "execution_count": 14, "id": "22", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.479137Z", "iopub.status.busy": "2026-08-18T18:35:19.478945Z", "iopub.status.idle": "2026-08-18T18:35:19.487711Z", "shell.execute_reply": "2026-08-18T18:35:19.484787Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "min: 8.835740516099717\n", "max: 170.0\n", "inc: 0.16132558506896924\n" ] } ], "source": [ "print('min:', experiment.data_range.two_theta_min.value)\n", "print('max:', experiment.data_range.two_theta_max.value)\n", "print('inc:', experiment.data_range.two_theta_inc.value)" ] }, { "cell_type": "markdown", "id": "23", "metadata": {}, "source": [ "### Set X-ray Polarization Optics\n", "\n", "The polarization coefficient defaults to `0.0`. Setting it to a\n", "nonzero value includes the Lorentz-polarization optics in the\n", "calculated X-ray intensities." ] }, { "cell_type": "code", "execution_count": 15, "id": "24", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:19.489898Z", "iopub.status.busy": "2026-08-18T18:35:19.489719Z", "iopub.status.idle": "2026-08-18T18:35:20.437613Z", "shell.execute_reply": "2026-08-18T18:35:20.436686Z" } }, "outputs": [], "source": [ "project.analysis.calculate()\n", "unpolarized_intensity = np.asarray(experiment.data.intensity_calc).copy()" ] }, { "cell_type": "code", "execution_count": 16, "id": "25", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:20.440149Z", "iopub.status.busy": "2026-08-18T18:35:20.439932Z", "iopub.status.idle": "2026-08-18T18:35:20.443614Z", "shell.execute_reply": "2026-08-18T18:35:20.442735Z" } }, "outputs": [], "source": [ "experiment.instrument.setup_polarization_coefficient = 0.5\n", "experiment.instrument.setup_monochromator_twotheta = 26.5650511771" ] }, { "cell_type": "code", "execution_count": 17, "id": "26", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:20.445780Z", "iopub.status.busy": "2026-08-18T18:35:20.445570Z", "iopub.status.idle": "2026-08-18T18:35:20.664490Z", "shell.execute_reply": "2026-08-18T18:35:20.663680Z" } }, "outputs": [], "source": [ "project.analysis.calculate()\n", "polarized_intensity = np.asarray(experiment.data.intensity_calc).copy()" ] }, { "cell_type": "code", "execution_count": 18, "id": "27", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:20.666224Z", "iopub.status.busy": "2026-08-18T18:35:20.666036Z", "iopub.status.idle": "2026-08-18T18:35:20.669701Z", "shell.execute_reply": "2026-08-18T18:35:20.668722Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "max intensity change: 236202.50313200592\n" ] } ], "source": [ "print('max intensity change:', np.max(np.abs(polarized_intensity - unpolarized_intensity)))" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "## 🚀 Perform Calculation\n", "\n", "### Display Pattern" ] }, { "cell_type": "code", "execution_count": 19, "id": "29", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:20.671304Z", "iopub.status.busy": "2026-08-18T18:35:20.671102Z", "iopub.status.idle": "2026-08-18T18:35:20.739468Z", "shell.execute_reply": "2026-08-18T18:35:20.738650Z" } }, "outputs": [ { "data": { "text/html": [ "
Loading plot…
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "project.display.pattern(expt_name='sim')" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "## 💾 Save Project" ] }, { "cell_type": "code", "execution_count": 20, "id": "31", "metadata": { "execution": { "iopub.execute_input": "2026-08-18T18:35:20.741128Z", "iopub.status.busy": "2026-08-18T18:35:20.740935Z", "iopub.status.idle": "2026-08-18T18:35:20.906626Z", "shell.execute_reply": "2026-08-18T18:35:20.905940Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;36mSaving project 📦 \u001b[0m\u001b[32m'nacl_simulation'\u001b[0m\u001b[1;36m to \u001b[0m\u001b[32m'../../../projects/simulate-nacl-xray'\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "├── 📄 project.edi\n", "├── 📁 structures/\n", "│ └── 📄 nacl.edi\n", "├── 📁 experiments/\n", "│ └── 📄 sim.edi\n", "├── 📁 analysis/\n", "│ └── 📄 analysis.edi\n", "└── 📁 reports/\n", " └── 📄 nacl_simulation.html\n" ] } ], "source": [ "project.save_as(dir_path='projects/simulate-nacl-xray')" ] } ], "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 }