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
6 changes: 3 additions & 3 deletions src/AcceleratorLattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ include("find.jl")

# Note! Element types, enums, and Holy traits are exported automatically when constructed.

export memloc, beamline, @ele, @eles, @construct_ele_type, enum, enum_add
export memloc, beamline, @ele, @eles, enum, enum_add, Ele, EleClass
export ele_name, show_name, show_ele, msng, E_kinetic, pc, β, β1, γ
export show_lat, show_branch, show_beamline, bookkeeper!, set_param!
export Branch, Lattice, BeamLineEle, superimpose!, multipole_type
export BeamLineItem, BeamLine, Ele, propagate_ele_geometry, coord_transform
export split!, construct_ele_type, ele_at_s, toggle_integrated!
export split!, ele_at_s, toggle_integrated!
export eles_search, eles_substitute_lords!, eles_sort!
export next_ele, ele_at_offset, ele_param_value_str, strip_AL, ele_param_group_symbols
export branch, matches_branch, create_ele_vars, eval_str, Vertex1, LatticeGlobal
Expand All @@ -78,7 +78,7 @@ export ele_param_struct_field_to_user_sym, multipole!, index, integer, rot_angle
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 DO_NOT_SHOW_PARAMS_LIST, ELE_PARAM_GROUP_INFO, ELE_TYPE_INFO, PARAM_GROUPS_LIST, OPEN, CLOSED
export DO_NOT_SHOW_PARAMS_LIST, ELE_PARAM_GROUP_INFO, PARAM_GROUPS_LIST, OPEN, CLOSED
export rotX, rotY, rotZ, rot, rot!, bend_quaternion, lat_ele_dict

# From LinearAlgebra
Expand Down
17 changes: 11 additions & 6 deletions src/accessor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Also see: `get_elegroup_param`
""" Base.getproperty(ele::Ele, sym::Symbol)

function Base.getproperty(ele::Ele, sym::Symbol)
if sym == :name; return getfield(ele, :name); end
if sym == :class; return getfield(ele, :class); end
if sym == :pdict; return getfield(ele, :pdict); end
pdict::Dict{Symbol,Any} = getfield(ele, :pdict)
branch = lat_branch(ele)
Expand Down Expand Up @@ -147,6 +149,9 @@ end
#-----------------

function Base.setproperty!(ele::Ele, sym::Symbol, value, check_settable = true)
if sym == :name; setfield!(ele, :name, value); return nothing; end
if sym == :class; setfield!(ele, :class, value); return nothing; end

pdict::Dict{Symbol,Any} = ele.pdict
if haskey(pdict, sym); pdict[sym] = value; return pdict[sym]; end
pinfo = ele_param_info(sym, ele)
Expand Down Expand Up @@ -379,16 +384,16 @@ end

function set_elegroup_param!(ele::Ele, group::EleParams, pinfo::ParamInfo, value)
if !isnothing(pinfo.sub_struct) # Example see: ParamInfo(:a_beta)
return setfield!(pinfo.sub_struct(group), pinfo.struct_sym, value)
return setproperty!(pinfo.sub_struct(group), pinfo.struct_sym, value)
else
return setfield!(base_field(group, pinfo), pinfo.struct_sym, value)
return setproperty!(base_field(group, pinfo), pinfo.struct_sym, value)
end
end

function set_elegroup_param!(ele::Ele, group::BMultipoleParams, pinfo::ParamInfo, value)
(mtype, order, group_type) = multipole_type(pinfo.user_sym)
mul = multipole!(group, order, insert = true)
if mtype == "tilt" || mtype == "integrated"; return setfield!(mul, pinfo.struct_sym, value); end
if mtype == "tilt" || mtype == "integrated"; return setproperty!(mul, pinfo.struct_sym, value); end

if isnothing(mul.integrated)
mul.integrated = (mtype[end] == 'L')
Expand All @@ -398,13 +403,13 @@ function set_elegroup_param!(ele::Ele, group::BMultipoleParams, pinfo::ParamInfo
"Use toggle_integrated! to change the integrated status.")
end

return setfield!(mul, pinfo.struct_sym, value)
return setproperty!(mul, pinfo.struct_sym, value)
end

function set_elegroup_param!(ele::Ele, group::EMultipoleParams, pinfo::ParamInfo, value)
(mtype, order, group_type) = multipole_type(pinfo.user_sym)
mul = multipole!(group, order, insert = true)
if mtype == "Etilt" || mtype == "Eintegrated"; return setfield!(mul, pinfo.struct_sym, value); end
if mtype == "Etilt" || mtype == "Eintegrated"; return setproperty!(mul, pinfo.struct_sym, value); end

if isnothing(mul.Eintegrated)
mul.Eintegrated = (mtype[end] == 'L')
Expand All @@ -414,7 +419,7 @@ function set_elegroup_param!(ele::Ele, group::EMultipoleParams, pinfo::ParamInfo
"Use toggle_integrated! to change the integrated status.")
end

return setfield!(mul, pinfo.struct_sym, value)
return setproperty!(mul, pinfo.struct_sym, value)
end

#---------------------------------------------------------------------------------------------------
Expand Down
24 changes: 12 additions & 12 deletions src/bookkeeper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function bookkeeper!(lat::Lattice; init::Bool = false)
end
end

# catch this_err
# catch
# pop_bookkeeping_state!(lat)
# rethrow(this_err)
# rethrow()
# end

pop_bookkeeping_state!(lat)
Expand All @@ -46,7 +46,7 @@ end
# check_if_settable

"""
Internal: check_is_settable(ele::Ele, sym::Symbol, pinfo::Union{ParamInfo, Nothing})
Internal: check_if_settable(ele::Ele, sym::Symbol, pinfo::Union{ParamInfo, Nothing})

Check that it is valid to have varied element parameters.
For example, parameters of a super slave element cannot be directly changed.
Expand Down Expand Up @@ -103,7 +103,7 @@ function bookkeeper_tracking_branch!(branch::Branch; init::Bool = false)

for ele in branch.ele[ix_min:end]
# A UnionEle may not be a super slave.
if typeof(ele) == UnionEle && ele.slave_status == Slave.SUPER
if ele.class == UnionEle && ele.slave_status == Slave.SUPER
bookkeeper_unionele_superslave!(ele, changed, previous_ele)
elseif ele.slave_status == Slave.SUPER
bookkeeper_superslave!(ele, changed, previous_ele)
Expand Down Expand Up @@ -139,14 +139,14 @@ These low level routines (there are several with this signature) are called via
""" bookkeeper_ele!(ele::Ele)

function bookkeeper_ele!(ele::Ele, changed::ChangedLedger, previous_ele::Ele)
for group in PARAM_GROUPS_LIST[typeof(ele)]
for group in PARAM_GROUPS_LIST[ele.class]
if !haskey(ELE_PARAM_GROUP_INFO, group) || !ELE_PARAM_GROUP_INFO[group].bookkeeping_needed; continue; end

try
ele_paramgroup_bookkeeper!(ele, group, changed, previous_ele)
catch this_err
catch
reinstate_changed!(ele, group) # Try to undo the dammage.
rethrow(this_err)
rethrow()
end
end

Expand Down Expand Up @@ -212,13 +212,13 @@ function bookkeeper_superslave!(slave::Ele, changed::ChangedLedger, previous_ele
end

# Transfer info from lord to slave
for group in PARAM_GROUPS_LIST[typeof(lord)]
for group in PARAM_GROUPS_LIST[lord.class]
if group == LengthParams; continue; end # Do not modify length of slave
if group == FloorParams; continue; end
if group == LordSlaveStatusParams; continue; end

group_changed = has_changed(lord, group)
if group_changed && group != AlginmentParams
if group_changed && group != BodyShiftParams
slave.pdict[Symbol(group)] = copy(lord.pdict[Symbol(group)])
slave.pdict[:changed][group] = "changed"
end
Expand Down Expand Up @@ -386,7 +386,7 @@ function param_conflict_check(ele::Ele, syms...)
if haskey(ele.changed, sym); push!(sym_in, sym); end
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
"be specified for a $(ele.class) element: $(ele.name)"); end
return sym_in

return
Expand Down Expand Up @@ -598,10 +598,10 @@ function ele_paramgroup_bookkeeper!(ele::Ele, group::Type{ForkParams},
return
end

# Transfer FloorPrams
# Transfer FloorParams

if has_changed(ele, FloorParams)
to_ele.FloorParams = copy(ele.FloorPrams)
to_ele.FloorParams = copy(ele.FloorParams)
to_ele.pdict[:changed][FloorParams] = true
set_branch_min_max_changed!(to_ele.branch, 1)
end
Expand Down
19 changes: 17 additions & 2 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end
# strip_AL

"""
function strip_AL(who) -> String
strip_AL(who) -> String

Returns a string stripped of prefix "AcceleratorLattice." if the prefix is present.

Expand All @@ -60,6 +60,22 @@ a string with an "AcceleratorLattice." prefix.

strip_AL(who) = replace(string(who), r"^AcceleratorLattice\." => "")

#---------------------------------------------------------------------------------------------------
# root_type

"""
root_type(typ::Union{DataType,UnionAll}) -> Union{DataType,UnionAll}

Returns the root (non-parameterized version) of a parameterized type.
If `typ` is not a parameterized type, this type is returned.

## Examples
- `root_type(T{X})` -> `T`
- `root_type(T)` -> `T`
""" root_type

root_type(typ::Union{DataType,UnionAll}) = Base.typename(typ).wrapper

#---------------------------------------------------------------------------------------------------
# memloc
# "To print memory location of object"
Expand All @@ -68,4 +84,3 @@ function memloc(@nospecialize(x))
y = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), x)
return repr(UInt64(y))
end

68 changes: 67 additions & 1 deletion src/enum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end
"""
holy_traits(atype::AbstractString, values::Vector, descrip::AbstractString = "")

Makes an abstract type from `atype` and makes concrete types (called "values" or "traits")
Makes an abstract type from `atype` and makes abstract types (called "values" or "traits")
from the `values`.
The values inherit from `atype`. This group can be used like an `enum` group.
The difference is that `holy_traits` values can be used with function dispatching.
Expand Down Expand Up @@ -152,3 +152,69 @@ holy_traits("EleGeometry", ["STRAIGHT", "CIRCULAR", "ZERO_LENGTH",
"Element geometry.")

holy_traits("FieldType", ["ELECTRIC", "MAGNETIC"], "Type of field.")

#---------------------------------------------------------------------------------------------------
# EleClass

"""
abstract type EleClass

Abstract class that all Ele class types like `Quadrupole`, `Patch`, etc., inherit from.
""" EleClass

abstract type EleClass end

#---------------------------------------------------------------------------------------------------
# Ele classes

"""
add_ele_class(ele_class::AbstractString, description::AbstractString)

Creates an element class (which is an abstract type) with name given by `ele_class`
and adds documentation for this using `description`.
"""
function add_ele_class(ele_class::AbstractString, description::AbstractString)
eval_str("abstract type $ele_class <: EleClass end")
eval_str("export $ele_class")

global doc_str = """
abstract type $ele_class <: EleClass
Element class instance used for simulating: $description
"""

eval_str("@doc doc_str $ele_class")
end


add_ele_class("ACKicker", "Time varying kicker.")
add_ele_class("BeamBeam", "Colliding beams.")
add_ele_class("BeginningEle", "Initial element at start of a branch.")
add_ele_class("Bend", "Dipole bend.")
add_ele_class("Collimator", "Collimation element.")
add_ele_class("Converter", "Target to produce new species. EG: Positron converter.")
add_ele_class("CrabCavity", "RF crab cavity.")
add_ele_class("Drift", "Field free region.")
add_ele_class("EGun", "Electron gun.")
add_ele_class("Fiducial", "Floor coordinate system fiducial point.")
add_ele_class("FloorShift", "Floor coordinates shift.")
add_ele_class("Foil", "Strips electrons from an atom.")
add_ele_class("Fork", "Connect lattice branches together.")
add_ele_class("Girder", "Support element.")
add_ele_class("Instrument", "Measurement element.")
add_ele_class("Kicker", "Particle kicker element.")
add_ele_class("LCavity", "Linac accelerating RF cavity.")
add_ele_class("Marker", "Zero length element to mark a particular position.")
add_ele_class("Match", "Orbit, Twiss, and dispersion matching element.")
add_ele_class("Multipole", "Zero length multipole.")
add_ele_class("NullEle", "Placeholder element type used for bookkeeping. Indicates the absence of any valid element.")
add_ele_class("Octupole", "Octupole element.")
add_ele_class("Patch", "Reference orbit shift.")
add_ele_class("Quadrupole", "Quadrupole element.")
add_ele_class("RFCavity", "RF cavity element.")
add_ele_class("Sextupole", "Sextupole element.")
add_ele_class("Solenoid", "Solenoid.")
add_ele_class("Taylor", "General Taylor map element.")
add_ele_class("Undulator", "Undulator.")
add_ele_class("UnionEle", "Container element for overlapping elements.")
add_ele_class("Wiggler", "Wiggler.")

Loading
Loading