diff --git a/src/interfaces/phase_space_point.jl b/src/interfaces/phase_space_point.jl index c90f106..9de4390 100644 --- a/src/interfaces/phase_space_point.jl +++ b/src/interfaces/phase_space_point.jl @@ -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( @@ -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)