diff --git a/src/relations.jl b/src/relations.jl index 599c3aef..ee41de65 100644 --- a/src/relations.jl +++ b/src/relations.jl @@ -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]) @@ -1450,7 +1454,7 @@ end q_tot, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) @@ -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 @@ -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) @@ -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 @@ -1548,7 +1555,7 @@ end q_tot, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) @@ -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 @@ -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)) @@ -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 @@ -1651,7 +1661,7 @@ end q_tot, phase_type, maxiter, - temperature_tol + tol, T_guess, ) @@ -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 @@ -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, @@ -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 @@ -1761,7 +1774,7 @@ end q_tot, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) Compute the temperature that is consistent with @@ -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 @@ -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 -> @@ -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 @@ -1966,7 +1982,7 @@ end q_tot, phase_type, maxiter, - temperature_tol, + tol, T_guess ) @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/src/states.jl b/src/states.jl index 2990def3..a5acd241 100644 --- a/src/states.jl +++ b/src/states.jl @@ -259,7 +259,7 @@ struct PhaseEquil{FT} <: AbstractPhaseEquil{FT} end """ - PhaseEquil_ρeq(param_set, ρ, e_int, q_tot[, maxiter, temperature_tol, sat_adjust_method, T_guess]) + PhaseEquil_ρeq(param_set, ρ, e_int, q_tot[, maxiter, tol, sat_adjust_method, T_guess]) Moist thermodynamic phase, given - `param_set` an `AbstractParameterSet`, see the [`Thermodynamics`](@ref) for more details @@ -268,7 +268,10 @@ Moist thermodynamic phase, given - `q_tot` total specific humidity and, optionally - `maxiter` maximum iterations for saturation adjustment - - `temperature_tol` temperature tolerance for saturation adjustment + - `tol` a tolerance (defined by RootSolvers) for saturation adjustment. 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` - `sat_adjust_method` the numerical method to use. See the [`Thermodynamics`](@ref) for options. - `T_guess` initial guess for temperature in saturation adjustment @@ -279,12 +282,12 @@ function PhaseEquil_ρeq( e_int::FT, q_tot::FT, maxiter::IT = nothing, - temperature_tol::FTT = nothing, + tol::Union{Nothing, RS.AbstractTolerance} = nothing, ::Type{sat_adjust_method} = RS.NewtonsMethod, T_guess::Union{FT, Nothing} = nothing, ) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, FTT <: TOLTYPE(FT)} maxiter === nothing && (maxiter = 8) - temperature_tol === nothing && (temperature_tol = FT(1e-1)) + tol === nothing && (tol = RS.RelativeSolutionTolerance(FT(1e-4))) phase_type = PhaseEquil{FT} q_tot_safe = clamp(q_tot, FT(0), FT(1)) T = saturation_adjustment( @@ -295,7 +298,7 @@ function PhaseEquil_ρeq( q_tot_safe, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) q_pt = PhasePartition_equil(param_set, T, ρ, q_tot_safe, phase_type) @@ -304,8 +307,7 @@ function PhaseEquil_ρeq( end # Convenience method for comparing Numerical -# methods without having to specify maxiter -# and temperature_tol. maxiter and temperature_tol +# methods without having to specify maxiter. maxiter # should be in sync with the PhaseEquil(...) constructor function PhaseEquil_dev_only( param_set::APS, @@ -313,7 +315,7 @@ function PhaseEquil_dev_only( e_int::FT, q_tot::FT; maxiter::Int = 8, - temperature_tol::FT = FT(1e-1), + tol::Union{Nothing, RS.AbstractTolerance} = nothing, sat_adjust_method::Type{NM} = RS.NewtonsMethod, ) where {FT <: Real, NM} return PhaseEquil_ρeq( @@ -322,13 +324,13 @@ function PhaseEquil_dev_only( e_int, q_tot, maxiter, - temperature_tol, + tol, sat_adjust_method, ) end """ - PhaseEquil_ρθq(param_set, ρ, θ_liq_ice, q_tot[, maxiter, temperature_tol, sat_adjust_method, T_guess]) + PhaseEquil_ρθq(param_set, ρ, θ_liq_ice, q_tot[, maxiter, tol, sat_adjust_method, T_guess]) Constructs a [`PhaseEquil`](@ref) thermodynamic state from: @@ -336,7 +338,10 @@ Constructs a [`PhaseEquil`](@ref) thermodynamic state from: - `ρ` (moist-)air density - `θ_liq_ice` liquid-ice potential temperature - `q_tot` total specific humidity - - `temperature_tol` temperature tolerance for saturation adjustment + - `tol` a tolerance (defined by RootSolvers) for saturation adjustment. 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 saturation adjustment - `T_guess` initial guess for temperature in saturation adjustment """ @@ -346,13 +351,12 @@ function PhaseEquil_ρθq( θ_liq_ice::FT, q_tot::FT, maxiter::IT = nothing, - temperature_tol::FTT = nothing, + tol::Union{Nothing, RS.AbstractTolerance} = nothing, T_guess::Union{FT, Nothing} = nothing, ) where {FT <: Real, IT <: ITERTYPE, FTT <: TOLTYPE(FT)} maxiter === nothing && (maxiter = 36) - temperature_tol === nothing && (temperature_tol = FT(1e-1)) + tol === nothing && (tol = RS.RelativeSolutionTolerance(FT(1e-5))) phase_type = PhaseEquil{FT} - tol = RS.ResidualTolerance(temperature_tol) T = saturation_adjustment_given_ρθq( param_set, ρ, @@ -417,7 +421,7 @@ function PhaseEquil_pTq( end """ - PhaseEquil_peq(param_set, p, e_int, q_tot[, maxiter, temperature_tol, sat_adjust_method, T_guess]) + PhaseEquil_peq(param_set, p, e_int, q_tot[, maxiter, tol, sat_adjust_method, T_guess]) Constructs a [`PhaseEquil`](@ref) thermodynamic state from temperature. @@ -433,12 +437,12 @@ function PhaseEquil_peq( e_int::FT, q_tot::FT, maxiter::IT = nothing, - temperature_tol::FTT = nothing, + tol::Union{Nothing, RS.AbstractTolerance} = nothing, ::Type{sat_adjust_method} = RS.SecantMethod, T_guess::Union{FT, Nothing} = nothing, ) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, FTT <: TOLTYPE(FT)} maxiter === nothing && (maxiter = 40) - temperature_tol === nothing && (temperature_tol = FT(1e-2)) + tol === nothing && (tol = RS.RelativeSolutionTolerance(FT(1e-4))) phase_type = PhaseEquil{FT} q_tot_safe = clamp(q_tot, FT(0), FT(1)) T = saturation_adjustment_given_peq( @@ -449,7 +453,7 @@ function PhaseEquil_peq( q_tot_safe, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) q_pt = PhasePartition_equil_given_p(param_set, T, p, q_tot_safe, phase_type) @@ -459,7 +463,7 @@ end """ - PhaseEquil_phq(param_set, p, h, q_tot[, maxiter, temperature_tol, sat_adjust_method, T_guess]) + PhaseEquil_phq(param_set, p, h, q_tot[, maxiter, tol, sat_adjust_method, T_guess]) Constructs a [`PhaseEquil`](@ref) thermodynamic state from temperature. @@ -475,12 +479,12 @@ function PhaseEquil_phq( h::FT, q_tot::FT, maxiter::IT = nothing, - temperature_tol::FTT = nothing, + tol::Union{Nothing, RS.AbstractTolerance} = nothing, ::Type{sat_adjust_method} = RS.SecantMethod, T_guess::Union{FT, Nothing} = nothing, ) where {FT <: Real, sat_adjust_method, IT <: ITERTYPE, FTT <: TOLTYPE(FT)} maxiter === nothing && (maxiter = 40) - temperature_tol === nothing && (temperature_tol = FT(1e-2)) + tol === nothing && (tol = RS.RelativeSolutionTolerance(FT(1e-4))) phase_type = PhaseEquil{FT} q_tot_safe = clamp(q_tot, FT(0), FT(1)) T = saturation_adjustment_given_phq( @@ -491,7 +495,7 @@ function PhaseEquil_phq( q_tot_safe, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) q_pt = PhasePartition_equil_given_p(param_set, T, p, q_tot_safe, phase_type) @@ -526,11 +530,11 @@ function PhaseEquil_ρpq( q_tot::FT, perform_sat_adjust = false, maxiter::Int = 5, - temperature_tol::FT = FT(sqrt(eps(FT))), + tol::Union{Nothing, RS.AbstractTolerance} = nothing, ::Type{sat_adjust_method} = RS.NewtonsMethodAD, T_guess::Union{FT, Nothing} = nothing, ) where {FT <: Real, sat_adjust_method} - + tol === nothing && (tol = RS.RelativeSolutionTolerance(FT(sqrt(eps(FT))))) phase_type = PhaseEquil{FT} if perform_sat_adjust T = saturation_adjustment_ρpq( @@ -541,7 +545,7 @@ function PhaseEquil_ρpq( q_tot, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) q_pt = PhasePartition_equil(param_set, T, ρ, q_tot, phase_type) @@ -556,7 +560,7 @@ end """ - PhaseEquil_pθq(param_set, θ_liq_ice, q_tot[, maxiter, temperature_tol, sat_adjust_method, T_guess]) + PhaseEquil_pθq(param_set, θ_liq_ice, q_tot[, maxiter, tol, sat_adjust_method, T_guess]) Constructs a [`PhaseEquil`](@ref) thermodynamic state from: @@ -564,7 +568,10 @@ Constructs a [`PhaseEquil`](@ref) thermodynamic state from: - `p` air pressure - `θ_liq_ice` liquid-ice potential temperature - `q_tot` total specific humidity - - `temperature_tol` temperature tolerance for saturation adjustment + - `tol` a tolerance (defined by RootSolvers) for saturation adjustment. 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 saturation adjustment - `sat_adjust_method` the numerical method to use. - `T_guess` initial guess for temperature in saturation adjustment @@ -575,12 +582,12 @@ function PhaseEquil_pθq( θ_liq_ice::FT, q_tot::FT, maxiter::IT = nothing, - temperature_tol::FTT = nothing, + tol::Union{Nothing, RS.AbstractTolerance} = nothing, ::Type{sat_adjust_method} = RS.SecantMethod, T_guess::Union{FT, Nothing} = nothing, ) where {FT <: Real, IT <: ITERTYPE, FTT <: TOLTYPE(FT), sat_adjust_method} maxiter === nothing && (maxiter = 50) - temperature_tol === nothing && (temperature_tol = FT(1e-3)) + tol === nothing && (tol = RS.RelativeSolutionTolerance(FT(1e-4))) phase_type = PhaseEquil{FT} q_tot_safe = clamp(q_tot, FT(0), FT(1)) T = saturation_adjustment_given_pθq( @@ -591,7 +598,7 @@ function PhaseEquil_pθq( q_tot_safe, phase_type, maxiter, - temperature_tol, + tol, T_guess, ) q_pt = PhasePartition_equil_given_p(param_set, T, p, q_tot_safe, phase_type) @@ -667,7 +674,10 @@ Constructs a [`PhaseNonEquil`](@ref) thermodynamic state from: - `θ_liq_ice` liquid-ice potential temperature - `q_pt` phase partition and, optionally - - `potential_temperature_tol` potential temperature for non-linear equation solve + - `tol` a tolerance (defined by RootSolvers) for saturation adjustment. 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 """ function PhaseNonEquil_ρθq( @@ -676,10 +686,11 @@ function PhaseNonEquil_ρθq( θ_liq_ice::FT, q_pt::PhasePartition{FT}, maxiter::Int = 10, - potential_temperature_tol::FT = FT(1e-2), + tol::Union{Nothing, RS.AbstractTolerance} = RS.RelativeSolutionTolerance( + FT(1e-2), + ), ) where {FT <: Real} phase_type = PhaseNonEquil{FT} - tol = RS.ResidualTolerance(potential_temperature_tol) T = air_temperature_given_ρθq_nonlinear( param_set, ρ, diff --git a/test/relations.jl b/test/relations.jl index cd145267..c667218b 100644 --- a/test/relations.jl +++ b/test/relations.jl @@ -373,7 +373,7 @@ end q_tot, phase_type, 10, - rtol_temperature, + RS.RelativeSolutionTolerance(rtol_temperature), ) ≈ 300.0 @test abs( TD.saturation_adjustment( @@ -384,7 +384,7 @@ end q_tot, phase_type, 10, - rtol_temperature, + RS.RelativeSolutionTolerance(rtol_temperature), ) - 300.0, ) < rtol_temperature @@ -399,7 +399,7 @@ end q_tot, phase_type, 10, - rtol_temperature, + RS.RelativeSolutionTolerance(rtol_temperature), ), 200.0, rtol = rtol_temperature, @@ -413,7 +413,7 @@ end q_tot, phase_type, 10, - rtol_temperature, + RS.RelativeSolutionTolerance(rtol_temperature), ) - 200.0, ) < rtol_temperature q = PhasePartition_equil(param_set, T, ρ, q_tot, phase_type) @@ -602,13 +602,21 @@ end _e_int, q_tot, 8, - FT(1e-1), + RS.RelativeSolutionTolerance(FT(1e-1)), RS.SecantMethod, ) @test all(air_temperature.(param_set, ts) .== Ref(_T_freeze)) # PhaseEquil - ts_exact = PhaseEquil_ρeq.(param_set, ρ, e_int, q_tot, 100, FT(1e-3)) + ts_exact = + PhaseEquil_ρeq.( + param_set, + ρ, + e_int, + q_tot, + 100, + RS.RelativeSolutionTolerance(FT(1e-6)), + ) ts = PhaseEquil_ρeq.(param_set, ρ, e_int, q_tot) @test all( isapprox.( @@ -683,7 +691,7 @@ end e_int, q_tot, 100, - FT(1e-3), + RS.RelativeSolutionTolerance(FT(1e-6)), RS.SecantMethod, ) ts = @@ -693,7 +701,7 @@ end e_int, q_tot, 35, - FT(1e-1), + nothing, RS.SecantMethod, ) # Needs to be in sync with default # Should be machine accurate (because ts contains `e_int`,`ρ`,`q_tot`): @@ -722,7 +730,15 @@ end ) # PhaseEquil_ρθq - ts_exact = PhaseEquil_ρθq.(param_set, ρ, θ_liq_ice, q_tot, 45, FT(1e-3)) + ts_exact = + PhaseEquil_ρθq.( + param_set, + ρ, + θ_liq_ice, + q_tot, + 45, + RS.RelativeSolutionTolerance(FT(1e-6)), + ) ts = PhaseEquil_ρθq.(param_set, ρ, θ_liq_ice, q_tot) # Should be machine accurate: @test all( @@ -753,7 +769,15 @@ end ) # PhaseEquil_pθq - ts_exact = PhaseEquil_pθq.(param_set, p, θ_liq_ice, q_tot, 40, FT(1e-3)) + ts_exact = + PhaseEquil_pθq.( + param_set, + p, + θ_liq_ice, + q_tot, + 40, + RS.RelativeSolutionTolerance(FT(1e-6)), + ) ts = PhaseEquil_pθq.(param_set, p, θ_liq_ice, q_tot) ts = @@ -763,7 +787,7 @@ end θ_liq_ice, q_tot, 40, - FT(1e-3), + nothing, RS.RegulaFalsiMethod, ) # Should be machine accurate: @@ -844,7 +868,14 @@ end # @show ρ, θ_liq_ice, q_pt # PhaseNonEquil_ρθq ts_exact = - PhaseNonEquil_ρθq.(param_set, ρ, θ_liq_ice, q_pt, 40, FT(1e-3)) + PhaseNonEquil_ρθq.( + param_set, + ρ, + θ_liq_ice, + q_pt, + 40, + RS.RelativeSolutionTolerance(FT(1e-6)), + ) ts = PhaseNonEquil_ρθq.(param_set, ρ, θ_liq_ice, q_pt) # Should be machine accurate: @test all(compare_moisture.(param_set, ts, ts_exact)) @@ -895,7 +926,7 @@ end q_tot, Ref(phase_type), 2, - FT(1e-10), + RS.RelativeSolutionTolerance(FT(1e-10)), ) @test_throws ErrorException TD.saturation_adjustment.( @@ -906,7 +937,7 @@ end q_tot, Ref(phase_type), 2, - FT(1e-10), + RS.RelativeSolutionTolerance(FT(1e-10)), ) @test_throws ErrorException TD.saturation_adjustment_given_peq.( @@ -917,7 +948,7 @@ end q_tot, Ref(phase_type), 2, - FT(1e-10), + RS.RelativeSolutionTolerance(FT(1e-10)), ) T_virt = T # should not matter: testing for non-convergence @@ -958,7 +989,7 @@ end q_tot, Ref(phase_type), 2, - FT(1e-10), + RS.RelativeSolutionTolerance(FT(1e-10)), ) @test_throws ErrorException TD.saturation_adjustment_given_pθq.( @@ -969,7 +1000,7 @@ end q_tot, Ref(phase_type), 2, - FT(1e-10), + RS.RelativeSolutionTolerance(FT(1e-10)), ) @test_throws ErrorException TD.saturation_adjustment_ρpq.( @@ -980,7 +1011,7 @@ end q_tot, Ref(phase_type), 2, - FT(1e-10), + RS.RelativeSolutionTolerance(FT(1e-10)), ) end @@ -1068,7 +1099,7 @@ end e_int, q_tot, 40, - FT(1e-1), + RS.RelativeSolutionTolerance(FT(1e-1)), RS.SecantMethod, ) @test all(internal_energy.(param_set, ts) .≈ e_int) @@ -1107,7 +1138,7 @@ end ts_pθq = PhaseEquil_pθq.(param_set, p, θ_liq_ice, q_tot) @test all(air_pressure.(param_set, ts_pθq) .≈ p) # TODO: Run some tests to make sure that this decreses with - # decreasing temperature_tol (and increasing maxiter) + # decreasing tolerance (and increasing maxiter) # @show maximum(abs.(liquid_ice_pottemp.(param_set, ts_pθq) .- θ_liq_ice)) @test all( isapprox.( @@ -1204,7 +1235,15 @@ end ) # PhaseEquil_ρθq - ts = PhaseEquil_ρθq.(param_set, ρ, θ_liq_ice, q_tot, 45, FT(1e-3)) + ts = + PhaseEquil_ρθq.( + param_set, + ρ, + θ_liq_ice, + q_tot, + 45, + RS.RelativeSolutionTolerance(FT(1e-3)), + ) @test all( isapprox.( liquid_ice_pottemp.(param_set, ts), @@ -1223,7 +1262,15 @@ end # precision for the input pressure. # PhaseEquil_pθq - ts = PhaseEquil_pθq.(param_set, p, θ_liq_ice, q_tot, 35, FT(1e-3)) + ts = + PhaseEquil_pθq.( + param_set, + p, + θ_liq_ice, + q_tot, + 35, + RS.RelativeSolutionTolerance(FT(1e-3)), + ) @test all( isapprox.( liquid_ice_pottemp.(param_set, ts), @@ -1260,7 +1307,15 @@ end @test all(total_energy.(param_set, ts, e_kin, e_pot) .≈ e_tot_proposed) # PhaseNonEquil_ρθq - ts = PhaseNonEquil_ρθq.(param_set, ρ, θ_liq_ice, q_pt, 5, FT(1e-3)) + ts = + PhaseNonEquil_ρθq.( + param_set, + ρ, + θ_liq_ice, + q_pt, + 5, + RS.RelativeSolutionTolerance(FT(1e-3)), + ) @test all( isapprox.( θ_liq_ice, @@ -1373,7 +1428,15 @@ end @test typeof.(internal_energy.(ρ, ρ .* e_int, Ref(ρu), e_pot)) == typeof.(e_int) - ts_eq = PhaseEquil_ρeq.(param_set, ρ, e_int, q_tot, 15, FT(1e-1)) + ts_eq = + PhaseEquil_ρeq.( + param_set, + ρ, + e_int, + q_tot, + 15, + RS.RelativeSolutionTolerance(FT(1e-4)), + ) e_tot = total_energy.(param_set, ts_eq, e_kin, e_pot) ts_T = @@ -1413,9 +1476,23 @@ end ts_pT_neq = PhaseNonEquil_pTq.(param_set, p, T, q_pt) ts_θ_liq_ice_eq = - PhaseEquil_ρθq.(param_set, ρ, θ_liq_ice, q_tot, 45, FT(1e-3)) + PhaseEquil_ρθq.( + param_set, + ρ, + θ_liq_ice, + q_tot, + 45, + RS.RelativeSolutionTolerance(FT(1e-4)), + ) ts_θ_liq_ice_eq_p = - PhaseEquil_pθq.(param_set, p, θ_liq_ice, q_tot, 40, FT(1e-3)) + PhaseEquil_pθq.( + param_set, + p, + θ_liq_ice, + q_tot, + 40, + RS.RelativeSolutionTolerance(FT(1e-4)), + ) ts_θ_liq_ice_neq = PhaseNonEquil_ρθq.(param_set, ρ, θ_liq_ice, q_pt) ts_θ_liq_ice_neq_p = PhaseNonEquil_pθq.(param_set, p, θ_liq_ice, q_pt) @@ -1677,7 +1754,7 @@ end profiles = TestedProfiles.PhaseEquilProfiles(param_set, ArrayType) @unpack p, ρ, e_int, h, θ_liq_ice, q_tot, T, phase_type = profiles T_guess = T .+ (FT(0.2) .* randn(FT, length(T))) - args = (q_tot, 40, FT(1e-1)) + args = (q_tot, 40, RS.RelativeSolutionTolerance(FT(1e-1))) ts = PhaseEquil_ρeq.(param_set, ρ, e_int, args..., RS.NewtonsMethod, T_guess) ts = PhaseEquil_ρθq.(param_set, ρ, θ_liq_ice, args..., T_guess) @@ -1691,7 +1768,7 @@ end q_tot, true, 40, - FT(1e-1), + RS.RelativeSolutionTolerance(FT(1e-4)), RS.NewtonsMethodAD, T_guess, )