{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Twiss parameter of a FODO lattice\n\nThis example shows how to calulate and plot the Twiss parameter of a FOOD lattice.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport apace as ap\n\nD1 = ap.Drift(\"D1\", length=0.55)\nd1 = ap.Drift(\"D1\", length=0.55)\nb1 = ap.Dipole(\"B1\", length=1.5, angle=0.392701, e1=0.1963505, e2=0.1963505)\nq1 = ap.Quadrupole(\"Q1\", length=0.2, k1=1.2)\nq2 = ap.Quadrupole(\"Q2\", length=0.4, k1=-1.2)\nfodo_cell = ap.Lattice(\"FODO_CELL\", [q1, d1, b1, d1, q2, d1, b1, d1, q1])\nfodo_ring = ap.Lattice(\"FODO_RING\", [fodo_cell] * 8)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Output some info on the FODO lattice\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(\n    f\"Overview of {fodo_ring.name}\",\n    f\"Num of elements: {len(fodo_ring.sequence)}\",\n    f\"Lattice Length : {fodo_ring.length}\",\n    f\"Cell Length    : {fodo_cell.length}\",\n    sep=\"\\n\",\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a new ``Twiss`` object to calculate the Twiss parameter\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "twiss = ap.Twiss(fodo_ring)\n\nprint(\n    f\"Twiss parameter of {fodo_ring.name}\",\n    f\"Stable in x-plane: {twiss.stable_x}\",\n    f\"Stable in y-plane: {twiss.stable_y}\",\n    f\"Horizontal tune  : {twiss.tune_x:.3f}\",\n    f\"Vertical tune    : {twiss.tune_y:.3f}\",\n    f\"Max beta x       : {np.max(twiss.beta_x):.3f}\",\n    f\"Max beta y       : {np.max(twiss.beta_y):.3f}\",\n    sep=\"\\n\",\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use the builtin `plot_lattice` utility function to plot the Twiss parameter\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from apace.plot import TwissPlot\n\nfig = TwissPlot(twiss, fodo_ring).fig"
      ]
    }
  ],
  "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
}