{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Select cavites to use for Chirp (cc_sel), FB+ (fp_sel) and FB- (fm_sel)\n",
    "#    Three CMs are assumed here, but any number can be used\n",
    "#    Avoid using common cavities for FB+ and FB-\n",
    "#    For L0 and L3, set cc_sel = fp_sel OR fm_sel even though the cavities   \n",
    "#    would not be used to change the section chirp\n",
    "\n",
    "cc_sel = np.array([1,0,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,0,1,1,0,1,1,1])\n",
    "fp_sel = np.array([0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1, 0,1,1,0,1,0,0,0])\n",
    "fm_sel = np.array([0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 1,0,0,1,0,1,1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load cavity ADES and PDES values (PDES should = 0 if cavity not powered)\n",
    "vg = 14*np.ones(24) + 2*np.random.random(24)                 # Cavity energy gains (MeV)\n",
    "ps = (np.pi/180)*(20*cc_sel + 12*fp_sel - 9*fm_sel)  # Initial cavity phases in radians "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((296.3957500094866+105.69028165564033j),\n",
       " (68.58056798737032+30.63353652735542j),\n",
       " (72.81613977292625+14.154023759276722j))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Cc = np.sum(cc_sel * vg * np.exp(1j*ps)); # Chirp complex energy\n",
    "Fp = np.sum(fp_sel * vg * np.exp(1j*ps)); # FB+ complex energy\n",
    "Fm = np.sum(fm_sel * vg * np.exp(1j*ps)); # FB- complex energy\n",
    "\n",
    "Cc, Fp, Fm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Derivatives wrt to phase \n",
    "dCc_dpCc = 1j*Cc;\n",
    "dFp_dpFp = 1j*Fp;\n",
    "dFm_dpFm = 1j*Fm;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Relation between complex energy changes and phase changes,\n",
    "#   and constraint equation so FB+ + FB- does not change Chirp\n",
    "# dE = real(dCc_dpCc) * dpCc + real(dFp_dpFp) * dpFp + real(dFm_dpFm) * dpFm\n",
    "# dC = imag(dCc_dpCc) * dpCc + imag(dFp_dpFp) * dpFp + imag(dFm_dpFm) * dpFm\n",
    "#  0 =                         imag(dFp_dpFp) * dpFp + imag(dFm_dpFm) * dpFm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Matrix formulaiton of these equations \n",
    "M = np.array([[np.real(dCc_dpCc), np.real(dFp_dpFp), np.real(dFm_dpFm)],\n",
    "     [np.imag(dCc_dpCc), np.imag(dFp_dpFp), np.imag(dFm_dpFm)],\n",
    "     [0,    np.imag(dFp_dpFp), np.imag(dFm_dpFm)  ]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "MI = np.linalg.inv(M); # Inverse of M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert np.allclose(np.dot(M, MI), np.eye(3)) # chk:  M * M^-1 = I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# FB operation \n",
    "# Example of complex energy corrections sent to FB algorithm\n",
    "#   For L0 and L3, dC should be zero"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dE = 0.25; dC = -0.11;  # Requested energy and chirp changes in MeV\n",
    "\n",
    "deltas = np.atleast_2d([dE, dC, 0])\n",
    "\n",
    "dFC = np.dot(MI, deltas.T)  # Computed phase changes to implement corrections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.00037113],\n",
       "       [-0.01218157],\n",
       "       [ 0.011473  ]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dFC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.24022625936368058-0.11325623107657634j)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check that complex energy changed as expected (not needed in actual FB loop) \n",
    "#  The quanity below should equal ~ dE + i*dC (i.e. to a first order approximation)\n",
    "np.sum(vg * np.exp(1j * (ps + (dFC[0] * cc_sel) + (dFC[1] * fp_sel) + (dFC[2] * fm_sel)))) -\\\n",
    "np.sum(vg * np.exp(1j * ps))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Updated cavity phases (in radians) - set PDES to these values\n",
    "ps = ps + dFC[0] * cc_sel + dFC[1] * fp_sel + dFC[2]*fm_sel; "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([19.97873608,  0.        , 19.97873608, 19.97873608, 19.97873608,\n",
       "       19.97873608, 19.97873608, 19.97873608, 19.97873608, 19.97873608,\n",
       "       19.97873608, 19.97873608, 19.97873608, 19.97873608, 31.28078326,\n",
       "       31.28078326, 11.63609035, 11.30204718, 31.28078326, 11.63609035,\n",
       "       11.30204718, 11.63609035, 11.63609035, 11.63609035])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rad2deg(ps)"
   ]
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
