ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfacePrecipitation.H
Go to the documentation of this file.
1 #ifndef ERF_SURFACE_PRECIPITATION_H_
2 #define ERF_SURFACE_PRECIPITATION_H_
3 
4 #include <AMReX_GpuQualifiers.H>
5 #include <AMReX_MultiFab.H>
6 #include <AMReX_MFIter.H>
7 #include <AMReX_REAL.H>
8 
9 // Shared microphysics-to-IO contract for surface precipitation accumulation.
10 // Scheme-native precipitation accumulators do not all have the same semantics.
11 // Some schemes store explicit species accumulations; others store total
12 // precipitation plus frozen-species subset accumulations. The IO layer uses
13 // these typed slots to build public kg/m^2 fields without double-counting.
14 
15 /**
16  * @brief Precipitation accumulation field and its conversion factor.
17  * @var SurfacePrecipAccumulationSource::accum Pointer to the accumulation MultiFab.
18  * @var SurfacePrecipAccumulationSource::native_to_kg_m2 Conversion factor from native units to kg/m^2.
19  */
21 {
22  const amrex::MultiFab* accum = nullptr;
23  amrex::Real native_to_kg_m2 = amrex::Real(1.0);
24 };
25 
26 /**
27  * @brief Collection of precipitation accumulation sources for different species.
28  * @var SurfacePrecipAccumulationSources::total Total precipitation source.
29  * @var SurfacePrecipAccumulationSources::rain Rain precipitation source.
30  * @var SurfacePrecipAccumulationSources::snow Snow precipitation source.
31  * @var SurfacePrecipAccumulationSources::graupel Graupel precipitation source.
32  * @var SurfacePrecipAccumulationSources::hail Hail precipitation source.
33  * @var SurfacePrecipAccumulationSources::rest_is_frozen True when what the total holds
34  * beyond rain, snow, graupel and hail is frozen. That rest is the cloud species
35  * that fell out. It is cloud ice alone in WSM6 and WDM6, so for them the frozen
36  * part is the total less the rain. It is false where the rest holds cloud water
37  * (Morrison) or where there is no total.
38  */
40 {
46  bool rest_is_frozen = false;
47 };
48 
49 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
51 {
52  return src.accum != nullptr;
53 }
54 
55 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
57 {
58  return surface_precip_has_source(src.total);
59 }
60 
61 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
63 {
64  return surface_precip_has_source(src.rain);
65 }
66 
67 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
69 {
70  return surface_precip_has_source(src.total)
71  || surface_precip_has_source(src.rain)
72  || surface_precip_has_source(src.snow)
73  || surface_precip_has_source(src.graupel)
74  || surface_precip_has_source(src.hail);
75 }
76 
77 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
79 {
80  // Public frozen precipitation is available whenever the scheme has any
81  // precipitation source, because the IO layer can derive the frozen field
82  // from the total or species accumulators and fill zero for warm-rain cases.
84 }
85 
86 /// Which part of the surface precipitation surface_precip_accum_k0 returns.
88 
89 /**
90  * @brief The work of surface_precip_accum_k0 and surface_precip_liquid_frozen_accum_k0:
91  * component 0 of accum_2d takes the @p phase part and, with @p frozen_in_comp1,
92  * component 1 takes the frozen part from the same pass over the accumulators.
93  */
94 inline void surface_precip_fill_k0 (amrex::MultiFab& accum_2d,
95  const SurfacePrecipAccumulationSources& sources,
96  int klo, SurfacePrecipPhase phase, bool frozen_in_comp1)
97 {
98  using namespace amrex;
99  AMREX_ALWAYS_ASSERT(accum_2d.nComp() >= (frozen_in_comp1 ? 2 : 1));
100  accum_2d.setVal(Real(0.0));
101  if (!surface_precip_has_any_source(sources)) { return; }
102 
103  const bool has_total = surface_precip_has_source(sources.total);
104  const bool has_rain = surface_precip_has_source(sources.rain);
105  const bool has_snow = surface_precip_has_source(sources.snow);
106  const bool has_graupel = surface_precip_has_source(sources.graupel);
107  const bool has_hail = surface_precip_has_source(sources.hail);
108  const Real f_total = sources.total.native_to_kg_m2;
109  const Real f_rain = sources.rain.native_to_kg_m2;
110  const Real f_snow = sources.snow.native_to_kg_m2;
111  const Real f_graupel = sources.graupel.native_to_kg_m2;
112  const Real f_hail = sources.hail.native_to_kg_m2;
113  // The total less the rain is the frozen part, and the rain is the liquid part
114  const bool rest_frozen = sources.rest_is_frozen && has_total;
115  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!rest_frozen || has_rain,
116  "surface_precip_fill_k0: rest_is_frozen needs a rain accumulator");
117 
118  // Whether box n of a source holds the plane k = klo.
119  auto holds_klo = [klo] (const SurfacePrecipAccumulationSource& src, int n) {
120  if (!surface_precip_has_source(src)) { return true; }
121  const Box& b = src.accum->boxArray()[n];
122  return b.smallEnd(2) <= klo && klo <= b.bigEnd(2);
123  };
124 
125  for (MFIter mfi(accum_2d, false); mfi.isValid(); ++mfi) {
126  const int n = mfi.index();
127  if (!(holds_klo(sources.total, n) && holds_klo(sources.rain, n) &&
128  holds_klo(sources.snow, n) && holds_klo(sources.graupel, n) &&
129  holds_klo(sources.hail, n))) { continue; }
130  Array4<Real> a = accum_2d.array(mfi);
131  Array4<const Real> total = has_total ? sources.total.accum->const_array(mfi) : Array4<const Real>{};
132  Array4<const Real> rain = has_rain ? sources.rain.accum->const_array(mfi) : Array4<const Real>{};
133  Array4<const Real> snow = has_snow ? sources.snow.accum->const_array(mfi) : Array4<const Real>{};
134  Array4<const Real> graupel = has_graupel ? sources.graupel.accum->const_array(mfi) : Array4<const Real>{};
135  Array4<const Real> hail = has_hail ? sources.hail.accum->const_array(mfi) : Array4<const Real>{};
136  ParallelFor(mfi.tilebox(), [=] AMREX_GPU_DEVICE (const IntVect& iv) noexcept
137  {
138  const int i = iv[0], j = iv[1];
139  Real frozen = Real(0.0);
140  if (phase != SurfacePrecipPhase::Total || frozen_in_comp1) {
141  if (rest_frozen) {
142  frozen = total(i, j, klo) * f_total - rain(i, j, klo) * f_rain;
143  } else {
144  if (has_snow) { frozen += snow(i, j, klo) * f_snow; }
145  if (has_graupel) { frozen += graupel(i, j, klo) * f_graupel; }
146  if (has_hail) { frozen += hail(i, j, klo) * f_hail; }
147  }
148  }
149  Real v;
150  if (phase == SurfacePrecipPhase::Frozen) {
151  v = frozen;
152  } else if (phase == SurfacePrecipPhase::Liquid) {
153  // No bound here: this is an accumulation, and a bound on it would change the
154  // difference of two of its values. The user bounds that difference.
155  if (rest_frozen) {
156  v = rain(i, j, klo) * f_rain;
157  } else if (has_total) {
158  v = total(i, j, klo) * f_total - frozen;
159  } else {
160  v = has_rain ? rain(i, j, klo) * f_rain : Real(0.0);
161  }
162  } else if (has_total) {
163  v = total(i, j, klo) * f_total;
164  } else {
165  v = Real(0.0);
166  if (has_rain) { v += rain(i, j, klo) * f_rain; }
167  if (has_snow) { v += snow(i, j, klo) * f_snow; }
168  if (has_graupel) { v += graupel(i, j, klo) * f_graupel; }
169  if (has_hail) { v += hail(i, j, klo) * f_hail; }
170  }
171  a(i, j, 0, 0) = v;
172  if (frozen_in_comp1) { a(i, j, 0, 1) = frozen; }
173  });
174  }
175 }
176 
177 /**
178  * @brief Total liquid-water-equivalent surface precipitation accumulation [kg/m2]
179  * of every column, from the scheme-native accumulators.
180  *
181  * Fills component 0 of accum_2d, a MultiFab on the level's boxes coarsened to one
182  * cell in z (box n of accum_2d is box n of the accumulators, with the same
183  * owner), from the k = klo plane of the sources. Schemes that provide a total
184  * give it directly; schemes that provide the species give their sum, so
185  * nothing is counted twice (the same rule as the 2D plotfile precipitation
186  * diagnostics). Without any source the field is zero. A box whose 3D box does
187  * not hold the k = klo plane (a fine level that does not span z, or the upper
188  * box of a column split in z) has no ground to read and stays zero, so no
189  * accumulator is read outside its box. Used by the fire layer's
190  * fuel moisture (ERF_FirePrecip.H) and the force-restore precipitation rate
191  * (ERF_ForceRestorePrecipRate.H).
192  *
193  * With phase = Frozen the field is the frozen part, snow + graupel + hail
194  * (zero for a scheme without those accumulators). With phase = Liquid it is
195  * the rest: the total minus the frozen part, or without a total the rain
196  * accumulator. Frozen plus Liquid is then the Total field for every scheme
197  * with a total, including one whose total holds more than its species
198  * (Morrison's also counts the cloud water and cloud ice that fell out, and
199  * both stay in its Liquid part). Where sources.rest_is_frozen is set (WSM6,
200  * WDM6: the rest is cloud ice), Frozen is the total minus the rain and Liquid
201  * is the rain. The
202  * part that is a difference is not bounded at zero: Liquid where the frozen
203  * accumulators exceed the total by round-off, and Frozen (rest_is_frozen)
204  * where the rain exceeds the total by round-off. A caller that takes the
205  * change over a step bounds that change (force_restore_rain_rate).
206  */
207 inline void surface_precip_accum_k0 (amrex::MultiFab& accum_2d,
208  const SurfacePrecipAccumulationSources& sources,
209  int klo,
211 {
212  surface_precip_fill_k0(accum_2d, sources, klo, phase, false);
213 }
214 
215 /**
216  * @brief The Liquid accumulation in component 0 and the Frozen one in component 1 of
217  * accum_2d (two components), from one pass over the accumulators; each as
218  * surface_precip_accum_k0 gives it.
219  */
220 inline void surface_precip_liquid_frozen_accum_k0 (amrex::MultiFab& accum_2d,
221  const SurfacePrecipAccumulationSources& sources,
222  int klo)
223 {
224  surface_precip_fill_k0(accum_2d, sources, klo, SurfacePrecipPhase::Liquid, true);
225 }
226 
227 #endif
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
const int klo
Definition: ERF_InitCustomPert_KE.H:9
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
void surface_precip_accum_k0(amrex::MultiFab &accum_2d, const SurfacePrecipAccumulationSources &sources, int klo, SurfacePrecipPhase phase=SurfacePrecipPhase::Total)
Total liquid-water-equivalent surface precipitation accumulation [kg/m2] of every column,...
Definition: ERF_SurfacePrecipitation.H:207
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool surface_precip_has_total_source(const SurfacePrecipAccumulationSources &src) noexcept
Definition: ERF_SurfacePrecipitation.H:56
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool surface_precip_has_any_frozen_source(const SurfacePrecipAccumulationSources &src) noexcept
Definition: ERF_SurfacePrecipitation.H:78
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool surface_precip_has_source(const SurfacePrecipAccumulationSource &src) noexcept
Definition: ERF_SurfacePrecipitation.H:50
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool surface_precip_has_any_source(const SurfacePrecipAccumulationSources &src) noexcept
Definition: ERF_SurfacePrecipitation.H:68
SurfacePrecipPhase
Which part of the surface precipitation surface_precip_accum_k0 returns.
Definition: ERF_SurfacePrecipitation.H:87
void surface_precip_liquid_frozen_accum_k0(amrex::MultiFab &accum_2d, const SurfacePrecipAccumulationSources &sources, int klo)
The Liquid accumulation in component 0 and the Frozen one in component 1 of accum_2d (two components)...
Definition: ERF_SurfacePrecipitation.H:220
void surface_precip_fill_k0(amrex::MultiFab &accum_2d, const SurfacePrecipAccumulationSources &sources, int klo, SurfacePrecipPhase phase, bool frozen_in_comp1)
The work of surface_precip_accum_k0 and surface_precip_liquid_frozen_accum_k0: component 0 of accum_2...
Definition: ERF_SurfacePrecipitation.H:94
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool surface_precip_has_explicit_rain_source(const SurfacePrecipAccumulationSources &src) noexcept
Definition: ERF_SurfacePrecipitation.H:62
@ graupel
Definition: ERF_NOAHMP_Fields.H:140
@ rain
Definition: ERF_NOAHMP_Fields.H:138
@ total
Definition: ERF_NOAHMP_Fields.H:137
@ hail
Definition: ERF_NOAHMP_Fields.H:141
@ snow
Definition: ERF_NOAHMP_Fields.H:139
Definition: ERF_ConsoleIO.cpp:17
Definition: ERF_SurfacePrecipitation.H:21
const amrex::MultiFab * accum
Definition: ERF_SurfacePrecipitation.H:22
amrex::Real native_to_kg_m2
Definition: ERF_SurfacePrecipitation.H:23
Definition: ERF_SurfacePrecipitation.H:40
bool rest_is_frozen
Definition: ERF_SurfacePrecipitation.H:46
SurfacePrecipAccumulationSource rain
Definition: ERF_SurfacePrecipitation.H:42
SurfacePrecipAccumulationSource snow
Definition: ERF_SurfacePrecipitation.H:43
SurfacePrecipAccumulationSource hail
Definition: ERF_SurfacePrecipitation.H:45
SurfacePrecipAccumulationSource total
Definition: ERF_SurfacePrecipitation.H:41
SurfacePrecipAccumulationSource graupel
Definition: ERF_SurfacePrecipitation.H:44