Skip to content

Commit

Permalink
Remove hardcoded parameters, some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nefrathenrici committed Sep 13, 2023
1 parent f751e69 commit 3c8fe8a
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 129 deletions.
6 changes: 0 additions & 6 deletions config/default_configs/default_edmf_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ edmfx_upwinding:
debugging_tc:
help: "Save most of the tc aux state to HDF5 file [`false` (default), `true`]"
value: false
entr_coeff:
help: "Entrainment coefficient"
value: 1.0
detr_coeff:
help: "Detrainment coefficient"
value: 0.001
edmfx_sgs_flux:
help: "If set to true, it switches on EDMFX SGS flux. [`true`, `false` (default)]"
value: false
1 change: 0 additions & 1 deletion config/model_configs/edmfx_bomex_box_v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ turbconv: "edmfx"
z_stretch: false
vert_diff: "true"
x_elem: 2
detr_coeff: 0.01
config: "box"
subsidence: "Bomex"
kappa_4: 1.0e12
Expand Down
4 changes: 2 additions & 2 deletions examples/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ version = "0.1.2"

[[deps.CLIMAParameters]]
deps = ["DocStringExtensions", "TOML", "Test"]
git-tree-sha1 = "7b3163ccbd056dabd7ec648101830e9a6ccd019d"
git-tree-sha1 = "7f3b89db2aabb81ec5750300a37d424f1f38240d"
uuid = "6eacf6c3-8458-43b9-ae03-caf5306d3d53"
version = "0.7.14"
version = "0.7.18"

[[deps.CPUSummary]]
deps = ["CpuId", "IfElse", "PrecompileTools", "Static"]
Expand Down
22 changes: 10 additions & 12 deletions src/parameterized_tendencies/radiation/held_suarez.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,24 @@ function forcing_tendency!(Yₜ, Y, p, t, colidx, ::HeldSuarezForcing)
MSLP = FT(CAP.MSLP(params))
p_ref_theta = FT(CAP.p_ref_theta(params))
grav = FT(CAP.grav(params))
ΔT_y_dry = FT(CAP.ΔT_y_dry(params))
ΔT_y_wet = FT(CAP.ΔT_y_wet(params))
Δθ_z = FT(CAP.Δθ_z(params))
T_min = FT(CAP.T_min_hs(params))
thermo_params = CAP.thermodynamics_params(params)
σ_b = CAP.σ_b(params)
k_a = 1 / (40 * day)
k_s = 1 / (4 * day)
k_f = 1 / day

z_surface =
Fields.level(Fields.coordinate_field(Y.f).z[colidx], Fields.half)

σ_b = FT(7 / 10)
k_a = 1 / (40 * day)
k_s = 1 / (4 * day)
k_f = 1 / day
if :ρq_tot in propertynames(Y.c)
ΔT_y = ΔT_y_wet
T_equator = FT(294)
ΔT_y = FT(CAP.ΔT_y_wet(params))
T_equator = FT(CAP.T_equator_wet(params))
else
ΔT_y = ΔT_y_dry
T_equator = FT(315)
ΔT_y = FT(CAP.ΔT_y_dry(params))
T_equator = FT(CAP.T_equator_wet(params))
end
Δθ_z = FT(10)
T_min = FT(200)

@. ᶜσ[colidx] =
ᶜp[colidx] / (
Expand Down
67 changes: 23 additions & 44 deletions src/parameters/Parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,29 @@ const ACAP = AbstractClimaAtmosParameters

Base.broadcastable(param_set::ACAP) = tuple(param_set)

Base.@kwdef struct ClimaAtmosParameters{FT, TP, RP, IP, MPP, SFP, TCP, SP} <:
ACAP
Omega::FT
f_plane_coriolis_frequency::FT
planet_radius::FT
astro_unit::FT
# TODO: remove defaults, or move these parameters to CLIMAParameters
f::FT = 0 # coriolis parameter. TODO: remove?
Cd::FT = 0 # drag coefficients. TODO: remove?
ug::FT = 0
vg::FT = 0
Base.@kwdef struct ClimaAtmosParameters{FT, TP, RP, IP, MPP, SFP, TCP} <: ACAP
thermodynamics_params::TP
rrtmgp_params::RP
insolation_params::IP
microphysics_params::MPP
surfacefluxes_params::SFP
surface_fluxes_params::SFP
turbconv_params::TCP
sponge_params::SP
entr_coeff::FT = 1
detr_coeff::FT = 0.001
# TODO: Figure out a better place for these held-suarez parameters
Omega::FT
f_plane_coriolis_frequency::FT
planet_radius::FT
astro_unit::FT
entr_coeff::FT
detr_coeff::FT
C_E::FT
# Held Suarez
ΔT_y_dry::FT
ΔT_y_wet::FT
C_E::FT
end

"""
SpongeParameters{FT}
Parameters for both the viscous and rayleigh sponge.
"""
Base.@kwdef struct SpongeParameters{FT}
σ_b::FT
T_equator_dry::FT
T_equator_wet::FT
T_min_hs::FT
Δθ_z::FT
# Sponge
alpha_rayleigh_w::FT
alpha_rayleigh_uh::FT
zd_viscous::FT
Expand All @@ -51,13 +42,6 @@ end

Base.eltype(::ClimaAtmosParameters{FT}) where {FT} = FT

rrtmgp_params(ps::ACAP) = ps.rrtmgp_params
thermodynamics_params(ps::ACAP) = ps.thermodynamics_params
surface_fluxes_params(ps::ACAP) = ps.surfacefluxes_params
microphysics_params(ps::ACAP) = ps.microphysics_params
insolation_params(ps::ACAP) = ps.insolation_params
turbconv_params(ps::ACAP) = ps.turbconv_params

# Forward Thermodynamics parameters
for var in fieldnames(TD.Parameters.ThermodynamicsParameters)
@eval $var(ps::ACAP) = TD.Parameters.$var(thermodynamics_params(ps))
Expand All @@ -72,23 +56,18 @@ cv_v(ps::ACAP) = TD.Parameters.cv_v(thermodynamics_params(ps))
cv_l(ps::ACAP) = TD.Parameters.cv_l(thermodynamics_params(ps))
cv_d(ps::ACAP) = TD.Parameters.cv_d(thermodynamics_params(ps))

Omega(ps::ACAP) = ps.Omega
f_plane_coriolis_frequency(ps::ACAP) = ps.f_plane_coriolis_frequency
planet_radius(ps::ACAP) = ps.planet_radius
astro_unit(ps::ACAP) = ps.astro_unit
f(ps::ACAP) = ps.f
Cd(ps::ACAP) = ps.Cd
uh_g(ps::ACAP) = CC.Geometry.UVVector(ps.ug, ps.vg)
entr_coeff(ps::ACAP) = ps.entr_coeff
detr_coeff(ps::ACAP) = ps.detr_coeff
ΔT_y_dry(ps::ACAP) = ps.ΔT_y_dry
ΔT_y_wet(ps::ACAP) = ps.ΔT_y_wet

# Forwarding CloudMicrophysics parameters
ρ_cloud_liq(ps::ACAP) = CM.Parameters.ρ_cloud_liq(microphysics_params(ps))

# Insolation parameters
day(ps::ACAP) = IP.day(insolation_params(ps))
tot_solar_irrad(ps::ACAP) = IP.tot_solar_irrad(insolation_params(ps))

# Define parameters as functions
# Place derived parameters in exceptions vector
exceptions = []
for var in filter(x -> !(x in exceptions), fieldnames(ClimaAtmosParameters))
@eval $var(ps::ACAP) = ps.$var
end

end
56 changes: 19 additions & 37 deletions src/parameters/create_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,49 +105,31 @@ function create_climaatmos_parameter_set(
params = override_climaatmos_defaults((; pairs...), overrides) # overrides
insolation_params = IP.InsolationParameters{FTD}(; params...)

pairs = CP.get_parameter_values!(
toml_dict,
[
"alpha_rayleigh_w",
"alpha_rayleigh_uh",
"zd_viscous",
"zd_rayleigh",
"kappa_2_sponge",
],
)
pairs = (; pairs...) # convert to NamedTuple
pairs = override_climaatmos_defaults((; pairs...), overrides)
sponge_params = CAP.SpongeParameters(; pairs...)

pairs = CP.get_parameter_values!(
toml_dict,
["Omega", "planet_radius", "astro_unit", "ΔT_y_dry", "ΔT_y_wet", "C_E"],
"ClimaAtmos",
)
pairs = (; pairs...) # convert to NamedTuple
pairs = override_climaatmos_defaults((; pairs...), overrides)

# This will be cleaned up after rebasing from #2017
subparam_struct_names = [
:thermodynamics_params,
:rrtmgp_params,
:insolation_params,
:microphysics_params,
:surface_fluxes_params,
:turbconv_params,
]
aliases =
string.(
setdiff(
fieldnames(CAP.ClimaAtmosParameters),
subparam_struct_names,
)
)
pairs = CP.get_parameter_values!(toml_dict, aliases)
param_set = CAP.ClimaAtmosParameters(;
ug = FTD(1.0), # for Ekman problem
vg = FTD(0.0), # for Ekman problem
f = FTD(5e-5), # for Ekman problem
Cd = FTD(0.01 / (2e2 / 30)), # for Ekman problem
Omega = FTD(pairs.Omega),
planet_radius = FTD(pairs.planet_radius),
astro_unit = FTD(pairs.astro_unit),
f_plane_coriolis_frequency = FTD(0),
thermodynamics_params = thermo_params,
microphysics_params = microphys_params,
insolation_params = insolation_params,
rrtmgp_params = rrtmgp_params,
surfacefluxes_params = surf_flux_params,
surface_fluxes_params = surf_flux_params,
turbconv_params = tc_params,
entr_coeff = FTD(parsed_args["entr_coeff"]),
detr_coeff = FTD(parsed_args["detr_coeff"]),
ΔT_y_dry = FTD(pairs.ΔT_y_dry),
ΔT_y_wet = FTD(pairs.ΔT_y_wet),
C_E = FTD(pairs.C_E),
sponge_params,
pairs...
)
# logfilepath = joinpath(@__DIR__, "logfilepath_$FT.toml")
# CP.log_parameter_information(toml_dict, logfilepath)
Expand Down
22 changes: 7 additions & 15 deletions src/solver/model_getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,35 +76,27 @@ function get_surface_model(parsed_args)
end
end

function get_viscous_sponge_model(
parsed_args,
sponge_params,
::Type{FT},
) where {FT}
function get_viscous_sponge_model(parsed_args, params, ::Type{FT}) where {FT}
vs_name = parsed_args["viscous_sponge"]
return if vs_name in ("false", false, "none")
nothing
elseif vs_name in ("true", true, "ViscousSponge")
zd = sponge_params.zd_viscous
κ₂ = sponge_params.kappa_2_sponge
zd = params.zd_viscous
κ₂ = params.kappa_2_sponge
ViscousSponge{FT}(; zd, κ₂)
else
error("Uncaught viscous sponge model `$vs_name`.")
end
end

function get_rayleigh_sponge_model(
parsed_args,
sponge_params,
::Type{FT},
) where {FT}
function get_rayleigh_sponge_model(parsed_args, params, ::Type{FT}) where {FT}
rs_name = parsed_args["rayleigh_sponge"]
return if rs_name in ("false", false)
nothing
elseif rs_name in ("true", true, "RayleighSponge")
zd = sponge_params.zd_rayleigh
α_uₕ = sponge_params.alpha_rayleigh_uh
α_w = sponge_params.alpha_rayleigh_w
zd = params.zd_rayleigh
α_uₕ = params.alpha_rayleigh_uh
α_w = params.alpha_rayleigh_w
RayleighSponge{FT}(; zd, α_uₕ, α_w)
else
error("Uncaught rayleigh sponge model `$rs_name`.")
Expand Down
14 changes: 3 additions & 11 deletions src/solver/type_getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ClimaTimeSteppers as CTS
import DiffEqCallbacks as DECB

function get_atmos(config::AtmosConfig, params)
(; turbconv_params, sponge_params) = params
(; turbconv_params) = params
(; parsed_args) = config
FT = eltype(config)
moisture_model = get_moisture_model(parsed_args)
Expand Down Expand Up @@ -71,16 +71,8 @@ function get_atmos(config::AtmosConfig, params)
),
hyperdiff = get_hyperdiffusion_model(parsed_args, FT),
vert_diff,
viscous_sponge = get_viscous_sponge_model(
parsed_args,
sponge_params,
FT,
),
rayleigh_sponge = get_rayleigh_sponge_model(
parsed_args,
sponge_params,
FT,
),
viscous_sponge = get_viscous_sponge_model(parsed_args, params, FT),
rayleigh_sponge = get_rayleigh_sponge_model(parsed_args, params, FT),
sfc_temperature = get_sfc_temperature_form(parsed_args),
surface_model = get_surface_model(parsed_args),
)
Expand Down
18 changes: 17 additions & 1 deletion test/parameter_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,27 @@ default_args = CA.cli_defaults(CA.argparse_settings())
"dt_save_to_disk" => "str",
"bubble" => true,
)
parsed_args = CA.AtmosConfig(; config_dict = config_dict).parsed_args
parsed_args = CA.AtmosConfig(; config_dict).parsed_args
@test parsed_args["krylov_rtol"] isa FT
@test parsed_args["newton_rtol"] isa FT
@test parsed_args["max_newton_iters_ode"] isa Int
@test parsed_args["nh_poly"] isa Int
@test parsed_args["dt_save_to_disk"] isa String
@test parsed_args["bubble"] isa Bool
end

@testset "Test unique aliases" begin
config_dict = Dict("toml" => ["parameter_tests.toml"])
config = CA.AtmosConfig(; config_dict)
@test_throws ErrorException CP.get_parameter_values!(config.toml_dict, ["same_alias"])
end

@testset "Test all parameter tomls in toml/" begin
toml_path = joinpath("..", "toml")
for toml in readdir(toml_path)
config_dict = Dict("toml" => [joinpath(toml_path, toml)])
config = CA.AtmosConfig(;config_dict);
# Ensure that there are no errors
@test CA.create_parameter_set(config) isa CA.Parameters.ClimaAtmosParameters
end
end
4 changes: 4 additions & 0 deletions toml/edmfx_bomex_box_v2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ alias = "C_E"
value = 0.044
type = "float"

[detr_coeff]
alias = "detr_coeff"
value = 0.01
type = "float"

0 comments on commit 3c8fe8a

Please sign in to comment.