<commit_before>import sys; sys.path.append('../')

import sixtracklib
import numpy as np

from scipy.constants import c as c_light
pmass_eV = 938.272046e6

V_RF = 10e6
lag_RF = np.pi
h_RF = 35000


import tfsdata

twdict = tfsdata.open('twiss.out')

machine = sixtracklib.CBlock()

p0c_eV = 450e9
gamma0 = np.sqrt(p0c_eV**2+pmass_eV**2)/pmass_eV
beta0 = p0c_eV/np.sqrt(p0c_eV**2+pmass_eV**2)

length = twdict['param']['length'] 
f_RF = h_RF*c_light*beta0/(length)

# I want to start in a place with 0 dispersion
start_at = 'AT_IP5'
i_start = np.where(twdict['name']==start_at)[0][0]

indices = range(i_start, len(twdict['name']))+range(0, i_start)

for i_ele in indices:

	name = twdict['name'][i_ele]

	if twdict['keyword'][i_ele]=='MULTIPOLE':
		if twdict['k0l'][i_ele] != 0:
			machine.add_Multipole(name=name, knl=[twdict['k0l'][i_ele]], hxl=twdict['k0l'][i_ele], length=1e20)
		else:
			#print name
			machine.add_Multipole(name=name, knl=[0.,twdict['k1l'][i_ele],twdict['k2l'][i_ele]/2.])
	elif twdict['keyword'][i_ele]=='DRIFT':
		machine.add_Drift(name=name, length=twdict['l'][i_ele])
	elif twdict['keyword'][i_ele]=='RFCAVITY':
		print('Found cavity: '+name)
		machine.add_Cavity(voltage=V_RF,frequency=f_RF,lag=lag_RF)
	else:
		print('Skipped: %s'%name)

#Add dipole corrector to distort the orbit
machine.add_Multipole(name=name, knl=[20.e-6])
machine.add_Multipole(name=name, ksl=[30.e-6])

def one_turn_map(coord):

	# coord = np.array([x, px, y, py, sigma, delta])

	npart = 1

	delta = coord[5]
	rpp = 1./(delta+1)
	pc_eV = p0c_eV/rpp
	gamma = np.sqrt(1. + (pc_eV/pmass_eV)**2)
	beta = np.sqrt(1.-1./gamma**2)
	rvv=beta/beta0
	psigma = pmass_eV*(gamma-gamma0)/(beta0*p0c_eV)

	bunch=sixtracklib.CParticles(npart=npart, 
			p0c=p0c_eV,
			beta0 = beta0,
			gamma0 = gamma0,
			delta = delta,
			rvv = rvv,
			rpp = rpp,
			psigma = psigma)
	bunch.x+=coord[0]
	bunch.px+=coord[1]
	bunch.y+=coord[2]
	bunch.py+=coord[3]
	bunch.sigma+=coord[4]

	particles,ebe,tbt = machine.track_cl(bunch,nturns=1,elembyelem=None,turnbyturn=None)

	return np.array([particles.x[0], particles.px[0], particles.y[0], particles.py[0], 
					particles.sigma[0], particles.delta[0]])


npart = 1

delta = np.array([0e-4])
rpp = 1./(delta+1)
pc_eV = p0c_eV/rpp
gamma = np.sqrt(1. + (pc_eV/pmass_eV)**2)
beta = np.sqrt(1.-1./gamma**2)
rvv=beta/beta0
psigma = pmass_eV*(gamma-gamma0)/(beta0*p0c_eV)

bunch=sixtracklib.CParticles(npart=npart, 
		p0c=p0c_eV,
		beta0 = beta0,
		gamma0 = gamma0,
		delta = delta,
		rvv = rvv,
		rpp = rpp,
		psigma = psigma)
bunch.x+=0.001<commit_msg>Don't manage to get convergence<commit_after>import sys; sys.path.append('../')

import sixtracklib
import numpy as np

import scipy.optimize as so

from scipy.constants import c as c_light
pmass_eV = 938.272046e6

V_RF = 10e6
lag_RF = np.pi
h_RF = 35000


import tfsdata

twdict = tfsdata.open('twiss.out')

machine = sixtracklib.CBlock()

p0c_eV = 450e9
gamma0 = np.sqrt(p0c_eV**2+pmass_eV**2)/pmass_eV
beta0 = p0c_eV/np.sqrt(p0c_eV**2+pmass_eV**2)

length = twdict['param']['length'] 
f_RF = h_RF*c_light*beta0/(length)

# I want to start in a place with 0 dispersion
start_at = 'AT_IP5'
i_start = np.where(twdict['name']==start_at)[0][0]

indices = range(i_start, len(twdict['name']))+range(0, i_start)

for i_ele in indices:

	name = twdict['name'][i_ele]

	if twdict['keyword'][i_ele]=='MULTIPOLE':
		if twdict['k0l'][i_ele] != 0:
			machine.add_Multipole(name=name, knl=[twdict['k0l'][i_ele]], hxl=twdict['k0l'][i_ele], length=1e20)
		else:
			#print name
			machine.add_Multipole(name=name, knl=[0.,twdict['k1l'][i_ele],twdict['k2l'][i_ele]/2.])
	elif twdict['keyword'][i_ele]=='DRIFT':
		machine.add_Drift(name=name, length=twdict['l'][i_ele])
	elif twdict['keyword'][i_ele]=='RFCAVITY':
		print('Found cavity: '+name)
		machine.add_Cavity(voltage=V_RF,frequency=f_RF,lag=lag_RF)
	else:
		print('Skipped: %s'%name)

#Add dipole corrector to distort the orbit
machine.add_Multipole(name=name, knl=[20.e-6])
machine.add_Multipole(name=name, ksl=[30.e-6])

def one_turn_map(coord):

	# coord = np.array([x, px, y, py, sigma, delta])

	npart = 1

	delta = coord[5]
	rpp = 1./(delta+1)
	pc_eV = p0c_eV/rpp
	gamma = np.sqrt(1. + (pc_eV/pmass_eV)**2)
	beta = np.sqrt(1.-1./gamma**2)
	rvv=beta/beta0
	psigma = pmass_eV*(gamma-gamma0)/(beta0*p0c_eV)

	bunch=sixtracklib.CParticles(npart=npart, 
			p0c=p0c_eV,
			beta0 = beta0,
			gamma0 = gamma0,
			delta = delta,
			rvv = rvv,
			rpp = rpp,
			psigma = psigma)
	bunch.x+=coord[0]
	bunch.px+=coord[1]
	bunch.y+=coord[2]
	bunch.py+=coord[3]
	bunch.sigma+=coord[4]

	particles,ebe,tbt = machine.track_cl(bunch,nturns=1,elembyelem=None,turnbyturn=True)

	return np.array([tbt.x[1], tbt.px[1], tbt.y[1], tbt.py[1], 
					tbt.sigma[1], tbt.delta[1]])


# fxdpt = so.fixed_point(one_turn_map, np.array([0.,0.,0.,0.,0.,0.]))

tominimize = lambda coord: np.sum((one_turn_map(coord)-coord)**2)

res = so.minimize(tominimize, np.array([0.,0.,0.,0.,0.,0.]), tol=1e-10)

npart = 1

delta = np.array([0e-4])
rpp = 1./(delta+1)
pc_eV = p0c_eV/rpp
gamma = np.sqrt(1. + (pc_eV/pmass_eV)**2)
beta = np.sqrt(1.-1./gamma**2)
rvv=beta/beta0
psigma = pmass_eV*(gamma-gamma0)/(beta0*p0c_eV)

bunch=sixtracklib.CParticles(npart=npart, 
		p0c=p0c_eV,
		beta0 = beta0,
		gamma0 = gamma0,
		delta = delta,
		rvv = rvv,
		rpp = rpp,
		psigma = psigma)
bunch.x+=0.001
{'fraction_non_alphanumeric': 0.18619402985074626, 'fraction_numerical': 0.04477611940298507, 'mean_word_length': 4.996644295302014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1977240', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2491, 'n_words': 388}