Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Add thermo mixing ratio functions #1928

Merged
merged 1 commit into from
Jan 14, 2021
Merged
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
2 changes: 2 additions & 0 deletions docs/src/APIs/Common/Thermodynamics.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ liquid_fraction
liquid_ice_pottemp
liquid_ice_pottemp_sat
liquid_specific_humidity
mixing_ratios
moist_static_energy
q_vap_saturation
q_vap_saturation_liquid
Expand All @@ -74,6 +75,7 @@ saturation_adjustment
saturation_excess
saturation_vapor_pressure
soundspeed_air
shum_to_mixing_ratio
specific_enthalpy
specific_volume
supersaturation
Expand Down
40 changes: 40 additions & 0 deletions src/Common/Thermodynamics/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export liquid_fraction, PhasePartition_equil
export dry_pottemp
export virtual_pottemp
export exner
export shum_to_mixing_ratio
export mixing_ratios
export liquid_ice_pottemp
export liquid_ice_pottemp_sat
export relative_humidity
Expand Down Expand Up @@ -2185,6 +2187,44 @@ exner(ts::ThermodynamicState) = exner(
PhasePartition(ts),
)

"""
shum_to_mixing_ratio(q, q_tot)

Mixing ratio, from specific humidity
- `q` specific humidity
- `q_tot` total specific humidity
"""
function shum_to_mixing_ratio(q::FT, q_tot::FT) where {FT <: Real}
return q / (1 - q_tot)
end

"""
mixing_ratios(q::PhasePartition)

Mixing ratios
- `r.tot` total mixing ratio
- `r.liq` liquid mixing ratio
- `r.ice` ice mixing ratio
given a phase partition, `q`.
"""
function mixing_ratios(q::PhasePartition{FT}) where {FT <: Real}
return PhasePartition(
shum_to_mixing_ratio(q.tot, q.tot),
shum_to_mixing_ratio(q.liq, q.tot),
shum_to_mixing_ratio(q.ice, q.tot),
)
end

"""
mixing_ratios(ts::ThermodynamicState)

Mixing ratios stored, in a phase partition, for
- total specific humidity
- liquid specific humidity
- ice specific humidity
"""
mixing_ratios(ts::ThermodynamicState) = mixing_ratios(PhasePartition(ts))

"""
relative_humidity(param_set, T, p, phase_type, q::PhasePartition)

Expand Down
18 changes: 18 additions & 0 deletions test/Common/Thermodynamics/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,20 @@ end
q_tot = FT(0.23)
@test TD.exner_given_pressure(param_set, p, PhasePartition(q_tot)) isa
typeof(p)

q_tot = 0.1
q_liq = 0.05
q_ice = 0.01
mr = shum_to_mixing_ratio(q_tot, q_tot)
@test mr == q_tot / (1 - q_tot)
mr = shum_to_mixing_ratio(q_liq, q_tot)
@test mr == q_liq / (1 - q_tot)

q = PhasePartition(q_tot, q_liq, q_ice)
mrs = mixing_ratios(q)
@test mrs.tot == q_tot / (1 - q_tot)
@test mrs.liq == q_liq / (1 - q_tot)
@test mrs.ice == q_ice / (1 - q_tot)
end


Expand Down Expand Up @@ -1087,6 +1101,10 @@ end
@test PhasePartition(ts_eq).liq ≈ PhasePartition(ts_dry).liq
@test PhasePartition(ts_eq).ice ≈ PhasePartition(ts_dry).ice

@test mixing_ratios(ts_eq).tot ≈ mixing_ratios(ts_dry).tot
@test mixing_ratios(ts_eq).liq ≈ mixing_ratios(ts_dry).liq
@test mixing_ratios(ts_eq).ice ≈ mixing_ratios(ts_dry).ice

ts_dry = PhaseDry.(param_set, e_int, ρ)
ts_eq = PhaseEquil.(param_set, e_int, ρ, q_tot .* 0)

Expand Down