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

Refac test implementation #146

Merged
merged 24 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
12 changes: 8 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name = "QEDbase"
uuid = "10e22c08-3ccb-4172-bfcf-7d7aa3d04d93"
authors = ["Uwe Hernandez Acosta <u.hernandez@hzdr.de>", "Simeon Ehrig", "Klaus Steiniger", "Tom Jungnickel", "Anton Reinhard"]
authors = [
"Uwe Hernandez Acosta <u.hernandez@hzdr.de>",
"Simeon Ehrig",
"Klaus Steiniger",
"Tom Jungnickel",
"Anton Reinhard",
]
version = "0.3.0"

[deps]
Expand All @@ -19,18 +25,16 @@ ArgCheck = "2.3.0"
ConstructionBase = "1"
DocStringExtensions = "0.8.5, 0.9"
PhysicalConstants = "0.2.1"
QEDcore = "0.2"
SimpleTraits = "0.9.4"
StaticArrays = "1.2.13"
julia = "1.10"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
QEDcore = "35dc0263-cb5f-4c33-a114-1d7f54ab753e"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"

[targets]
test = ["SafeTestsets", "Test", "Pkg", "Suppressor", "QEDcore"]
test = ["SafeTestsets", "Test", "Pkg", "Suppressor"]
145 changes: 116 additions & 29 deletions test/cross_sections.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
using Random
using QEDbase
using QEDcore

RNG = MersenneTwister(137137)
ATOL = 0.0
RTOL = sqrt(eps())

include("test_implementation/TestImplementation.jl")
TESTMODEL = TestImplementation.TestModel()
TESTPSDEF = TestImplementation.TestPhasespaceDef()
using .TestImplementation

MOM_TYPE = TestMomentum{Float64}
TESTMODEL = TestModel()
TESTPSDEF = TestPhasespaceDef{MOM_TYPE}()

TESTMODEL_FAIL = TestImplementation.TestModel_FAIL()
TESTPSDEF_FAIL = TestImplementation.TestPhasespaceDef_FAIL()

@testset "($N_INCOMING,$N_OUTGOING)" for (N_INCOMING, N_OUTGOING) in Iterators.product(
(1, rand(RNG, 2:8)), (1, rand(RNG, 2:8))
)
INCOMING_PARTICLES = Tuple(rand(RNG, TestImplementation.PARTICLE_SET, N_INCOMING))
OUTGOING_PARTICLES = Tuple(rand(RNG, TestImplementation.PARTICLE_SET, N_OUTGOING))

TESTPROC = TestImplementation.TestProcess(INCOMING_PARTICLES, OUTGOING_PARTICLES)
TESTPROC = TestProcess(INCOMING_PARTICLES, OUTGOING_PARTICLES)

TESTPROC_FAIL_DIFFCS = TestImplementation.TestProcess_FAIL_DIFFCS(
INCOMING_PARTICLES, OUTGOING_PARTICLES
)

# single ps points
p_in_phys = TestImplementation._rand_momenta(RNG, N_INCOMING)
p_in_phys_invalid = TestImplementation._rand_momenta(RNG, N_INCOMING + 1)
p_in_unphys = TestImplementation._rand_in_momenta_failing(RNG, N_INCOMING)
p_in_phys = TestImplementation._rand_momenta(RNG, N_INCOMING, MOM_TYPE)
p_in_phys_invalid = TestImplementation._rand_momenta(RNG, N_INCOMING + 1, MOM_TYPE)
p_in_unphys = TestImplementation._rand_in_momenta_failing(RNG, N_INCOMING, MOM_TYPE)

p_out_phys = TestImplementation._rand_momenta(RNG, N_OUTGOING)
p_out_unphys = TestImplementation._rand_out_momenta_failing(RNG, N_OUTGOING)
p_out_phys = TestImplementation._rand_momenta(RNG, N_OUTGOING, MOM_TYPE)
p_out_unphys = TestImplementation._rand_out_momenta_failing(RNG, N_OUTGOING, MOM_TYPE)

p_in_all = (p_in_phys, p_in_unphys)

Expand All @@ -33,9 +42,77 @@ TESTPSDEF = TestImplementation.TestPhasespaceDef()
# all combinations
p_combs = Iterators.product(p_in_all, p_out_all)

@testset "interface" begin
@testset "incident flux" begin
test_incident_flux = QEDbase._incident_flux(
TestInPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys)
)
groundtruth = TestImplementation._groundtruth_incident_flux(p_in_phys)
@test isapprox(test_incident_flux, groundtruth, atol=ATOL, rtol=RTOL)

test_incident_flux = QEDbase._incident_flux(
TestPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys)
)
@test isapprox(test_incident_flux, groundtruth, atol=ATOL, rtol=RTOL)

#@test_throws MethodError QEDbase._incident_flux(
# OutPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, OUT_PS)
#)
end

@testset "averaging norm" begin
test_avg_norm = QEDbase._averaging_norm(TESTPROC)
groundtruth = TestImplementation._groundtruth_averaging_norm(TESTPROC)
@test isapprox(test_avg_norm, groundtruth, atol=ATOL, rtol=RTOL)
end

@testset "matrix element" begin
test_matrix_element = QEDbase._matrix_element(
TestPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys)
)
groundtruth = TestImplementation._groundtruth_matrix_element(
p_in_phys, p_out_phys
)
@test length(test_matrix_element) == length(groundtruth)
for i in eachindex(test_matrix_element)
@test isapprox(test_matrix_element[i], groundtruth[i], atol=ATOL, rtol=RTOL)
end
end

@testset "is in phasespace" begin
@test @inferred QEDbase._is_in_phasespace(
TestPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys)
)

PSP_unphysical_in_ps = TestPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, p_in_unphys, p_out_phys
)
PSP_unphysical_out_ps = TestPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_unphys
)
PSP_unphysical = TestPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, p_in_unphys, p_out_unphys
)

@test !QEDbase._is_in_phasespace(PSP_unphysical_in_ps)
@test !QEDbase._is_in_phasespace(PSP_unphysical_out_ps)
@test !QEDbase._is_in_phasespace(PSP_unphysical)
end

@testset "phase space factor" begin
test_phase_space_factor = QEDbase._phase_space_factor(
TestPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys)
)
groundtruth = TestImplementation._groundtruth_phase_space_factor(
p_in_phys, p_out_phys
)
@test isapprox(test_phase_space_factor, groundtruth, atol=ATOL, rtol=RTOL)
end
end

@testset "differential cross section" begin
@testset "unsafe compute" begin
PS_POINT = PhaseSpacePoint(
PS_POINT = TestPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys
)

Expand All @@ -49,7 +126,7 @@ TESTPSDEF = TestImplementation.TestPhasespaceDef()

@testset "safe compute" begin
for (P_IN, P_OUT) in p_combs
PS_POINT = PhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, P_IN, P_OUT)
PS_POINT = TestPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, P_IN, P_OUT)

diffCS_on_psp = differential_cross_section(PS_POINT)
groundtruth = TestImplementation._groundtruth_safe_diffCS(
Expand All @@ -59,29 +136,46 @@ TESTPSDEF = TestImplementation.TestPhasespaceDef()
@test isapprox(diffCS_on_psp, groundtruth, atol=ATOL, rtol=RTOL)
end
end

@testset "failed" begin
@testset "$PROC $MODEL" for (PROC, MODEL) in Iterators.product(
(TESTPROC, TESTPROC_FAIL_DIFFCS), (TESTMODEL, TESTMODEL_FAIL)
)
if TestImplementation._any_fail(PROC, MODEL)
for (P_IN, P_OUT) in p_combs
psp = TestPhaseSpacePoint(PROC, MODEL, TESTPSDEF, P_IN, P_OUT)
@test_throws MethodError QEDbase._incident_flux(psp)
@test_throws MethodError QEDbase._averaging_norm(psp)
@test_throws MethodError QEDbase._matrix_element(psp)
end
end

for PS_DEF in (TESTPSDEF, TESTPSDEF_FAIL)
if TestImplementation._any_fail(PROC, MODEL, PS_DEF)
for (P_IN, P_OUT) in p_combs
psp = TestPhaseSpacePoint(PROC, MODEL, PS_DEF, P_IN, P_OUT)
@test_throws MethodError QEDbase._phase_space_factor(psp)
end
end
end
end
end
end

@testset "total cross section" begin
@testset "compute" begin
COORDS_IN = TestImplementation.flat_components(p_in_phys)

IN_PS_POINT = InPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys)
IN_PS_POINT_COORDS = InPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, COORDS_IN
)
IN_PS_POINT = TestInPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys)

groundtruth = TestImplementation._groundtruth_total_cross_section(p_in_phys)
totCS_on_moms = total_cross_section(IN_PS_POINT)
totCS_on_coords = total_cross_section(IN_PS_POINT_COORDS)

@test isapprox(totCS_on_moms, groundtruth, atol=ATOL, rtol=RTOL)
@test isapprox(totCS_on_coords, groundtruth, atol=ATOL, rtol=RTOL)
end
end

@testset "differential probability" begin
@testset "unsafe compute" begin
PS_POINT = PhaseSpacePoint(
PS_POINT = TestPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys
)
prop_on_psp = unsafe_differential_probability(PS_POINT)
Expand All @@ -93,7 +187,7 @@ TESTPSDEF = TestImplementation.TestPhasespaceDef()

@testset "safe compute" begin
for (P_IN, P_OUT) in p_combs
PS_POINT = PhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, P_IN, P_OUT)
PS_POINT = TestPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, P_IN, P_OUT)
prop_on_psp = differential_probability(PS_POINT)
groundtruth = TestImplementation._groundtruth_safe_probability(
TESTPROC, P_IN, P_OUT
Expand All @@ -105,19 +199,12 @@ TESTPSDEF = TestImplementation.TestPhasespaceDef()

@testset "total probability" begin
@testset "compute" begin
COORDS_IN = TestImplementation.flat_components(p_in_phys)

IN_PS_POINT = InPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys)
IN_PS_POINT_COORDS = InPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, COORDS_IN
)
IN_PS_POINT = TestInPhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys)

groundtruth = TestImplementation._groundtruth_total_probability(p_in_phys)
totCS_on_moms = TestImplementation.total_probability(IN_PS_POINT)
totCS_on_coords = TestImplementation.total_probability(IN_PS_POINT_COORDS)

@test isapprox(totCS_on_moms, groundtruth, atol=ATOL, rtol=RTOL)
@test isapprox(totCS_on_coords, groundtruth, atol=ATOL, rtol=RTOL)
end
end
end
18 changes: 10 additions & 8 deletions test/gpu/momentum_map.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
if isempty(GPUS)
@info """No GPU tests are enabled, skipping tests...
To test GPU functionality, please use 'TEST_<GPU> = 1 julia ...' for one of GPU=[CUDA, AMDGPU, METAL, ONEAPI]"""
return nothing
#return nothing
szabo137 marked this conversation as resolved.
Show resolved Hide resolved
end

using Random
using QEDbase, QEDcore
using QEDbase

RNG = MersenneTwister(137137)
ATOL = 0.0
RTOL = sqrt(eps())

MOM_TYPE = TestImplementation.TestMomentum{Float64}

TESTMODEL = TestImplementation.TestModel()
TESTPSDEF = TestImplementation.TestPhasespaceDef()
TESTTRAFO = TestImplementation.TestCoordTrafo()
TESTPSDEF = TestImplementation.TestPhasespaceDef{MOM_TYPE}()
TESTTRAFO = TestImplementation.TestCoordinateTrafo()

@testset "Testing with $GPU_MODULE" for (GPU_MODULE, VECTOR_TYPE) in GPUS
@testset "momentum map" begin
@testset "momenta" begin
test_moms = rand(RNG, SFourMomentum, 100)
test_moms = [MOM_TYPE(rand(RNG, 4)) for _ in 1:100]
gpu_moms = VECTOR_TYPE(test_moms)

test_moms_prime = TESTTRAFO.(test_moms)
Expand All @@ -45,12 +47,12 @@ TESTTRAFO = TestImplementation.TestCoordTrafo()
)

test_psps = [
PhaseSpacePoint(
TestImplementation.TestPhaseSpacePoint(
TESTPROC,
TESTMODEL,
TESTPSDEF,
TestImplementation._rand_momenta(RNG, N_INCOMING),
TestImplementation._rand_momenta(RNG, N_OUTGOING),
TestImplementation._rand_momenta(RNG, N_INCOMING, MOM_TYPE),
TestImplementation._rand_momenta(RNG, N_OUTGOING, MOM_TYPE),
) for _ in 1:100
]
gpu_test_psps = VECTOR_TYPE(test_psps)
Expand Down
27 changes: 15 additions & 12 deletions test/interfaces/coordinate_transforms.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
using Random
using QEDbase
using QEDcore

RNG = MersenneTwister(137137)
ATOL = 0.0
RTOL = sqrt(eps())

include("../test_implementation/TestImplementation.jl")
TESTMODEL = TestImplementation.TestModel()
TESTPSDEF = TestImplementation.TestPhasespaceDef()
#include("../test_implementation/TestImplementation.jl")
using Main.TestImplementation
szabo137 marked this conversation as resolved.
Show resolved Hide resolved

TESTTRAFO = TestImplementation.TestCoordTrafo()
TESTMODEL = TestModel()
TESTPSDEF = TestPhasespaceDef{TestMomentum}()

TESTTRAFO = TestCoordinateTrafo()

@testset "broadcast" begin
test_func(trafo) = trafo
@test test_func.(TESTTRAFO) == TESTTRAFO
end

@testset "single momenta" begin
test_mom = rand(RNG, SFourMomentum)
test_mom = TestMomentum(rand(RNG, 4))

test_mom_prime = @inferred TESTTRAFO(test_mom)

@test isapprox(test_mom_prime, TestImplementation._groundtruth_coord_trafo(test_mom))
end

@testset "set of momenta" begin
test_moms = rand(RNG, SFourMomentum, 3)
test_moms = [TestMomentum(rand(RNG, 4)) for _ in 1:3]
test_moms_prime = TESTTRAFO.(test_moms)

@test isapprox(test_moms_prime, TestImplementation._groundtruth_coord_trafo.(test_moms))
Expand All @@ -39,14 +40,16 @@ end
INCOMING_PARTICLES = Tuple(rand(RNG, TestImplementation.PARTICLE_SET, N_INCOMING))
OUTGOING_PARTICLES = Tuple(rand(RNG, TestImplementation.PARTICLE_SET, N_OUTGOING))

TESTPROC = TestImplementation.TestProcess(INCOMING_PARTICLES, OUTGOING_PARTICLES)
TESTPROC = TestProcess(INCOMING_PARTICLES, OUTGOING_PARTICLES)

p_in_phys = TestImplementation._rand_momenta(RNG, N_INCOMING)
p_out_phys = TestImplementation._rand_momenta(RNG, N_OUTGOING)
p_in_phys = TestImplementation._rand_momenta(RNG, N_INCOMING, TestMomentum)
p_out_phys = TestImplementation._rand_momenta(RNG, N_OUTGOING, TestMomentum)

PS_POINT = PhaseSpacePoint(TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys)
PS_POINT = TestPhaseSpacePoint(
TESTPROC, TESTMODEL, TESTPSDEF, p_in_phys, p_out_phys
)

test_psp_prime = @inferred TESTTRAFO(PS_POINT)
test_psp_prime = TESTTRAFO(PS_POINT)

@test test_psp_prime == TestImplementation._groundtruth_coord_trafo(PS_POINT)
end
Expand Down
11 changes: 5 additions & 6 deletions test/interfaces/model.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using QEDbase

struct TestModel <: AbstractModelDefinition end
QEDbase.fundamental_interaction_type(::TestModel) = :test_interaction

struct TestModel_FAIL <: AbstractModelDefinition end
include("../test_implementation/TestImplementation.jl")
using .TestImplementation: TestModel, TestModel_FAIL

@testset "hard interface" begin
TESTMODEL = TestModel()
@test fundamental_interaction_type(TESTMODEL) == :test_interaction
TESTMODEL = @inferred TestModel()
@test @inferred fundamental_interaction_type(TESTMODEL) ==
TestImplementation._groundtruth_interaction_type()
end

@testset "interface fail" begin
Expand Down
Loading
Loading