In [2]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

Synchronous Condition

In [3]:
energy=np.linspace(0.938+0.2, 100, 1000) # GeV
gamma=energy/0.938
beta=np.sqrt(1-1/gamma/gamma)

fig,ax=plt.subplots()
ax.set_xlabel("Beam Energy")
ax.set_ylabel("Relative frequency")
ax.set_ylim(0.4,1.1)
ax.plot(energy, beta,label='Frequency')
#ax.set_yscale('log')
for hl in [0.5,0.6,0.7,0.8,0.9,0.99]:
    ax.axhline(y=hl, color='k',lw=0.1)
ax.set_xscale('log')
ax.legend(loc=2)
ax_m=ax.twinx()
ax_m.set_ylabel(r"B$\rho$")
ax_m.plot(energy,beta*gamma*0.938*10/3.0, color='c', label='Bending field')
ax_m.set_yscale('log')
ax_m.legend(loc=4)
Out[3]:
<matplotlib.legend.Legend at 0x111c40780>

Single Turn Injection

In [4]:
from map2D import map2D
# Optics of the 2D map
beta=1
alpha=0

tunex=0.25  # initial tune

#generating the macroparticles with beta=1, alpha=0 and shift it to septum
npar=10000
xlist=np.random.randn(npar)*5e-4+5e-3
pxlist=np.random.randn(npar)*5e-4*2

xpx=map2D(npart=1, twiss=[beta,alpha], tune=tunex, chrom=2.0, espr=3.0e-4,
                  particles=np.vstack([xlist,pxlist]))



import matplotlib.pyplot as plt
%matplotlib notebook
fig,ax=plt.subplots()
ax.set_xlabel(r'$x$ [mm]')
ax.set_ylabel(r"$x'$ [mrad]")
ax.set_xlim(-7, 7)
ax.set_ylim(-7, 7)

line,=ax.plot([],[],linestyle='', marker='.', markersize=0.5)
time_text = ax.text(0.05, 0.95,'',horizontalalignment='left',verticalalignment='top', transform=ax.transAxes)
def init():
    line.set_data(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3)
    time_text.set_text('Turns: {:d}, tune:{:f}'.format(xpx.propagated_turns, xpx.phi/2/np.pi))
    return line,time_text

def run(i):
    # update the data
    s=60*0
    oct=5500.0*0
    if i==0:
        xpx.propagate()
        
    elif i==1:
        xpx.coor2D[1,:] += 0.005
        xpx.propagate()
        
    elif i>1:
        if i<382:
            xpx.set_map(twiss=[beta,alpha], tune=tunex+0.0001*i, chrom=0.0)
            xpx.coor2D[1,:] += s*xpx.coor2D[0,:]*xpx.coor2D[0,:]
            xpx.coor2D[1,:] += oct*xpx.coor2D[0,:]*xpx.coor2D[0,:]*xpx.coor2D[0,:]
            xpx.propagate()
            
        else:
            for j in range(13):
                xpx.coor2D[1,:] += s*xpx.coor2D[0,:]*xpx.coor2D[0,:]
                xpx.coor2D[1,:] += oct*xpx.coor2D[0,:]*xpx.coor2D[0,:]*xpx.coor2D[0,:]
                xpx.propagate()
                
    time_text.set_text('Turns: {:d}, tune:{:f}'.format(xpx.propagated_turns, xpx.phi/2/np.pi))
    line.set_data(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3)
    return line,time_text

#for i in range(turns):
#    xpx.propagate()
#    ax.plot(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3, linestyle='', marker='.', markersize=0.5)
#ax.plot(stats[2,0]*1e3,stats[2,1]*1e3, linestyle='', marker='+', markersize=12, color='g')
from matplotlib.animation import FuncAnimation
anim = FuncAnimation(fig, run, frames=np.arange(1000), 
                     init_func=init, interval=50)

Slow Resonance Extraction

In [5]:
from map2D import map2D

beta=1
alpha=0
tunex=0.335

npar=50000
xlist=np.random.randn(npar)*5e-4
pxlist=np.random.randn(npar)*5e-4

xpx=map2D(npart=1, twiss=[beta,alpha], tune=tunex, chrom=0.0, espr=5.0e-4,
                  particles=np.vstack([xlist,pxlist]))



import matplotlib.pyplot as plt
%matplotlib notebook
fig,ax=plt.subplots()
ax.set_xlabel(r'$x$ [mm]')
ax.set_ylabel(r"$x'$ [mrad]")
ax.set_xlim(-7, 7)
ax.set_ylim(-7, 7)

line,=ax.plot([],[],linestyle='', marker='.', markersize=0.5)
time_text = ax.text(0.05, 0.95,'',horizontalalignment='left',verticalalignment='top', transform=ax.transAxes)

def init():
    line.set_data(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3)
    time_text.set_text('Turns: {:d}, tune:{:f}'.format(xpx.propagated_turns, xpx.phi/2/np.pi))
    return line,time_text

def run(i):
    # update the data
    s=20
    if i<50:
        pass
    elif i<80:
        xpx.set_map(twiss=[beta,alpha], tune=xpx.phi/2/np.pi-0.00001, chrom=0.0)
    elif i<200:
        xpx.set_map(twiss=[beta,alpha], tune=xpx.phi/2/np.pi-0.000007, chrom=0.0)    
    elif i<400:
        xpx.set_map(twiss=[beta,alpha], tune=xpx.phi/2/np.pi-0.000004, chrom=0.0)
    elif i<600:
        xpx.set_map(twiss=[beta,alpha], tune=xpx.phi/2/np.pi-0.000003, chrom=0.0)
    elif i<800:
        xpx.set_map(twiss=[beta,alpha], tune=xpx.phi/2/np.pi-0.000002, chrom=0.0)
        
    for j in range(10):        
        xpx.coor2D[1,:] += s*xpx.coor2D[0,:]*xpx.coor2D[0,:]
        xpx.propagate()
        xpx.septum(0.01)
    
    line.set_data(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3)
    time_text.set_text('Turns: {:d}, tune:{:f}'.format(xpx.propagated_turns, xpx.phi/2/np.pi))
    return line,time_text

#for i in range(turns):
#    xpx.propagate()
#    ax.plot(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3, linestyle='', marker='.', markersize=0.5)
#ax.plot(stats[2,0]*1e3,stats[2,1]*1e3, linestyle='', marker='+', markersize=12, color='g')
from matplotlib.animation import FuncAnimation
anim = FuncAnimation(fig, run, frames=np.arange(600), 
                     init_func=init, interval=10, repeat=False)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/matplotlib/cbook/__init__.py", line 215, in process
    func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/matplotlib/animation.py", line 1462, in _stop
    self.event_source.remove_callback(self._loop_delay)
AttributeError: 'NoneType' object has no attribute 'remove_callback'
In [6]:
fig,ax=plt.subplots()
ax.set_ylim(0,40)
ax.plot(xpx.extract_history)
Out[6]:
[<matplotlib.lines.Line2D at 0x116796128>]

Lossless Resonance Extraction

In [8]:
from map2D import map2D

beta=1
alpha=0
tunex=0.248


num_run=0
npar=10000
xlist=np.random.randn(npar)*5e-4
pxlist=np.random.randn(npar)*5e-4


xpx=map2D(npart=1, twiss=[beta,alpha], tune=tunex, chrom=0.0, espr=5.0e-4,
                  particles=np.vstack([xlist,pxlist]))




import matplotlib.pyplot as plt
%matplotlib notebook
fig,ax=plt.subplots()
ax.set_xlabel(r'$x$ [mm]')
ax.set_ylabel(r"$x'$ [mrad]")
ax.set_xlim(-7, 7)
ax.set_ylim(-7, 7)

line,=ax.plot([],[],linestyle='', marker='.', markersize=0.5)
time_text = ax.text(0.05, 0.95,'',horizontalalignment='left',verticalalignment='top', transform=ax.transAxes)
def init():
    line.set_data(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3)
    time_text.set_text('Turns: {:d}, tune:{:f}'.format(xpx.propagated_turns, xpx.phi/2/np.pi))
    return line, time_text

def run(i):
    # update the data
    s=-180
    oct=3000.0
    if i<800:
        
        for j in range(21):
            xpx.set_map(twiss=[beta,alpha], tune=tunex+0.00001*i, chrom=0.0)
            xpx.coor2D[1,:] += s*xpx.coor2D[0,:]*xpx.coor2D[0,:]
            xpx.coor2D[1,:] += oct*xpx.coor2D[0,:]*xpx.coor2D[0,:]*xpx.coor2D[0,:]
            xpx.propagate()
        
    
    else:
        
        
        xpx.coor2D[1,:] += s*xpx.coor2D[0,:]*xpx.coor2D[0,:]
        xpx.coor2D[1,:] += oct*xpx.coor2D[0,:]*xpx.coor2D[0,:]*xpx.coor2D[0,:]
        xpx.propagate()
        
    
    line.set_data(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3)
    time_text.set_text('Turns: {:d}, tune:{:f}'.format(xpx.propagated_turns, xpx.phi/2/np.pi))
    
    return line, time_text

#for i in range(turns):
#    xpx.propagate()
#    ax.plot(xpx.coor2D[0,:]*1e3,xpx.coor2D[1,:]*1e3, linestyle='', marker='.', markersize=0.5)
#ax.plot(stats[2,0]*1e3,stats[2,1]*1e3, linestyle='', marker='+', markersize=12, color='g')
from matplotlib.animation import FuncAnimation
anim = FuncAnimation(fig, run, frames=np.arange(5000), 
                      interval=100, blit=False)
In [10]:
len(xpx.coor2D[0,xpx.lost])
Out[10]:
2
In [9]:
xpx.extract_history
Out[9]:
[206, 205]