Example #1
0
 def twiss(self):
     if self.tws == None:
         _logger.warning(
             "BeamTransform: x_opt and y_opt are obsolete, use Twiss")
         tws = Twiss()
         tws.alpha_x, tws.beta_x, tws.mux = self.x_opt
         tws.alpha_y, tws.beta_y, tws.muy = self.y_opt
     else:
         tws = self.tws
     return tws
Example #2
0
def twiss2input(tws):

    lines = []
    tws_ref = Twiss()
    lines.append('tws0 = Twiss()\n')
    for param in tws.__dict__:
        if tws.__dict__[param] != tws_ref.__dict__[param]:
            lines.append('tws0.' + str(param) + ' = ' +
                         str(tws.__dict__[param]) + '\n')

    return lines
 def modulator_amplifier(self,i_f_mamp):
     f_old = getattr(self,'file_pout')
     if i_f_mamp == 'mod' or i_f_mamp == 'amp':
         setattr(self,'file_pout',f_old+i_f_mamp+'/')
         file_in = 'demo_'+i_f_mamp
         gen_f = [self.pathgf+files for files in os.listdir(self.pathgf) if files.endswith('.in') and files.startswith(file_in)][0]
         setattr(self,'gen_file',gen_f)
         f_inp = super(s2e_afterburner,self).read_GEN_input_file()
         setattr(self,'idump',1)
         if i_f_mamp =='mod': 
             aw0 = np.sqrt(float((2.0*np.square(self.gamma)*getattr(f_inp,'xlamds')/(getattr(f_inp,'xlamd')))-1.0))
             setattr(f_inp,'ippart',int(1))
             setattr(f_inp,'ispart',int(1))
             setattr(f_inp,'idmppar',int(1))
             setattr(f_inp,'gamma0',getattr(self,'gamma'))
             setattr(f_inp,'aw0',aw0)
             setattr(f_inp,'awd',aw0)
             f_inp2 = super(s2e_afterburner,self).GEN_simul_preproc(f_inp) 
             f_out = read_out_file(self.search_input(f_inp2[0].run_dir,'.gout'))
             f_out.filePath = self.search_input(f_inp2[0].run_dir,'.gout')
             f_inpamp = f_inp2[0]
         elif i_f_mamp == 'amp':
             tw = Twiss()
             f_inp2 =  self.prep_mod_amp_aft(f_old+'mod/scan_0/ip_seed_-1/' ,f_inp)
             f_inp2 = super(s2e_afterburner,self).beta_matching(f_inp2,f_old)
             dict_tw = {'beta_x':getattr(f_inp2,'gamma0')*(getattr(f_inp2,'rxbeam')**2)/(getattr(f_inp2,'emitx')), 
                        'beta_y':getattr(f_inp2,'gamma0')*(getattr(f_inp2,'rybeam')**2)/getattr(f_inp2,'emity'), 
                        'alpha_x':getattr(f_inp2,'alphax'),
                        'alpha_y':getattr(f_inp2,'alphay')}                 
             for key,value in dict_tw.items():
                 setattr(tw,key,value)                
             setattr(f_inp2,'edist',rematch_edist(getattr(f_inp2,'edist'),tw))
             setattr(f_inp2,'beam',edist2beam(rematch_edist(getattr(f_inp2,'edist'),tw),step=float(self.outlambda),i_aft=1))
             setattr(f_inp2.beam,'filePath',f_old+'mod/scan_0/ip_seed_-1/'+'mod'+'_beam')
             setattr(f_inp2,'edist',None)
             setattr(f_inp2,'i_rewrite',1)
             setattr(f_inp2,'par_rew',{'ntail':0}) 
             f_inp0 = self.GEN_simul_preproc(f_inp2)
             f_inpamp = f_inp0[0]
             setattr(f_inpamp,'beam',None)
             setattr(f_inpamp,'dpa',None)
             setattr(f_inpamp,'beamfile',None)
             setattr(f_inpamp,'partfile',None)
             setattr(f_inpamp,'edistfile',None)
             f_oamp = read_out_file(self.search_input(f_inp0[0].run_dir,'.gout'))
             f_oamp.filePath = self.search_input(f_inp0[0].run_dir,'.gout')
             setattr(f_inpamp,'fieldfile',None)
         setattr(self,'file_pout',f_old)
         return f_inpamp                
     else:
         print(' No modulator or amplifier ')
         return
def periodic_twiss(tws, R):
    '''
    initial conditions for a periodic Twiss slution
    '''
    tws = Twiss(tws)

    cosmx = (R[0, 0] + R[1, 1]) / 2.
    cosmy = (R[2, 2] + R[3, 3]) / 2.

    if abs(cosmx) >= 1 or abs(cosmy) >= 1:
        logger.warn(
            "************ periodic solution does not exist. return None ***********"
        )
        #print("************ periodic solution does not exist. return None ***********")
        return None
    sinmx = np.sign(R[0, 1]) * sqrt(1. - cosmx * cosmx)
    sinmy = np.sign(R[2, 3]) * sqrt(1. - cosmy * cosmy)

    tws.beta_x = abs(R[0, 1] / sinmx)
    tws.beta_y = abs(R[2, 3] / sinmy)

    tws.alpha_x = (R[0, 0] - R[1, 1]) / (2. * sinmx)  # X[0,0]

    tws.gamma_x = (1. + tws.alpha_x * tws.alpha_x) / tws.beta_x  # X[1,0]

    tws.alpha_y = (R[2, 2] - R[3, 3]) / (2 * sinmy)  # Y[0,0]
    tws.gamma_y = (1. + tws.alpha_y * tws.alpha_y) / tws.beta_y  # Y[1,0]

    Hx = array([[R[0, 0] - 1, R[0, 1]], [R[1, 0], R[1, 1] - 1]])
    Hhx = array([[R[0, 5]], [R[1, 5]]])
    hh = dot(inv(-Hx), Hhx)
    tws.Dx = hh[0, 0]
    tws.Dxp = hh[1, 0]
    Hy = array([[R[2, 2] - 1, R[2, 3]], [R[3, 2], R[3, 3] - 1]])
    Hhy = array([[R[2, 5]], [R[3, 5]]])
    hhy = dot(inv(-Hy), Hhy)
    tws.Dy = hhy[0, 0]
    tws.Dyp = hhy[1, 0]
    #tws.display()
    return tws
    def map_x_twiss(self, tws0):
        E = tws0.E
        M = self.R(E)
        #print(E, self.delta_e, M)
        zero_tol = 1.e-10
        if abs(self.delta_e) > zero_tol:
            #M = self.R(E + )
            Ei = tws0.E
            Ef = tws0.E + self.delta_e  #* cos(self.phi)
            #print "Ei = ", Ei, "Ef = ", Ef
            k = np.sqrt(Ef / Ei)
            M[0, 0] = M[0, 0] * k
            M[0, 1] = M[0, 1] * k
            M[1, 0] = M[1, 0] * k
            M[1, 1] = M[1, 1] * k
            M[2, 2] = M[2, 2] * k
            M[2, 3] = M[2, 3] * k
            M[3, 2] = M[3, 2] * k
            M[3, 3] = M[3, 3] * k
            #M[4, 5] = M[3, 3]*k
            E = Ef
        m = tws0
        tws = Twiss(tws0)
        tws.E = E
        tws.p = m.p
        tws.beta_x = M[0, 0] * M[0, 0] * m.beta_x - 2 * M[0, 1] * M[
            0, 0] * m.alpha_x + M[0, 1] * M[0, 1] * m.gamma_x
        # tws.beta_x = ((M[0,0]*tws.beta_x - M[0,1]*m.alpha_x)**2 + M[0,1]*M[0,1])/m.beta_x
        tws.beta_y = M[2, 2] * M[2, 2] * m.beta_y - 2 * M[2, 3] * M[
            2, 2] * m.alpha_y + M[2, 3] * M[2, 3] * m.gamma_y
        # tws.beta_y = ((M[2,2]*tws.beta_y - M[2,3]*m.alpha_y)**2 + M[2,3]*M[2,3])/m.beta_y
        tws.alpha_x = -M[0, 0] * M[1, 0] * m.beta_x + (
            M[0, 1] * M[1, 0] +
            M[1, 1] * M[0, 0]) * m.alpha_x - M[0, 1] * M[1, 1] * m.gamma_x
        tws.alpha_y = -M[2, 2] * M[3, 2] * m.beta_y + (
            M[2, 3] * M[3, 2] +
            M[3, 3] * M[2, 2]) * m.alpha_y - M[2, 3] * M[3, 3] * m.gamma_y

        tws.gamma_x = (1. + tws.alpha_x * tws.alpha_x) / tws.beta_x
        tws.gamma_y = (1. + tws.alpha_y * tws.alpha_y) / tws.beta_y

        tws.Dx = M[0, 0] * m.Dx + M[0, 1] * m.Dxp + M[0, 5]
        tws.Dy = M[2, 2] * m.Dy + M[2, 3] * m.Dyp + M[2, 5]

        tws.Dxp = M[1, 0] * m.Dx + M[1, 1] * m.Dxp + M[1, 5]
        tws.Dyp = M[3, 2] * m.Dy + M[3, 3] * m.Dyp + M[3, 5]
        denom_x = M[0, 0] * m.beta_x - M[0, 1] * m.alpha_x
        if denom_x == 0.:
            d_mux = np.pi / 2. * M[0, 1] / np.abs(M[0, 1])
        else:
            d_mux = np.arctan(M[0, 1] / denom_x)

        if d_mux < 0:
            d_mux += np.pi
        tws.mux = m.mux + d_mux
        #print M[0, 0]*m.beta_x - M[0, 1]*m.alpha_x, arctan(M[2, 3]/(M[2, 2]*m.beta_y - M[2, 3]*m.alpha_y))
        denom_y = M[2, 2] * m.beta_y - M[2, 3] * m.alpha_y
        if denom_y == 0.:
            d_muy = np.pi / 2. * M[2, 3] / np.abs(M[2, 3])
        else:
            d_muy = np.arctan(M[2, 3] / denom_y)
        if d_muy < 0:
            d_muy += np.pi
        tws.muy = m.muy + d_muy
        return tws
Example #6
0
def periodic_twiss(tws, R):
    '''
    initial conditions for a periodic Twiss slution
    '''
    tws = Twiss(tws)

    cosmx = (R[0, 0] + R[1, 1]) / 2.
    cosmy = (R[2, 2] + R[3, 3]) / 2.

    if abs(cosmx) >= 1 or abs(cosmy) >= 1:
        logger.warn("************ periodic solution does not exist. return None ***********")
        # print("************ periodic solution does not exist. return None ***********")
        return None
    sinmx = np.sign(R[0, 1]) * sqrt(1. - cosmx * cosmx)
    sinmy = np.sign(R[2, 3]) * sqrt(1. - cosmy * cosmy)

    tws.beta_x = abs(R[0, 1] / sinmx)
    tws.beta_y = abs(R[2, 3] / sinmy)

    tws.alpha_x = (R[0, 0] - R[1, 1]) / (2. * sinmx)  # X[0,0]

    tws.gamma_x = (1. + tws.alpha_x * tws.alpha_x) / tws.beta_x  # X[1,0]

    tws.alpha_y = (R[2, 2] - R[3, 3]) / (2 * sinmy)  # Y[0,0]
    tws.gamma_y = (1. + tws.alpha_y * tws.alpha_y) / tws.beta_y  # Y[1,0]

    Hx = array([[R[0, 0] - 1, R[0, 1]], [R[1, 0], R[1, 1] - 1]])
    Hhx = array([[R[0, 5]], [R[1, 5]]])
    hh = dot(inv(-Hx), Hhx)
    tws.Dx = hh[0, 0]
    tws.Dxp = hh[1, 0]
    Hy = array([[R[2, 2] - 1, R[2, 3]], [R[3, 2], R[3, 3] - 1]])
    Hhy = array([[R[2, 5]], [R[3, 5]]])
    hhy = dot(inv(-Hy), Hhy)
    tws.Dy = hhy[0, 0]
    tws.Dyp = hhy[1, 0]
    # tws.display()
    return tws
Example #7
0
    def map_x_twiss(self, tws0):
        E = tws0.E
        M = self.R(E)
        # print(E, self.delta_e, M)
        zero_tol = 1.e-10
        if abs(self.delta_e) > zero_tol:
            # M = self.R(E + )
            Ei = tws0.E
            Ef = tws0.E + self.delta_e  # * cos(self.phi)
            # print "Ei = ", Ei, "Ef = ", Ef
            k = np.sqrt(Ef / Ei)
            M[0, 0] = M[0, 0] * k
            M[0, 1] = M[0, 1] * k
            M[1, 0] = M[1, 0] * k
            M[1, 1] = M[1, 1] * k
            M[2, 2] = M[2, 2] * k
            M[2, 3] = M[2, 3] * k
            M[3, 2] = M[3, 2] * k
            M[3, 3] = M[3, 3] * k
            # M[4, 5] = M[3, 3]*k
            E = Ef

        m = tws0
        tws = Twiss(tws0)
        tws.E = E
        tws.p = m.p
        tws.beta_x = M[0, 0] * M[0, 0] * m.beta_x - 2 * M[0, 1] * M[0, 0] * m.alpha_x + M[0, 1] * M[0, 1] * m.gamma_x
        # tws.beta_x = ((M[0,0]*tws.beta_x - M[0,1]*m.alpha_x)**2 + M[0,1]*M[0,1])/m.beta_x
        tws.beta_y = M[2, 2] * M[2, 2] * m.beta_y - 2 * M[2, 3] * M[2, 2] * m.alpha_y + M[2, 3] * M[2, 3] * m.gamma_y
        # tws.beta_y = ((M[2,2]*tws.beta_y - M[2,3]*m.alpha_y)**2 + M[2,3]*M[2,3])/m.beta_y
        tws.alpha_x = -M[0, 0] * M[1, 0] * m.beta_x + (M[0, 1] * M[1, 0] + M[1, 1] * M[0, 0]) * m.alpha_x - M[0, 1] * M[
            1, 1] * m.gamma_x
        tws.alpha_y = -M[2, 2] * M[3, 2] * m.beta_y + (M[2, 3] * M[3, 2] + M[3, 3] * M[2, 2]) * m.alpha_y - M[2, 3] * M[
            3, 3] * m.gamma_y

        tws.gamma_x = (1. + tws.alpha_x * tws.alpha_x) / tws.beta_x
        tws.gamma_y = (1. + tws.alpha_y * tws.alpha_y) / tws.beta_y

        tws.Dx = M[0, 0] * m.Dx + M[0, 1] * m.Dxp + M[0, 5]
        tws.Dy = M[2, 2] * m.Dy + M[2, 3] * m.Dyp + M[2, 5]

        tws.Dxp = M[1, 0] * m.Dx + M[1, 1] * m.Dxp + M[1, 5]
        tws.Dyp = M[3, 2] * m.Dy + M[3, 3] * m.Dyp + M[3, 5]
        denom_x = M[0, 0] * m.beta_x - M[0, 1] * m.alpha_x
        if denom_x == 0.:
            d_mux = np.pi / 2. * M[0, 1] / np.abs(M[0, 1])
        else:
            d_mux = np.arctan(M[0, 1] / denom_x)

        if d_mux < 0:
            d_mux += np.pi
        tws.mux = m.mux + d_mux
        # print M[0, 0]*m.beta_x - M[0, 1]*m.alpha_x, arctan(M[2, 3]/(M[2, 2]*m.beta_y - M[2, 3]*m.alpha_y))
        denom_y = M[2, 2] * m.beta_y - M[2, 3] * m.alpha_y
        if denom_y == 0.:
            d_muy = np.pi / 2. * M[2, 3] / np.abs(M[2, 3])
        else:
            d_muy = np.arctan(M[2, 3] / denom_y)
        if d_muy < 0:
            d_muy += np.pi
        tws.muy = m.muy + d_muy
        # print("new")
        # print(tws)
        return tws
Example #8
0
__author__ = 'Sergey Tomin'

from ocelot.cpbd.beam import Twiss
from ocelot.gui.accelerator import *
from matplotlib import pylab as plt
import numpy as np
import xlrd
rb = xlrd.open_workbook('component_list.xls', formatting_info=True)
sheet = rb.sheet_by_index(1)

tw = Twiss()
tws_L1 = []
L = 0.
tws_I1 = []
for rownum in range(2, 4246):
    row = sheet.row_values(rownum)
    tw = Twiss()
    length = row[7]
    L += length
    strength = row[8]
    lag = row[9]
    freq = row[10]
    tilt = row[11]
    s = row[12]
    st = row[13]
    x, y, z = row[14], row[15], row[16]
    energy = row[26]

    tw.beta_x = row[27]
    tw.alpha_x = row[28]
    tw.mu_x = row[29]