ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SEBSoilInit.cpp File Reference
#include <algorithm>
#include <cmath>
#include <limits>
#include <AMReX.H>
#include <AMReX_GpuContainers.H>
#include <AMReX_MFIter.H>
#include <AMReX_Math.H>
#include <AMReX_Reduce.H>
#include "ERF_SEBLandMask.H"
#include "ERF_SEBSoilInit.H"
Include dependency graph for ERF_SEBSoilInit.cpp:

Functions

bool seb_soil_layer_weights (const Vector< Real > &thickness, const Real depth, Vector< Real > &weights)
 
SEBSoilInitReport seb_init_soil_moisture_from_file (MultiFab &q, const MultiFab &file_moisture, const Vector< Real > &weights, const iMultiFab *land_mask, const Real q_lo, const Real q_hi)
 

Function Documentation

◆ seb_init_soil_moisture_from_file()

SEBSoilInitReport seb_init_soil_moisture_from_file ( MultiFab &  q,
const MultiFab &  file_moisture,
const Vector< Real > &  weights,
const iMultiFab *  land_mask,
const Real  q_lo,
const Real  q_hi 
)
45 {
46  if (q.boxArray() != file_moisture.boxArray() ||
47  q.DistributionMap() != file_moisture.DistributionMap()) {
48  Abort("seb_init_soil_moisture_from_file: the file's soil moisture is not on the grids "
49  "of the balance's soil water");
50  }
51  const int nlayers = static_cast<int>(weights.size());
52  if (nlayers < 1 || nlayers != file_moisture.nComp()) {
53  Abort("seb_init_soil_moisture_from_file: " + std::to_string(nlayers) +
54  " layer weight(s) for a soil moisture field of " +
55  std::to_string(file_moisture.nComp()) + " layer(s)");
56  }
57  seb_assert_land_mask_layout(land_mask, q, "seb_init_soil_moisture_from_file");
58 
59  // The weights on the device; the vector outlives the kernels below.
60  Gpu::DeviceVector<Real> weights_d(nlayers);
61  Gpu::copy(Gpu::hostToDevice, weights.begin(), weights.end(), weights_d.begin());
62  const Real* w = weights_d.data();
63 
64  // One pass: the mean, the copy and the counts. A refused column is left as it was, so
65  // the caller's message is the only effect of a bad value.
66  ReduceOps<ReduceOpSum, ReduceOpSum, ReduceOpMin, ReduceOpMax, ReduceOpMax> reduce_op;
67  ReduceData<Long, Long, Real, Real, Real> reduce_data(reduce_op);
68  using ReduceTuple = typename decltype(reduce_data)::Type;
69  const Real huge = std::numeric_limits<Real>::max();
70  // The single-precision allowance at the two bounds (seb_soil_value_rel_tol)
71  const Real q_lo_ok = q_lo - seb_soil_value_rel_tol * std::abs(q_lo);
72  const Real q_hi_ok = q_hi + seb_soil_value_rel_tol * std::abs(q_hi);
73 
74  for (MFIter mfi(q, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
75  const Box& bx = mfi.tilebox();
76  const Array4<Real>& dst = q.array(mfi);
77  const Array4<const Real>& file = file_moisture.const_array(mfi);
78  const SEBLandMaskView mask = seb_land_mask_view(land_mask, mfi);
79  reduce_op.eval(bx, reduce_data,
80  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept -> ReduceTuple
81  {
82  if (mask.water(i, j)) { return {Long(0), Long(0), huge, -huge, -huge}; }
83  Real mean = Real(0.0);
84  Real lowest = huge; // of the layers the mean is made of
85  Real highest = -huge;
86  for (int n = 0; n < nlayers; ++n) {
87  // A layer below the depth has no weight and is not read
88  if (w[n] > Real(0.0)) {
89  const Real value = file(i, j, k, n);
90  mean += w[n] * value;
91  lowest = amrex::min(lowest, value);
92  highest = amrex::max(highest, value);
93  }
94  }
95  if (!amrex::Math::isfinite(mean)) { return {Long(0), Long(1), huge, -huge, -huge}; }
96  // The weights sum to one only to rounding, so the sum of a uniform profile can
97  // be one unit in the last place off the layer value. A mean lies between the
98  // lowest and the highest of its layers: a profile that sits on a bound of the
99  // usable range (water content 1, or the porosity, in every layer) is then on it.
100  mean = amrex::min(amrex::max(mean, lowest), highest);
101  if (mean < q_lo_ok || mean > q_hi_ok) { return {Long(0), Long(1), huge, -huge, mean}; }
102  // Inside the allowance: the bound itself, so no column starts outside the range
103  mean = amrex::min(amrex::max(mean, q_lo), q_hi);
104  dst(i, j, k) = mean;
105  return {Long(1), Long(0), mean, mean, -huge};
106  });
107  }
108 
109  // The reduction is read here, before weights_d goes out of scope; value() synchronizes
110  const ReduceTuple result = reduce_data.value(reduce_op);
111  SEBSoilInitReport report;
112  report.n_set = amrex::get<0>(result);
113  report.n_refused = amrex::get<1>(result);
114  report.q_min = amrex::get<2>(result);
115  report.q_max = amrex::get<3>(result);
116  report.q_refused_max = amrex::get<4>(result);
117  return report;
118 }
SEBLandMaskView seb_land_mask_view(const amrex::iMultiFab *mask, const amrex::MFIter &mfi)
The view of mask on the box of mfi; off when mask is nullptr.
Definition: ERF_SEBLandMask.H:54
void seb_assert_land_mask_layout(const amrex::iMultiFab *mask, const amrex::FabArrayBase &field, const char *who)
Stop when mask is given and is not on the grids of field.
Definition: ERF_SEBLandMask.H:43
constexpr amrex::Real seb_soil_value_rel_tol
Relative distance outside [q_lo, q_hi] at which seb_init_soil_moisture_from_file still takes a mean,...
Definition: ERF_SEBSoilInit.H:51
@ q
Definition: ERF_WSM6.H:296
The land mask of one box, for a device kernel. Captured by value.
Definition: ERF_SEBLandMask.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool water(int i, int j) const noexcept
True when the balance does not run on column (i, j).
Definition: ERF_SEBLandMask.H:35
What seb_init_soil_moisture_from_file did on the boxes of this rank.
Definition: ERF_SEBSoilInit.H:76
amrex::Real q_min
lowest value set [m^3/m^3]; meaningful when n_set > 0
Definition: ERF_SEBSoilInit.H:79
amrex::Real q_refused_max
highest finite refused mean; the lowest Real when no refused mean is finite
Definition: ERF_SEBSoilInit.H:82
amrex::Long n_refused
land columns whose mean is not usable (left as they were)
Definition: ERF_SEBSoilInit.H:78
amrex::Long n_set
land columns that took the file's mean
Definition: ERF_SEBSoilInit.H:77
amrex::Real q_max
Definition: ERF_SEBSoilInit.H:80

Referenced by ERF::init_seb_soil_moisture_from_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ seb_soil_layer_weights()

bool seb_soil_layer_weights ( const Vector< Real > &  thickness,
const Real  depth,
Vector< Real > &  weights 
)
18 {
19  weights.clear();
20  if (thickness.empty() || !(std::isfinite(depth) && depth > Real(0.0))) { return false; }
21 
22  Vector<Real> overlap(thickness.size(), Real(0.0));
23  Real top = Real(0.0); // depth of the top of layer n
24  Real covered = Real(0.0); // sum of the overlaps
25  for (int n = 0; n < static_cast<int>(thickness.size()); ++n) {
26  if (!(std::isfinite(thickness[n]) && thickness[n] > Real(0.0))) { return false; }
27  const Real bottom = top + thickness[n];
28  overlap[n] = std::max(Real(0.0), std::min(bottom, depth) - top);
29  covered += overlap[n];
30  top = bottom;
31  }
32  if (covered < depth * (Real(1.0) - seb_soil_depth_rel_tol)) { return false; }
33 
34  weights.resize(thickness.size());
35  for (int n = 0; n < static_cast<int>(thickness.size()); ++n) {
36  weights[n] = overlap[n] / covered;
37  }
38  return true;
39 }
constexpr amrex::Real seb_soil_depth_rel_tol
Relative shortfall of the file's soil depth that seb_soil_layer_weights accepts: the thicknesses are ...
Definition: ERF_SEBSoilInit.H:40

Referenced by ERF::init_seb_soil_moisture_from_file().

Here is the caller graph for this function: