Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scalar broadcasting for Particles, Directions, Spins and Polarization #62

Merged
merged 6 commits into from
May 14, 2024
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
1 change: 1 addition & 0 deletions src/interfaces/particle_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The second type of functions define a hard interface for `AbstractParticle`:
These functions must be implemented in order to have the subtype of `AbstractParticle` work with the functionalities of `QEDprocesses.jl`.
"""
abstract type AbstractParticle end
Base.broadcastable(part::AbstractParticle) = Ref(part)
AntonReinhard marked this conversation as resolved.
Show resolved Hide resolved

"""
$(TYPEDSIGNATURES)
Expand Down
1 change: 1 addition & 0 deletions src/particles/particle_direction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Abstract base type for the directions of particles in the context of processes, i.e. either they are *incoming* or *outgoing*. Subtypes of this are mostly used for dispatch.
"""
abstract type ParticleDirection end
Base.broadcastable(dir::ParticleDirection) = Ref(dir)

"""
Incoming <: ParticleDirection
Expand Down
1 change: 1 addition & 0 deletions src/particles/particle_spin_pol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Abstract base type for the spin or polarization of [`FermionLike`](@ref) or [`BosonLike`](@ref) particles, respectively.
"""
abstract type AbstractSpinOrPolarization end
Base.broadcastable(spin_or_pol::AbstractSpinOrPolarization) = Ref(spin_or_pol)

"""
Abstract base type for the spin of [`FermionLike`](@ref) particles.
Expand Down
30 changes: 30 additions & 0 deletions test/particles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,34 @@ PHIS = (

X, Y, Z = rand(RNG, 3)

# test function to test scalar broadcasting
test_broadcast(x::AbstractParticle) = x
test_broadcast(x::ParticleDirection) = x
test_broadcast(x::AbstractSpinOrPolarization) = x

@testset "scalar broadcasting" begin
@testset "directions" begin
@testset "$dir" for dir in (Incoming(), Outgoing())
@test test_broadcast.(dir) == dir
end
end

@testset "spins and polarization" begin
@testset "$spin_or_pol" for spin_or_pol in (
SpinUp(), SpinDown(), AllSpin(), PolX(), PolY(), AllPol()
)
@test test_broadcast.(spin_or_pol) == spin_or_pol
end
end
end

@testset "fermion likes" begin
@testset "fermion" begin
struct TestFermion <: Fermion end
@test is_fermion(TestFermion())
@test is_particle(TestFermion())
@test !is_anti_particle(TestFermion())
@test test_broadcast.(TestFermion()) == TestFermion()

@testset "$p $d" for (p, d) in
Iterators.product((Electron, Positron), (Incoming, Outgoing))
Expand Down Expand Up @@ -68,13 +90,15 @@ X, Y, Z = rand(RNG, 3)
@test is_fermion(TestAntiFermion())
@test !is_particle(TestAntiFermion())
@test is_anti_particle(TestAntiFermion())
@test test_broadcast.(TestAntiFermion()) == TestAntiFermion()
end

@testset "majorana fermion" begin
struct TestMajoranaFermion <: MajoranaFermion end
@test is_fermion(TestMajoranaFermion())
@test is_particle(TestMajoranaFermion())
@test is_anti_particle(TestMajoranaFermion())
@test test_broadcast.(TestMajoranaFermion()) == TestMajoranaFermion()
end

@testset "electron" begin
Expand All @@ -83,6 +107,7 @@ X, Y, Z = rand(RNG, 3)
@test !is_anti_particle(Electron())
@test mass(Electron()) == 1.0
@test charge(Electron()) == -1.0
@test test_broadcast.(Electron()) == Electron()
end

@testset "positron" begin
Expand All @@ -91,6 +116,7 @@ X, Y, Z = rand(RNG, 3)
@test is_anti_particle(Positron())
@test mass(Positron()) == 1.0
@test charge(Positron()) == 1.0
@test test_broadcast.(Positron()) == Positron()
end
end

Expand All @@ -101,6 +127,7 @@ end
@test is_boson(TestBoson())
@test is_particle(TestBoson())
@test !is_anti_particle(TestBoson())
@test test_broadcast.(TestBoson()) == TestBoson()
end

@testset "antiboson" begin
Expand All @@ -109,6 +136,7 @@ end
@test is_boson(TestAntiBoson())
@test !is_particle(TestAntiBoson())
@test is_anti_particle(TestAntiBoson())
@test test_broadcast.(TestAntiBoson()) == TestAntiBoson()
end

@testset "majorana boson" begin
Expand All @@ -117,6 +145,7 @@ end
@test is_boson(TestMajoranaBoson())
@test is_particle(TestMajoranaBoson())
@test is_anti_particle(TestMajoranaBoson())
@test test_broadcast.(TestMajoranaBoson()) == TestMajoranaBoson()
end
end

Expand All @@ -127,6 +156,7 @@ end
@test is_anti_particle(Photon())
@test charge(Photon()) == 0.0
@test mass(Photon()) == 0.0
@test test_broadcast.(Photon()) == Photon()

@testset "$D" for D in [Incoming, Outgoing]
@testset "$om $cth $phi" for (om, cth, phi) in
Expand Down
Loading