diff --git a/src/accessor.jl b/src/accessor.jl index 71b5d8c..9d98272 100644 --- a/src/accessor.jl +++ b/src/accessor.jl @@ -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) @@ -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) @@ -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 #--------------------------------------------------------------------------------------------------- diff --git a/src/show.jl b/src/show.jl index fddaae6..c28b751 100644 --- a/src/show.jl +++ b/src/show.jl @@ -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 @@ -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 diff --git a/test/superimpose_test.jl b/test/superimpose_test.jl index 107a105..544a2fd 100644 --- a/test/superimpose_test.jl +++ b/test/superimpose_test.jl @@ -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(); @@ -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);