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
4 changes: 2 additions & 2 deletions src/AcceleratorLattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export TrackingParams, LengthParams, ReferenceParams, DownstreamReferenceParams,
export MasterParams, LordSlaveStatusParams, ACKickerParams
export GirderParams, PatchParams, RFAutoParams, OutputParams, full_parameter_name
export BeginningParams, Twiss1, Wall2D, Vertex1, InitSpinParams, InitParticleParams, show_changed
export info, ctrl, var, create_external_ele, ele_param_info, units, ele_param_group_syms
export info, ele_param_info, param_units, ele_param_group_syms
export show_group, switch_list_dict, lat_sanity_check, NULL_ELE, NULL_BRANCH, is_null
export ele_param_struct_field_to_user_sym, multipole!, index, integer, rot_angles, Quaternion
export machine_location, body_location, EleRegion, holy_traits, output_parameter
export BranchType, LordBranch, TrackingBranch, MultipassBranch, SuperBranch, transform
export str_split, str_match, str_unquote, str_quote, str_to_int, associated_names
export ELE_PARAM_GROUP_INFO, ELE_TYPE_INFO, PARAM_GROUPS_LIST, OPEN, CLOSED
export DO_NOT_SHOW_PARAMS_LIST, ELE_PARAM_GROUP_INFO, ELE_TYPE_INFO, PARAM_GROUPS_LIST, OPEN, CLOSED
export rotX, rotY, rotZ, rot, rot!, bend_quaternion, lat_ele_dict

# From LinearAlgebra
Expand Down
50 changes: 24 additions & 26 deletions src/accessor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function Base.getproperty(ele::Ele, sym::Symbol)
if !isnothing(pinfo.output_group); return output_parameter(sym, ele, pinfo.output_group); end

symparent = Symbol(pinfo.parent_group)
if !haskey(pdict, symparent); error(f"Cannot find {sym} in element {ele_name(ele)}"); end
if !haskey(pdict, symparent); error("Cannot find $sym in element $(ele_name(ele))"); end

return get_elegroup_param(ele, pdict[symparent], pinfo)
end
Expand Down Expand Up @@ -345,8 +345,8 @@ function get_elegroup_param(ele::Ele, group::Union{BMultipoleParams, EMultipoleP
if mtype[end] == 'L' && !integrated
return val * ele.L
elseif mtype[end] != 'L' && integrated
if ele.L == 0; error(f"Cannot compute non-integrated multipole value {pinfo.user_sym} for" *
f" integrated multipole of element with zero length: {ele_name(ele)}"); end
if ele.L == 0; error("Cannot compute non-integrated multipole value $(pinfo.user_sym) for" *
" integrated multipole of element with zero length: $(ele_name(ele))"); end
return val / ele.L
else
return val
Expand Down Expand Up @@ -478,49 +478,47 @@ function output_parameter(sym::Symbol, ele::Ele, output_group::Type{T}) where T
if :DownstreamReferenceParams ∉ keys(ele.pdict); return NaN; end
return ele.E_tot_ref_downstream / massof(ele.species_ref_downstream)

elseif sym == :q_shift
elseif sym == :q_body
if :BodyShiftParams ∉ keys(ele.pdict); return NaN; end
ag = ele.pdict[:BodyShiftParams]
return Quaternion(ag.x_rot, ag.y_rot, ag.z_rot)

elseif sym == :offset_tot
elseif sym == :offset_body_tot
if :BodyShiftParams ∉ keys(ele.pdict); return NaN; end
if isnothing(girder(ele)); return ele.offset; end
if isnothing(girder(ele)); return ele.offset_body; end
ag = ele.pdict[:BodyShiftParams]
orient_girder = FloorParams(girder(ele).offset_tot, girder(ele).q_shift_tot)
orient_ele = FloorParams(ele.offset, ele.q_shift)
orient_girder = FloorParams(girder(ele).offset_body_tot, girder(ele).q_shift_body_tot)
orient_ele = FloorParams(ele.offset_body, ele.q_body)
return coord_transform(orient_ele, orient_girder).r

elseif sym == :x_rot_tot
elseif sym == :x_rot_body_tot
if :BodyShiftParams ∉ keys(ele.pdict); return NaN; end
if isnothing(girder(ele)); return ele.x_rot; end
if isnothing(girder(ele)); return ele.x_rot_body; end
ag = ele.pdict[:BodyShiftParams]
orient_girder = FloorParams(girder(ele).offset_tot, girder(ele).q_shift_tot)
orient_ele = FloorParams(ele.offset, ele.q_shift)
orient_girder = FloorParams(girder(ele).offset_body_tot, girder(ele).q_body_tot)
orient_ele = FloorParams(ele.offset_body, ele.q_body)
return rot_angles(coord_transform(orient_ele, orient_girder).q)[1]

elseif sym == :y_rot_tot
elseif sym == :y_rot_body_tot
if :BodyShiftParams ∉ keys(ele.pdict); return NaN; end
if isnothing(girder(ele)); return ele.y_rot; end
if isnothing(girder(ele)); return ele.y_rot_body; end
ag = ele.pdict[:BodyShiftParams]
orient_girder = FloorParams(girder(ele).offset_tot, girder(ele).q_shift_tot)
orient_ele = FloorParams(ele.offset, ele.q_shift)
orient_girder = FloorParams(girder(ele).offset_body_tot, girder(ele).q_body_tot)
orient_ele = FloorParams(ele.offset_body, ele.q_body)
return rot_angles(coord_transform(orient_ele, orient_girder).q)[2]

elseif sym == :z_rot_tot
elseif sym == :z_rot_body_tot
if :BodyShiftParams ∉ keys(ele.pdict); return NaN; end
if isnothing(girder(ele)); return ele.z_rot; end
ag = ele.pdict[:BodyShiftParams]
orient_girder = FloorParams(girder(ele).offset_tot, girder(ele).q_shift_tot)
orient_ele = FloorParams(ele.offset, ele.q_shift)
if isnothing(girder(ele)); return ele.z_rot_body; end
orient_girder = FloorParams(girder(ele).offset_body_tot, girder(ele).q_body_tot)
orient_ele = FloorParams(ele.offset_body, ele.q_body)
return rot_angles(coord_transform(orient_ele, orient_girder).q)[3]

elseif sym == :q_shift_tot
elseif sym == :q_body_tot
if :BodyShiftParams ∉ keys(ele.pdict); return NaN; end
if isnothing(girder(ele)); return ele.q_shift; end
ag = ele.pdict[:BodyShiftParams]
orient_girder = FloorParams(girder(ele).offset_tot, girder(ele).q_shift_tot)
orient_ele = FloorParams(ele.offset, ele.q_shift)
if isnothing(girder(ele)); return ele.q_body; end
orient_girder = FloorParams(girder(ele).offset_body_tot, girder(ele).q_body_tot)
orient_ele = FloorParams(ele.offset_body, ele.q_body)
return coord_transform(orient_ele, orient_girder).q
end

Expand Down
12 changes: 6 additions & 6 deletions src/bookkeeper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function param_conflict_check(ele::Ele, syms...)
for sym in syms
if haskey(ele.changed, sym); push!(sym_in, sym); end
end
if length(sym_in) > 1; error(f"Conflict: {s[1]} and {s[2]} cannot both " *
f"be specified for a {typeof(ele)} element: {ele.name}"); end
if length(sym_in) > 1; error("Conflict: $(s[1]) and $(s[2]) cannot both " *
"be specified for a $(typeof(ele)) element: $(ele.name)"); end
return sym_in

return
Expand Down Expand Up @@ -499,12 +499,12 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{BendParams}, changed::Change
L = bg.g * bg.angle
elseif haskey(cdict, :angle) && haskey(cdict, :L_chord)
if bg.L_chord == 0 && bg.angle != 0;
error(f"Bend cannot have finite angle and zero length: {ele_name(ele)}"); end
error("Bend cannot have finite angle and zero length: $(ele_name(ele))"); end
bg.angle == 0 ? bg.g = 0.0 : bg.g = 2.0 * sin(bg.angle/2) / bg.L_chord
L = bg.angle * bg.g
elseif haskey(cdict, :angle)
L = ele.L
if L == 0 && bg.angle != 0; error(f"Bend cannot have finite angle and zero length: {ele_name(ele)}"); end
if L == 0 && bg.angle != 0; error("Bend cannot have finite angle and zero length: $(ele_name(ele))"); end
bg.angle == 0 ? bg.g = 0 : bg.g = bg.angle / L
else
L = ele.L
Expand Down Expand Up @@ -646,13 +646,13 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{ReferenceParams}, changed::C

if is_null(previous_ele) # implies BeginningEle
if !changed.reference; return; end
if rg.species_ref == Species(); error(f"Species not set for first element in branch: {ele_name(ele)}"); end
if rg.species_ref == Species(); error("Species not set for first element in branch: $(ele_name(ele))"); end
drg.species_ref_downstream = rg.species_ref

rg.time_ref_downstream = rg.time_ref + rg.extra_dtime_ref

if count([haskey(cdict, :pc_ref), haskey(cdict, :E_tot_ref), haskey(cdict, :β_ref), haskey(cdict, :γ_ref)]) > 1
error(f"Beginning element has more than one of pc_ref, E_tot_ref, β_ref, and γ_ref set in {ele_name(ele)}")
error("Beginning element has more than one of pc_ref, E_tot_ref, β_ref, and γ_ref set in $(ele_name(ele))")
elseif haskey(cdict, :E_tot_ref)
rg.pc_ref = calc_pc(rg.species_ref, E_tot = rg.E_tot_ref)
elseif haskey(cdict, :pc_ref)
Expand Down
8 changes: 4 additions & 4 deletions src/find.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function ele_at_offset(reference::Union{Ele,Branch}, offset::Int, wrap::Bool)
indx = mod(indx-1, n) + 1
return branch.ele[indx]
else
if indx < 1 || indx > n; error(f"BoundsError: " *
if indx < 1 || indx > n; error("BoundsError: " *
"Element index: $indx out of range in branch $(branch.ix_branch): $(branch.name)"); end
return branch.ele[indx]
end
Expand Down Expand Up @@ -436,22 +436,22 @@ Returns `nothing` if no branch can be matched.
""" lat_branch

function lat_branch(lat::Lattice, ix::Int)
if ix < 1 || ix > length(lat.branch); error(f"Branch index {ix} out of bounds."); end
if ix < 1 || ix > length(lat.branch); error("Branch index $ix out of bounds."); end
return lat.branch[ix]
end

function lat_branch(lat::Lattice, who::AbstractString)
for branch in lat.branch
if branch.name == who; return branch; end
end
error(f"Cannot find branch with name {name} in lattice.")
error("Cannot find branch with name $name in lattice.")
end

function lat_branch(lat::Lattice, who::Type{T}) where T <: BranchType
for branch in lat.branch
if branch.pdict[:type] == who; return branch; end
end
error(f"Cannot find branch with type {name} in lattice.")
error("Cannot find branch with type $name in lattice.")
end

function lat_branch(ele::Ele)
Expand Down
6 changes: 3 additions & 3 deletions src/lat_construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Base.:*(n::Int, beamline::BeamLine) = BeamLine(beamline.id * "_m" * string(n),
[(n > 0 ? beamline : reflect(beamline)) for i in 1:abs(n)], beamline.pdict)

function Base.:*(n::Int, ele::Ele)
if n < 0; error(f"BoundsError: Negative multiplier does not make sense."); end
if n < 0; error("BoundsError: Negative multiplier does not make sense."); end
BeamLine(ele.id * "_m" * string(n), [BeamLineEle(ele) for i in 1:n], false, +1)
end

Expand Down Expand Up @@ -185,7 +185,7 @@ function add_beamline_item_to_branch!(branch::Branch, item::BeamLineItem, info::
elseif item isa BeamLine
add_beamline_line_to_branch!(branch, item, info)
else
error(f"ArgumentError: Beamline item not recognized: {item}")
error("ArgumentError: Beamline item not recognized: $(item)")
end
return nothing
end
Expand All @@ -203,7 +203,7 @@ Used by the `Lattice` function.

function add_beamline_line_to_branch!(branch::Branch, beamline::BeamLine, info::LatticeConstructionInfo)
info.n_loop += 1
if info.n_loop > 100; error(f"InfiniteLoop: Infinite loop of beam lines calling beam lines detected."); end
if info.n_loop > 100; error("InfiniteLoop: Infinite loop of beam lines calling beam lines detected."); end

info = deepcopy(info)
info.orientation_here = info.orientation_here * beamline.pdict[:orientation]
Expand Down
57 changes: 31 additions & 26 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ ELE_PARAM_INFO_DICT = Dict(
:z_rot_body => ParamInfo(BodyShiftParams, Number, "Z-axis body rotation of element.", "rad", nothing, :z_rot),

:q_body => ParamInfo(BodyShiftParams, Quaternion, "Quaternion orientation of body.", "", OutputParams),
:q_tot => ParamInfo(BodyShiftParams, Quaternion, "Quaternion orientation including Girder orientation.", "", OutputParams),
:offset_tot => ParamInfo(BodyShiftParams, Vector{Number}, "[x, y, z] element offset including Girder orientation.", "m", OutputParams),
:x_rot_tot => ParamInfo(BodyShiftParams, Number, "X-axis body rotation including Girder orientation.", "rad", OutputParams),
:y_rot_tot => ParamInfo(BodyShiftParams, Number, "Y-axis body rotation including Girder orientation.", "rad", OutputParams),
:z_rot_tot => ParamInfo(BodyShiftParams, Number, "Z-axis body rotation including Girder orientation.", "rad", OutputParams),
:q_body_tot => ParamInfo(BodyShiftParams, Quaternion, "Quaternion orientation including Girder orientation.", "", OutputParams),
:offset_body_tot => ParamInfo(BodyShiftParams, Vector{Number}, "[x, y, z] element offset including Girder orientation.", "m", OutputParams),
:x_rot_body_tot => ParamInfo(BodyShiftParams, Number, "X-axis body rotation including Girder orientation.", "rad", OutputParams),
:y_rot_body_tot => ParamInfo(BodyShiftParams, Number, "Y-axis body rotation including Girder orientation.", "rad", OutputParams),
:z_rot_body_tot => ParamInfo(BodyShiftParams, Number, "Z-axis body rotation including Girder orientation.", "rad", OutputParams),

:aperture_shape => ParamInfo(ApertureParams, ApertureShape, "Aperture shape. Default is ELLIPTICAL."),
:aperture_at => ParamInfo(ApertureParams, BodyLoc.T, "Aperture location. Default is BodyLoc.ENTRANCE_END."),
Expand Down Expand Up @@ -184,10 +184,10 @@ ELE_PARAM_INFO_DICT = Dict(
:flexible => ParamInfo(PatchParams, Bool, "Flexible patch?"),
:ref_coords => ParamInfo(PatchParams, BodyLoc.T, "Patch coords with respect to BodyLoc.ENTRANCE_END or BodyLoc.EXIT_END?"),

:offset_position => ParamInfo(BodyShiftParams, Vector{Number}, "[x, y, z] offset.", "m", nothing, :offset),
:x_rot_position => ParamInfo(BodyShiftParams, Number, "X-axis rotation.", "rad", nothing, :x_rot),
:y_rot_position => ParamInfo(BodyShiftParams, Number, "Y-axis rotation.", "rad", nothing, :y_rot),
:z_rot_position => ParamInfo(BodyShiftParams, Number, "Z-axis rotation.", "rad", nothing, :z_rot),
:offset_position => ParamInfo(PositionParams, Vector{Number}, "[x, y, z] offset.", "m", nothing, :offset),
:x_rot_position => ParamInfo(PositionParams, Number, "X-axis rotation.", "rad", nothing, :x_rot),
:y_rot_position => ParamInfo(PositionParams, Number, "Y-axis rotation.", "rad", nothing, :y_rot),
:z_rot_position => ParamInfo(PositionParams, Number, "Z-axis rotation.", "rad", nothing, :z_rot),

:species_ref => ParamInfo(ReferenceParams, Species, "Reference species."),
:pc_ref => ParamInfo(ReferenceParams, Number, "Reference momentum * c.", "eV"),
Expand Down Expand Up @@ -247,22 +247,27 @@ end
# associated_names(group::Type{T})

"""
associated_names(group::Type{T}) -> Vector{Symbol}
associated_names(group::Type{T}; exclude_do_not_show::Bool = true) -> Vector{Symbol}

List of names (symbols) of parameters associated with element parameter group struct `group`.
Associated parameters are the fields of the struct plus any associated output parameters.

If the "user name" is different from the group field name, the user name is used.
For example, for a `FloorParams`, `r_floor` will be in the name list instead of `r`.
For example, for a `FloorParams`, `:r_floor` will be in the name list instead of `:r`.

If `exclude_do_not_show` is `true`, parameters in the `DO_NOT_SHOW_PARAMS_LIST` are excluded
from the output list.
""" associated_names

function associated_names(group::Type{T}) where T <: EleParams
function associated_names(group::Type{T}; exclude_do_not_show::Bool = true) where T <: EleParams
names = [field for field in fieldnames(group)]
for (key, pinfo) in ELE_PARAM_INFO_DICT
if pinfo.parent_group != group; continue; end
if pinfo.user_sym ∉ names; push!(names, pinfo.user_sym); end
if pinfo.struct_sym != pinfo.user_sym; deleteat!(names, names .== pinfo.struct_sym); end
end

if exclude_do_not_show; names = setdiff(names, DO_NOT_SHOW_PARAMS_LIST); end
return names
end

Expand Down Expand Up @@ -314,18 +319,18 @@ for (param, info) in ELE_PARAM_INFO_DICT
end

#---------------------------------------------------------------------------------------------------
# units
# param_units

"""
units(param::Union{Symbol,DataType}) -> units::String
units(param::Union{Symbol,DataType}, eletype::Type{T}) where T <: Ele -> units::String
param_units(param::Union{Symbol,DataType}) -> units::String
param_units(param::Union{Symbol,DataType}, eletype::Type{T}) where T <: Ele -> units::String

Returns the units associated with symbol. EG: `m` (meters) for `param` = `:L`.
`param` may be an element parameter group type (EG: `LengthParams`) in which
case `units` returns a blank string.
""" units
case `param_units` returns a blank string.
""" param_units

function units(param::Union{Symbol,DataType})
function param_units(param::Union{Symbol,DataType})
if typeof(param) == DataType; return ""; end

if param in keys(ele_param_struct_field_to_user_sym)
Expand All @@ -341,8 +346,8 @@ end

#-

function units(param::Union{Symbol,DataType}, eletype::Type{T}) where T <: Ele
return units(param)
function param_units(param::Union{Symbol,DataType}, eletype::Type{T}) where T <: Ele
return param_units(param)
end

#---------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -506,7 +511,7 @@ function ele_param_info(who::Union{Symbol,DataType}; throw_error = true)
end

# A DataType means `who` is not an element parameter.
if throw_error; error(f"Unrecognized element parameter: {who}"); end
if throw_error; error("Unrecognized element parameter: $who"); end
return nothing
end

Expand All @@ -525,17 +530,17 @@ function ele_param_info(who::Union{Symbol,DataType}, ele::Ele; throw_error = tru
end
end

error(f"Symbol {who} not in element {ele_name(ele)} which is of type {typeof(ele)}")
error("Symbol $who not in element $(ele_name(ele)) which is of type $(typeof(ele))")

else
if param_info.parent_group in PARAM_GROUPS_LIST[typeof(ele)] ||
param_info.parent_group == Nothing; return param_info; end
error(f"Symbol {who} not in element {ele_name(ele)} which is of type {typeof(ele)}")
error("Symbol $who not in element $(ele_name(ele)) which is of type $(typeof(ele))")
end
end

# A DataType means `who` is not an element parameter.
if throw_error; error(f"Unrecognized element parameter: {who}"); end
if throw_error; error("Unrecognized element parameter: $who"); end
return nothing
end

Expand Down Expand Up @@ -565,7 +570,7 @@ function toggle_integrated!(ele::Ele, ftype::Type{MAGNETIC}, order::Int)
mul.Bs = mul.Bs * L
else
if L == 0
error(f"Cannot convert from integrated multipole to non-integrated for element of zero length: {ele_name(ele)}")
error("Cannot convert from integrated multipole to non-integrated for element of zero length: $(ele_name(ele))")
end
mul.Kn = mul.Kn / L
mul.Ks = mul.Ks / L
Expand All @@ -587,7 +592,7 @@ function toggle_integrated!(ele::Ele, ftype::Type{ELECTRIC}, order::Int)
mul.Es = mul.Es * L
else
if L == 0
error(f"Cannot convert from integrated multipole to non-integrated for element of zero length: {ele_name(ele)}")
error("Cannot convert from integrated multipole to non-integrated for element of zero length: $(ele_name(ele))")
end
mul.En = mul.En / L
mul.Es = mul.Es / L
Expand Down
Loading
Loading