Skip to content
Merged
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
5 changes: 3 additions & 2 deletions src/enum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ end
@enumit("BendType SECTOR RECTANGULAR")
@enumit("BodyLoc ENTRANCE_END CENTER EXIT_END BOTH_ENDS NOWHERE EVERYWHERE")
@enumit("BranchGeometry OPEN CLOSED")
## @enumit("EleGeometry STRAIGHT CIRCULAR ZEROLENGTH PATCH GIRDER CRYSTAL MIRROR")
## @enumit("EleGeometry STRAIGHT CIRCULAR ZEROLENGTH PATCH GIRDER CRYSTAL MIRROR") # See core.jl
@enumit("Cavity STANDING_WAVE TRAVELING_WAVE")
@enumit("SlaveControl DELTA ABSOLUTE NOT_SET")
@enumit("FieldCalc MAP STANDARD")
@enumit("Interpolation LINEAR SPLINE")
@enumit("Lord NOT SUPER MULTIPASS GOVERNOR")
@enumit("Slave NOT SUPER MULTIPASS")
@enumit("StreamLoc UPSTREAM_END CENTER INSIDE DOWNSTREAM_END")
@enumit("Loc UPSTREAM_END CENTER INSIDE DOWNSTREAM_END")
@enumit("Select UPSTREAM DOWNSTREAM")

@enumit("TrackingMethod RUNGE_KUTTA TIME_RUNGE_KUTTA STANDARD")
@enumit("ParticleState PREBORN ALIVE PRETRACK LOST LOST_NEG_X LOST_POS_X LOST_NEG_Y LOST_POS_Y LOST_PZ LOST_Z")
Expand Down
26 changes: 13 additions & 13 deletions src/find.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ele_at_offset(reference::Union{Ele,Branch}, offset::Int, wrap::Bool)
error("Non-zero offset ($offset) not allowed for Governor reference elements ($reference.name).")

elseif branch.type == SuperLordBranch
if offset > 0 ? ref = reference.slaves[end] : ref = reference.slaves[1]; end
offset > 0 ? ref = reference.slaves[end] : ref = reference.slaves[1]
branch = ref.branch
indx = ref.ix_ele + offset

Expand Down Expand Up @@ -305,7 +305,7 @@ end
# ele_at_s

"""
ele_at_s(branch::Branch, s::Real; choose::StreamLoc.T = StreamLoc.UPSTREAM_END, ele_near::ELE = NULL_ELE)
ele_at_s(branch::Branch, s::Real; select::Select.T = Select.UPSTREAM, ele_near::ELE = NULL_ELE)
-> ele_overlap::Ele

Returns lattice element `ele_overlap` that overlaps a given longitudinal s-position.
Expand All @@ -315,9 +315,9 @@ That is, `s` will be in the interval `[ele_overlap.s, ele_overlap.s_downstream]`

- `branch` Lattice `Branch` to search.
- `s` Longitudinal position to match to.
- `choose` If there is a choice of elements, which can happen if `s` corresponds to a boundary
point between two elements, `choose` is used to pick either the `StreamLoc.UPSTREAM_END`
element (default) or `StreamLoc.DOWNSTREAM_END` element.
- `select` If there is a choice of elements, which can happen if `s` corresponds to a boundary
point between two elements, `select` is used to pick either the `Select.UPSTREAM`
element (default) or `Select.DOWNSTREAM` element.
- `ele_near` If there are elements with negative drift lengths (generally this will be a
`drift` or `patch` element), there might be multiple solutions. If `ele_near`
is specified, this routine will choose the solution nearest `ele_near`.
Expand All @@ -328,9 +328,9 @@ That is, `s` will be in the interval `[ele_overlap.s, ele_overlap.s_downstream]`

""" ele_at_s

function ele_at_s(branch::Branch, s::Real; choose::StreamLoc.T = StreamLoc.UPSTREAM_END, ele_near::Ele = NULL_ELE)
function ele_at_s(branch::Branch, s::Real; select::Select.T = Select.UPSTREAM, ele_near::Ele = NULL_ELE)
check_if_s_in_branch_range(branch, s)
if choose != StreamLoc.UPSTREAM_END && choose != StreamLoc.DOWNSTREAM_END; error("Bad `choose` argument: $choose"); end
if select != Select.UPSTREAM && select != Select.DOWNSTREAM; error("Bad `select` argument: $select"); end

# If ele_near is not set
if is_null(ele_near)
Expand All @@ -340,11 +340,11 @@ function ele_at_s(branch::Branch, s::Real; choose::StreamLoc.T = StreamLoc.UPSTR
while true
if n3 == n1 + 1; break; end
n2 = div(n1 + n3, 2)
s < branch.ele[n2].s || (choose == StreamLoc.UPSTREAM_END && branch.ele[n2].s == s) ? n3 = n2 : n1 = n2
s < branch.ele[n2].s || (select == Select.UPSTREAM && branch.ele[n2].s == s) ? n3 = n2 : n1 = n2
end

# Solution is n1 except in one case.
if choose == StreamLoc.DOWNSTREAM_END && branch.ele[n3].s == s
if select == Select.DOWNSTREAM && branch.ele[n3].s == s
return branch.ele[n3]
else
return branch.ele[n1]
Expand All @@ -354,19 +354,19 @@ function ele_at_s(branch::Branch, s::Real; choose::StreamLoc.T = StreamLoc.UPSTR
# If ele_near is used
ele = ele_near
if ele.branch.type <: LordBranch
choose == StreamLoc.DOWNSTREAM_END ? ele = ele.slaves[end] : ele = ele.slaves[1]
select == Select.DOWNSTREAM ? ele = ele.slaves[end] : ele = ele.slaves[1]
end


if s > ele.s_downstream || (choose == StreamLoc.DOWNSTREAM_END && s == ele.s_downstream)
if s > ele.s_downstream || (select == Select.DOWNSTREAM && s == ele.s_downstream)
while true
ele = next_ele(ele)
if s < ele.s_downstream || (s == ele.s_downstream && choose == StreamLoc.UPSTREAM_END); return ele; end
if s < ele.s_downstream || (s == ele.s_downstream && select == Select.UPSTREAM); return ele; end
end

else
while true
if s > ele.s || (choose == StreamLoc.DOWNSTREAM_END && ele.s == s); return ele; end
if s > ele.s || (select == Select.DOWNSTREAM && ele.s == s); return ele; end
ele = next_ele(ele, -1)
end
end
Expand Down
24 changes: 12 additions & 12 deletions src/manipulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ end
# split!

"""
split!(branch::Branch, s_split::Real; choose::StreamLoc.T = StreamLoc.UPSTREAM_END, ele_near::Ele = NULL_ELE)
split!(branch::Branch, s_split::Real; select::Select.T = Select.UPSTREAM, ele_near::Ele = NULL_ELE)

Routine to split an lattice element of a branch into two to create a branch that has an element
boundary at the point s = `s_split`.
Expand All @@ -110,13 +110,13 @@ than 2*`LatticeGlobal.significant_length`.
### Input
- `branch` -- Lattice branch
- `s_split` -- Position at which branch is to be split.
- `choose` -- logical, optional: If no splitting of an element is needed, that is,
- `select` -- logical, optional: If no splitting of an element is needed, that is,
`s_split` is at an element boundary, there can be multiple possible `ele_split` elements to
return if there exist zero length elements at the split location.
If `choose` = `StreamLoc.DOWNSTREAM_END`, the returned `ele_split` element will be chosen to be
at the maximal downstream element. If `choose` = `StreamLoc.UPSTREAM_END`, the returned `ele_split` element
If `select` = `Select.DOWNSTREAM`, the returned `ele_split` element will be chosen to be
at the maximal downstream element. If `select` = `Select.UPSTREAM`, the returned `ele_split` element
will be chosen to be the maximal upstream location.
If `s_split` is not at an element boundary, the setting of `choose` is immaterial.
If `s_split` is not at an element boundary, the setting of `select` is immaterial.
- `ele_near` -- Element near the point to be split. `ele_near` is useful in the case where
there is a patch with a negative length which can create an ambiguity as to where to do the split
In this case `ele_near` will remove the ambiguity. Also useful to ensure where to split if there
Expand All @@ -127,18 +127,18 @@ than 2*`LatticeGlobal.significant_length`.
- `split_done` -- true if lat was split, false otherwise.
""" split!(branch::Branch)

function split!(branch::Branch, s_split::Real; choose::StreamLoc.T = StreamLoc.UPSTREAM_END, ele_near::Ele = NULL_ELE)
function split!(branch::Branch, s_split::Real; select::Select.T = Select.UPSTREAM, ele_near::Ele = NULL_ELE)
check_if_s_in_branch_range(branch, s_split)
if choose != StreamLoc.UPSTREAM_END && choose != StreamLoc.DOWNSTREAM_END; error("Bad `choose` argument: $choose"); end
slave1 = ele_at_s(branch, s_split, choose = choose, ele_near = ele_near)
if select != Select.UPSTREAM && select != Select.DOWNSTREAM; error("Bad `select` argument: $select"); end
slave1 = ele_at_s(branch, s_split, select = select, ele_near = ele_near)

# Make sure split does create an element that is less than min_len in length.
min_len = min_ele_length(branch.lat)
if choose == StreamLoc.UPSTREAM_END && slave1.s > s_split-min_len
slave1 = ele_at_s(branch, slave1.s, choose = StreamLoc.DOWNSTREAM_END)
if select == Select.UPSTREAM && slave1.s > s_split-min_len
slave1 = ele_at_s(branch, slave1.s, select = Select.DOWNSTREAM)
s_split = slave1.s_downstream
elseif choose == StreamLoc.DOWNSTREAM_END && slave1.s_downstream < s_split+min_len
slave1 = ele_at_s(branch, slave1.s_downstream, choose = StreamLoc.DOWNSTREAM_END)
elseif select == Select.DOWNSTREAM && slave1.s_downstream < s_split+min_len
slave1 = ele_at_s(branch, slave1.s_downstream, select = Select.DOWNSTREAM)
s_split = slave1.s
end

Expand Down
2 changes: 1 addition & 1 deletion src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ele_param_info_dict = Dict(
:psi_floor => ParamInfo(FloorPositionGroup, Number, "Element floor psi angle orientation", "rad", :psi),

:origin_ele => ParamInfo(GirderGroup, Ele, "Coordinate reference element."),
:origin_ele_ref_pt => ParamInfo(GirderGroup, StreamLoc.T, "Reference location on reference element. Default is StreamLoc.CENTER."),
:origin_ele_ref_pt => ParamInfo(GirderGroup, Loc.T, "Reference location on reference element. Default is Loc.CENTER."),
:dr_girder => ParamInfo(GirderGroup, Vector{Number}, "3-vector of girder position with respect to ref ele.", "m", :dr),
:dtheta_girder => ParamInfo(GirderGroup, Number, "Theta angle orientation with respect to ref ele.", "rad", :dtheta),
:dphi_girder => ParamInfo(GirderGroup, Number, "Phi angle orientation with respect to ref ele.", "rad", :dphi),
Expand Down
26 changes: 13 additions & 13 deletions src/query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# machine_location

"""
machine_location(loc::BodyLoc.T, orientation::Int)::StreamLoc.T
machine_location(loc::BodyLoc.T, orientation::Int)::Loc.T

Given a location with respect to an element's `local` orientation and the element's `orientation`,
return the equivalent location in machine coordinates.
Expand All @@ -17,16 +17,16 @@ The reverse function is body_location.

### Output

- Returns: `StreamLoc.UPSTREAM_END`, `StreamLoc.CENTER`, or `StreamLoc.DOWNSTREAM_END` (a `StreamLoc.T` value).
- Returns: `Loc.UPSTREAM_END`, `Loc.CENTER`, or `Loc.DOWNSTREAM_END` (a `Loc.T` value).
""" machine_location

function machine_location(loc::BodyLoc.T, orientation::Int)::StreamLoc.T
if loc == BodyLoc.CENTER; return StreamLoc.CENTER; end
function machine_location(loc::BodyLoc.T, orientation::Int)::Loc.T
if loc == BodyLoc.CENTER; return Loc.CENTER; end

if loc == BodyLoc.ENTRANCE_END
orientation == 1 ? (return StreamLoc.UPSTREAM_END) : return StreamLoc.DOWNSTREAM_END
orientation == 1 ? (return Loc.UPSTREAM_END) : return Loc.DOWNSTREAM_END
elseif loc == BodyLoc.EXIT_END
orientation == 1 ? (return StreamLoc.DOWNSTREAM_END) : return StreamLoc.UPSTREAM_END
orientation == 1 ? (return Loc.DOWNSTREAM_END) : return Loc.UPSTREAM_END
else
error(f"loc argument values limited to `BodyLoc.ENTRANCE_END`, `BodyLoc.CENTER`, or `BodyLoc.EXIT_END`. Not: {loc}")
end
Expand All @@ -36,7 +36,7 @@ end
# body_location

"""
body_location(loc::StreamLoc.T, orientation::Int)::BodyLoc.T
body_location(loc::Loc.T, orientation::Int)::BodyLoc.T

Given an element location with respect to machine coordinates,
along with the element's orientation with respect to machine coordinates,
Expand All @@ -46,20 +46,20 @@ The reverse function is machine_location.

### Input

- `loc` Possible values: `StreamLoc.UPSTREAM_END`, `StreamLoc.CENTER`, or `StreamLoc.DOWNSTREAM_END` .
- `loc` Possible values: `Loc.UPSTREAM_END`, `Loc.CENTER`, or `Loc.DOWNSTREAM_END` .
- `orientation` Possible values: -1 or +1.

### Output

- Returns: `BodyLoc.entrance_end`, `StreamLoc.CENTER`, `BodyLoc.EXIT_END`.
- Returns: `BodyLoc.entrance_end`, `Loc.CENTER`, `BodyLoc.EXIT_END`.
""" body_location

function body_location(loc::StreamLoc.T, orientation::Int)
if loc == StreamLoc.CENTER; return b_enter; end
function body_location(loc::Loc.T, orientation::Int)
if loc == Loc.CENTER; return b_enter; end

if loc == StreamLoc.UPSTREAM_END
if loc == Loc.UPSTREAM_END
orientation == 1 ? (return BodyLoc.ENTRANCE_END) : return BodyLoc.EXIT_END
elseif loc == StreamLoc.DOWNSTREAM_END
elseif loc == Loc.DOWNSTREAM_END
orientation == 1 ? (return BodyLoc.EXIT_END) : return BodyLoc.ENTRANCE_END
else
error(f"ConfusedError: Should not be here! Please report this!")
Expand Down
4 changes: 2 additions & 2 deletions src/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ end

@kwdef mutable struct BeamBeamGroup <: EleParameterGroup
n_slice::Number = 1
z0_crossing::Number = 0 # Weak particle phase space z when strong beam StreamLoc.CENTER passes
z0_crossing::Number = 0 # Weak particle phase space z when strong beam Loc.CENTER passes
# the BeamBeam element.
repetition_freq:: Number = 0 # Strong beam repetition rate.
twiss::Twiss = Twiss() # Strong beam Twiss.
Expand Down Expand Up @@ -477,7 +477,7 @@ Girder parameter struct.
"""
@kwdef mutable struct GirderGroup <: EleParameterGroup
origin_ele::Ele = NullEle
origin_ele_ref_pt::StreamLoc.T = StreamLoc.CENTER
origin_ele_ref_pt::Loc.T = Loc.CENTER
dr::Vector = [0.0, 0.0, 0.0]
dtheta::Number = 0.0
dphi::Number = 0.0
Expand Down
36 changes: 20 additions & 16 deletions src/superimpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Thus, a positive `offset` will displace the superposition location downstream if
a normal orientation and vice versa for a reversed orientation.

- When `offset` = 0, for zero length `super_ele` elements the superposition location is at the
entrance end of `ref` except if `ref_origin` is set to `StreamLoc.DOWNSTREAM_END` in which case the
entrance end of `ref` except if `ref_origin` is set to `BodyLoc.EXIT_END` in which case the
superposition location is at the exit end of `ref`.

- The superimposed element will inherit the orientation of the `ref` element.
Expand Down Expand Up @@ -87,11 +87,11 @@ function superimpose!(super_ele::Ele, ref::T; ele_origin::BodyLoc.T = BodyLoc.CE

# Insertion of zero length element with zero offset at edge of an element.
if L_super == 0 && offset == 0
if machine_ref_origin == StreamLoc.UPSTREAM_END || (machine_ref_origin == StreamLoc.CENTER && ref_ele.L == 0)
if machine_ref_origin == Loc.UPSTREAM_END || (machine_ref_origin == Loc.CENTER && ref_ele.L == 0)
ix_insert = max(ref_ix_ele, 2)
insert!(branch, ix_insert, super_ele)
continue
elseif machine_ref_origin == StreamLoc.DOWNSTREAM_END
elseif machine_ref_origin == Loc.DOWNSTREAM_END
ix_insert = min(ref_ix_ele+1, length(ref_ele.branch.ele))
insert!(branch, ix_insert, super_ele)
continue
Expand All @@ -100,28 +100,31 @@ function superimpose!(super_ele::Ele, ref::T; ele_origin::BodyLoc.T = BodyLoc.CE

# Super_ele end locations: s1 and s2.
if branch.type <: LordBranch
if machine_ref_origin == StreamLoc.UPSTREAM_END; s1 = ref_ele.slaves[1].s
elseif machine_ref_origin == StreamLoc.CENTER; s1 = 0.5 * (ref_ele.slaves[1].s + ref_ele.slaves[end].s_downstream)
if machine_ref_origin == Loc.UPSTREAM_END; s1 = ref_ele.slaves[1].s
elseif machine_ref_origin == Loc.CENTER; s1 = 0.5 * (ref_ele.slaves[1].s + ref_ele.slaves[end].s_downstream)
else; s1 = ref_ele.slaves[end].s_downstream
end
else # Not a lord branch
if machine_ref_origin == StreamLoc.UPSTREAM_END; s1 = ref_ele.s
elseif machine_ref_origin == StreamLoc.CENTER; s1 = 0.5 * (ref_ele.s + ref_ele.s_downstream)
if machine_ref_origin == Loc.UPSTREAM_END; s1 = ref_ele.s
elseif machine_ref_origin == Loc.CENTER; s1 = 0.5 * (ref_ele.s + ref_ele.s_downstream)
else; s1 = ref_ele.s_downstream
end
end

if machine_ele_origin == StreamLoc.UPSTREAM_END; s1 = s1 + offset
elseif machine_ele_origin == StreamLoc.CENTER; s1 = s1 + offset - 0.5 * L_super
if machine_ele_origin == Loc.UPSTREAM_END; s1 = s1 + offset
elseif machine_ele_origin == Loc.CENTER; s1 = s1 + offset - 0.5 * L_super
else; s1 = s1 + offset - L_super
end

s2 = s1 + L_super

# If super_ele has zero length just insert it.
if L_super == 0
machine_ref_origin == StreamLoc.DOWNSTREAM_END ? choose = StreamLoc.UPSTREAM_END : choose = StreamLoc.DOWNSTREAM_END
ele_at, _ = split!(branch, s1, choose = choose, ele_near = ref_ele)
if machine_ref_origin == Loc.DOWNSTREAM_END
ele_at, _ = split!(branch, s1, select = Select.DOWNSTREAM, ele_near = ref_ele)
else
ele_at, _ = split!(branch, s1, select = Select.UPSTREAM, ele_near = ref_ele)
end
insert!(branch, ele_at.ix_ele, super_ele)
continue
end
Expand Down Expand Up @@ -149,8 +152,8 @@ function superimpose!(super_ele::Ele, ref::T; ele_origin::BodyLoc.T = BodyLoc.CE

# Split points are chosen to avoid creating elements with non-zero length below the minimum.
# And super_lord length will be adjusted accordingly.
ele1 = ele_at_s(branch, s1, choose = StreamLoc.UPSTREAM_END, ele_near = ref_ele)
ele2 = ele_at_s(branch, s2, choose = StreamLoc.DOWNSTREAM_END, ele_near = ref_ele)
ele1 = ele_at_s(branch, s1, select = Select.UPSTREAM, ele_near = ref_ele)
ele2 = ele_at_s(branch, s2, select = Select.DOWNSTREAM, ele_near = ref_ele)

min_len = min_ele_length(branch.lat)

Expand All @@ -170,11 +173,11 @@ function superimpose!(super_ele::Ele, ref::T; ele_origin::BodyLoc.T = BodyLoc.CE
super_ele.L = super_ele.L + ele2.s_downstream - s2
end

# `choose` is set to minimize number of elements in the superposition region.
# `select` is set to minimize number of elements in the superposition region.
# The superposition region is from beginning of ele1 to the beginning of ele2.

ele2, _ = split!(branch, s2, choose = StreamLoc.UPSTREAM_END) # Notice that s2 split must be done first!
ele1, _ = split!(branch, s1, choose = StreamLoc.DOWNSTREAM_END)
ele2, _ = split!(branch, s2, select = Select.UPSTREAM) # Notice that s2 split must be done first!
ele1, _ = split!(branch, s1, select = Select.DOWNSTREAM)

# If there are just drifts here then no superimpose needed.
# Note: In this case there cannot be wrap around.
Expand Down Expand Up @@ -219,6 +222,7 @@ function superimpose!(super_ele::Ele, ref::T; ele_origin::BodyLoc.T = BodyLoc.CE
ele2.L = ele.L
ele2.pdict[:super_lords] = Vector{Ele}([lord1])
push!(lord1.slaves, ele2)
set_drift_slice_names(ele)

elseif ele.slave_status != Slave.SUPER
lord2 = ele
Expand Down
2 changes: 1 addition & 1 deletion src/tracking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ abstract type AbstractTrackPoint end
direction::Int = 1
time_dir::Float64 = 1
species::Species = Species("not_set")
location::StreamLoc.T = StreamLoc.UPSTREAM_END
location::Loc.T = Loc.UPSTREAM_END
end
Loading
Loading