Skip to content

Commit

Permalink
Add overload for single particle momentum
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonReinhard committed Jul 9, 2024
1 parent ddcb164 commit d12a99a
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/interfaces/phase_space_point.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ function momentum(psp::AbstractPhaseSpacePoint, dir::ParticleDirection, n::Int)
end

function _momentum_helper(particles::Tuple{}, species::SPECIES, n::Val{N}) where {SPECIES,N}
throw(BoundsError("requested $species momentum is not in this phase space point"))
throw(
BoundsError(
"momentum(): requested $species momentum is not in this phase space point"
),
)
end

function _momentum_helper(
Expand Down Expand Up @@ -110,6 +114,64 @@ function momentum(
return _momentum_helper(particles(psp, dir), species, n)
end

function _assert_one_particle(
particles::Tuple{AbstractParticleStateful{DIR,SPECIES,EL},Vararg},
dir::DIR,
species::SPECIES,
n::Val{1},
) where {DIR,SPECIES,EL}
throw(
InvalidInputError(
"momentum(): more than one $dir $species exists in this phase space point"
),
)
end

function _assert_one_particle(
particles::Tuple{}, dir::DIR, species::SPECIES, n::Val{0}
) where {DIR,SPECIES}
throw(
InvalidInputError("momentum(): no $dir $species exists in this phase space point")
)
end

function _assert_one_particle(
particles::Tuple{}, dir::DIR, species::SPECIES, n::Val{1}
) where {DIR,SPECIES}
return nothing
end

function _assert_one_particle(
particles::Tuple{AbstractParticleStateful{DIR,SPECIES,EL},Vararg},
dir::DIR,
species::SPECIES,
n::Val{0},
) where {DIR,SPECIES,EL}
return _assert_one_particle(particles[2:end], dir, species, Val(1))
end

function _assert_one_particle(
particles::Tuple{AbstractParticleStateful{DIR,SPECIES,EL},Vararg},
dir::DIR2,
species::SPECIES2,
n::Val{N},
) where {DIR,SPECIES,EL,DIR2,SPECIES2,N}
return _assert_one_particle(particles[2:end], dir, species, n)
end

"""
momentum(psp::AbstractPhaseSpacePoint, dir::ParticleDirection, species::AbstractParticleType)
Returns the momentum of the particle in the given [`AbstractPhaseSpacePoint`](@ref) with `dir` and `species`, *if* there is only one such particle. If there are multiple or none, an [`InvalidInputError`](@ref) is thrown.
"""
function momentum(
psp::AbstractPhaseSpacePoint, dir::ParticleDirection, species::AbstractParticleType
)
_assert_one_particle(particles(psp, dir), dir, species, Val(0))

return momentum(psp, dir, species, Val(1))
end

"""
momentum(psp::AbstractPhaseSpacePoint, dir::ParticleDirection, species::AbstractParticleType, n::Int)
Expand Down

0 comments on commit d12a99a

Please sign in to comment.