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
25 changes: 11 additions & 14 deletions src/enum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ macro enumit(str::AbstractString)
eval( Meta.parse("export $str2") )
end

@enumit("ApertureTypeSwitch rectangular elliptical")
@enumit("BendTypeSwitch sbend rbend")
@enumit("BodyLocationSwitch entrance_end b_center exit_end both_ends nowhere everywhere")
@enumit("BoolSwitch no not_set yes")
@enumit("BranchGeometrySwitch open closed")
@enumit("CavityTypeSwitch standing_wave traveling_wave")
@enumit("ControlSlaveTypeSwitch delta absolute control_not_set")
@enumit("FieldCalcMethodSwitch field_map field_standard")
@enumit("InterpolationSwitch linear spline")
@enumit("LordStatusSwitch not_a_lord super_lord multipass_lord governor")
@enumit("SlaveStatusSwitch not_a_slave super_slave multipass_slave")
@enumit("StreamLocationSwitch upstream_end center inside downstream_end")
@enumit("TrackingMethodSwitch rungekutta time_rungekutta standard_tracking")
@enumit("TrackingStateSwitch preborn alive pretrack lost lost_neg_x lost_pos_x lostNegY LostPosY LostPz LostZ")
@enumit("ApertureType RECTANGULAR ELLIPTICAL")
@enumit("BendType SBEND RBEND")
@enumit("BodyLocation ENTRANCE_END B_CENTER EXIT_END BOTH_ENDS NOWHERE EVERYWHERE")
@enumit("BranchGeometry OPEN CLOSED")
@enumit("CavityType STANDING_WAVE TRAVELING_WAVE")
@enumit("ControlSlaveType DELTA ABSOLUTE CONTROL_NOT_SET")
@enumit("FieldCalcMethod FIELD_MAP FIELD_STANDARD")
@enumit("Interpolation LINEAR SPLINE")
@enumit("LordStatus NOT_A_LORD SUPER_LORD MULTIPASS_LORD GOVERNOR")
@enumit("SlaveStatus NOT_A_SLAVE SUPER_SLAVE MULTIPASS_SLAVE")
@enumit("StreamLocation UPSTREAM_END CENTER INSIDE DOWNSTREAM_END")
22 changes: 11 additions & 11 deletions src/find.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ end
# ele_at_s

"""
ele_at_s(branch::Branch, s::Real; choose::StreamLocationSwitch = upstream_end, ele_near::ELE = NULL_ELE)
ele_at_s(branch::Branch, s::Real; choose::StreamLocation = UPSTREAM_END, ele_near::ELE = NULL_ELE)
-> ele_overlap::Ele

Returns lattice element `ele_overlap` that overlaps a given longitudinal s-position.
Expand All @@ -234,8 +234,8 @@ 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 `upstream_end`
element (default) or `downstream_end` element.
point between two elements, `choose` is used to pick either the `UPSTREAM_END`
element (default) or `DOWNSTREAM_END` 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 @@ -246,9 +246,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::StreamLocationSwitch = upstream_end, ele_near::Ele = NULL_ELE)
function ele_at_s(branch::Branch, s::Real; choose::StreamLocation = UPSTREAM_END, ele_near::Ele = NULL_ELE)
check_if_s_in_branch_range(branch, s)
if choose != upstream_end && choose != downstream_end; error("Bad `choose` argument: $choose"); end
if choose != UPSTREAM_END && choose != DOWNSTREAM_END; error("Bad `choose` argument: $choose"); end

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

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


if s > ele.s_downstream || (choose == downstream_end && s == ele.s_downstream)
if s > ele.s_downstream || (choose == DOWNSTREAM_END && s == ele.s_downstream)
while true
ele = next_ele(ele)
if s < ele.s_downstream || (s == ele.s_downstream && choose == upstream_end); return ele; end
if s < ele.s_downstream || (s == ele.s_downstream && choose == UPSTREAM_END); return ele; end
end

else
while true
if s > ele.s || (choose == downstream_end && ele.s == s); return ele; end
if s > ele.s || (choose == DOWNSTREAM_END && ele.s == s); return ele; end
ele = next_ele(ele, -1)
end
end
Expand Down
18 changes: 9 additions & 9 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::StreamLocationSwitch = upstream_end, ele_near::Ele = NULL_ELE)
split!(branch::Branch, s_split::Real; choose::StreamLocation = UPSTREAM_END, 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 @@ -113,8 +113,8 @@ than 2*`LatticeGlobal.significant_length`.
- `choose` -- 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` = `downstream_end`, the returned `ele_split` element will be chosen to be
at the maximal downstream element. If `choose` = `upstream_end`, the returned `ele_split` element
If `choose` = `DOWNSTREAM_END`, the returned `ele_split` element will be chosen to be
at the maximal downstream element. If `choose` = `UPSTREAM_END`, 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.
- `ele_near` -- Element near the point to be split. `ele_near` is useful in the case where
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::StreamLocationSwitch = upstream_end, ele_near::Ele = NULL_ELE)
function split!(branch::Branch, s_split::Real; choose::StreamLocation = UPSTREAM_END, ele_near::Ele = NULL_ELE)
check_if_s_in_branch_range(branch, s_split)
if choose != upstream_end && choose != downstream_end; error("Bad `choose` argument: $choose"); end
if choose != UPSTREAM_END && choose != DOWNSTREAM_END; error("Bad `choose` argument: $choose"); end
slave1 = ele_at_s(branch, s_split, choose = choose, 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 == upstream_end && slave1.s > s_split-min_len
slave1 = ele_at_s(branch, slave1.s, choose = downstream_end)
if choose == UPSTREAM_END && slave1.s > s_split-min_len
slave1 = ele_at_s(branch, slave1.s, choose = DOWNSTREAM_END)
s_split = slave1.s_downstream
elseif choose == downstream_end && slave1.s_downstream < s_split+min_len
slave1 = ele_at_s(branch, slave1.s_downstream, choose = downstream_end)
elseif choose == DOWNSTREAM_END && slave1.s_downstream < s_split+min_len
slave1 = ele_at_s(branch, slave1.s_downstream, choose = DOWNSTREAM_END)
s_split = slave1.s
end

Expand Down
30 changes: 15 additions & 15 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract type Pointer end

@kwdef mutable struct ParamInfo
parent_group::T where T <: Union{DataType,Vector} # Use the parent_group function to get the parent group.
kind::Union{T, Union} where T <: DataType # Something like ApertureTypeSwitch is a Union.
kind::Union{T, Union} where T <: DataType # Something like ApertureType is a Union.
description::String = ""
units::String = ""
struct_sym::Symbol # Symbol in struct.
Expand Down Expand Up @@ -81,7 +81,7 @@ ele_param_info_dict = Dict(
:angle => ParamInfo(BendGroup, Number, "Design bend angle", "rad"),
:bend_field => ParamInfo(BendGroup, Number, "Design bend field corresponding to g bending", "T"),
:rho => ParamInfo(BendGroup, Number, "Design bend radius", "m"),
:g => ParamInfo(BendGroup, Number, "Design bend strength (1/rho)", "1/m"),
:g => ParamInfo(BendGroup, Number, "Design bend strength (1/rho)", "1/m"),
:e1 => ParamInfo(BendGroup, Number, "Bend entrance face angle.", "rad"),
:e2 => ParamInfo(BendGroup, Number, "Bend exit face angle.", "rad"),
:e1_rect => ParamInfo(BendGroup, Number, "Bend entrance face angles relative to a rectangular geometry.", "rad"),
Expand All @@ -95,7 +95,7 @@ ele_param_info_dict = Dict(
:hgap => ParamInfo(Nothing, Number, "Used to set hgap1 and hgap2 both at once.", ""),
:hgap1 => ParamInfo(BendGroup, Number, "Bend entrance edge pole gap height.", "m"),
:hgap2 => ParamInfo(BendGroup, Number, "Bend exit edge pole gap height.", "m"),
:bend_type => ParamInfo(BendGroup, BendTypeSwitch, "Sets how face angles varies with bend angle."),
:bend_type => ParamInfo(BendGroup, BendType, "Sets how face angles varies with bend angle."),

:offset => ParamInfo([AlignmentGroup,PatchGroup], Vector{Number}, "3-Vector of [x, y, z] element offsets.", "m"),
:x_rot => ParamInfo([AlignmentGroup,PatchGroup], Number, "X-axis element rotation.", "rad"),
Expand All @@ -112,18 +112,18 @@ ele_param_info_dict = Dict(
:pc_exit => ParamInfo(PatchGroup, Number, "Reference momentum at exit end.", "eV"),
:flexible => ParamInfo(PatchGroup, Bool, "Flexible patch?"),
:user_sets_length => ParamInfo(PatchGroup, Bool, "Does Bmad calculate the patch length?"),
:ref_coords => ParamInfo(PatchGroup, BodyLocationSwitch, "Patch coords with respect to entrance_end or exit_end?"),
:ref_coords => ParamInfo(PatchGroup, BodyLocation, "Patch coords with respect to ENTRANCE_END or EXIT_END?"),

:voltage => ParamInfo(RFFieldGroup, Number, "RF voltage.", "volt"),
:gradient => ParamInfo(RFFieldGroup, Number, "RF gradient.", "volt/m"),
:phase => ParamInfo(RFFieldGroup, Number, "RF phase.", "rad"),

:multipass_phase => ParamInfo(RFGroup, Number,
"RF phase which can differ from multipass element to multipass element.", "rad"),
:frequency => ParamInfo(RFGroup, Number, "RF frequency.", "Hz"),
:harmon => ParamInfo(RFGroup, Number, "RF frequency harmonic number.", ""),
:cavity_type => ParamInfo(RFGroup, CavityTypeSwitch, "Type of cavity."),
:n_cell => ParamInfo(RFGroup, Int, "Number of RF cells."),
:frequency => ParamInfo(RFGroup, Number, "RF frequency.", "Hz"),
:harmon => ParamInfo(RFGroup, Number, "RF frequency harmonic number.", ""),
:cavity_type => ParamInfo(RFGroup, CavityType, "Type of cavity."),
:n_cell => ParamInfo(RFGroup, Int, "Number of RF cells."),

:voltage_ref => ParamInfo(LCavityGroup, Number, "Reference RF voltage.", "volt"),
:voltage_err => ParamInfo(LCavityGroup, Number, "RF voltage error.", "volt"),
Expand All @@ -143,13 +143,13 @@ ele_param_info_dict = Dict(
:do_auto_phase => ParamInfo(RFMasterGroup, Bool, "Autoscale phase?"),
:do_auto_scale => ParamInfo(Nothing, Bool, "Used to set do_auto_amp and do_auto_phase both at once.", ""),

:tracking_method => ParamInfo(TrackingGroup, TrackingMethodSwitch, "Nominal method used for tracking."),
:field_calc => ParamInfo(TrackingGroup, FieldCalcMethodSwitch, "Nominal method used for calculating the EM field."),
:tracking_method => ParamInfo(TrackingGroup, TrackingMethod, "Nominal method used for tracking."),
:field_calc => ParamInfo(TrackingGroup, FieldCalcMethod, "Nominal method used for calculating the EM field."),
:num_steps => ParamInfo(TrackingGroup, Int, "Nominal number of tracking steps."),
:ds_step => ParamInfo(TrackingGroup, Number, "Nominal distance between tracking steps.", "m"),

:aperture_type => ParamInfo(ApertureGroup, ApertureTypeSwitch, "Type of aperture. Default is Elliptical."),
:aperture_at => ParamInfo(ApertureGroup, BodyLocationSwitch, "Where the aperture is. Default is entrance_end."),
:aperture_type => ParamInfo(ApertureGroup, ApertureType, "Type of aperture. Default is Elliptical."),
:aperture_at => ParamInfo(ApertureGroup, BodyLocation, "Where the aperture is. Default is ENTRANCE_END."),
:offset_moves_aperture
=> ParamInfo(ApertureGroup, Bool, "Does moving the element move the aperture?"),
:x_limit => ParamInfo(ApertureGroup, Vector{Number}, "2-Vector of horizontal aperture limits.", "m"),
Expand All @@ -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, StreamLocationSwitch, "Reference location on reference element. Default is Center."),
:origin_ele_ref_pt => ParamInfo(GirderGroup, StreamLocation, "Reference location on reference element. Default is 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 All @@ -174,8 +174,8 @@ ele_param_info_dict = Dict(
:slave => ParamInfo(ControlSlaveGroup, Vector{ControlSlave}, "Controlled parameters info."),
:variable => ParamInfo(ControlVarGroup, Vector{ControlVar}, "Controller variables."),

:slave_status => ParamInfo(LordSlaveGroup, SlaveStatusSwitch, "Slave status."),
:lord_status => ParamInfo(LordSlaveGroup, LordStatusSwitch, "Lord status."),
:slave_status => ParamInfo(LordSlaveGroup, SlaveStatus, "Slave status."),
:lord_status => ParamInfo(LordSlaveGroup, LordStatus, "Lord status."),

:spin => ParamInfo(InitParticleGroup, Vector{Number}, "Initial particle spin"),
:orbit => ParamInfo(InitParticleGroup, Vector{Number}, "Initial particle position."),
Expand Down
38 changes: 19 additions & 19 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::BodyLocationSwitch, orientation::Int) -> StreamLocationSwitch
machine_location(loc::BodyLocation, orientation::Int) -> StreamLocation

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 @@ -12,31 +12,31 @@ The reverse function is body_location.
### Input

- `loc` Location with respect to an element's `local` orientation.
Possible values: `entrance_end`, `b_center`, or `exit_end`
Possible values: `ENTRANCE_END`, `B_CENTER`, or `EXIT_END`
- `orientation` Element orientation. Possible values: -1 or +1.

### Output

- Returns: `upstream_end`, `center`, or `downstream_end` (a `StreamLocationSwitch` value).
- Returns: `UPSTREAM_END`, `CENTER`, or `DOWNSTREAM_END` (a `StreamLocation` value).
""" machine_location

function machine_location(loc::BodyLocationSwitch, orientation::Int)
if loc == b_center; return center; end
function machine_location(loc::BodyLocation, orientation::Int)
if loc == B_CENTER; return CENTER; end

if loc == entrance_end
orientation == 1 ? (return upstream_end) : return downstream_end
elseif loc == exit_end
orientation == 1 ? (return downstream_end) : return upstream_end
if loc == ENTRANCE_END
orientation == 1 ? (return UPSTREAM_END) : return DOWNSTREAM_END
elseif loc == EXIT_END
orientation == 1 ? (return DOWNSTREAM_END) : return UPSTREAM_END
else
error(f"loc argument values limited to `entrance_end`, `b_center`, or `exit_end`. Not: {loc}")
error(f"loc argument values limited to `ENTRANCE_END`, `B_CENTER`, or `EXIT_END`. Not: {loc}")
end
end

#---------------------------------------------------------------------------------------------------
# body_location

"""
body_location(loc::StreamLocationSwitch, orientation::Int) -> BodyLocationSwitch
body_location(loc::StreamLocation, orientation::Int) -> BodyLocation

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

### Input

- `loc` Possible values: `upstream_end`, `b_center`, or `downstream_end` .
- `loc` Possible values: `UPSTREAM_END`, `B_CENTER`, or `DOWNSTREAM_END` .
- `orientation` Possible values: -1 or +1.

### Output

- Returns: `entranc_end`, `b_center`, `exit_end`.
- Returns: `entranc_end`, `B_CENTER`, `EXIT_END`.
""" body_location

function body_location(loc::StreamLocationSwitch, orientation::Int)
if loc == center; return b_enter; end
function body_location(loc::StreamLocation, orientation::Int)
if loc == CENTER; return b_enter; end

if loc == upstream_end
orientation == 1 ? (return entrance_end) : return exit_end
elseif loc == downstream_end
orientation == 1 ? (return exit_end) : return entrance_end
if loc == UPSTREAM_END
orientation == 1 ? (return ENTRANCE_END) : return EXIT_END
elseif loc == DOWNSTREAM_END
orientation == 1 ? (return EXIT_END) : return ENTRANCE_END
else
error(f"ConfusedError: Should not be here! Please report this!")
end
Expand Down
Loading
Loading