Skip to content
Merged

More. #172

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
23 changes: 18 additions & 5 deletions src/accessor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@ end
# Base.getindex(lat::Lattice, name::AbstractString)

"""
Base.getindex(lat::Lattice}, name::AbstractString)
Base.getindex(lat::Lattice, name::AbstractString)
Base.getindex(lat::Lattice, ix_branch::Int)

If `lat[name]` matches a branch name, return the branch. No wild cards permitted here.
If `lat[name]` does not match a branch name, return list of matching lattice elements.
In this case the returned vector is equivalent to `eles_search(lat, name)`.

For `lat[ix_branch]`, return `lat.branch[ix_branch]`.
""" Base.getindex(lat::Lattice, name::AbstractString)

function Base.getindex(lat::Lattice, name::AbstractString)
Expand All @@ -219,6 +222,11 @@ function Base.getindex(lat::Lattice, name::AbstractString)
return eles_search(lat, name)
end

function Base.getindex(lat::Lattice, ix_branch::Int)
if ix_branch > length(lat.branch); error("Index above length of lat.branch[] array."); end
return lat.branch[ix_branch]
end

#---------------------------------------------------------------------------------------------------
# Base.getindex(branch::Vector{Branch}, name::AbstractString)

Expand All @@ -240,15 +248,20 @@ end
# Base.getindex(branch::Branch, name::AbstractString)

"""
Base.getindex(branch::Branch}, name::AbstractString) -> Ele[]
Base.getindex(branch::Branch, name::AbstractString) -> Ele[]
Base.getindex(branch::Branch, ix_ele::Int) -> Ele

Match `branch[name]` to all lattice elements in `branch.ele[]` array.
""" Base.getindex(branch::Branch, name::AbstractString)
`branch[name]` matches to all lattice elements in `branch.ele[]` array.
`branch[ix_ele]` matches to `branch.ele[ix_ele]`.
""" Base.getindex(branch::Branch, name::AbstractString), Base.getindex(branch::Branch, ix_ele::Int)

function Base.getindex(branch::Branch, name::AbstractString)
return eles_search(branch, name)
end

error(f"No element with name: {name}")
function Base.getindex(branch::Branch, ix_ele::Int)
if ix_ele > length(branch.ele); error("Index above length of branch.ele[] array."); end
return branch.ele[ix_ele]
end

#---------------------------------------------------------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ Two column printing of an element group without any docstring.
""" show_elegroup_wo_doc

function show_elegroup_wo_doc(io::IO, group::BaseEleParameterGroup, ele::Ele; indent = 0, group_show_name::Symbol = :NONE)
# If output field for column 1 or column 2 is wider than this, print the fields on two lines.
col_width_cut = 55

gtype = typeof(group)
if gtype ∉ keys(show_column2)
if group_show_name == :NONE
Expand Down Expand Up @@ -470,11 +473,11 @@ function show_elegroup_wo_doc(io::IO, group::BaseEleParameterGroup, ele::Ele; in
vstr = ele_param_value_str(ele, field2_sym)
str2 = f" {field_name} {vstr} {units(field2_sym)}" # Second column entry.

if length(str) > 50 || length(str2) > 50 # If length is too big print in two lines.
if length(str) > col_width_cut || length(str2) > col_width_cut # If length is too big print in two lines.
println(io, " "^indent * str)
println(io, " "^indent * str2)
else # Can print as a single line.
println(io, " "^indent * f"{rpad(str, 50)}{str2}")
println(io, " "^indent * f"{rpad(str, col_width_cut)}{str2}")
end

else
Expand Down
5 changes: 1 addition & 4 deletions test/superimpose_test.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using AcceleratorLattice, Test

@ele z = Bend()

@eles begin
beginning = BeginningEle(pc_ref = 1e7, species_ref = Species("electron"))
q1 = Quadrupole(L = 0.6, x_rot = 2, ID = "qz", Ks8L = 123, tilt8 = 2, Bn9 = 3,
En1L = 1, Etilt1 = 2, Es2 = 3, Etilt2 = 4)
q2 = Quadrupole(L = 0.6, Kn1 = -0.3);
d1 = Drift(L = 1.0);
lc1 = LCavity(L = 1.0, dE_ref = 2e7, voltage = 10e7);
lc1 = LCavity(L = 1.0, dE_ref = 2e7, voltage = 10e7, extra_dtime_ref = 1e-8);
d3 = Drift(L = 1.0);
m1 = Marker();
m2 = Marker();
Expand All @@ -26,7 +24,6 @@ ln1 = BeamLine([beginning, d1, lc1, d1, d3]);
lat = Lattice([ln1])

sup_zs2 = superimpose!(zs2, eles_search(lat, "d1"), offset = 0.25)

sup_m1 = superimpose!(m1, lat.branch[1], offset = 0, ref_origin = BodyLoc.ENTRANCE_END)
sup_sm1 = superimpose!(zm1, eles_search(lat, "m1"), ref_origin = BodyLoc.ENTRANCE_END);
sup_zm4 = superimpose!(zm4, eles_search(lat, "lc1"), offset = 0.2);
Expand Down
Loading