#lhc = sixtracktools.SixInput('.') #line, rest, iconv = lhc.expand_struct() #elements=sim.Elements.fromline(line) elements = sim.Elements() lhc = sixtracktools.SixInput('.') line, rest, iconv = lhc.expand_struct(convert=elements.gen_builder()) cljob = sim.TrackJobCL(particles, elements, device="0.0", dump_element=nturns) cljob.track() cljob.collect() out = cljob.dump_element refline, rest, iconv = lhc.expand_struct(convert=pysixtrack.element_types) refline = pysixtrack.Line(elements=[l[2] for l in refline]) refline.elements.append(pysixtrack.Monitor()) prun = pysixtrack.Particles(p0c=7000e9, x=x0) refout = refline.track_elem_by_elem(prun) refx = np.array([p.x for p in refout]) refzeta = np.array([p.zeta for p in refout]) plot(out.x - refx) plot(out.zeta - refzeta) #turn-by-turn nturn = 128 elements.Monitor(turns=nturn) particles = sim.Particles(nparticles=npart)
bb.Ddelta_sub = ptemp.delta - ptempin.delta # Check that the closed orbit is not kicked for bb, ibb in zip(listBB6D, ind_BB6D): ptemp = closed_orbit[ibb].copy() ptempin = ptemp.copy() bb.track(ptemp) print('Again kick', ptemp.x - ptempin.x) with open('line.pkl', 'wb') as fid: pickle.dump(line, fid) lineobj = pysixtrack.Line(elements=[elem for label, elem_type, elem in line]) with open('lineobj.pkl', 'wb') as fid: pickle.dump(lineobj, fid) # Compare tracking results sixdump = sixdump_all[1::2] # Particle with deviation from CO # sixdump = sixdump_all[::2] # Particle on CO p_in_st = pysixtrack.Particles(**sixdump[0].get_minimal_beam()) p_out_st = pysixtrack.Particles(**sixdump[1].get_minimal_beam()) p_in_pyst = p_in_st.copy() p_out_pyst = p_in_pyst.copy() if listBB6D: listBB6D[0].track(p_out_pyst)
order = np.random.randint(0, 10) in_knl = np.random.uniform(0.0, 100.0, order + 1) in_ksl = np.random.uniform(0.0, 100.0, order + 1) bal_length = 2 * order + 2 in_bal = np.zeros(bal_length) for ii in range(0, len(in_ksl)): in_bal[2 * ii] = in_knl[ii] / factorial(ii, exact=True) in_bal[2 * ii + 1] = in_ksl[ii] / factorial(ii, exact=True) pyst_line.append(pyst.elements.Multipole(knl=in_knl, ksl=in_ksl)) in_knl_data.append(in_knl) in_ksl_data.append(in_ksl) in_bal_data.append(in_bal) temp = pyst.Line(pyst_line) st_line = st.Elements() st_line.append_line(temp) assert st_line.cbuffer.n_objects == num_tests assert len(in_knl_data) == num_tests assert len(in_ksl_data) == num_tests assert len(in_bal_data) == num_tests for ii in range(0, num_tests): assert pyst_line[ii].order == st_line.get(ii).order assert np.allclose(in_knl_data[ii], st_line.get(ii).knl, rtol=1e-15) assert np.allclose(in_ksl_data[ii], st_line.get(ii).ksl, rtol=1e-15) assert np.allclose(in_bal_data[ii], st_line.get(ii).bal, rtol=1e-15) for ii in range(0, num_tests):
def test_line(): rfmultipole = elements.RFMultipole(frequency=100, knl=[0.1, 0.2], ksl=[0.3, 0.4]) zero_drift = elements.Drift(0) line = pysixtrack.Line( elements=[zero_drift, rfmultipole, zero_drift], element_names=["zero_drift", "rfmultipole", "zero_drift"], ) length = 1.4 drift_exact = elements.DriftExact(length) multipole = elements.Multipole(knl=[0.1]) aperture = elements.LimitEllipse(a=0.08, b=0.02) n_elements = 3 position = 1 drift_exact_name = "exact drift" line.insert_element(position, drift_exact, drift_exact_name) n_elements += 1 assert len(line) == n_elements assert line.find_element_ids(drift_exact_name)[0] == position assert line.get_length() == length multipole_name = "multipole" line.insert_element(position, multipole, multipole_name) n_elements += 1 line.insert_element(position + 1, aperture, "multipole_aperture") n_elements += 1 assert len(line) == n_elements line._add_offset_error_to(multipole_name, dx=0, dy=0) n_elements += 2 assert len(line) == n_elements line._add_offset_error_to(multipole_name, dx=0.2, dy=-0.003) n_elements += 2 assert len(line) == n_elements line._add_tilt_error_to(multipole_name, angle=0) n_elements += 2 assert len(line) == n_elements line._add_tilt_error_to(multipole_name, angle=0.1) n_elements += 2 assert len(line) == n_elements line._add_multipole_error_to(multipole_name, knl=[0, 0.1], ksl=[-0.03, 0.01]) # line._add_multipole_error_to(drift_exact,knl=[0,0.1],ksl=[-0.03,0.01]) line_dict = line.to_dict() line = pysixtrack.Line.from_dict(line_dict) assert len(line) == n_elements line.append_line(pysixtrack.Line.from_dict(line_dict)) n_elements *= 2 assert len(line) == n_elements assert line.get_length() == 2 * length s_elements_d = line.get_s_elements("downstream") s_elements_u = line.get_s_elements("upstream") assert max(np.array(s_elements_d) - np.array(s_elements_u)) == length line.insert_element(1, elements.Multipole(), "inactive_multipole") n_elements += 1 assert len(line) == n_elements assert len(line.remove_inactive_multipoles()) == n_elements - 1 assert len(line) == n_elements line.remove_inactive_multipoles(inplace=True) n_elements -= 1 assert len(line) == n_elements assert len(line.merge_consecutive_drifts()) == n_elements - 1 line.merge_consecutive_drifts(inplace=True) n_elements -= 1 assert len(line) == n_elements assert len(line.get_elements_of_type(elements.Drift)) == 2 drifts = line.get_elements_of_type(elements.Drift)[0] n_zerolength_drifts = len([d for d in drifts if d.length == 0]) assert (len(line.remove_zero_length_drifts()) == n_elements - n_zerolength_drifts) line.remove_zero_length_drifts(inplace=True) n_elements -= n_zerolength_drifts assert len(line) == n_elements
from pyoptics import madlang, optics import pysixtrack # see sps/madx/a001_track_thin.madx mad = madlang.open("madx/SPS_Q20_thin.seq") mad.acta_31637.volt = 4.5 mad.acta_31637.lag = 0.5 elems, rest, iconv = mad.sps.expand_struct(pysixtrack.element_types) pbench = optics.open("madx/track.obs0001.p0001") sps = pysixtrack.Line(elements=[e[2] for e in elems]) def get_part(pbench, ii): pstart = [pbench[n][ii] for n in "x px y py t pt".split()] pstart = dict(zip("x px y py tau ptau".split(), pstart)) prun = pysixtrack.Particles(energy0=pbench.e[ii] * 1e9, **pstart) return prun def compare(prun, pbench): out = [] for att in "x px y py tau ptau".split(): vrun = getattr(prun, att) vbench = getattr(pbench, att) diff = vrun - vbench out.append(abs(diff)) print(f"{att:<5} {vrun:22.13e} {vbench:22.13e} {diff:22.13g}") print(f"max {max(out):21.12e}") return max(out)