Skip to content

Commit

Permalink
Add a Unitful extension
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmatz committed Sep 2, 2023
1 parent a4d99a3 commit 33859c5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@ uuid = "13e0c4e3-fd7a-51bf-9e22-ec7679f18909"
authors = ["Daniel Matz <[email protected]>"]
version = "0.2.0"

[weakdeps]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
UnitfulExt = ["Unitful"]

[compat]
Unitful = "1"
julia = "1"
34 changes: 34 additions & 0 deletions ext/UnitfulExt.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 33859c5

Please sign in to comment.