ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
erf_detail_SurfaceLayer Namespace Reference

Classes

struct  NearestColumn
 
struct  LsmFluxColumn
 
struct  CoupledCurrentCells
 

Functions

Gpu::HostVector< Real > read_eb_roughness (std::istream &file, const std::string &fname, const Geometry &geom, int lev)
 
bool covers_columns (const Array4< const Real > &a, const Box &b)
 
NearestColumn surface_columns (const Geometry &geom, const IntVect &ng)
 
void fill_face_current (MultiFab &current, int dir, const iMultiFab *lmask, const NearestColumn &in_domain, Real speed, const CoupledCurrentCells *coupled)
 

Function Documentation

◆ covers_columns()

bool erf_detail_SurfaceLayer::covers_columns ( const Array4< const Real > &  a,
const Box &  b 
)

Whether a planar field covers the columns of b laterally; a null field covers nothing and is not tested.

186 {
187  return !a || (a.begin[0] <= b.smallEnd(0) && a.end[0] > b.bigEnd(0) &&
188  a.begin[1] <= b.smallEnd(1) && a.end[1] > b.bigEnd(1));
189 }

Referenced by SurfaceLayer::compute_SurfaceLayer_bcs().

Here is the caller graph for this function:

◆ fill_face_current()

void erf_detail_SurfaceLayer::fill_face_current ( MultiFab &  current,
int  dir,
const iMultiFab *  lmask,
const NearestColumn &  in_domain,
Real  speed,
const CoupledCurrentCells *  coupled 
)

Set one component of the surface current on the faces normal to dir, ghost cells included. Only water moves: a face takes the current where the cells on both sides of it are water (land mask 0) and, with a coupler, covered by the ocean; it is at rest elsewhere. Without a coupler the current is uniform; with one it is the average over the two cells of the coupler's current turned to the grid axes.

Parameters
[out]currentone component of the current, on 2D boxes at k = klo
[in]dir0 for the x-component on x-faces, 1 for the y-component on y-faces
[in]lmaskland mask on one 2D box for each box of the current, or null
[in]in_domainthe cells a face reads (surface_columns)
[in]speeduniform current [m/s], used without a coupler
[in]coupledthe coupler's current at the cells, or null without a coupler
247 {
248  const bool has_mask = (lmask != nullptr);
249  const bool is_coupled = (coupled != nullptr);
250  const int di = (dir == 0) ? 1 : 0;
251  const int dj = (dir == 1) ? 1 : 0;
252 #ifdef _OPENMP
253 #pragma omp parallel if (Gpu::notInLaunchRegion())
254 #endif
255  for (MFIter mfi(current, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
256  const auto c = current.array(mfi);
257  const auto m = has_mask ? lmask->const_array(mfi) : Array4<const int>{};
258  const int km = has_mask ? amrex::lbound(m).z : 0;
259  const auto en = is_coupled ? coupled->east_north->const_array(mfi) : Array4<const Real>{};
260  const auto rot = is_coupled ? coupled->rotation->const_array(mfi) : Array4<const Real>{};
261  const auto cov = is_coupled ? coupled->covered->const_array(mfi) : Array4<const int>{};
262  ParallelFor(mfi.growntilebox(), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
263  {
264  bool moves = true;
265  Real sum = zero;
266  for (int side = 0; side < 2; ++side) {
267  const int ii = in_domain.i(i - di * (1 - side));
268  const int jj = in_domain.j(j - dj * (1 - side));
269  moves = moves && (!has_mask || m(ii,jj,km) == 0) &&
270  (!is_coupled || cov(ii,jj,0) != 0);
271  if (is_coupled) {
272  Real u_grid, v_grid;
273  erf_earth_wind::earth_to_grid(en(ii,jj,0,0), en(ii,jj,0,1),
274  rot(ii,jj,0,0), rot(ii,jj,0,1),
275  u_grid, v_grid);
276  sum += (dir == 0) ? u_grid : v_grid;
277  }
278  }
279  c(i,j,k) = !moves ? zero : (is_coupled ? myhalf * sum : speed);
280  });
281  }
282 }
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);})
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void earth_to_grid(amrex::Real u_east, amrex::Real v_north, amrex::Real cos_alpha, amrex::Real sin_alpha, amrex::Real &u, amrex::Real &v) noexcept
Definition: ERF_EarthRelativeWind.H:39
real(c_double), private di
Definition: ERF_module_mp_morr_two_moment.F90:203
const iMultiFab * covered
Definition: ERF_SurfaceLayer.cpp:226
const MultiFab * east_north
Definition: ERF_SurfaceLayer.cpp:224
const MultiFab * rotation
Definition: ERF_SurfaceLayer.cpp:225
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int j(int jj) const noexcept
Definition: ERF_SurfaceLayer.cpp:42
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int i(int ii) const noexcept
Definition: ERF_SurfaceLayer.cpp:39

Referenced by SurfaceLayer::fill_surface_current().

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

◆ read_eb_roughness()

Gpu::HostVector<Real> erf_detail_SurfaceLayer::read_eb_roughness ( std::istream &  file,
const std::string &  fname,
const Geometry &  geom,
int  lev 
)
50 {
51  const std::string context = "MOST roughness file \"" + fname + "\" at level " +
52  std::to_string(lev);
53  const Box& dom = geom.Domain();
54  const int nx = dom.length(0), ny = dom.length(1);
55  const Long nvalues = Long(nx) * Long(ny);
56  if (nvalues > std::numeric_limits<int>::max()) {
57  Abort(context + ": horizontal grid is too large for the roughness broadcast");
58  }
59  Gpu::HostVector<Real> values(nvalues);
60  Vector<Long> source_line((Long(nx)+1) * (Long(ny)+1), -1);
61  const auto dx = geom.CellSizeArray();
62  const auto prob_lo = geom.ProbLoArray();
63  const auto prob_hi = geom.ProbHiArray();
64  const Real tol = Real(1.e-4);
65  const std::array<const char*, 3> names{"x", "y", "z0"};
66  auto location = [] (Real x, Real y) {
67  std::ostringstream os;
68  os << std::setprecision(std::numeric_limits<Real>::max_digits10)
69  << "(" << x << ", " << y << ")";
70  return os.str();
71  };
72 
73  std::string line;
74  Long lineno = 0, nrecords = 0;
75  while (std::getline(file, line)) {
76  ++lineno;
77  if (line.find_first_not_of(" \t\r") == std::string::npos) { continue; }
78  auto line_error = [&] (const std::string& problem) {
79  Abort(context + ": line " + std::to_string(lineno) + ": " + problem);
80  };
81  std::istringstream record(line);
82  std::array<std::string, 3> tokens;
83  std::string extra;
84  if (!(record >> tokens[0] >> tokens[1] >> tokens[2]) || record >> extra) {
85  line_error("expected exactly three numbers: x y z0");
86  }
87  std::array<Real, 3> point{};
88  for (int c = 0; c < 3; ++c) {
89  const char* first = tokens[c].c_str();
90  char* end = nullptr;
91  // strtod is available on older standard libraries. Suppress conversion
92  // exceptions so out-of-range input reports an error even with FPE traps.
93  std::fenv_t saved_env;
94  if (std::feholdexcept(&saved_env) != 0) {
95  line_error("could not suspend floating-point exceptions for input validation");
96  }
97  errno = 0;
98  const double parsed = std::strtod(first, &end);
99  const int parse_errno = errno;
100  std::fesetenv(&saved_env);
101  if (end == first || *end != '\0') {
102  line_error(std::string("invalid number for ") + names[c]);
103  }
104  // Keep representable subnormal values; strtod may set ERANGE for them.
105  if (parse_errno == ERANGE && (parsed == 0.0 || !std::isfinite(parsed))) {
106  line_error(std::string("number out of range for ") + names[c]);
107  }
108  if (!std::isfinite(parsed) || (c == 2 && parsed <= 0.0)) {
109  line_error(std::string(names[c]) +
110  (c == 2 ? " must be finite and strictly positive" : " must be finite"));
111  }
112  if (std::abs(parsed) > double(std::numeric_limits<Real>::max())) {
113  line_error(std::string("number out of range for ") + names[c]);
114  }
115  point[c] = Real(parsed);
116  if (c == 2 && point[c] <= zero) {
117  line_error("z0 must be finite and strictly positive at the model precision");
118  }
119  }
120  ++nrecords;
121  const Real x = point[0], y = point[1];
122  // Extra samples outside the model grid remain allowed, but every
123  // record above is checked even when it will not be used.
124  if (x < prob_lo[0]-tol || x > prob_hi[0]+tol ||
125  y < prob_lo[1]-tol || y > prob_hi[1]+tol) { continue; }
126  const Real ri = std::round((x-prob_lo[0]) / dx[0]);
127  const Real rj = std::round((y-prob_lo[1]) / dx[1]);
128  if (ri < zero || ri > Real(nx) || rj < zero || rj > Real(ny)) { continue; }
129  const int i = static_cast<int>(ri), j = static_cast<int>(rj);
130  const Real model_x = prob_lo[0] + i*dx[0];
131  const Real model_y = prob_lo[1] + j*dx[1];
132  if (std::hypot(x-model_x, y-model_y) >= tol) { continue; }
133  const Long node = Long(i) + Long(j)*(Long(nx)+1);
134  if (source_line[node] >= 0) {
135  line_error("duplicate grid location " + location(model_x, model_y) +
136  ", already supplied on line " + std::to_string(source_line[node]));
137  }
138  source_line[node] = lineno;
139  if (i < nx && j < ny) { values[Long(i)+Long(j)*nx] = point[2]; }
140  }
141  if (file.bad()) { Abort(context + ": could not read the complete file"); }
142  if (nrecords == 0) { Abort(context + ": holds no (x, y, z0) triples"); }
143  for (int j = 0; j < ny; ++j) {
144  for (int i = 0; i < nx; ++i) {
145  if (source_line[Long(i)+Long(j)*(Long(nx)+1)] < 0) {
146  Abort(context + ": missing horizontal model coordinate " +
147  location(prob_lo[0]+i*dx[0], prob_lo[1]+j*dx[1]) +
148  "; domain-wide horizontal coverage is required");
149  }
150  }
151  }
152  return values;
153 }
std::vector< int > point
Definition: ERF_ActuatorSpreading.cpp:221
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
const int nx
Definition: ERF_InitCustomPertVels_CloudChamber.H:14
const int ny
Definition: ERF_InitCustomPertVels_CloudChamber.H:15
const GpuArray< Real, AMREX_SPACEDIM > prob_lo
Definition: ERF_InitCustomPert_CloudChamber.H:34
const amrex::Real * prob_hi
Definition: ERF_InitCustomPert_DataAssimilation_ISV.H:17

Referenced by SurfaceLayer::read_custom_roughness().

Here is the caller graph for this function:

◆ surface_columns()

NearestColumn erf_detail_SurfaceLayer::surface_columns ( const Geometry &  geom,
const IntVect &  ng 
)

The cell a face of the surface current reads for a cell beside it: the cell itself, or outside a non-periodic domain the nearest one inside it, since the surface fields' ghost cells there hold no data. The periodic ghost cells, ng deep, hold their images.

It also picks the column whose lowest state cell gives the surface pressure of a cell of a planar surface field, halo included. The choice depends on the domain only, never on the box holding the cell, and the cell read lies in the domain or its periodic image: a valid cell of the box or a ghost cell of the state, filled by FillBoundary from another grid of the level or a periodic image, or at a coarse/fine boundary by FillPatch from the coarser level.

Parameters
[in]geomgeometry of the level
[in]nglateral ghost cells of the surface fields read
208 {
209  Box columns = geom.Domain();
210  for (int d = 0; d < 2; ++d) {
211  if (geom.isPeriodic(d)) { columns.grow(d, ng[d]); }
212  }
213  return NearestColumn(columns);
214 }
@ ng
Definition: ERF_Morrison.H:51
Definition: ERF_SurfaceLayer.cpp:32

Referenced by SurfaceLayer::fill_surface_current(), SurfaceLayer::fill_tsurf_with_coupled_sst(), SurfaceLayer::fill_tsurf_with_sfc_sst(), and SurfaceLayer::fill_tsurf_with_sst_and_tsk().

Here is the caller graph for this function: