Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions ext/BeamTrackingBeamlinesExt/BeamTrackingBeamlinesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ function track!(
return bunch
end

check_Brho(bl.Brho_ref, bunch)

if !outer_particle_loop
for ele in bl.line
track!(bunch, ele; kwargs...)
Expand All @@ -65,8 +63,6 @@ function track!(
return bunch
end

check_Brho(NaN, bunch)

if !outer_particle_loop
if !isnothing(bbl.rep)
i = 1
Expand Down
11 changes: 9 additions & 2 deletions ext/BeamTrackingBeamlinesExt/unpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ function _track!(
pp = ele.PatchParams
# bc BitsLineElement does not support ApertureParams yet
dp = ele isa BitsLineElement ? nothing : ele.ApertureParams
Bρ = ele isa BitsLineElement ? nothing : get_Brho(ele.beamline.Brho_ref, bunch)

# Function barrier
universal!(i, b, tm, bunch, L, ap, bp, bm, pp, dp; kwargs...)
universal!(i, b, tm, bunch, L, ap, bp, bm, pp, dp, Bρ; kwargs...)
end

# Step 2: Push particles through -----------------------------------------
Expand All @@ -31,10 +32,16 @@ function universal!(
bendparams,
bmultipoleparams,
patchparams,
apertureparams;
apertureparams,
Bρ;
kwargs...
)
kc = KernelChain(Val{1}())
if !isnothing(Bρ) && !(Bρ[1] ≈ Bρ[2])
kc = KernelChain(Val{2}())
kc = push(kc, KernelCall(ExactTracking.update_P0!, (Bρ[1], Bρ[2])))
end

if isactive(alignmentparams)
if isactive(patchparams)
error("Tracking through a LineElement containing both PatchParams and AlignmentParams is undefined")
Expand Down
20 changes: 9 additions & 11 deletions ext/BeamTrackingBeamlinesExt/utils.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
function check_Brho(Brho_ref, bunch::Bunch)
if isnan(bunch.Brho_ref)
if isnan(Brho_ref)
@warn "Both the bunch and beamline do not have any set Brho_ref. If any LineElements have unnormalized fields stored as independent variables, tracking results will be NaNs"
else
@info "Setting bunch.Brho_ref = $Brho_ref (from the Beamline)"
setfield!(bunch, :Brho_ref, typeof(bunch.Brho_ref)(Brho_ref)) #Brho_ref = Brho_ref
end
elseif !isnan(Brho_ref) && !(Brho_ref ≈ bunch.Brho_ref)
@warn "The reference energy of the bunch does NOT equal the reference energy of the Beamline.
Normalized field strengths in tracking ALWAYS use the reference energy of the bunch."
function get_Brho(Brho_ref, bunch::Bunch)
if isnan(bunch.Brho_ref) && isnan(Brho_ref)
@warn "Both the bunch and beamline do not have any set Brho_ref. If any LineElements have unnormalized fields stored as independent variables, tracking results will be NaNs"
setfield!(bunch, :Brho_ref, typeof(bunch.Brho_ref)(Brho_ref))
Bρ = (Brho_ref, Brho_ref)
else
Bρ = (bunch.Brho_ref, Brho_ref)
setfield!(bunch, :Brho_ref, typeof(bunch.Brho_ref)(Brho_ref)) # Currently does not support GTPSA-type Brho_ref
end
return Bρ
end

get_n_multipoles(::BMultipoleParams{T,N}) where {T,N} = N
Expand Down
15 changes: 6 additions & 9 deletions src/modules/ExactTracking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ using ..BeamTracking: C_LIGHT
const TRACKING_METHOD = Exact

# Update the reference energy of the canonical coordinates
# BUG: z and pz are not updated correctly
#=
@makekernel fastgtpsa=true function update_P0!(i, b, Brho_initial, Brho_final)
@inbounds begin
@FastGTPSA! v[i,PXI] = v[i,PXI] * Brho_initial / Brho_final
@FastGTPSA! v[i,PYI] = v[i,PYI] * Brho_initial / Brho_final
@FastGTPSA! v[i,PZI] = v[i,PZI] * Brho_initial / Brho_final
@makekernel fastgtpsa=true function update_P0!(i, b::BunchView, Brho_initial, Brho_final)
v = b.v
@inbounds begin # Currently does not support GTPSA-type Brho_ref
v[i,PXI] *= Brho_initial / Brho_final
v[i,PYI] *= Brho_initial / Brho_final
v[i,PZI] += (Brho_initial - Brho_final) / Brho_final
end
return v
end
=#

#
# =============== E X A C T D R I F T ===============
Expand Down
13 changes: 12 additions & 1 deletion test/BeamlinesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@
track!(b0, bl)
v_expected = read_map("bmad_maps/solenoid.jl")
@test coeffs_approx_equal(v_expected, b0.v, 5e-10)


empty_ele = LineElement(tracking_method=Exact())
empty_bl = Beamline([empty_ele], Brho_ref = 1.0)
v0 = [0.0 0.1 0.0 0.1 0.0 0.0]
b0 = Bunch(copy(v0), Brho_ref = empty_bl.Brho_ref)
track!(b0, empty_bl)
@test b0.Brho_ref == empty_bl.Brho_ref
@test b0.v == v0
empty_bl.Brho_ref = 2.0
track!(b0, empty_bl)
@test b0.Brho_ref == empty_bl.Brho_ref
@test b0.v == [0.0 0.05 0.0 0.05 0.0 -0.5]

# Errors:
ele_kick = LineElement(L=1.0, Kn0L=1.0, tracking_method=Exact())
Expand Down
Loading