Skip to content

Commit

Permalink
Reduce number of ^ calls
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Aug 9, 2022
1 parent e6eaddc commit 52feb04
Showing 1 changed file with 66 additions and 38 deletions.
104 changes: 66 additions & 38 deletions src/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1003,41 +1003,39 @@ end
Compute the saturation specific humidity, given a thermodynamic state `ts`.
"""
q_vap_saturation(param_set::APS, ts::ThermodynamicState) = q_vap_saturation(
param_set,
air_temperature(param_set, ts),
air_density(param_set, ts),
typeof(ts),
PhasePartition(param_set, ts),
)
function q_vap_saturation(param_set::APS, ts::ThermodynamicState)
T = air_temperature(param_set, ts)
ρ = air_density(param_set, ts)
q = PhasePartition(param_set, ts)
p_v_sat = saturation_vapor_pressure(param_set, typeof(ts), T, q)
return q_vap_saturation_from_density(param_set, T, ρ, p_v_sat)
end

"""
q_vap_saturation_liquid(param_set::APS, ts::ThermodynamicState)
Compute the saturation specific humidity over liquid,
given a thermodynamic state `ts`.
"""
q_vap_saturation_liquid(param_set::APS, ts::ThermodynamicState) =
q_vap_saturation_generic(
param_set,
air_temperature(param_set, ts),
air_density(param_set, ts),
Liquid(),
)
function q_vap_saturation_liquid(param_set::APS, ts::ThermodynamicState)
T = air_temperature(param_set, ts)
ρ = air_density(param_set, ts)
p_v_sat = saturation_vapor_pressure(param_set, T, Liquid())
return q_vap_saturation_from_density(param_set, T, ρ, p_v_sat)
end

"""
q_vap_saturation_ice(param_set::APS, ts::ThermodynamicState)
Compute the saturation specific humidity over ice,
given a thermodynamic state `ts`.
"""
q_vap_saturation_ice(param_set::APS, ts::ThermodynamicState) =
q_vap_saturation_generic(
param_set,
air_temperature(param_set, ts),
air_density(param_set, ts),
Ice(),
)
function q_vap_saturation_ice(param_set::APS, ts::ThermodynamicState)
T = air_temperature(param_set, ts)
ρ = air_density(param_set, ts)
p_v_sat = saturation_vapor_pressure(param_set, T, Ice())
return q_vap_saturation_from_density(param_set, T, ρ, p_v_sat)
end

"""
q_vap_saturation_from_density(param_set, T, ρ, p_v_sat)
Expand Down Expand Up @@ -1152,14 +1150,26 @@ The saturation excess is the difference between the total specific humidity `q.t
and the saturation specific humidity in equilibrium, and it is defined to be
nonzero only if this difference is positive.
"""
function saturation_excess(
param_set::APS,
T::FT,
ρ::FT,
p_vap_sat::FT,
q::PhasePartition{FT},
) where {FT <: Real, phase_type <: ThermodynamicState}
q_vap_sat = q_vap_saturation_from_density(param_set, T, ρ, p_vap_sat)
return max(0, q.tot - q_vap_sat)
end

function saturation_excess(
param_set::APS,
T::FT,
ρ::FT,
::Type{phase_type},
q::PhasePartition{FT},
) where {FT <: Real, phase_type <: ThermodynamicState}
return max(0, q.tot - q_vap_saturation(param_set, T, ρ, phase_type, q))
p_vap_sat = saturation_vapor_pressure(param_set, phase_type, T)
return saturation_excess(param_set, T, ρ, p_vap_sat, q)
end

"""
Expand Down Expand Up @@ -1262,6 +1272,7 @@ liquid_fraction(param_set::APS, ts::ThermodynamicState) = liquid_fraction(

"""
PhasePartition_equil(param_set, T, ρ, q_tot, phase_type)
PhasePartition_equil(param_set, T, ρ, q_tot, p_vap_sat, liquid_frac)
Partition the phases in equilibrium, returning a [`PhasePartition`](@ref) object using the
[`liquid_fraction`](@ref) function where
Expand All @@ -1271,22 +1282,35 @@ Partition the phases in equilibrium, returning a [`PhasePartition`](@ref) object
- `ρ` (moist-)air density
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `p_vap_sat` saturation vapor pressure
- `liquid_frac` liquid fraction
The residual `q.tot - q.liq - q.ice` is the vapor specific humidity.
"""
function PhasePartition_equil(
param_set::APS,
T::FT,
ρ::FT,
q_tot::FT,
p_vap_sat::FT,
liquid_frac::FT,
) where {FT <: Real}
q_c = saturation_excess(param_set, T, ρ, p_vap_sat, PhasePartition(q_tot)) # condensate specific humidity
q_liq = liquid_frac * q_c # liquid specific humidity
q_ice = (1 - liquid_frac) * q_c # ice specific humidity
return PhasePartition(q_tot, q_liq, q_ice)
end

function PhasePartition_equil(
param_set::APS,
T::FT,
ρ::FT,
q_tot::FT,
::Type{phase_type},
) where {FT <: Real, phase_type <: ThermodynamicState}
_liquid_frac = liquid_fraction(param_set, T, phase_type) # fraction of condensate that is liquid
q_c = saturation_excess(param_set, T, ρ, phase_type, PhasePartition(q_tot)) # condensate specific humidity
q_liq = _liquid_frac * q_c # liquid specific humidity
q_ice = (1 - _liquid_frac) * q_c # ice specific humidity

return PhasePartition(q_tot, q_liq, q_ice)
p_vap_sat = saturation_vapor_pressure(param_set, phase_type, T)
liquid_frac = liquid_fraction(param_set, T, phase_type) # fraction of condensate that is liquid
return PhasePartition_equil(param_set, T, ρ, q_tot, p_vap_sat, liquid_frac)
end

PhasePartition_equil(param_set::APS, ts::AbstractPhaseNonEquil) =
Expand Down Expand Up @@ -1328,13 +1352,16 @@ end

PhasePartition(param_set::APS, ts::AbstractPhaseDry{FT}) where {FT <: Real} =
q_pt_0(FT)
PhasePartition(param_set::APS, ts::AbstractPhaseEquil) = PhasePartition_equil(
param_set,
air_temperature(param_set, ts),
air_density(param_set, ts),
total_specific_humidity(param_set, ts),
typeof(ts),
)
function PhasePartition(param_set::APS, ts::AbstractPhaseEquil)
T = air_temperature(param_set, ts)
ρ = air_density(param_set, ts)
q_tot = total_specific_humidity(param_set, ts)
phase_type = typeof(ts)
p_vap_sat = saturation_vapor_pressure(param_set, phase_type, T)
liquid_frac = liquid_fraction(param_set, T, phase_type) # fraction of condensate that is liquid

return PhasePartition_equil(param_set, T, ρ, q_tot, p_vap_sat, liquid_frac)
end
PhasePartition(param_set::APS, ts::AbstractPhaseNonEquil) = ts.q

function ∂e_int_∂T(
Expand All @@ -1358,11 +1385,12 @@ function ∂e_int_∂T(
T_i::FT = TP.T_icenuc(param_set)
n_i::FT = TP.pow_icenuc(param_set)

q = PhasePartition_equil(param_set, T, ρ, q_tot, phase_type)
p_vap_sat = saturation_vapor_pressure(param_set, phase_type, T)
λ = liquid_fraction(param_set, T, phase_type)
q = PhasePartition_equil(param_set, T, ρ, q_tot, p_vap_sat, λ)
q_c = condensate(q)
cvm = cv_m(param_set, q)
q_vap_sat = q_vap_saturation(param_set, T, ρ, phase_type)
λ = liquid_fraction(param_set, T, phase_type)
q_vap_sat = q_vap_saturation_from_density(param_set, T, ρ, p_vap_sat)
L = λ * LH_v0 + (1 - λ) * LH_s0

∂λ_∂T = (T_i < T < T_f) ? (1 / (T_f - T_i))^n_i * n_i * T^(n_i - 1) : FT(0)
Expand Down

0 comments on commit 52feb04

Please sign in to comment.