{ "cells": [ { "cell_type": "markdown", "id": "39bb27c0-ad6f-44e5-8cda-be7ce1321003", "metadata": {}, "source": [ "# Absolute and relative cavity entry phases" ] }, { "cell_type": "markdown", "id": "074a688c-57a8-44a1-be6d-7dedf02cabca", "metadata": {}, "source": [ "There are three ways to define the phase of a cavity: by its relative entry phase $\\phi_{0,\\,\\mathrm{rel}}$, its absolute entry phase $\\phi_{0,\\,\\mathrm{abs}}$ or its synchronous phase $\\phi_s$ (still under implementation).\n", "\n", "The relation between relative and entry phases is:\n", "\\begin{equation}\n", "E_0\\cos{\\phi_{0,\\,\\mathrm{abs}}} = E_0\\cos{(\\phi_{0,\\,\\mathrm{rel}} + \\phi_\\mathrm{in})}\n", "\\end{equation}\n", "where $\\phi_\\mathrm{in}$ is the phase at which the synchronous particle enters the cavity, $E_0$ is the amplitude of the electric field." ] }, { "cell_type": "markdown", "id": "94694e66-b40c-42f7-afd7-485e4e38f597", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "markdown", "id": "f5dcfd42-7320-42a2-b468-5112ee13b628", "metadata": {}, "source": [ "### Load libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "9a9755b1-b827-49f4-a092-183f5c63022e", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/placais/LightWin/venv/lib/python3.12/site-packages/pymoo/core/decision_making.py:2: DeprecationWarning: Please import `cKDTree` from the `scipy.spatial` namespace; the `scipy.spatial.ckdtree` namespace is deprecated and will be removed in SciPy 2.0.0.\n", " from scipy.spatial.ckdtree import cKDTree\n" ] } ], "source": [ "from pathlib import Path\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "from lightwin.config_manager import process_config\n", "from lightwin.beam_calculation.beam_calculator import BeamCalculator\n", "from lightwin.beam_calculation.factory import BeamCalculatorsFactory\n", "from lightwin.beam_calculation.simulation_output.simulation_output import SimulationOutput\n", "from lightwin.core.accelerator.accelerator import Accelerator\n", "from lightwin.core.accelerator.factory import NoFault, WithFaults\n", "from lightwin.failures.fault_scenario import FaultScenario, fault_scenario_factory\n", "from lightwin.visualization import plot\n", "from lightwin.constants import example_config, example_results\n", "\n", "plt.rcParams[\"figure.figsize\"] = (15, 5)" ] }, { "cell_type": "markdown", "id": "fcd631a0-2635-4ed5-b0d3-feda011692c3", "metadata": {}, "source": [ "### Set configuration dicts" ] }, { "cell_type": "code", "execution_count": 2, "id": "03add597-9c4c-4ff3-9f3d-5792412fe0ed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0;37m[INFO ] [files_specs.py ]\u001b[0m Setting project_path = PosixPath('/home/placais/LightWin/data/example/results_tests')\n", "Setting log_file = 'lightwin.log'\n" ] } ], "source": [ "CONFIG_KEYS = {\n", " 'files': 'files',\n", " 'beam_calculator': 'generic_envelope1d',\n", " 'beam': 'beam',\n", " 'plots': 'plots_minimal',\n", " 'wtf': 'generic_wtf',\n", " 'design_space': 'generic_design_space',\n", "}\n", "\n", "override = {'beam_calculator': {'flag_phi_abs': True}, 'plots': {'energy': False}}\n", "config_abs = process_config(example_config, CONFIG_KEYS, warn_mismatch=True, override=override)\n", "plots_phase = config_abs['plots']" ] }, { "cell_type": "code", "execution_count": 3, "id": "7c3be5fa-3aad-4c60-b504-340841da109d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0;37m[INFO ] [table_spec.py ]\u001b[0m .toml table [files] loaded!\n", "\u001b[0;37m[INFO ] [table_spec.py ]\u001b[0m .toml table [beam] loaded!\n", "\u001b[0;37m[INFO ] [table_spec.py ]\u001b[0m .toml table [generic_envelope1d] loaded!\n", "\u001b[0;37m[INFO ] [table_spec.py ]\u001b[0m .toml table [plots_minimal] loaded!\n", "\u001b[0;37m[INFO ] [table_spec.py ]\u001b[0m .toml table [generic_design_space] loaded!\n", "\u001b[0;37m[INFO ] [table_spec.py ]\u001b[0m .toml table [generic_wtf] loaded!\n", "\u001b[0;37m[INFO ] [files_specs.py ]\u001b[0m Setting project_path = PosixPath('/home/placais/LightWin/data/example/results_tests')\n", "Setting log_file = 'lightwin.log'\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/placais/LightWin/src/lightwin/util/log_manager.py:67: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/placais/LightWin/data/example/results_tests/lightwin.log' mode='a' encoding='UTF-8'>\n", " del logging.root.handlers[:]\n", "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n" ] } ], "source": [ "override = {\n", " 'beam_calculator':\n", " {\n", " 'flag_phi_abs': False\n", " },\n", " 'plots': {\n", " 'cav': True\n", " },\n", " 'wtf': {\n", " 'objective_preset': 'rephased_ADS',\n", " }\n", "}\n", "config_rel = process_config(example_config, CONFIG_KEYS, warn_mismatch=True, override=override)\n", "plots_complete = config_rel['plots']" ] }, { "cell_type": "markdown", "id": "adb4ccdd-cd4c-4f84-9a02-ef340b315633", "metadata": {}, "source": [ "### Set BeamCalculator objects" ] }, { "cell_type": "code", "execution_count": 4, "id": "ebf76a62-ba8a-479f-a8d1-7646a5286984", "metadata": {}, "outputs": [], "source": [ "factory = BeamCalculatorsFactory(**config_abs)\n", "solver_abs = factory.run_all()[0]\n", "\n", "factory = BeamCalculatorsFactory(**config_rel)\n", "solver_rel = factory.run_all()[0]" ] }, { "cell_type": "markdown", "id": "b474ac08-4d2e-44bc-869a-859bc97ca661", "metadata": {}, "source": [ "### Set Accelerator objects" ] }, { "cell_type": "code", "execution_count": 5, "id": "10819d41-5296-4706-adc9-7e5c110233e9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0;37m[INFO ] [factory.py ]\u001b[0m First initialisation of ListOfElements, ecompassing all linac. Created with dat_file = PosixPath('/home/placais/LightWin/data/example/example.dat')\n", "\u001b[0;37m[INFO ] [list_of_elements.py ]\u001b[0m Successfully created a ListOfElements with self.w_kin_in = 20.0 MeV and self.phi_abs_in = 0.0 rad.\n", "\u001b[1;33m[WARNING ] [factory.py ]\u001b[0m You asked LW a simulation in absolute phase, while there is at least one cavity in relative phase in the .dat file used by TW. You can expect a phase difference between LightWin results and TraceWin results for the same input .dat, after the first failed cavity. No difference should appear with the output .dat, or when using TraceWin solver within LightWin.\n", "\u001b[0;37m[INFO ] [factory.py ]\u001b[0m First initialisation of ListOfElements, ecompassing all linac. Created with dat_file = PosixPath('/home/placais/LightWin/data/example/example.dat')\n", "\u001b[0;37m[INFO ] [list_of_elements.py ]\u001b[0m Successfully created a ListOfElements with self.w_kin_in = 20.0 MeV and self.phi_abs_in = 0.0 rad.\n", "\u001b[1;33m[WARNING ] [factory.py ]\u001b[0m You asked LW a simulation in absolute phase, while there is at least one cavity in relative phase in the .dat file used by TW. You can expect a phase difference between LightWin results and TraceWin results for the same input .dat, after the first failed cavity. No difference should appear with the output .dat, or when using TraceWin solver within LightWin.\n", "\u001b[0;37m[INFO ] [factory.py ]\u001b[0m First initialisation of ListOfElements, ecompassing all linac. Created with dat_file = PosixPath('/home/placais/LightWin/data/example/example.dat')\n", "\u001b[0;37m[INFO ] [list_of_elements.py ]\u001b[0m Successfully created a ListOfElements with self.w_kin_in = 20.0 MeV and self.phi_abs_in = 0.0 rad.\n", "\u001b[0;37m[INFO ] [factory.py ]\u001b[0m First initialisation of ListOfElements, ecompassing all linac. Created with dat_file = PosixPath('/home/placais/LightWin/data/example/example.dat')\n", "\u001b[0;37m[INFO ] [list_of_elements.py ]\u001b[0m Successfully created a ListOfElements with self.w_kin_in = 20.0 MeV and self.phi_abs_in = 0.0 rad.\n" ] } ], "source": [ "factory = WithFaults(beam_calculators=solver_abs, **config_abs)\n", "accelerators_abs = factory.run_all()\n", "for acc in accelerators_abs:\n", " acc.name += r' (absolute $\\phi_0$)'\n", "working_abs = accelerators_abs[0]\n", "broken_abs = accelerators_abs[1]\n", "\n", "factory = WithFaults(beam_calculators=solver_rel, **config_rel)\n", "accelerators_rel = factory.run_all()\n", "for acc in accelerators_rel:\n", " acc.name += r' (relative $\\phi_0$)'\n", "working_rel = accelerators_rel[0]\n", "broken_rel = accelerators_rel[1]\n", "\n", "working = (working_abs, working_rel)\n", "broken = (broken_abs, broken_rel)" ] }, { "cell_type": "markdown", "id": "66ff1571-84b7-47ed-bdeb-dee134cf814c", "metadata": {}, "source": [ "## Propagate the beam" ] }, { "cell_type": "markdown", "id": "b2dac5f0-9b97-43dd-abce-157dfb5302c9", "metadata": {}, "source": [ "### Nominal linac" ] }, { "cell_type": "code", "execution_count": 6, "id": "11551a0e-ef0f-4740-86ff-1e3e53ba2804", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1;33m[WARNING ] [simulation_output.py]\u001b[0m data_in_tw_fashion is bugged\n", "\u001b[0;37m[INFO ] [dat_files.py ]\u001b[0m New dat saved in /home/placais/LightWin/data/example/results_tests/000000_ref/0_Envelope1D/example.dat.\n", "\u001b[0;37m[INFO ] [beam_calculator.py ]\u001b[0m Elapsed time in beam calculation: 0:00:01.733981\n", "\u001b[1;33m[WARNING ] [simulation_output.py]\u001b[0m data_in_tw_fashion is bugged\n", "\u001b[0;37m[INFO ] [dat_files.py ]\u001b[0m New dat saved in /home/placais/LightWin/data/example/results_tests/000000_ref/0_Envelope1D/example.dat.\n", "\u001b[0;37m[INFO ] [beam_calculator.py ]\u001b[0m Elapsed time in beam calculation: 0:00:01.679793\n" ] } ], "source": [ "_ = solver_abs.compute(working_abs)\n", "_ = solver_rel.compute(working_rel)\n", "figs = plot.factory(working, plots_phase, save_fig=False, clean_fig=False)" ] }, { "cell_type": "markdown", "id": "7379807e-6b2d-49f9-b076-e8b7b20334e1", "metadata": {}, "source": [ "