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
16 changes: 8 additions & 8 deletions src/bookkeeper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ end
function elegroup_bookkeeper!(ele::Ele, group::Type{BMultipoleParams}, changed::ChangedLedger, previous_ele::Ele)
bmg = ele.BMultipoleParams
cdict = ele.changed
if !has_changed(ele, BMultipoleParams) && !changed.this_ele_length && !changed.ref_group; return; end
if !has_changed(ele, BMultipoleParams) && !changed.this_ele_length && !changed.reference; return; end

ff = ele.pc_ref / (C_LIGHT * charge(ele.species_ref))

Expand Down Expand Up @@ -434,7 +434,7 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{BMultipoleParams}, changed::
end

# Update multipoles if the reference energy has changed.
if changed.ref_group
if changed.reference
if ele.field_master
for mul in bmg.pole
mul.Kn = mul.Bn / ff
Expand All @@ -461,7 +461,7 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{BendParams}, changed::Change
bg = ele.BendParams
cdict = ele.changed

if !has_changed(ele, BendParams) && !changed.this_ele_length && !changed.ref_group; return; end
if !has_changed(ele, BendParams) && !changed.this_ele_length && !changed.reference; return; end

if ele.slave_status == Slave.SUPER
lord = ele.super_lords[1]
Expand Down Expand Up @@ -633,7 +633,7 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{ReferenceParams}, changed::C
drg = ele.DownstreamReferenceParams
cdict = ele.changed

if has_changed(ele, ReferenceParams); changed.ref_group = true; end
if has_changed(ele, ReferenceParams); changed.reference = true; end

if ele.slave_status == Slave.SUPER
lord = ele.super_lords[1]
Expand All @@ -645,7 +645,7 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{ReferenceParams}, changed::C
#

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

Expand Down Expand Up @@ -676,7 +676,7 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{ReferenceParams}, changed::C

# Propagate from previous ele

if !changed.this_ele_length && !changed.ref_group; return; end
if !changed.this_ele_length && !changed.reference; return; end

old_drg = copy(drg)

Expand Down Expand Up @@ -710,7 +710,7 @@ function elegroup_bookkeeper!(ele::Ele, group::Type{ReferenceParams}, changed::C
# End stuff

clear_changed!(ele, ReferenceParams)
changed.ref_group = (old_drg != drg)
changed.reference = (old_drg != drg)

return
end
Expand Down Expand Up @@ -742,7 +742,7 @@ end
function elegroup_bookkeeper!(ele::Ele, group::Type{SolenoidParams}, changed::ChangedLedger, previous_ele::Ele)
sg = ele.SolenoidParams
cdict = ele.changed
if !has_changed(ele, SolenoidParams) && !changed.ref_group; return; end
if !has_changed(ele, SolenoidParams) && !changed.reference; return; end

ff = ele.pc_ref / (C_LIGHT * charge(ele.species_ref))

Expand Down
2 changes: 1 addition & 1 deletion src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function coord_transform(ds::Number, g::Number, tilt_ref::Number = 0.0)

else
angle = ds/g
r_vec = [-ds * un_sinc(0.5*angle) * sin(angle), 0.0, ds * un_sinc(0.5*angle)]
r_vec = ds * [-angle * un_cosc, 0.0, un_sinc(angle)]

qa = rotY(-angle)
if tilt_ref == 0
Expand Down
1 change: 1 addition & 0 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ ELE_PARAM_INFO_DICT = Dict(
:super_lords => ParamInfo(Nothing, Vector{Ele}, "Array of element's super lords. Will not be present if no lords exist."),
:slaves => ParamInfo(Nothing, Vector{Ele}, "Array of slaves of element. Will not be present if no slaves exist."),
:girder => ParamInfo(Nothing, Ele, "Supporting Girder element. Will not be present if no supporting girder."),
:from_forks => ParamInfo(Nothing Vector{Ele}, "List of fork elements that fork to this element."),

:amp_function => ParamInfo(ACKickerParams, Function, "Amplitude function."),

Expand Down
38 changes: 19 additions & 19 deletions src/struct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Element constructor Example:
@ele q1 = Quadrupole(L = 0.2, Ks1 = 0.67, ...)
Result: The variable `q1` is a `Quadrupole` with the argument values put the the appropriate place.

Note: All element parameter groups associated with the element type will be constructed. Thus, in the
Note: All element parameter structs associated with the element type will be constructed. Thus, in the
above example,`q1` above will have `q1.LengthParams` (equivalent to `q1.pdict[:LengthParams]`) created.
"""
macro ele(expr)
Expand Down Expand Up @@ -137,9 +137,9 @@ function (::Type{T})(; kwargs...) where T <: Ele
pdict = ele.pdict
pdict[:changed] = Dict{Union{Symbol,DataType},Any}()

# Setup parameter groups.
for group in PARAM_GROUPS_LIST[typeof(ele)]
pdict[Symbol(group)] = group()
# Setup parameter structs.
for struct in PARAM_GROUPS_LIST[typeof(ele)]
pdict[Symbol(struct)] = struct()
end

# Put name in first in case there are errors and the ele name needs to be printed.
Expand All @@ -149,7 +149,7 @@ function (::Type{T})(; kwargs...) where T <: Ele
pdict[:name] = "Not Set!"
end

# Put parameters in parameter groups and changed area
# Put parameters in parameter structs and changed area
for (sym, val) in kwargs
if sym == :name; continue; end
Base.setproperty!(ele, sym, val)
Expand Down Expand Up @@ -254,13 +254,13 @@ end
"""
Internal: struct EleParamsInfo

Struct holding information on a single `EleParams` group.
Struct holding information on a single `EleParams` struct.
Used in constructing the `ELE_PARAM_GROUP_INFO` Dict.

## Contains
• `description::String` - Descriptive string. \\
• `bookkeeping_needed::Bool - If true, this indicates there exists a bookkeeping function for the \\
parameter group that needs to be called if a parameter of the group is changed. \\
parameter struct that needs to be called if a parameter of the struct is changed. \\
"""
struct EleParamsInfo
description::String
Expand All @@ -275,12 +275,12 @@ end
abstract type EleParams <: BaseEleParams
abstract type EleParameterSubParams <: BaseEleParams

`EleParams` is the base type for all element parameter groups.
`EleParams` is the base type for all element parameter structs.
`EleParameterSubParams` is the base type for structs that are used as components of an element
parameter group.
parameter struct.

To see in which element types contain a given parameter group, use the `info(::EleParams)`
method. To see what parameter groups are contained in a Example:
To see in which element types contain a given parameter struct, use the `info(::EleParams)`
method. To see what parameter structs are contained in a Example:
```
info(BodyShiftParams) # List element types that contain BodyShiftParams
```
Expand Down Expand Up @@ -669,7 +669,7 @@ end
"""
mutable struct DownstreamReferenceParams <: EleParams

Downstream end of element reference energy and species. This group is useful for
Downstream end of element reference energy and species. This struct is useful for
elements where the reference energy or species is not constant.
Elements where this is true include `LCavity`, `Foil`, and `Converter`.

Expand Down Expand Up @@ -825,7 +825,7 @@ end
mutable struct OrientationParams <: EleParams

Position and angular orientation.
In a lattice element, this group gives the orientation at the entrance end of the element
In a lattice element, this struct gives the Floor coordinates at the entrance end of the element
ignoring alignment shifts.

# Fields
Expand Down Expand Up @@ -1018,7 +1018,7 @@ end
"""
mutable struct TwissParams <: EleParams

Lattice element parameter group storing Twiss, dispersion and coupling parameters
Lattice element parameter struct storing Twiss, dispersion and coupling parameters
for an element.
""" TwissParams

Expand All @@ -1035,10 +1035,10 @@ end
"""
abstract type BaseOutput

Abstract type from which output group structs inherit.
Abstract type from which output parameter structs inherit.
AcceleratorLattice defines `OutputParams <: BaseOutput` which is used for output parameters
defined by AcceleratorLattice. Custom output parameters may be defined by defining a new
output group struct and a new `output_parameter` function method.
output parameter struct and a new `output_parameter` function method.

"""
abstract type BaseOutput end
Expand All @@ -1062,7 +1062,7 @@ end
struct AllParams

Struct used for element parameter bookkeeping whose presence represents that parameters
in all parameter groups may have changed.
in all parameter structs may have changed.
""" AllParams

struct AllParams; end
Expand Down Expand Up @@ -1202,14 +1202,14 @@ an element.
# Fields
• `this_ele_length::Bool` - The length of the current element has changed. \\
• `s_position::Bool` - The longitudinal element position has changed. \\
• `ref_group::Bool` - Reference property (species, energy, or time) has changed. \\
• `reference::Bool` - Reference property (species, energy, or time) has changed. \\
• `floor_position::Bool` - The branch coordinate system has changed with respect to the floor coordinates. \\

""" ChangedLedger

@kwdef mutable struct ChangedLedger
this_ele_length::Bool = false
s_position::Bool = false
ref_group::Bool = false
refreference::Bool = false
floor_position::Bool = false
end
5 changes: 4 additions & 1 deletion src/superimpose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ function superimpose!(super_ele::Ele, ref::T; ele_origin::BodyLoc.T = BodyLoc.CE

for lat in lat_list
pop_bookkeeping_state!(lat)
if lat.autobookkeeping; bookkeeper!(lat); end
if lat.autobookkeeping
bookkeeper!(lat)
lat_sanity_check(lat)
end
end

return super_list
Expand Down
76 changes: 71 additions & 5 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,87 @@ Does some self consistency checks on a lattice and throws an error if there is a

function lat_sanity_check(lat::Lattice)
for (ib, branch) in enumerate(lat.branch)
if ib != branch.ix_branch; error("SanityCheck: Branch with branch index: $ib has branch.ix_branch set to $(branch.ix_branch)"); end
if lat !== branch.lat; error("SanityCheck: Branch $ib has branch.lat not pointing to parient lat."); end
if ib != branch.ix_branch
error("SanityCheck: Branch with branch index: $ib has branch.ix_branch set to $(branch.ix_branch)")
end

if !(lat === branch.lat)
error("SanityCheck: Branch $ib has branch.lat not pointing to parient lat.")
end

for (ie, ele) in enumerate(branch.ele)
pdict = ele.pdict


if ie != ele.ix_ele; error("SanityCheck: Ele $(ele.name) in branch $ib with"*
" element index: $ie has ele.ix_ele set to $(ele.ix_ele)"); end

if branch !== ele.branch; error("SanityCheck: Ele $(ele_name(ele)) has ele.branch not pointing to parient branch."); end
if !(branch === ele.branch)
error("SanityCheck: Ele $(ele_name(ele)) has ele.branch not pointing to parient branch.")
end

if branch.type == TrackingBranch && !haskey(pdict, :LengthParams)
error("Sanity check: Ele $(ele_name(ele)) does not have a LengthParams group.")
end

if haskey(pdict, :girder)
lat_sanity_check(lat, ele, pdict[:girder], "girder")
end

if haskey(pdict, :multipass_lord)
lat_sanity_check(lat, ele, pdict[:multipass_lord], "multipass lord")
end

if haskey(pdict, :ForkParams)
lat_sanity_check(lat, ele, pdict[:ForkParams].to_ele, "a forked-to element")
end

if haskey(pdict, :OriginEleParams)
lat_sanity_check(lat, ele, pdict[:OriginEleParams].origin_ele, "the element's origin element")
end

if branch.type == TrackingBranch
if !haskey(ele.pdict, :LengthParams) error("Sanity check: Ele $(ele_name(ele)) does not have a LengthParams group."); end
if haskey(pdict, :super_lords)
for lord in pdict[:super_lords]
lat_sanity_check(lat, ele, lord, "super lord")
end
end

if haskey(pdict, :slaves)
for slave in pdict[:slaves]
lat_sanity_check(lat, ele, slave, "slave")
end
end

if haskey(pdict, :from_forks)
for fork in pdict[:from_forks]
lat_sanity_check(lat, ele, fork, "a fork that is forking to this element")
end
end

if haskey(pdict, :GirderParams)
for slave in pdict[:GirderParams].supported
lat_sanity_check(lat, ele, slave, "supported element")
end
end

end
end
end

#-------------------
# Internal lat_sanity_check method

"""
Internal:

""" lat_sanity_check

function lat_sanity_check(lat::Lattice, base_ele::Ele, pointed_ele::Ele, err_string::String)
pele = lat.branch[pele.branch.ix_branch].ele[pele.ix_ele]
if !(pointed_ele === pele)
error("Element $(ele_name(ele)) has a $err_string pointer to $(ele_name(pele)) but this\n" *
" pointed to element is in a different lattice!!!")
end
end

#---------------------------------------------------------------------------------------------------
Expand Down
Loading