From 5c7fe6a1f0e3ec6d8d887f341e2d0a6d7cc0080c Mon Sep 17 00:00:00 2001 From: Anton Reinhard Date: Fri, 17 May 2024 16:27:28 +0200 Subject: [PATCH] Rework printing according to review discussion --- src/models/perturbative_qed.jl | 4 +- src/patch_QEDbase.jl | 22 ++-- src/phase_spaces.jl | 103 +++++++++--------- src/processes/one_photon_compton/process.jl | 22 ++-- test/phase_spaces.jl | 49 ++++++++- .../one_photon_compton/perturbative.jl | 9 ++ test/processes/one_photon_compton/process.jl | 29 +++++ 7 files changed, 159 insertions(+), 79 deletions(-) diff --git a/src/models/perturbative_qed.jl b/src/models/perturbative_qed.jl index b24eaaa..39d47ac 100644 --- a/src/models/perturbative_qed.jl +++ b/src/models/perturbative_qed.jl @@ -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 @@ -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 diff --git a/src/patch_QEDbase.jl b/src/patch_QEDbase.jl index 5e28117..2cc492f 100644 --- a/src/patch_QEDbase.jl +++ b/src/patch_QEDbase.jl @@ -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) diff --git a/src/phase_spaces.jl b/src/phase_spaces.jl index ae961de..48e6c51 100644 --- a/src/phase_spaces.jl +++ b/src/phase_spaces.jl @@ -46,16 +46,6 @@ 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 @@ -63,23 +53,26 @@ struct PhasespaceDefinition{CS<:AbstractCoordinateSystem,F<:AbstractFrameOfRefer 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 @@ -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. @@ -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 @@ -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 @@ -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{ @@ -342,7 +350,6 @@ function momentum(psp::PhaseSpacePoint, dir::ParticleDirection, n::Int) end """ - generate_phase_space( proc::AbstractProcessDefinition, model::AbstractModelDefinition, @@ -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 diff --git a/src/processes/one_photon_compton/process.jl b/src/processes/one_photon_compton/process.jl index c0fa4b9..6d5443e 100644 --- a/src/processes/one_photon_compton/process.jl +++ b/src/processes/one_photon_compton/process.jl @@ -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 { @@ -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 diff --git a/test/phase_spaces.jl b/test/phase_spaces.jl index 09ad1a3..817d6d3 100644 --- a/test/phase_spaces.jl +++ b/test/phase_spaces.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/processes/one_photon_compton/perturbative.jl b/test/processes/one_photon_compton/perturbative.jl index 9d28869..96b51ea 100644 --- a/test/processes/one_photon_compton/perturbative.jl +++ b/test/processes/one_photon_compton/perturbative.jl @@ -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 diff --git a/test/processes/one_photon_compton/process.jl b/test/processes/one_photon_compton/process.jl index 00c4169..8b6ced3 100644 --- a/test/processes/one_photon_compton/process.jl +++ b/test/processes/one_photon_compton/process.jl @@ -6,6 +6,7 @@ POLS = [PolX(), PolY(), AllPol()] SPINS = [SpinUp(), SpinDown(), AllSpin()] POL_AND_SPIN_COMBINATIONS = Iterators.product(SPINS, POLS, SPINS, POLS) POL_COMBINATIONS = Iterators.product(POLS, POLS) +BUF = IOBuffer() @testset "constructor" begin @testset "default" begin @@ -14,6 +15,13 @@ POL_COMBINATIONS = Iterators.product(POLS, POLS) @test QEDprocesses._spin_or_pol(proc, Electron(), Incoming()) == AllSpin() @test QEDprocesses._spin_or_pol(proc, Photon(), Outgoing()) == AllPol() @test QEDprocesses._spin_or_pol(proc, Electron(), Outgoing()) == AllSpin() + + print(BUF, proc) + @test String(take!(BUF)) == "one-photon Compton scattering" + + show(BUF, MIME"text/plain"(), proc) + @test String(take!(BUF)) == + "one-photon Compton scattering\n incoming: electron ($(AllSpin())), photon ($(AllPol()))\n outgoing: electron ($(AllSpin())), photon ($(AllPol()))\n" end @testset "in_pol" begin @@ -23,6 +31,13 @@ POL_COMBINATIONS = Iterators.product(POLS, POLS) @test QEDprocesses._spin_or_pol(proc, Photon(), Incoming()) == pol @test QEDprocesses._spin_or_pol(proc, Electron(), Outgoing()) == AllSpin() @test QEDprocesses._spin_or_pol(proc, Photon(), Outgoing()) == AllPol() + + print(BUF, proc) + @test String(take!(BUF)) == "one-photon Compton scattering" + + show(BUF, MIME"text/plain"(), proc) + @test String(take!(BUF)) == + "one-photon Compton scattering\n incoming: electron ($(AllSpin())), photon ($(pol))\n outgoing: electron ($(AllSpin())), photon ($(AllPol()))\n" end end @@ -33,6 +48,13 @@ POL_COMBINATIONS = Iterators.product(POLS, POLS) @test QEDprocesses._spin_or_pol(proc, Photon(), Incoming()) == in_pol @test QEDprocesses._spin_or_pol(proc, Electron(), Outgoing()) == AllSpin() @test QEDprocesses._spin_or_pol(proc, Photon(), Outgoing()) == out_pol + + print(BUF, proc) + @test String(take!(BUF)) == "one-photon Compton scattering" + + show(BUF, MIME"text/plain"(), proc) + @test String(take!(BUF)) == + "one-photon Compton scattering\n incoming: electron ($(AllSpin())), photon ($(in_pol))\n outgoing: electron ($(AllSpin())), photon ($(out_pol))\n" end end @testset "all spins+pols" begin @@ -44,6 +66,13 @@ POL_COMBINATIONS = Iterators.product(POLS, POLS) @test QEDprocesses._spin_or_pol(proc, Photon(), Incoming()) == in_pol @test QEDprocesses._spin_or_pol(proc, Electron(), Outgoing()) == out_spin @test QEDprocesses._spin_or_pol(proc, Photon(), Outgoing()) == out_pol + + print(BUF, proc) + @test String(take!(BUF)) == "one-photon Compton scattering" + + show(BUF, MIME"text/plain"(), proc) + @test String(take!(BUF)) == + "one-photon Compton scattering\n incoming: electron ($(in_spin)), photon ($(in_pol))\n outgoing: electron ($(out_spin)), photon ($(out_pol))\n" end end end