{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:19.064609Z", "iopub.status.busy": "2026-06-30T22:38:19.064395Z", "iopub.status.idle": "2026-06-30T22:38:19.071565Z", "shell.execute_reply": "2026-06-30T22:38:19.071121Z" }, "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": [ "# 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-06-30T22:38:19.074781Z", "iopub.status.busy": "2026-06-30T22:38:19.074601Z", "iopub.status.idle": "2026-06-30T22:38:22.162973Z", "shell.execute_reply": "2026-06-30T22:38:22.162210Z" } }, "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-06-30T22:38:22.164652Z", "iopub.status.busy": "2026-06-30T22:38:22.164382Z", "iopub.status.idle": "2026-06-30T22:38:22.403827Z", "shell.execute_reply": "2026-06-30T22:38:22.403025Z" } }, "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-06-30T22:38:22.405335Z", "iopub.status.busy": "2026-06-30T22:38:22.405177Z", "iopub.status.idle": "2026-06-30T22:38:22.408416Z", "shell.execute_reply": "2026-06-30T22:38:22.407626Z" } }, "outputs": [], "source": [ "project.structures.create(name='nacl')" ] }, { "cell_type": "code", "execution_count": 5, "id": "8", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:22.409767Z", "iopub.status.busy": "2026-06-30T22:38:22.409629Z", "iopub.status.idle": "2026-06-30T22:38:22.412119Z", "shell.execute_reply": "2026-06-30T22:38:22.411384Z" } }, "outputs": [], "source": [ "structure = project.structures['nacl']" ] }, { "cell_type": "code", "execution_count": 6, "id": "9", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:22.413749Z", "iopub.status.busy": "2026-06-30T22:38:22.413615Z", "iopub.status.idle": "2026-06-30T22:38:22.416515Z", "shell.execute_reply": "2026-06-30T22:38:22.415800Z" } }, "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-06-30T22:38:22.417921Z", "iopub.status.busy": "2026-06-30T22:38:22.417780Z", "iopub.status.idle": "2026-06-30T22:38:22.420602Z", "shell.execute_reply": "2026-06-30T22:38:22.419790Z" } }, "outputs": [], "source": [ "structure.cell.length_a = 5.62" ] }, { "cell_type": "code", "execution_count": 8, "id": "11", "metadata": { "execution": { "iopub.execute_input": "2026-06-30T22:38:22.422024Z", "iopub.status.busy": "2026-06-30T22:38:22.421866Z", "iopub.status.idle": "2026-06-30T22:38:22.426427Z", "shell.execute_reply": "2026-06-30T22:38:22.425683Z" } }, "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-06-30T22:38:22.427838Z", "iopub.status.busy": "2026-06-30T22:38:22.427679Z", "iopub.status.idle": "2026-06-30T22:38:22.489320Z", "shell.execute_reply": "2026-06-30T22:38:22.488115Z" } }, "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-06-30T22:38:22.494898Z", "iopub.status.busy": "2026-06-30T22:38:22.494674Z", "iopub.status.idle": "2026-06-30T22:38:22.501190Z", "shell.execute_reply": "2026-06-30T22:38:22.499910Z" } }, "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-06-30T22:38:22.503067Z", "iopub.status.busy": "2026-06-30T22:38:22.502867Z", "iopub.status.idle": "2026-06-30T22:38:22.510398Z", "shell.execute_reply": "2026-06-30T22:38:22.507855Z" } }, "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-06-30T22:38:22.513230Z", "iopub.status.busy": "2026-06-30T22:38:22.512567Z", "iopub.status.idle": "2026-06-30T22:38:22.518726Z", "shell.execute_reply": "2026-06-30T22:38:22.517973Z" } }, "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-06-30T22:38:22.523223Z", "iopub.status.busy": "2026-06-30T22:38:22.520429Z", "iopub.status.idle": "2026-06-30T22:38:22.528485Z", "shell.execute_reply": "2026-06-30T22:38:22.525597Z" } }, "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-06-30T22:38:22.534240Z", "iopub.status.busy": "2026-06-30T22:38:22.532292Z", "iopub.status.idle": "2026-06-30T22:38:22.537997Z", "shell.execute_reply": "2026-06-30T22:38:22.537195Z" } }, "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-06-30T22:38:22.539410Z", "iopub.status.busy": "2026-06-30T22:38:22.539267Z", "iopub.status.idle": "2026-06-30T22:38:23.676415Z", "shell.execute_reply": "2026-06-30T22:38:23.675463Z" } }, "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-06-30T22:38:23.678221Z", "iopub.status.busy": "2026-06-30T22:38:23.677999Z", "iopub.status.idle": "2026-06-30T22:38:23.681599Z", "shell.execute_reply": "2026-06-30T22:38:23.680651Z" } }, "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-06-30T22:38:23.682986Z", "iopub.status.busy": "2026-06-30T22:38:23.682840Z", "iopub.status.idle": "2026-06-30T22:38:23.950495Z", "shell.execute_reply": "2026-06-30T22:38:23.948918Z" } }, "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-06-30T22:38:23.958131Z", "iopub.status.busy": "2026-06-30T22:38:23.957890Z", "iopub.status.idle": "2026-06-30T22:38:23.965035Z", "shell.execute_reply": "2026-06-30T22:38:23.963080Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "max intensity change: 236202.50313200545\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-06-30T22:38:23.967214Z", "iopub.status.busy": "2026-06-30T22:38:23.966580Z", "iopub.status.idle": "2026-06-30T22:38:24.064009Z", "shell.execute_reply": "2026-06-30T22:38:24.063065Z" } }, "outputs": [ { "data": { "text/html": [ "