Skip to content

Commit

Permalink
Rework printing according to review discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonReinhard committed May 17, 2024
1 parent 46650f9 commit 5c7fe6a
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 79 deletions.
4 changes: 1 addition & 3 deletions src/models/perturbative_qed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ fundamental_interaction_type(::PerturbativeQED) = :electromagnetic
"""
in_phase_space_dimension(proc::AbstractProcessDefinition, ::PerturbativeQED)
Return the number of degrees of freedom to determine the incoming phase space for processes in PerturbativeQED.
!!! note "Convention"
The current implementation only supports the case where two of the incoming particles collide head-on.
"""
function in_phase_space_dimension(proc::AbstractProcessDefinition, ::PerturbativeQED)
return 3 * number_incoming_particles(proc) - 4 - 1
Expand All @@ -22,7 +20,7 @@ function out_phase_space_dimension(proc::AbstractProcessDefinition, ::Perturbati
return 3 * number_outgoing_particles(proc) - 4
end

function Base.show(io::IO, ::MIME"text/plain", ::PerturbativeQED)
function Base.show(io::IO, ::PerturbativeQED)
print(io, "perturbative QED")
return nothing
end
22 changes: 11 additions & 11 deletions src/patch_QEDbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#############

# fix: https://github.com/QEDjl-project/QEDbase.jl/pull/61
Base.show(io::IO, ::MIME"text/plain", ::Electron) = print(io, "electron")
Base.show(io::IO, ::MIME"text/plain", ::Positron) = print(io, "positron")
Base.show(io::IO, ::MIME"text/plain", ::Photon) = print(io, "photon")
Base.show(io::IO, ::MIME"text/plain", ::Incoming) = print(io, "incoming")
Base.show(io::IO, ::MIME"text/plain", ::Outgoing) = print(io, "outgoing")
Base.show(io::IO, ::MIME"text/plain", ::PolX) = print(io, "x-polarized")
Base.show(io::IO, ::MIME"text/plain", ::PolY) = print(io, "y-polarized")
Base.show(io::IO, ::MIME"text/plain", ::AllPol) = print(io, "all polarizations")
Base.show(io::IO, ::MIME"text/plain", ::SpinUp) = print(io, "spin up")
Base.show(io::IO, ::MIME"text/plain", ::SpinDown) = print(io, "spin down")
Base.show(io::IO, ::MIME"text/plain", ::AllSpin) = print(io, "all spins")
Base.show(io::IO, ::Electron) = print(io, "electron")
Base.show(io::IO, ::Positron) = print(io, "positron")
Base.show(io::IO, ::Photon) = print(io, "photon")
Base.show(io::IO, ::Incoming) = print(io, "incoming")
Base.show(io::IO, ::Outgoing) = print(io, "outgoing")
Base.show(io::IO, ::PolX) = print(io, "x-polarized")
Base.show(io::IO, ::PolY) = print(io, "y-polarized")
Base.show(io::IO, ::AllPol) = print(io, "all polarizations")
Base.show(io::IO, ::SpinUp) = print(io, "spin up")
Base.show(io::IO, ::SpinDown) = print(io, "spin down")
Base.show(io::IO, ::AllSpin) = print(io, "all spins")

# fix: https://github.com/QEDjl-project/QEDbase.jl/pull/62
Broadcast.broadcastable(dir::Incoming) = Ref(dir)
Expand Down
103 changes: 54 additions & 49 deletions src/phase_spaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,33 @@ Broadcast.broadcastable(ps_def::AbstractPhasespaceDefinition) = Ref(ps_def)
PhasespaceDefinition(coord_sys::AbstractCoordinateSystem, frame::AbstractFrameOfReference)
Convenient type to dispatch on coordiante systems and frames of reference.
```jldoctest
julia> using QEDprocesses
julia> PhasespaceDefinition(SphericalCoordinateSystem(), ElectronRestFrame())
phase space in spherical coordinates and electron rest frame
julia> PhasespaceDefinition(SphericalCoordinateSystem(), CenterOfMomentumFrame())
phase space in spherical coordinates and center-of-momentum frame
```
"""
struct PhasespaceDefinition{CS<:AbstractCoordinateSystem,F<:AbstractFrameOfReference} <:
AbstractPhasespaceDefinition
coord_sys::CS
frame::F
end

function Base.show(io::IO, ::MIME"text/plain", ::SphericalCoordinateSystem)
function Base.show(io::IO, ::SphericalCoordinateSystem)
print(io, "spherical coordinates")
return nothing
end
function Base.show(io::IO, ::MIME"text/plain", ::CenterOfMomentumFrame)
function Base.show(io::IO, ::CenterOfMomentumFrame)
print(io, "center-of-momentum frame")
return nothing
end
function Base.show(io::IO, ::MIME"text/plain", ::ElectronRestFrame)
function Base.show(io::IO, ::ElectronRestFrame)
print(io, "electron rest frame")
return nothing
end
function Base.show(io::IO, m::MIME"text/plain", ps_def::PhasespaceDefinition)
print(io, "phase space in ")
show(io, m, ps_def.coord_sys)
print(io, " and ")
show(io, m, ps_def.frame)
println(io, "PhasespaceDefinition")
println(io, " coordinate system: $(ps_def.coord_sys)")
println(io, " frame: $(ps_def.frame)")
return nothing
end
function Base.show(io::IO, ps_def::PhasespaceDefinition)
print(io, "$(ps_def.coord_sys) in $(ps_def.frame)")
return nothing
end

Expand Down Expand Up @@ -146,7 +139,7 @@ Representation of a particle with a state. It has four fields:
- `dir::ParticleDirection`: The direction of the particle, `QEDbase.Incoming()` or `QEDbase.Outgoing()`.
- `species::AbstractParticleType`: The species of the particle, `QEDbase.Electron()`, `QEDbase.Positron()` etc.
- `mom::AbstractFourMomentum`: The momentum of the particle.
- `spin_or_pol::AbstractSpinOrPolarization`: The spin or polarization of the particle, `QEDbase.SpinUp()`, `QEDbase.PolX() etc. Can only use spins with fermions and polarizations with bosons.
- `spin_or_pol::AbstractSpinOrPolarization`: The spin or polarization of the particle, `QEDbase.SpinUp()`, `QEDbase.PolX() etc. Can only use spins with fermions and polarizations with bosons. `AllSpin` or `AllPol` by default.
Overloads for `QEDbase.is_fermion`, `QEDbase.is_boson`, `QEDbase.is_particle`, `QEDbase.is_anti_particle`, `QEDbase.is_incoming`, `QEDbase.is_outgoing`, `QEDbase.mass`, and `QEDbase.charge` are provided, delegating the call to the correct field and thus implementing the `QEDbase.AbstractParticle` interface.
Expand All @@ -156,10 +149,14 @@ The legality of the combination of `species` and `spin_or_pol` is checked on con
julia> using QEDbase; using QEDprocesses
julia> ParticleStateful(Incoming(), Electron(), SFourMomentum(1, 0, 0, 0))
incoming electron: [1.0, 0.0, 0.0, 0.0], all spins
ParticleStateful: incoming electron
spin: all spins
momentum: [1.0, 0.0, 0.0, 0.0]
julia> ParticleStateful(Outgoing(), Photon(), SFourMomentum(1, 0, 0, 0), PolX())
outgoing photon: [1.0, 0.0, 0.0, 0.0], x-polarized
ParticleStateful: outgoing photon
polarization: x-polarized
momentum: [1.0, 0.0, 0.0, 0.0]
```
"""
struct ParticleStateful{ElType<:AbstractFourMomentum} <: AbstractParticle
Expand Down Expand Up @@ -206,12 +203,20 @@ momentum(part::ParticleStateful) = part.mom
particle.spin_or_pol
@inline polarization(particle::ParticleStateful) = _polarization(particle.species, particle)

function Base.show(io::IO, particle::ParticleStateful)
print(
io, "$(particle.dir) $(particle.species) ($(particle.spin_or_pol)): $(particle.mom)"
)
return nothing
end

function Base.show(io::IO, m::MIME"text/plain", particle::ParticleStateful)
show(io, m, particle.dir)
print(io, " ")
show(io, m, particle.species)
print(io, ": $(particle.mom), ")
show(io, m, particle.spin_or_pol)
println(io, "ParticleStateful: $(particle.dir) $(particle.species)")
println(
io,
" $(particle.spin_or_pol isa AbstractSpin ? "spin" : "polarization"): $(particle.spin_or_pol)",
)
println(io, " momentum: $(particle.mom)")
return nothing
end

Expand All @@ -238,13 +243,16 @@ julia> PhaseSpacePoint(
ParticleStateful(Outgoing(), Photon(), SFourMomentum(1, 0, 0, 0))
]
)
Phase space point of 1-photon Compton process (all spins, all polarizations) -> (all spins, all polarizations) using perturbative QED and phase space in spherical coordinates and electron rest frame
Incoming particles:
-> incoming electron: [1.0, 0.0, 0.0, 0.0], all spins
-> incoming photon: [1.0, 0.0, 0.0, 0.0], all polarizations
Outgoing particles
-> outgoing electron: [1.0, 0.0, 0.0, 0.0], all spins
-> outgoing photon: [1.0, 0.0, 0.0, 0.0], all polarizations
PhaseSpacePoint:
process: one-photon Compton scattering
model: perturbative QED
phasespace definition: spherical coordinates in electron rest frame
incoming particles:
-> incoming electron (all spins): [1.0, 0.0, 0.0, 0.0]
-> incoming photon (all polarizations): [1.0, 0.0, 0.0, 0.0]
outgoing particles:
-> outgoing electron (all spins): [1.0, 0.0, 0.0, 0.0]
-> outgoing photon (all polarizations): [1.0, 0.0, 0.0, 0.0]
```
"""
struct PhaseSpacePoint{
Expand Down Expand Up @@ -342,7 +350,6 @@ function momentum(psp::PhaseSpacePoint, dir::ParticleDirection, n::Int)
end

"""
generate_phase_space(
proc::AbstractProcessDefinition,
model::AbstractModelDefinition,
Expand Down Expand Up @@ -381,25 +388,23 @@ function generate_phase_space(
return PhaseSpacePoint(proc, model, ps_def, in_parts, out_parts)
end

function Base.show(io::IO, m::MIME"text/plain", psp::PhaseSpacePoint)
print(io, "Phase space point of ")
show(io, m, psp.proc)
print(io, " using ")
show(io, m, psp.model)
print(io, " and ")
show(io, m, psp.ps_def)
println(io)
println(io, " Incoming particles:")
function Base.show(io::IO, psp::PhaseSpacePoint)
print(io, "PhaseSpacePoint of $(psp.proc)")
return nothing
end

function Base.show(io::IO, ::MIME"text/plain", psp::PhaseSpacePoint)
println(io, "PhaseSpacePoint:")
println(io, " process: $(psp.proc)")
println(io, " model: $(psp.model)")
println(io, " phasespace definition: $(psp.ps_def)")
println(io, " incoming particles:")
for p in psp.in_particles
print(io, " -> ")
show(io, m, p)
println(io)
println(io, " -> $(p)")
end
println(io, " Outgoing particles")
println(io, " outgoing particles:")
for p in psp.out_particles
print(io, " -> ")
show(io, m, p)
println(io)
println(io, " -> $(p)")
end
return nothing
end
22 changes: 9 additions & 13 deletions src/processes/one_photon_compton/process.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""
Compton(
in_spin [= AllSpin()]
in_pol [= AllPol()]
out_spin [= AllSpin()]
out_pol [= AllPol()]
)
)
"""
struct Compton{InElectronSpin,InPhotonPol,OutElectronSpin,OutPhotonPol} <:
AbstractProcessDefinition where {
Expand Down Expand Up @@ -60,15 +58,13 @@ function _spin_or_pol(process::Compton, ::Photon, ::Outgoing)
return process.out_pol
end

function Base.show(io::IO, m::MIME"text/plain", proc::Compton)
print(io, "1-photon Compton process (")
show(io, m, proc.in_spin)
print(io, ", ")
show(io, m, proc.in_pol)
print(io, ") -> (")
show(io, m, proc.out_spin)
print(io, ", ")
show(io, m, proc.out_pol)
print(io, ")")
function Base.show(io::IO, ::Compton)
print(io, "one-photon Compton scattering")
return nothing
end
function Base.show(io::IO, ::MIME"text/plain", proc::Compton)
println(io, "one-photon Compton scattering")
println(io, " incoming: electron ($(proc.in_spin)), photon ($(proc.in_pol))")
println(io, " outgoing: electron ($(proc.out_spin)), photon ($(proc.out_pol))")
return nothing
end
49 changes: 46 additions & 3 deletions test/phase_spaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ TESTMODEL = TestImplementation.TestModel()
TESTPSDEF = TestImplementation.TestPhasespaceDef()

RNG = Random.MersenneTwister(727)
BUF = IOBuffer()

@testset "broadcast" begin
test_func(ps_def) = ps_def
Expand Down Expand Up @@ -40,7 +41,6 @@ end
@test charge(particle_stateful) == charge(species)

# accessors

@test particle_stateful.dir == dir
@test particle_direction(particle_stateful) == particle_stateful.dir
@test particle_stateful.species == species
Expand All @@ -54,6 +54,14 @@ end
@test polarization(particle_stateful) == spin_or_pol
@test_throws MethodError spin(particle_stateful)
end

# printing
print(BUF, particle_stateful)
@test String(take!(BUF)) == "$(dir) $(species) ($(spin_or_pol)): $(mom)"

show(BUF, MIME"text/plain"(), particle_stateful)
@test String(take!(BUF)) ==
"ParticleStateful: $(dir) $(species)\n $(spin_or_pol isa AbstractSpin ? "spin" : "polarization"): $(spin_or_pol)\n momentum: $(mom)\n"
else
if (VERSION >= v"1.8")
# julia versions before 1.8 did not have support for regex matching in @test_throws
Expand Down Expand Up @@ -94,6 +102,15 @@ end
process, model, phasespace_def, in_particles_valid, out_particles_valid
)

print(BUF, psp)
@test String(take!(BUF)) == "PhaseSpacePoint of $(process)"

show(BUF, MIME"text/plain"(), psp)
@test match(
r"PhaseSpacePoint:\n process: (.*)TestProcess(.*)\n model: (.*)TestModel(.*)\n phasespace definition: (.*)TestPhasespaceDef(.*)\n incoming particles:\n -> incoming electron \(all spins\): (.*)\n -> incoming photon \(all polarizations\): (.*)\n outgoing particles:\n -> outgoing electron \(all spins\): (.*)\n -> outgoing photon \(all polarizations\): (.*)\n",
String(take!(BUF)),
) isa RegexMatch

@testset "Accessor" begin
@test momentum(psp, Incoming(), 1) == in_el.mom
@test momentum(psp, Incoming(), 2) == in_ph.mom
Expand All @@ -117,11 +134,11 @@ end
process, model, phasespace_def, in_particles_valid, out_particles_invalid
)

@test_throws r"process given particle species \((.*)Electron\(\)\) does not match stateful particle species \((.*)Photon\(\)\)" PhaseSpacePoint(
@test_throws r"process given particle species \(electron\) does not match stateful particle species \(photon\)" PhaseSpacePoint(
process, model, phasespace_def, SVector(in_ph, in_el), out_particles_valid
)

@test_throws r"process given particle species \((.*)Electron\(\)\) does not match stateful particle species \((.*)Photon\(\)\)" PhaseSpacePoint(
@test_throws r"process given particle species \(electron\) does not match stateful particle species \(photon\)" PhaseSpacePoint(
process, model, phasespace_def, in_particles_valid, SVector(out_ph, out_el)
)
end
Expand Down Expand Up @@ -168,3 +185,29 @@ end
@test test_psp[Outgoing(), 2] == out_ph
end
end

@testset "Coordinate System" begin
@testset "Pretty printing" begin
print(BUF, SphericalCoordinateSystem())
@test String(take!(BUF)) == "spherical coordinates"
end
end
@testset "Reference Frame" begin
@testset "Pretty printing" begin
print(BUF, ElectronRestFrame())
@test String(take!(BUF)) == "electron rest frame"
print(BUF, CenterOfMomentumFrame())
@test String(take!(BUF)) == "center-of-momentum frame"
end
end

@testset "Phasespace Definition" for (coord_sys, frame) in Iterators.product(
[SphericalCoordinateSystem()], [ElectronRestFrame(), CenterOfMomentumFrame()]
)
ps_def = PhasespaceDefinition(coord_sys, frame)

@testset "Pretty printing" begin
print(BUF, ps_def)
@test String(take!(BUF)) == "$coord_sys in $frame"
end
end
9 changes: 9 additions & 0 deletions test/processes/one_photon_compton/perturbative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ const OMEGAS = (1e-6 * rand(RNG), 1e-3 * rand(RNG), rand(RNG), 1e3 * rand(RNG))
const COS_THETAS = [-1.0, 2 * rand(RNG) - 1, 0.0, 1.0]
const PHIS = [0, 2 * pi, rand(RNG) * 2 * pi]

@testset "pretty-printing" begin
buf = IOBuffer()
print(buf, MODEL)
@test String(take!(buf)) == "perturbative QED"

show(buf, MIME"text/plain"(), MODEL)
@test String(take!(buf)) == "perturbative QED"
end

@testset "perturbative kinematics" begin
PROC = Compton()
@testset "momentum generation" begin
Expand Down
Loading

0 comments on commit 5c7fe6a

Please sign in to comment.