Skip to content

Commit

Permalink
Add some additional fixes to support Dual numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisYatunin committed Feb 16, 2024
1 parent c54e008 commit bc86a88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Thermodynamics"
uuid = "b60c26fb-14c3-4610-9d3e-2d17fe7ff00c"
authors = ["Climate Modeling Alliance"]
version = "0.12.0"
version = "0.12.1"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
38 changes: 24 additions & 14 deletions src/states.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ end
@inline PhasePartition(q_tot::FT) where {FT <: Real} =
PhasePartition(q_tot, zero(FT), zero(FT))

Base.convert(::Type{PhasePartition{FT}}, q_pt::PhasePartition) where {FT} =
PhasePartition(FT(q_pt.tot), FT(q_pt.liq), FT(q_pt.ice))

function promote_phase_partition(x1, x2, q_pt::PhasePartition)
(x1, x2, tot, liq, ice) = promote(x1, x2, q_pt.tot, q_pt.liq, q_pt.tot)
return (x1, x2, PhasePartition(tot, liq, ice))
end

const ITERTYPE = Union{Int, Nothing}
TOLTYPE(FT) = Union{FT, Nothing}
const TOLTYPE = Union{Real, Nothing}

#####
##### Dry states
Expand Down Expand Up @@ -120,6 +123,9 @@ end

Base.zero(::Type{PhaseDry{FT}}) where {FT} = PhaseDry{FT}(0, 0)

Base.convert(::Type{PhaseDry{FT}}, ts::PhaseDry) where {FT} =
PhaseDry{FT}(ts.e_int, ts.ρ)

"""
PhaseDry_ρe(param_set, ρ, e_int)
Expand Down Expand Up @@ -244,8 +250,7 @@ Constructs a [`PhaseDry`](@ref) thermodynamic state from:
e_int = internal_energy(param_set, T)
return PhaseDry{FT}(e_int, ρ)
end
PhaseDry_ρT(param_set::APS, ρ, θ_dry) =
PhaseDry_ρT(param_set, promote(ρ, θ_dry)...)
PhaseDry_ρT(param_set::APS, ρ, T) = PhaseDry_ρT(param_set, promote(ρ, T)...)

Check warning on line 253 in src/states.jl

View check run for this annotation

Codecov / codecov/patch

src/states.jl#L253

Added line #L253 was not covered by tests

"""
PhaseDry_ρp(param_set, ρ, p)
Expand Down Expand Up @@ -297,6 +302,8 @@ end
@inline Base.zero(::Type{PhaseEquil{FT}}) where {FT} =
PhaseEquil{FT}(0, 0, 0, 0, 0)

Base.convert(::Type{PhaseEquil{FT}}, ts::PhaseEquil) where {FT} =
PhaseEquil{FT}(ts.ρ, ts.p, ts.e_int, ts.q_tot, ts.T)

@inline ifnothing(x, y) = x
@inline ifnothing(x::Nothing, y) = y
Expand All @@ -322,10 +329,10 @@ and, optionally
e_int::FT,
q_tot::FT,
maxiter::IT = nothing,
relative_temperature_tol::FTT = nothing,
relative_temperature_tol::TT = nothing,
::Type{sat_adjust_method} = RS.NewtonsMethod,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, FTT <: TOLTYPE(FT)}
) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, TT <: TOLTYPE}
maxiter = ifnothing(maxiter, 8)
relative_temperature_tol = ifnothing(relative_temperature_tol, FT(1e-4))
phase_type = PhaseEquil{FT}
Expand Down Expand Up @@ -391,9 +398,9 @@ Constructs a [`PhaseEquil`](@ref) thermodynamic state from:
θ_liq_ice::FT,
q_tot::FT,
maxiter::IT = nothing,
relative_temperature_tol::FTT = nothing,
relative_temperature_tol::TT = nothing,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, IT <: ITERTYPE, FTT <: TOLTYPE(FT)}
) where {FT <: Real, IT <: ITERTYPE, TT <: TOLTYPE}
maxiter === nothing && (maxiter = 36)
relative_temperature_tol === nothing &&
(relative_temperature_tol = FT(1e-5))
Expand Down Expand Up @@ -485,10 +492,10 @@ Constructs a [`PhaseEquil`](@ref) thermodynamic state from temperature.
e_int::FT,
q_tot::FT,
maxiter::IT = nothing,
relative_temperature_tol::FTT = nothing,
relative_temperature_tol::TT = nothing,
::Type{sat_adjust_method} = RS.SecantMethod,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, FTT <: TOLTYPE(FT)}
) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, TT <: TOLTYPE}
maxiter === nothing && (maxiter = 40)
relative_temperature_tol === nothing &&
(relative_temperature_tol = FT(1e-4))
Expand Down Expand Up @@ -530,10 +537,10 @@ Constructs a [`PhaseEquil`](@ref) thermodynamic state from temperature.
h::FT,
q_tot::FT,
maxiter::IT = nothing,
relative_temperature_tol::FTT = nothing,
relative_temperature_tol::TT = nothing,
::Type{sat_adjust_method} = RS.SecantMethod,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, FTT <: TOLTYPE(FT)}
) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, TT <: TOLTYPE}
maxiter === nothing && (maxiter = 40)
relative_temperature_tol === nothing &&
(relative_temperature_tol = FT(1e-4))
Expand Down Expand Up @@ -584,7 +591,7 @@ TODO: change input argument order: perform_sat_adjust is
q_tot::FT,
perform_sat_adjust = false,
maxiter::Int = 5,
relative_temperature_tol::FT = FT(sqrt(eps(FT))),
relative_temperature_tol::Real = FT(sqrt(eps(FT))),
::Type{sat_adjust_method} = RS.NewtonsMethodAD,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method}
Expand Down Expand Up @@ -635,10 +642,10 @@ Constructs a [`PhaseEquil`](@ref) thermodynamic state from:
θ_liq_ice::FT,
q_tot::FT,
maxiter::IT = nothing,
relative_temperature_tol::FTT = nothing,
relative_temperature_tol::TT = nothing,
::Type{sat_adjust_method} = RS.SecantMethod,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, IT <: ITERTYPE, FTT <: TOLTYPE(FT), sat_adjust_method}
) where {FT <: Real, IT <: ITERTYPE, TT <: TOLTYPE, sat_adjust_method}
maxiter === nothing && (maxiter = 50)
relative_temperature_tol === nothing &&
(relative_temperature_tol = FT(1e-4))
Expand Down Expand Up @@ -694,6 +701,9 @@ end
@inline Base.zero(::Type{PhaseNonEquil{FT}}) where {FT} =
PhaseNonEquil{FT}(0, 0, zero(PhasePartition{FT}))

Base.convert(::Type{PhaseNonEquil{FT}}, ts::PhaseNonEquil) where {FT} =
PhaseNonEquil{FT}(ts.e_int, ts.ρ, ts.q)

@inline function PhaseNonEquil(
param_set::APS,
e_int::FT,
Expand Down

0 comments on commit bc86a88

Please sign in to comment.