{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Tune Measurement\n\nGet tune from Fourier-Transform of transversal particle oscillation at fixed position\n\n**Lattice file:**\n\n.. literalinclude:: ../../data/lattices/fodo_cell.json\n   :language: json\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Some imports ...\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom pathlib import Path\nfrom scipy.fftpack import fft\nimport apace as ap\nimport matplotlib.pyplot as plt\nfrom math import sqrt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load FODO lattice from file\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fodo = ap.Lattice.from_file(\"../data/lattices/fodo_cell.json\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create particle distribution\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n_particles = 5\nn_turns = 50\nposition = 0\ndist = ap.distribution(n_particles, x_dist=\"uniform\", x_width=0.002, x_center=0.001)\n\nmatrix_tracking = ap.TrackingMatrix(fodo, dist, turns=n_turns, watch_points=[0])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot x-x' phase space\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.subplot(2, 2, 1)\n\nfor i in range(n_particles):\n    plt.plot(matrix_tracking.x[:, i], matrix_tracking.x_dds[:, i], \"o\")\n\nplt.xlabel(\"x / m\")\nplt.ylabel(\"x'\")\n\nfreq = np.linspace(0.0, 1.0 / (2.0 * fodo.length / 299_792_458), n_turns // 2)\nfft_tracking = 2.0 / n_turns * np.abs(fft(matrix_tracking.x[:, -1])[: n_turns // 2])\nmain_freq = freq[np.argmax(fft_tracking)]\n\n# Plot horizontal frequency spectrum\nplt.subplot(2, 2, 2)\nplt.plot(freq, fft_tracking)\nplt.xlabel(\"Freq / Hz\")\nplt.ylabel(\"Fourier transform\")\nplt.axvline(x=main_freq, color=\"k\")\n\n# Plot horizontal offset for fixed position\nplt.subplot(2, 2, 3)\nplt.plot(matrix_tracking.orbit_position, matrix_tracking.x[:, -1], \"rx\")\nplt.xlabel(f\"orbit position / s\")\nplt.ylabel(f\"horizontal offset x at fixed position {position} / m\")\n\n# Plot horizontal offset for multiple positions\nmatrix_tracking_all_positions = ap.TrackingMatrix(fodo, dist, turns=n_turns)\n\nplt.subplot(2, 2, 4)\nplt.plot(\n    matrix_tracking_all_positions.orbit_position,\n    matrix_tracking_all_positions.x[:, -1],\n    linewidth=0.5,\n)\nplt.plot(matrix_tracking.orbit_position, matrix_tracking.x[:, -1], \"rx\")\nplt.xlabel(\"orbit position / s\")\nplt.ylabel(\"horizontal offset for all positions / m\")\n\n\nplt.gcf().set_size_inches(16, 8)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "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.8.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}