From 33859c51cacef1845cfc8a95c9a5ea48f47e052d Mon Sep 17 00:00:00 2001 From: Daniel Matz Date: Sat, 2 Sep 2023 10:53:16 -0500 Subject: [PATCH] Add a Unitful extension --- Project.toml | 7 +++++++ ext/UnitfulExt.jl | 34 ++++++++++++++++++++++++++++++++++ test/Project.toml | 1 + test/runtests.jl | 13 +++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 ext/UnitfulExt.jl diff --git a/Project.toml b/Project.toml index d3b9a52..42296bc 100644 --- a/Project.toml +++ b/Project.toml @@ -3,5 +3,12 @@ uuid = "13e0c4e3-fd7a-51bf-9e22-ec7679f18909" authors = ["Daniel Matz "] version = "0.2.0" +[weakdeps] +Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" + +[extensions] +UnitfulExt = ["Unitful"] + [compat] +Unitful = "1" julia = "1" diff --git a/ext/UnitfulExt.jl b/ext/UnitfulExt.jl new file mode 100644 index 0000000..158b6ca --- /dev/null +++ b/ext/UnitfulExt.jl @@ -0,0 +1,34 @@ +module UnitfulExt + +using COESA +import COESA: + atmosphere, + altitude, + mean_molecular_weight, + temperature, + pressure, + density, + speed_of_sound, + dynamic_viscosity + +using Unitful + +struct UnitfulState + state::COESA.State +end + +altitude(s::UnitfulState) = altitude(s.state) * u"m" +mean_molecular_weight(s::UnitfulState) = mean_molecular_weight(s.state) * u"kg/kmol" +temperature(s::UnitfulState) = temperature(s.state) * u"K" +pressure(s::UnitfulState) = pressure(s.state) * u"Pa" +density(s::UnitfulState) = density(s.state) * u"kg/m^3" +speed_of_sound(s::UnitfulState) = speed_of_sound(s.state) * u"m/s" +dynamic_viscosity(s::UnitfulState) = dynamic_viscosity(s.state) * u"N*s/m^2" + +function atmosphere(Z::Quantity) + Z′ = ustrip(u"m", Z) + state = atmosphere(Z′) + UnitfulState(state) +end + +end diff --git a/test/Project.toml b/test/Project.toml index 737dfda..1368296 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -2,3 +2,4 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" diff --git a/test/runtests.jl b/test/runtests.jl index 67b8c62..e873bb6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,9 @@ using COESA using Test using Aqua using JET +using Unitful + +quantity_type(T, units) = Quantity{T, dimension(units), typeof(units)} M0 = COESA.M0 @@ -239,6 +242,16 @@ M0 = COESA.M0 @test_throws ErrorException dynamic_viscosity(atmos) end + @testset "Unitful Extension" begin + atmos = atmosphere(0u"m") + @test round(quantity_type(Float64, u"kg/kmol"), mean_molecular_weight(atmos), sigdigits = 5) == 28.964u"kg/kmol" + @test round(quantity_type(Float64, u"K"), temperature(atmos), sigdigits = 6) == 288.150u"K" + @test round(quantity_type(Float64, u"Pa"), pressure(atmos), sigdigits = 6) == 101325u"Pa" + @test round(quantity_type(Float64, u"kg/m^3"), density(atmos), sigdigits = 5) == 1.2250u"kg/m^3" + @test round(quantity_type(Float64, u"m/s"), speed_of_sound(atmos), sigdigits = 5) == 340.29u"m/s" + @test round(quantity_type(Float64, u"N*s/m^2"), dynamic_viscosity(atmos), sigdigits = 5) == 1.7894e-5u"N*s/m^2" + end + @testset "Code Quality" begin @testset "Aqua" begin Aqua.test_all(COESA, project_extras = false)