Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect against zero divisions in RH for cold T #230

Merged
merged 1 commit into from
Oct 14, 2024
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
4 changes: 2 additions & 2 deletions src/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,7 @@ vol_vapor_mixing_ratio(param_set, ts::ThermodynamicState) =
"""
relative_humidity(param_set, T, p, phase_type, q::PhasePartition)

The relative humidity, given
The relative humidity (clipped between 0 and 1), given
- `param_set` an `AbstractParameterSet`, see the [`Thermodynamics`](@ref) for more details
- `p` pressure
- `phase_type` a thermodynamic state type
Expand All @@ -2796,7 +2796,7 @@ and, optionally,
q_vap = vapor_specific_humidity(q)
p_vap = q_vap * air_density(param_set, T, p, q) * R_v * T
p_vap_sat = saturation_vapor_pressure(param_set, phase_type, T)
return p_vap / p_vap_sat
return max(FT(0), min(FT(1), p_vap / (p_vap_sat + eps(FT(0)))))
end

"""
Expand Down
13 changes: 13 additions & 0 deletions test/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,19 @@ end
vmrs = vol_vapor_mixing_ratio(param_set, q)
q_vap = vapor_specific_humidity(q)
@test vmrs ≈ _molmass_ratio * shum_to_mixing_ratio(q_vap, q.tot)

# Relative humidity sanity checks
for phase_type in [PhaseDry, PhaseEquil, PhaseNonEquil]
for T in [FT(40), FT(140), FT(240), FT(340), FT(440)]
for p in [FT(1e3), FT(1e4), FT(1e5)]
for q in [FT(-1), FT(1e-45), FT(0), FT(1e-3), FT(10)]
q_pt = PhasePartition(FT(q))
RH = relative_humidity(param_set, T, p, phase_type, q_pt)
@test RH >= FT(0) && RH <= FT(1)
end
end
end
end
end


Expand Down
Loading