Skip to content

Commit 4510ae5

Browse files
Merge pull request #3694 from CliMA/ck/rrtmgpi_refactor3
Refactor RRTMGP interface
2 parents aca95c1 + 7c35dac commit 4510ae5

File tree

1 file changed

+47
-84
lines changed

1 file changed

+47
-84
lines changed

src/parameterized_tendencies/radiation/RRTMGPInterface.jl

Lines changed: 47 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ struct RRTMGPModel{R, I, B, L, P, LWS, SWS, AS, V}
264264
radiation_mode::R
265265
interpolation::I
266266
bottom_extrapolation::B
267-
implied_values::Symbol
268267
lookups::L
269268
params::P
270269
lw_solver::LWS
@@ -303,11 +302,8 @@ After constructing an `RRTMGPModel`, use it as follows:
303302
- use the values of any fluxes of interest; e.g.,
304303
`field2array(face_flux_field) .= model.face_flux`
305304
306-
To construct the `RRTMGPModel`, one must specify `center_pressure` and
307-
`center_temperature`, or `face_pressure` and `face_temperature`, or all four
308-
values. If only the values at cell centers are specified, the values at cell
309-
faces are "implied values". Likewise, if only the values at faces are specified,
310-
the values at centers are "implied values".
305+
The `RRTMGPModel` assumes that pressure and temperature live on cell centers,
306+
and internally interpolates data to cell faces when needed.
311307
312308
Every keyword argument that corresponds to an array of cell center or cell face
313309
values can be specified as a scalar (corresponding to a constant value
@@ -408,7 +404,7 @@ array.
408404
- `center_z`: z-coordinate in m at cell centers
409405
- `face_z`: z-coordinate in m at cell faces
410406
"""
411-
function RRTMGPModel(
407+
RRTMGPModel(
412408
params::RRTMGP.Parameters.ARP,
413409
context;
414410
ncol::Int,
@@ -418,6 +414,29 @@ function RRTMGPModel(
418414
bottom_extrapolation::AbstractBottomExtrapolation = SameAsInterpolation(),
419415
use_global_means_for_well_mixed_gases::Bool = false,
420416
kwargs...,
417+
) = _RRTMGPModel(
418+
params,
419+
context,
420+
radiation_mode;
421+
ncol,
422+
domain_nlay,
423+
interpolation,
424+
bottom_extrapolation,
425+
use_global_means_for_well_mixed_gases,
426+
kwargs...,
427+
)
428+
429+
# TODO: make this the new interface for the next breaking release.
430+
function _RRTMGPModel(
431+
params::RRTMGP.Parameters.ARP,
432+
context,
433+
radiation_mode::AbstractRRTMGPMode = ClearSkyRadiation();
434+
ncol::Int,
435+
domain_nlay::Int,
436+
interpolation::AbstractInterpolation = NoInterpolation(),
437+
bottom_extrapolation::AbstractBottomExtrapolation = SameAsInterpolation(),
438+
use_global_means_for_well_mixed_gases::Bool = false,
439+
kwargs...,
421440
)
422441
device = ClimaComms.device(context)
423442
DA = ClimaComms.array_type(device)
@@ -430,49 +449,9 @@ function RRTMGPModel(
430449
GrayRadiation"
431450
end
432451

433-
if (
434-
:center_pressure in keys(dict) &&
435-
:center_temperature in keys(dict) &&
436-
:face_pressure in keys(dict) &&
437-
:face_temperature in keys(dict)
438-
)
439-
if !(interpolation isa NoInterpolation)
440-
@warn "interpolation is ignored if both center and face pressures/\
441-
temperatures are specified"
442-
end
443-
implied_values = :none
444-
elseif (
445-
:center_pressure in keys(dict) &&
446-
:center_temperature in keys(dict) &&
447-
!(:face_pressure in keys(dict)) &&
448-
!(:face_temperature in keys(dict))
449-
)
450-
if interpolation isa NoInterpolation
451-
error("interpolation cannot be NoInterpolation if only center \
452-
pressures/temperatures are specified")
453-
end
454-
implied_values = :face
455-
elseif (
456-
!(:center_pressure in keys(dict)) &&
457-
!(:center_temperature in keys(dict)) &&
458-
:face_pressure in keys(dict) &&
459-
:face_temperature in keys(dict)
460-
)
461-
if interpolation isa NoInterpolation
462-
error("interpolation cannot be NoInterpolation if only face \
463-
pressures/temperatures are specified")
464-
end
465-
implied_values = :center
466-
else
467-
error("please specify either center_pressure and center_temperature, \
468-
or face_pressure and face_temperature, or all four values")
469-
end
470-
471-
if implied_values != :face
472-
if !(bottom_extrapolation isa SameAsInterpolation)
473-
@warn "bottom_extrapolation is ignored if face_pressure and \
474-
face_temperature are specified"
475-
end
452+
if interpolation isa NoInterpolation
453+
error("interpolation cannot be NoInterpolation if only center \
454+
pressures/temperatures are specified")
476455
end
477456

478457
lookups = NamedTuple()
@@ -652,20 +631,14 @@ function RRTMGPModel(
652631

653632
p_lev = DA{FT}(undef, nlay + 1, ncol)
654633
t_lev = DA{FT}(undef, nlay + 1, ncol)
655-
if implied_values != :face
656-
set_and_save!(p_lev, "face_pressure", t..., dict)
657-
set_and_save!(t_lev, "face_temperature", t..., dict)
658-
end
659634
t_sfc = DA{FT}(undef, ncol)
660635
set_and_save!(t_sfc, "surface_temperature", t..., dict)
661636

662637
if radiation_mode isa GrayRadiation
663638
p_lay = DA{FT}(undef, nlay, ncol)
664639
t_lay = DA{FT}(undef, nlay, ncol)
665-
if implied_values != :center
666-
set_and_save!(p_lay, "center_pressure", t..., dict)
667-
set_and_save!(t_lay, "center_temperature", t..., dict)
668-
end
640+
set_and_save!(p_lay, "center_pressure", t..., dict)
641+
set_and_save!(t_lay, "center_temperature", t..., dict)
669642

670643
z_lev = DA{FT}(undef, nlay + 1, ncol) # TODO: z_lev required but unused
671644

@@ -696,11 +669,9 @@ function RRTMGPModel(
696669
p_lay = view(layerdata, 2, :, :)
697670
t_lay = view(layerdata, 3, :, :)
698671
rh_lay = view(layerdata, 4, :, :)
699-
if implied_values != :center
700-
set_and_save!(p_lay, "center_pressure", t..., dict)
701-
set_and_save!(t_lay, "center_temperature", t..., dict)
702-
set_and_save!(rh_lay, "center_relative_humidity", t..., dict)
703-
end
672+
set_and_save!(p_lay, "center_pressure", t..., dict)
673+
set_and_save!(t_lay, "center_temperature", t..., dict)
674+
set_and_save!(rh_lay, "center_relative_humidity", t..., dict)
704675
vmr_str = "volume_mixing_ratio_"
705676
gas_names = filter(
706677
gas_name ->
@@ -899,7 +870,6 @@ function RRTMGPModel(
899870
radiation_mode,
900871
interpolation,
901872
bottom_extrapolation,
902-
implied_values,
903873
lookups,
904874
params,
905875
lw_solver,
@@ -988,7 +958,7 @@ NVTX.@annotate function update_fluxes!(model, seedval)
988958
)
989959
radiation_mode.reset_rng_seed && Random.seed!(seedval)
990960
end
991-
model.implied_values != :none && update_implied_values!(model)
961+
update_implied_values!(model)
992962
model.radiation_mode.add_isothermal_boundary_layer &&
993963
update_boundary_layer!(model)
994964
clip_values!(model)
@@ -1015,25 +985,18 @@ function update_implied_values!(model)
1015985
z_lay = parent(model.center_z)
1016986
z_lev = parent(model.face_z)
1017987
end
1018-
if model.implied_values == :center
1019-
mode = model.interpolation
1020-
outs = requires_z(mode) ? (p_lay, t_lay, z_lay) : (p_lay, t_lay)
1021-
ins = requires_z(mode) ? (p_lev, t_lev, z_lev) : (p_lev, t_lev)
1022-
update_views(interp!, mode, outs, ins, (), 1:nlay, 1:nlay, 2:(nlay + 1))
1023-
else # model.implied_values == :face
1024-
mode = model.interpolation
1025-
outs = requires_z(mode) ? (p_lev, t_lev, z_lev) : (p_lev, t_lev)
1026-
ins = requires_z(mode) ? (p_lay, t_lay, z_lay) : (p_lay, t_lay)
1027-
update_views(interp!, mode, outs, ins, (), 2:nlay, 1:(nlay - 1), 2:nlay)
1028-
others = (t_sfc, model.params)
1029-
update_views(extrap!, mode, outs, ins, others, nlay + 1, nlay, nlay - 1)
1030-
mode =
1031-
model.bottom_extrapolation isa SameAsInterpolation ?
1032-
model.interpolation : model.bottom_extrapolation
1033-
outs = requires_z(mode) ? (p_lev, t_lev, z_lev) : (p_lev, t_lev)
1034-
ins = requires_z(mode) ? (p_lay, t_lay, z_lay) : (p_lay, t_lay)
1035-
update_views(extrap!, mode, outs, ins, others, 1, 1, 2)
1036-
end
988+
mode = model.interpolation
989+
outs = requires_z(mode) ? (p_lev, t_lev, z_lev) : (p_lev, t_lev)
990+
ins = requires_z(mode) ? (p_lay, t_lay, z_lay) : (p_lay, t_lay)
991+
update_views(interp!, mode, outs, ins, (), 2:nlay, 1:(nlay - 1), 2:nlay)
992+
others = (t_sfc, model.params)
993+
update_views(extrap!, mode, outs, ins, others, nlay + 1, nlay, nlay - 1)
994+
mode =
995+
model.bottom_extrapolation isa SameAsInterpolation ?
996+
model.interpolation : model.bottom_extrapolation
997+
outs = requires_z(mode) ? (p_lev, t_lev, z_lev) : (p_lev, t_lev)
998+
ins = requires_z(mode) ? (p_lay, t_lay, z_lay) : (p_lay, t_lay)
999+
update_views(extrap!, mode, outs, ins, others, 1, 1, 2)
10371000
end
10381001

10391002
update_views(f, mode, outs, ins, others, out_range, in_range1, in_range2) = f(

0 commit comments

Comments
 (0)