Skip to content

Commit

Permalink
enhance type stability in ES scattering functions and add correspondi…
Browse files Browse the repository at this point in the history
…ng tests
  • Loading branch information
Christian Haack committed Jan 24, 2025
1 parent 9147994 commit 8f98c9c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/scattering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ end
Einstein-Smoluchowsky Scattering PDF.
"""
function es_scattering(cos_theta::T, b::T) where {T<:Real}
a = 1/(4*pi) * 1 / (1+b/3)
return a*(1+b*cos_theta^2)
a::T = 1/(4*pi) * 1 / (1+b/3)
return T(a*(1+b*cos_theta^2))
end

"""
Expand All @@ -107,8 +107,8 @@ end
Anti-derivative of the ES scattering function.
"""
function es_scattering_integral(cos_theta::T, b::T) where {T<:Real}
a = 1/(4*pi) * 1 / (1+b/3)
return a*cos_theta*(1 + (b*cos_theta^2)/3) * 2*pi
a::T = 1/(4*T(pi)) * 1 / (1+b/3)
return T(a*cos_theta*(1 + (b*cos_theta^2)/3) * 2*pi)
end

"""
Expand All @@ -117,7 +117,7 @@ end
Integral of ES scattering function from -1 to cos_theta
"""
function es_scattering_cumulative(cos_theta::T, b::T) where {T<:Real}
return es_scattering_integral(cos_theta, b) - es_scattering_integral(-1., b)
return es_scattering_integral(cos_theta, b) - es_scattering_integral(-one(T), b)
end

"""
Expand Down
24 changes: 23 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CherenkovMediumBase
using CherenkovMediumBase: es_scattering, es_scattering_integral, es_scattering_cumulative
using Test
using Random

Expand Down Expand Up @@ -115,8 +116,29 @@ end
@testset "EinsteinSmoluchowsky" begin
scattering_function = EinsteinSmoluchowsky(0.835)
@test rand(scattering_function) -0.41788 atol=1e-5
@test @inferred rand(scattering_function) isa Float64
end


@testset "Type Stability" begin

let b = 0.835, cos_theta = 0.5

@test @inferred es_scattering(cos_theta, b) isa Float64
@test @inferred es_scattering_integral(cos_theta, b) isa Float64
@test @inferred es_scattering_cumulative(cos_theta, b) isa Float64
end

let b = 0.835f0, cos_theta = 0.5f0

@test @inferred es_scattering(cos_theta, b) isa Float32
@test @inferred es_scattering_integral(cos_theta, b) isa Float32
@test @inferred es_scattering_cumulative(cos_theta, b) isa Float32
end




end
end

@testset "KopelevichScatteringModel" begin
Expand Down

0 comments on commit 8f98c9c

Please sign in to comment.