Skip to content

Commit

Permalink
Preallocate thermal state
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed May 6, 2024
1 parent 95cb4f4 commit 7ff391a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/src/APIs/shared_utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ ClimaLand.turbulent_fluxes
ClimaLand.turbulent_fluxes_at_a_point
ClimaLand.radiative_fluxes_at_a_point
ClimaLand.construct_atmos_ts
ClimaLand.set_atmos_ts!
ClimaLand.surface_air_density
ClimaLand.liquid_precipitation
ClimaLand.snow_precipitation
Expand Down
29 changes: 21 additions & 8 deletions src/shared_utilities/drivers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export AbstractAtmosphericDrivers,
CoupledRadiativeFluxes,
compute_ρ_sfc,
construct_atmos_ts,
set_atmos_ts!,
turbulent_fluxes,
net_radiation,
turbulent_fluxes_at_a_point,
Expand Down Expand Up @@ -175,6 +176,20 @@ function construct_atmos_ts(
return ts_in
end

"""
set_atmos_ts!(ts_in, atmos::PrescribedAtmosphere{FT}, p)
Fill the pre-allocated ts_in `Field` with a thermodynamic state computed from the
atmosphere.
"""
function set_atmos_ts!(ts_in, atmos::PrescribedAtmosphere{FT}, p) where {FT}
thermo_params = LP.thermodynamic_parameters(p.parameters.earth_param_set)
P = p.drivers.P
T = p.drivers.T
q = p.drivers.q
ts_in .= Thermodynamics.PhaseEquil_pTq.(thermo_params, P, T, q)
return nothing
end

"""
turbulent_fluxes(atmos::PrescribedAtmosphere,
Expand Down Expand Up @@ -206,9 +221,6 @@ function turbulent_fluxes(
h_sfc = surface_height(model, Y, p)
r_sfc = surface_resistance(model, Y, p, t)
d_sfc = displacement_height(model, Y, p)
thermo_params =
LP.thermodynamic_parameters(model.parameters.earth_param_set)
ts_air = construct_atmos_ts(atmos, p, thermo_params)
u_air = p.drivers.u
h_air = atmos.h

Expand All @@ -220,7 +232,7 @@ function turbulent_fluxes(
h_sfc,
r_sfc,
d_sfc,
ts_air,
p.drivers.thermal_state,
u_air,
h_air,
atmos.gustiness,
Expand Down Expand Up @@ -518,8 +530,7 @@ function surface_air_density(
)
thermo_params =
LP.thermodynamic_parameters(model.parameters.earth_param_set)
ts_in = construct_atmos_ts(atmos, p, thermo_params)
return compute_ρ_sfc.(thermo_params, ts_in, T_sfc)
return compute_ρ_sfc.(thermo_params, p.drivers.thermal_state, T_sfc)
end


Expand Down Expand Up @@ -662,8 +673,9 @@ horizontal wind speed `u`, specific humidity `q`, and CO2 concentration
`c_co2`.
"""
function initialize_drivers(a::PrescribedAtmosphere{FT}, coords) where {FT}
keys = (:P_liq, :P_snow, :T, :P, :u, :q, :c_co2)
types = ([FT for k in keys]...,)
keys = (:P_liq, :P_snow, :T, :P, :u, :q, :c_co2, :thermal_state)
# The thermal state is a different type
types = ([FT for k in keys[1:(end - 1)]]..., Thermodynamics.PhaseEquil{FT})
domain_names = ([:surface for k in keys]...,)
model_name = :drivers
# intialize_vars packages the variables as a named tuple,
Expand Down Expand Up @@ -813,6 +825,7 @@ function make_update_drivers(a::PrescribedAtmosphere{FT}) where {FT}
evaluate!(p.drivers.u, a.u, t)
evaluate!(p.drivers.q, a.q, t)
evaluate!(p.drivers.c_co2, a.c_co2, t)
set_atmos_ts!(p.drivers.thermal_state, a, p)
end
return update_drivers!
end
Expand Down
9 changes: 9 additions & 0 deletions src/shared_utilities/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ driver variables in the cache.
add_drivers_to_cache(p, model::AbstractModel) = p


"""
add_parameters_to_cache(p, model::AbstractModel)
Add `parameters` to `p`.
"""
add_parameters_to_cache(p, model::AbstractModel) =
merge(p, (; model.parameters))

"""
make_imp_tendency(model::AbstractImExModel)
Expand Down Expand Up @@ -418,5 +426,6 @@ function initialize(model::AbstractModel{FT}) where {FT}
Y = initialize_prognostic(model, coords)
p = initialize_auxiliary(model, coords)
p = add_drivers_to_cache(p, model, coords)
p = add_parameters_to_cache(p, model)
return Y, p, coords
end

0 comments on commit 7ff391a

Please sign in to comment.