Skip to content
Snippets Groups Projects
IOC_vs_XS_double.ipynb 536 KiB
Newer Older
mwyman's avatar
mwyman committed
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0dc0eac1-8214-4d13-b3d6-55d28ed5d86a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.optimize import root_scalar\n",
    "import xraylib\n",
    "import matplotlib.pyplot as plt\n",
    "import epics\n",
    "import time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "07b6fc70-6c8a-44bb-a25d-afd559cd9817",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Beamline input block\n",
    "energy = 15000.0            # Energy in eV\n",
    "energy_keV = energy/1000.0  # Energy in keV\n",
    "wl = 1239.84 / (energy * 10**9)\n",
    "d_StoL1 = 51.9              # Source-to-CRL1 distance, in m\n",
    "d_StoL2 = 62.1              # Source-to-CRL2 distance, in m\n",
    "d_Stof  = 66.2              # Source-to-focus distance, in m\n",
    "\n",
    "#slit1_H = 500.0e-6          # H slit size before CRL 1\n",
    "#slit1_V = 300.0e-6          # V slit size before CRL 1\n",
    "#slit2_H = 500.0e-6          # H slit size before CRL 2\n",
    "#slit2_V = 300.0e-6          # V slit size before CRL 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f7317f0f-e9f5-461c-8576-8f7d03113fd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# CRL input block\n",
    "d_min   = 3.0e-5            # Minimum thickness at the apex in m\n",
    "stack_d = 50.0e-3           # Stack thickness in m\n",
    "L1_n    = np.array([1,      1,      1,      1,      1,      1,      2,      4,      8,      16])                # CRL1 number of lenses in each stack\n",
    "L1_R    = np.array([2.0e-3, 1.0e-3, 5.0e-4, 3.0e-4, 2.0e-4, 1.0e-4, 1.0e-4, 1.0e-4, 1.0e-4, 1.0e-4])            # CRL1 lens radius in each stack\n",
    "L1_mater= np.array([\"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\"])              # CRL1 lens material in each stack\n",
    "L1_loc  = np.array([4.5,    3.5,    2.5,    1.5,    0.5,    -0.5,   -1.5,   -2.5,   -3.5,   -4.5])*stack_d      # CRL1 lens stack location relative to center stack, positive means upstream\n",
    "L1_HE   = np.array([1.0e-6, 1.0e-6, 1.0e-6, 1.0e-6, 1.0e-6, 1.0e-6, 1.4e-6, 2.0e-6, 2.8e-6, 4.0e-6])            # CRL1 lens RMS thickness error\n",
    "L2_n    = np.array([1,      1,      1,      1,      1,      1,      2,      4,      8,      16])                # CRL2 number of lenses in each stack\n",
    "L2_R    = np.array([2.0e-3, 1.0e-3, 5.0e-4, 3.0e-4, 2.0e-4, 1.0e-4, 1.0e-4, 1.0e-4, 1.0e-4, 1.0e-4])            # CRL2 lens radius in each stack\n",
    "L2_mater= np.array([\"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\",   \"Be\"])              # CRL2 lens material in each stack\n",
    "L2_loc  = np.array([4.5,    3.5,    2.5,    1.5,    0.5,    -0.5,   -1.5,   -2.5,   -3.5,   -4.5])*stack_d      # CRL2 lens stack location relative to center stack, positive means upstream\n",
    "L2_HE   = np.array([1.0e-6, 1.0e-6, 1.0e-6, 1.0e-6, 1.0e-6, 1.0e-6, 1.4e-6, 2.0e-6, 2.8e-6, 4.0e-6])           # CRL2 lens RMS thickness error\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c1d1228a-2bdd-47d4-ba9e-0d6e79c635a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Source size input block\n",
    "L_und = 4.7                 # undulator length\n",
    "sigmaH_e = 14.8e-6          # Sigma electron source size in H direction in m\n",
    "sigmaV_e = 3.7e-6           # Sigma electron source size in V direction in m\n",
    "sigmaHp_e = 2.8e-6          # Sigma electron divergence in H direction in rad\n",
    "sigmaVp_e = 1.5e-6          # Sigma electron divergence in V direction in rad\n",
    "sigmaH = (sigmaH_e**2 + wl*L_und/2/np.pi/np.pi)**0.5\n",
    "sigmaV = (sigmaV_e**2 + wl*L_und/2/np.pi/np.pi)**0.5\n",
    "sigmaHp = (sigmaHp_e**2 + wl/L_und/2)**0.5\n",
    "sigmaVp = (sigmaVp_e**2 + wl/L_und/2)**0.5\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b6ff87f5-75b3-43bd-94fd-ec8fd880ab7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Lookup table where each entry is a tuple (column1, column2)\n",
    "Lens_diameter_table = [\n",
    "    (50, 450.0),\n",
    "    (100, 632.0),\n",
    "    (200, 894.0),\n",
    "    (300, 1095.0),\n",
    "    (500, 1414.0),\n",
    "    (1000, 2000.0),\n",
    "    (1500, 2450.0),\n",
    "]\n",
    "\n",
    "# Convert the lookup table to a dictionary for faster lookup\n",
    "Lens_diameter_dict = {int(col1): col2 for col1, col2 in Lens_diameter_table}\n",
    "\n",
    "def lookup_diameter(lens_radius):\n",
    "    # Convert the input float to an integer\n",
    "    input_int = int(round(lens_radius*1.0e6))\n",
    "    return Lens_diameter_dict.get(input_int, (lens_radius*1.0e6)**0.5*63.222+ 0.73)/1.0e6\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "401b6573-1cbb-4503-bbda-e864bd8c5969",
   "metadata": {},
   "outputs": [],
   "source": [
    "def index_to_binary_list(index, length):\n",
    "    \"\"\"\n",
    "    Converts an index number to its binary representation as a list of digits,\n",
    "    and pads the list with zeros in front to achieve the desired length.\n",
    "\n",
    "    Parameters:\n",
    "        index (int): The index number to be converted.\n",
    "        length (int): The desired length of the binary list.\n",
    "\n",
    "    Returns:\n",
    "        list: A list of digits representing the binary representation of the index.\n",
    "    \"\"\"\n",
    "    # Convert the index to a binary string and remove the '0b' prefix\n",
    "    binary_str = bin(index)[2:]\n",
    "\n",
    "    # Pad the binary string with zeros in front to achieve the desired length\n",
    "    #padded_binary_str = binary_str.zfill(length)\n",
    "\n",
    "    # Reverse the binary string\n",
    "    reversed_binary_str = binary_str[::-1]\n",
    "\n",
    "    # Convert the reversed binary string to a list of integers\n",
    "    binary_list = [int(digit) for digit in reversed_binary_str]\n",
    "\n",
    "    # Pad the list with zeros at the end to achieve the desired length\n",
    "    while len(binary_list) < length:\n",
    "        binary_list.append(0)\n",
    "    return binary_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7c6792ad-6436-4864-bdb9-68ad571d13ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "def binary_list_to_index(binary_list, length):\n",
    "    \"\"\"\n",
    "    Converts a list of binary digits in reverse order to its integer representation,\n",
    "    padding the list with zeros at the end to have a fixed number of elements.\n",
    "\n",
    "    Parameters:\n",
    "        binary_list (list): A list of digits representing the binary number in reverse order.\n",
    "        length (int): The fixed number of elements the list should have.\n",
    "\n",
    "    Returns:\n",
    "        int: The integer representation of the binary number.\n",
    "    \"\"\"\n",
    "    # Pad the list with zeros at the end to achieve the desired length\n",
    "    while len(binary_list) < length:\n",
    "        binary_list.append(0)\n",
    "\n",
    "    # Convert the binary list to an integer\n",
    "    index = 0\n",
    "    for i, digit in enumerate(binary_list):\n",
    "        index += digit * 2**i\n",
    "\n",
    "    return index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "620e8ed0-0098-463f-8035-0e69ee7bc216",
   "metadata": {},
   "outputs": [],
   "source": [
    "def materials_to_deltas(material_list, energy):\n",
    "    \"\"\"\n",
    "    Convert a list of material names to a list of delta values at a given energy.\n",
    "\n",
    "    Parameters:\n",
    "        material_list (list): A list of material names.\n",
    "        energy (float): The energy in keV.\n",
    "\n",
    "    Returns:\n",
    "        list: A list of delta values for the given materials at the given energy.\n",
    "    \"\"\"\n",
    "    # The list to store delta values\n",
    "    delta_list = []\n",
    "\n",
    "    # Iterate through each material in the input list\n",
    "    for material in material_list:\n",
    "        # Compute the delta value for the current material at the given energy\n",
    "        Z = xraylib.SymbolToAtomicNumber(material)\n",
    "        density = xraylib.ElementDensity(Z)\n",
Loading
Loading full blame...