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

Change to RootSolvers tolerance #141

Closed
wants to merge 2 commits into from
Closed
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
64 changes: 42 additions & 22 deletions src/relations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export specific_entropy
export saturated

heavisided(x) = (x > 0) * x
freezing_temperature_tol(tol::RS.ResidualTolerance, ::Type{FT}) where {FT} =
tol.tol
freezing_temperature_tol(tol::RS.AbstractTolerance, ::Type{FT}) where {FT} =
FT(0.1)

"""
gas_constant_air(param_set, [q::PhasePartition])
Expand Down Expand Up @@ -1450,7 +1454,7 @@ end
q_tot,
phase_type,
maxiter,
temperature_tol,
tol,
T_guess,
)

Expand All @@ -1464,7 +1468,10 @@ Compute the temperature that is consistent with
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `maxiter` maximum iterations for non-linear equation solve
- `temperature_tol` temperature tolerance
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `T_guess` initial temperature guess

by finding the root of
Expand All @@ -1483,11 +1490,10 @@ function saturation_adjustment(
q_tot::FT,
::Type{phase_type},
maxiter::Int,
temperature_tol::FT,
tol::RS.AbstractTolerance,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, phase_type <: PhaseEquil}
_T_min::FT = TP.T_min(param_set)
tol = RS.SolutionTolerance(temperature_tol)

T_1 = max(_T_min, air_temperature(param_set, e_int, PhasePartition(q_tot))) # Assume all vapor
q_v_sat = q_vap_saturation(param_set, T_1, ρ, phase_type)
Expand All @@ -1496,6 +1502,7 @@ function saturation_adjustment(
return T_1
end
_T_freeze::FT = TP.T_freeze(param_set)
temperature_tol = freezing_temperature_tol(tol, FT)
e_int_sat(T) =
internal_energy_sat(param_set, heavisided(T), ρ, q_tot, phase_type)
e_int_upper = e_int_sat(_T_freeze + temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
Expand Down Expand Up @@ -1548,7 +1555,7 @@ end
q_tot,
phase_type,
maxiter,
temperature_tol,
tol,
T_guess,
)

Expand All @@ -1562,7 +1569,10 @@ Compute the temperature that is consistent with
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `maxiter` maximum iterations for non-linear equation solve
- `temperature_tol` temperature tolerance
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `T_guess` initial temperature guess

by finding the root of
Expand All @@ -1583,11 +1593,10 @@ function saturation_adjustment_given_peq(
q_tot::FT,
::Type{phase_type},
maxiter::Int,
temperature_tol::FT,
tol::RS.AbstractTolerance,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, phase_type <: PhaseEquil}
_T_min::FT = TP.T_min(param_set)
tol = RS.SolutionTolerance(temperature_tol)

T_1 = max(_T_min, air_temperature(param_set, e_int, PhasePartition(q_tot))) # Assume all vapor
ρ_T(T) = air_density(param_set, T, p, PhasePartition(q_tot))
Expand All @@ -1601,6 +1610,7 @@ function saturation_adjustment_given_peq(
e_int_sat(T) =
internal_energy_sat(param_set, heavisided(T), ρ_T(T), q_tot, phase_type)

temperature_tol = freezing_temperature_tol(tol, FT)
e_int_upper = e_int_sat(_T_freeze + temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
e_int_lower = e_int_sat(_T_freeze - temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
if e_int_lower < e_int < e_int_upper
Expand Down Expand Up @@ -1651,7 +1661,7 @@ end
q_tot,
phase_type,
maxiter,
temperature_tol
tol,
T_guess,
)

Expand All @@ -1665,7 +1675,10 @@ Compute the temperature that is consistent with
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `maxiter` maximum iterations for non-linear equation solve
- `temperature_tol` temperature tolerance
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `T_guess` initial temperature guess

by finding the root of
Expand All @@ -1686,11 +1699,10 @@ function saturation_adjustment_given_phq(
q_tot::FT,
::Type{phase_type},
maxiter::Int,
temperature_tol::FT,
tol::RS.AbstractTolerance,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, phase_type <: PhaseEquil}
_T_min::FT = TP.T_min(param_set)
tol = RS.SolutionTolerance(temperature_tol)

T_1 = max(
_T_min,
Expand All @@ -1712,6 +1724,7 @@ function saturation_adjustment_given_phq(
phase_type,
)

temperature_tol = freezing_temperature_tol(tol, FT)
h_upper = h_sat(_T_freeze + temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
h_lower = h_sat(_T_freeze - temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
if h_lower < h < h_upper
Expand Down Expand Up @@ -1761,7 +1774,7 @@ end
q_tot,
phase_type,
maxiter,
temperature_tol,
tol,
T_guess,
)
Compute the temperature that is consistent with
Expand All @@ -1773,7 +1786,10 @@ Compute the temperature that is consistent with
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `maxiter` maximum iterations for non-linear equation solve
- `temperature_tol` temperature tolerance
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `T_guess` initial temperature guess
by finding the root of

Expand All @@ -1796,10 +1812,9 @@ function saturation_adjustment_ρpq(
q_tot::FT,
::Type{phase_type},
maxiter::Int,
temperature_tol::FT = sqrt(eps(FT)),
tol::RS.AbstractTolerance,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, phase_type <: PhaseEquil}
tol = RS.SolutionTolerance(temperature_tol)
# Use `oftype` to preserve diagonalized type signatures:
sol = RS.find_zero(
T ->
Expand Down Expand Up @@ -1893,7 +1908,8 @@ Compute the temperature `T` that is consistent with
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `maxiter` maximum iterations for non-linear equation solve
- `tol` absolute tolerance for saturation adjustment iterations. Can be one of:
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `T_guess` initial temperature guess
Expand Down Expand Up @@ -1966,7 +1982,7 @@ end
q_tot,
phase_type,
maxiter,
temperature_tol,
tol,
T_guess
)

Expand All @@ -1977,7 +1993,10 @@ Compute the temperature `T` that is consistent with
- `θ_liq_ice` liquid-ice potential temperature
- `q_tot` total specific humidity
- `phase_type` a thermodynamic state type
- `temperature_tol` temperature tolerance
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `maxiter` maximum iterations for non-linear equation solve
- `sat_adjust_method` the numerical method to use.
- `T_guess` initial temperature guess
Expand All @@ -1996,10 +2015,9 @@ function saturation_adjustment_given_pθq(
q_tot::FT,
::Type{phase_type},
maxiter::Int,
temperature_tol::FT,
tol::RS.AbstractTolerance,
T_guess::Union{FT, Nothing} = nothing,
) where {FT <: Real, sat_adjust_method, phase_type <: PhaseEquil}
tol = RS.ResidualTolerance(temperature_tol)
T_min::FT = TP.T_min(param_set)
T_freeze::FT = TP.T_freeze(param_set)
cp_d::FT = TP.cp_d(param_set)
Expand Down Expand Up @@ -2028,6 +2046,7 @@ function saturation_adjustment_given_pθq(
if unsaturated && T_1 > T_min
return T_1
end
temperature_tol = freezing_temperature_tol(tol, FT)
θ_liq_ice_upper = θ_liq_ice_closure(T_freeze + temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
θ_liq_ice_lower = θ_liq_ice_closure(T_freeze - temperature_tol / 2) # /2 => resulting interval is `temperature_tol` wide
if θ_liq_ice_lower < θ_liq_ice < θ_liq_ice_upper
Expand Down Expand Up @@ -2307,7 +2326,8 @@ Computes temperature `T` given
- `param_set` an `AbstractParameterSet`, see the [`Thermodynamics`](@ref) for more details
- `θ_liq_ice` liquid-ice potential temperature
- `ρ` (moist-)air density
- `tol` absolute tolerance for non-linear equation iterations. Can be one of:
- `tol` a tolerance (defined by RootSolvers). Can be one of:
- `RelativeSolutionTolerance()` to stop when `|x_2 - x_1|/x_1 < tol`
- `SolutionTolerance()` to stop when `|x_2 - x_1| < tol`
- `ResidualTolerance()` to stop when `|f(x)| < tol`
- `maxiter` maximum iterations for non-linear equation solve
Expand Down
Loading