Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.
Open
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
12 changes: 11 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ authors = ["Miles Lucas <[email protected]> and contributors"]
version = "0.1.0"

[deps]
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"

[weakdeps]
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
PlanckDynamicQuantitiesExt = "DynamicQuantities"
PlanckUnitfulExt = "Unitful"

[compat]
DynamicQuantities = "1, 1.8"
TestItemRunner = "1.1.0"
Unitful = "1"
julia = "1.6"
julia = "1.10"
34 changes: 34 additions & 0 deletions ext/PlanckDynamicQuantitiesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module PlanckDynamicQuantitiesExt
import Planck

using DynamicQuantities: UnionAbstractQuantity, Dimensions, dimension, uexpand
using DynamicQuantities.Constants: h, c, k_B

using TestItemRunner: @testitem

isfreq(q::UnionAbstractQuantity) = (dimension ∘ uexpand)(q) == Dimensions(time=-1)
islength(q::UnionAbstractQuantity) = (dimension ∘ uexpand)(q) == Dimensions(length=1)

function Planck.blackbody(q::UnionAbstractQuantity, T)
if isfreq(q)
return 2 * h * q^3 / c^2 / expm1(h * q / (k_B * T))
end
if islength(q)
return 2 * h * c^2 / q^5 / expm1(h * c / (q * k_B * T))
end
end

@testitem "Consistent unit conversions" begin
using Planck, DynamicQuantities

@test (ustrip ∘ blackbody)(545us"nm", 6000u"K") ≈ blackbody(545e-9, 6000)
@test (ustrip ∘ blackbody)(500_000us"GHz", 6000u"K") ≈ 3.441e-8 rtol=1e-3
end

@testitem "Known temperatures" begin
using Planck, DynamicQuantities

@test blackbody(545us"nm", 6000u"K") ≈ 3.079e13u"W/m^3" rtol=1e-3
@test blackbody(500_000u"GHz", 6000u"K") ≈ 3.441e-8u"W/m^2/Hz" rtol=1e-3
end
end # end
31 changes: 31 additions & 0 deletions ext/PlanckUnitfulExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module PlanckUnitfulExt
import Planck

using Unitful: AbstractQuantity
using Unitful: 𝐋, 𝐓
using Unitful: h, c0, k

using TestItemRunner: @testitem

function Planck.blackbody(ν::AbstractQuantity{V,inv(𝐓)}, T) where V
2 * h * ν^3 / c0^2 / expm1(h * ν / (k * T))
end

function Planck.blackbody(λ::AbstractQuantity{V,𝐋}, T) where V
2 * h * c0^2 / λ^5 / expm1(h * c0 / (λ * k * T))
end

@testitem "Consistent unit conversions" begin
using Planck, Unitful

@test ustrip(u"W/m^3", blackbody(545u"nm", 6000u"K")) ≈ blackbody(545e-9, 6000)
@test ustrip(u"W/m^2/Hz", blackbody(500u"THz", 6000u"K")) ≈ 3.441e-8 rtol=1e-3
end

@testitem "Known temperatures" begin
using Planck, Unitful

@test blackbody(545u"nm", 6000u"K") ≈ 3.079e13u"W/m^3" rtol=1e-3
@test blackbody(500u"THz", 6000u"K") ≈ 3.441e-8u"W/m^2/Hz" rtol=1e-3
end
end # end
28 changes: 12 additions & 16 deletions src/Planck.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
module Planck

using Unitful
using Unitful: AbstractQuantity
using Unitful: 𝐋, 𝐓
using Unitful: h, c0, k

export blackbody

# constants in SI units
const _h = ustrip(u"J*s", h)
const _c0 = ustrip(u"m/s", c0)
const _k = ustrip(u"J/K", k)
using TestItemRunner: @testitem

# constants in SI units
const _h = 6.62607015e-34
const _c0 = 299792458
const _k = 1.380649e-23

"""
blackbody([OT], x, T)
Expand Down Expand Up @@ -51,18 +47,18 @@ julia> blackbody(u"erg/s/cm^2/nm/sr", 600u"nm", 5850u"K")
"""
blackbody(OT, x, T) = OT(blackbody(x, T))

function blackbody(ν::AbstractQuantity{V,inv(𝐓)}, T) where V
2 * h * ν^3 / c0^2 / expm1(h * ν / (k * T))
end

function blackbody(λ::AbstractQuantity{V,𝐋}, T) where V
2 * h * c0^2 / λ^5 / expm1(h * c0 / (λ * k * T))
end

# SI (meters, Kelvin)
function blackbody(λ, T)
2 * _h * _c0^2 / λ^5 / expm1(_h * _c0 / (λ * _k * T)) # W / m^3
end

@testitem "Known temperatures" begin
using Planck, Unitful

@test blackbody(545e-9, 6000) ≈ 3.079e13 rtol=1e-3
@test blackbody(Float32, 545e-9, 6000) ≈ 3.079f13 rtol=1f-3
end


end # end
6 changes: 6 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[deps]
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
Planck = "49ffdefc-b0d8-4915-a1d4-43900e16f762"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[sources]
Planck = {path = ".."}
18 changes: 2 additions & 16 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
using Planck
using Test
using Unitful
using TestItemRunner

@testset "Planck.jl" begin
@testset "Consistent unit conversions" begin
@test ustrip(u"W/m^3", blackbody(545u"nm", 6000u"K")) ≈ blackbody(545e-9, 6000)
@test ustrip(u"W/m^2/Hz", blackbody(500u"THz", 6000u"K")) ≈ 3.441e-8 rtol=1e-3
end
@testset "Known temperatures" begin
@test blackbody(545e-9, 6000) ≈ 3.079e13 rtol=1e-3
@test blackbody(Float32, 545e-9, 6000) ≈ 3.079f13 rtol=1f-3

@test blackbody(545u"nm", 6000u"K") ≈ 3.079e13u"W/m^3" rtol=1e-3
@test blackbody(500u"THz", 6000u"K") ≈ 3.441e-8u"W/m^2/Hz" rtol=1e-3
end
end
@run_package_tests filter=ti -> occursin("Planck", ti.filename) verbose=true
Loading