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

Add optional initial temperature guess #128

Merged
merged 3 commits into from
Oct 1, 2022
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
12 changes: 3 additions & 9 deletions perf/allocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ import Thermodynamics
import RootSolvers
import CLIMAParameters
import ReportMetrics
const RM = ReportMetrics
td_dir = RM.mod_dir(Thermodynamics)

dirs_to_monitor = [
".",
RM.mod_dir(Thermodynamics),
RM.mod_dir(RootSolvers),
RM.mod_dir(CLIMAParameters),
]
dirs_to_monitor =
[".", pkgdir(Thermodynamics), pkgdir(RootSolvers), pkgdir(CLIMAParameters)]

for constructor in ["ρeq", "pθq", "pTq"]
ENV["ALLOCATION_CONSTRUCTOR"] = constructor
RM.report_allocs(;
ReportMetrics.report_allocs(;
job_name = constructor,
run_cmd = `$(Base.julia_cmd()) --project=perf/ --track-allocation=all perf/alloc_per_constructor.jl`,
dirs_to_monitor = dirs_to_monitor,
Expand Down
64 changes: 55 additions & 9 deletions src/config_numerical_method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ function print_numerical_method(
end
end

function print_T_guess(
::Type{sat_adjust_method},
T_guess::TT,
) where {sat_adjust_method, TT <: Union{Real, Nothing}}
if sat_adjust_method <: RS.NewtonsMethod
KA.@print(", T_guess=", T_guess)
elseif sat_adjust_method <: RS.NewtonsMethodAD
KA.@print(", T_guess=", T_guess)
end
end

#####
##### Thermodynamic variable inputs: ρ, e_int, q_tot
#####
Expand All @@ -31,10 +42,14 @@ function sa_numerical_method(
e_int::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.NewtonsMethod, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
T_init =
T_init = if T_guess isa Nothing
max(T_min, air_temperature(param_set, e_int, PhasePartition(q_tot))) # Assume all vapor
else
T_guess
end
return RS.NewtonsMethod(
T_init,
T_ -> ∂e_int_∂T(param_set, heavisided(T_), e_int, ρ, q_tot, phase_type),
Expand All @@ -48,10 +63,14 @@ function sa_numerical_method(
e_int::FT,
q_tot::FT,
::Type{phase_type},
T_guess::FT,
) where {FT, NM <: RS.NewtonsMethodAD, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
T_init =
T_init = if T_guess isa Nothing
max(T_min, air_temperature(param_set, e_int, PhasePartition(q_tot))) # Assume all vapor
else
T_guess
end
return RS.NewtonsMethodAD(T_init)
end

Expand All @@ -62,6 +81,7 @@ function sa_numerical_method(
e_int::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.SecantMethod, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
q_pt = PhasePartition(q_tot, FT(0), q_tot) # Assume all ice
Expand All @@ -78,6 +98,7 @@ function sa_numerical_method(
e_int::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.RegulaFalsiMethod, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
q_pt = PhasePartition(q_tot, FT(0), q_tot) # Assume all ice
Expand All @@ -98,9 +119,14 @@ function sa_numerical_method_ρpq(
p::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.NewtonsMethodAD, phase_type <: PhaseEquil}
q_pt = PhasePartition(q_tot)
T_init = air_temperature_from_ideal_gas_law(param_set, p, ρ, q_pt)
T_init = if T_guess isa Nothing
air_temperature_from_ideal_gas_law(param_set, p, ρ, q_pt)
else
T_guess
end
return RS.NewtonsMethodAD(T_init)
end

Expand All @@ -111,6 +137,7 @@ function sa_numerical_method_ρpq(
p::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.RegulaFalsiMethod, phase_type <: PhaseEquil}
q_pt = PhasePartition(q_tot)
T_1 = air_temperature_from_ideal_gas_law(param_set, p, ρ, q_pt) - 5
Expand All @@ -129,10 +156,14 @@ function sa_numerical_method_peq(
e_int::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.NewtonsMethodAD, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
T_init =
T_init = if T_guess isa Nothing
max(T_min, air_temperature(param_set, e_int, PhasePartition(q_tot))) # Assume all vapor
else
T_guess
end
return RS.NewtonsMethodAD(T_init)
end

Expand All @@ -143,6 +174,7 @@ function sa_numerical_method_peq(
e_int::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.SecantMethod, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
q_pt = PhasePartition(q_tot, FT(0), q_tot) # Assume all ice
Expand All @@ -163,12 +195,17 @@ function sa_numerical_method_phq(
h::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.NewtonsMethodAD, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
T_init = max(
T_min,
air_temperature_from_enthalpy(param_set, h, PhasePartition(q_tot)),
) # Assume all vapor
T_init = if T_guess isa Nothing # Assume all vapor
max(
T_min,
air_temperature_from_enthalpy(param_set, h, PhasePartition(q_tot)),
)
else
T_guess
end
return RS.NewtonsMethodAD(T_init)
end

Expand All @@ -179,6 +216,7 @@ function sa_numerical_method_phq(
h::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.SecantMethod, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
q_pt = PhasePartition(q_tot, FT(0), q_tot) # Assume all ice
Expand All @@ -198,6 +236,7 @@ function sa_numerical_method_phq(
h::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.RegulaFalsiMethod, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
q_pt = PhasePartition(q_tot, FT(0), q_tot) # Assume all ice
Expand All @@ -221,6 +260,7 @@ function sa_numerical_method_pθq(
θ_liq_ice::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.RegulaFalsiMethod, phase_type <: PhaseEquil}
_T_min::FT = TP.T_min(param_set)
_T_max::FT = TP.T_max(param_set)
Expand All @@ -238,6 +278,7 @@ function sa_numerical_method_pθq(
θ_liq_ice::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.SecantMethod, phase_type <: PhaseEquil}
_T_min::FT = TP.T_min(param_set)
air_temp(q) = air_temperature_given_pθq(param_set, p, θ_liq_ice, q)
Expand All @@ -254,9 +295,14 @@ function sa_numerical_method_pθq(
θ_liq_ice::FT,
q_tot::FT,
::Type{phase_type},
T_guess::Union{FT, Nothing},
) where {FT, NM <: RS.NewtonsMethodAD, phase_type <: PhaseEquil}
T_min::FT = TP.T_min(param_set)
air_temp(q) = air_temperature_given_pθq(param_set, p, θ_liq_ice, q)
T_init = max(T_min, air_temp(PhasePartition(q_tot))) # Assume all vapor
T_init = if T_guess isa Nothing
max(T_min, air_temp(PhasePartition(q_tot))) # Assume all vapor
else
T_guess
end
return RS.NewtonsMethodAD(T_init)
end
Loading