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

Namespaces

 erf_detail_ActuatorSampling
 

Classes

struct  StagedBuffer
 
struct  ActuatorDeviceBuffers
 
struct  ZBounds
 
class  LevelHeights
 
struct  ColumnCells
 
struct  CoverageGrids
 
class  RunningStats
 
class  WakeLines
 

Enumerations

enum class  CoverZ { Reach , Footprint , Column }
 

Functions

template<class V >
void grow_buffer (V &v, std::size_t n)
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real face_height (int dir, int i, int j, int k, amrex::Array4< amrex::Real const > const &znd, bool has_znd, amrex::Real zlo, amrex::Real dz)
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real face_volume (int dir, int i, int j, int k, amrex::Array4< amrex::Real const > const &detj, bool has_detj, amrex::Real dxdydz)
 
void check_same_points_everywhere (int npts, const char *who)
 Abort, naming who, unless every rank passes the same number of points (one collective). More...
 
std::vector< Real > wrap_periodic (const std::vector< Real > &pos, const Geometry &geom)
 
void sample_velocity_local (const MultiFab &U, const MultiFab &V, const MultiFab &W, const MultiFab *z_phys_nd, const Geometry &geom, const std::vector< Real > &wpos, std::vector< Real > &vel, std::vector< int > &cnt, ActuatorDeviceBuffers *buffers)
 
void sample_velocity (const MultiFab &U, const MultiFab &V, const MultiFab &W, const MultiFab *z_phys_nd, const Geometry &geom, const std::vector< Real > &pos, std::vector< Real > &vel)
 
void sample_cell_scalar (const MultiFab &mf, int comp, const MultiFab *z_phys_nd, const Geometry &geom, const std::vector< Real > &pos, std::vector< Real > &val)
 
void terrain_heights (const MultiFab *z_phys_nd, const Geometry &geom, const std::vector< Real > &pos, std::vector< Real > &h)
 
ZBounds mesh_z_bounds (const MultiFab &z_phys_nd, const Geometry &geom, bool over_all_ranks)
 
ZBounds mesh_z_levels (const MultiFab &z_phys_nd, const Geometry &geom)
 
void column_cell_ranges (const MultiFab &z_phys_nd, const Geometry &geom, const std::vector< Real > &pos, const std::vector< Real > &reach, ColumnCells &cells)
 
bool points_covered_by (const BoxArray &ba, const Geometry &geom, const std::vector< Real > &pos, Real reach, std::string &first_outside, CoverZ z, const ZBounds *zb, const ColumnCells *cells)
 
bool points_covered_by (const CoverageGrids &grids, const Geometry &geom, const std::vector< Real > &pos, Real reach, std::string &first_outside, CoverZ z, const ZBounds *zb, const ColumnCells *cells)
 
bool region_touches_grids (const BoxArray &ba, const Geometry &geom, const RealBox &region, const ZBounds *zb)
 
void sample_velocity (const amrex::MultiFab &U, const amrex::MultiFab &V, const amrex::MultiFab &W, const amrex::MultiFab *z_phys_nd, const amrex::Geometry &geom, const std::vector< amrex::Real > &pos, std::vector< amrex::Real > &vel)
 
void sample_velocity_local (const amrex::MultiFab &U, const amrex::MultiFab &V, const amrex::MultiFab &W, const amrex::MultiFab *z_phys_nd, const amrex::Geometry &geom, const std::vector< amrex::Real > &wpos, std::vector< amrex::Real > &vel, std::vector< int > &cnt, ActuatorDeviceBuffers *buffers=nullptr)
 
std::vector< amrex::Real > wrap_periodic (const std::vector< amrex::Real > &pos, const amrex::Geometry &geom)
 
void sample_cell_scalar (const amrex::MultiFab &mf, int comp, const amrex::MultiFab *z_phys_nd, const amrex::Geometry &geom, const std::vector< amrex::Real > &pos, std::vector< amrex::Real > &val)
 
void terrain_heights (const amrex::MultiFab *z_phys_nd, const amrex::Geometry &geom, const std::vector< amrex::Real > &pos, std::vector< amrex::Real > &h)
 
ZBounds mesh_z_bounds (const amrex::MultiFab &z_phys_nd, const amrex::Geometry &geom, bool over_all_ranks=true)
 
ZBounds mesh_z_levels (const amrex::MultiFab &z_phys_nd, const amrex::Geometry &geom)
 
void column_cell_ranges (const amrex::MultiFab &z_phys_nd, const amrex::Geometry &geom, const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &reach, ColumnCells &cells)
 
bool points_covered_by (const amrex::BoxArray &ba, const amrex::Geometry &geom, const std::vector< amrex::Real > &pos, amrex::Real reach, std::string &first_outside, CoverZ z=CoverZ::Reach, const ZBounds *zb=nullptr, const ColumnCells *cells=nullptr)
 
bool points_covered_by (const CoverageGrids &grids, const amrex::Geometry &geom, const std::vector< amrex::Real > &pos, amrex::Real reach, std::string &first_outside, CoverZ z=CoverZ::Reach, const ZBounds *zb=nullptr, const ColumnCells *cells=nullptr)
 
bool region_touches_grids (const amrex::BoxArray &ba, const amrex::Geometry &geom, const amrex::RealBox &region, const ZBounds *zb=nullptr)
 
void spread_forces (const std::vector< Real > &pos, const std::vector< Real > &force, Real epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z)
 
std::string spread_points_problem (const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon)
 
void check_spread_points (const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon)
 
void check_spread_layout (const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const MultiFab &src_x, const MultiFab &src_y, const MultiFab &src_z)
 
void spread_partial_norms (const std::vector< Real > &pos, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, const MultiFab &src_x, std::vector< Real > &norm, const ZBounds *zb_local, ActuatorDeviceBuffers *buffers)
 
void add_scaled_kernels (const std::vector< Real > &pos, const std::vector< Real > &f_over_s, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z, const ZBounds *zb_local, ActuatorDeviceBuffers *buffers)
 
void add_spread_forces (const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z)
 
void spread_forces (const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z)
 
Real integrate_source (int dir, const MultiFab &src, const MultiFab *detJ_cc, const Geometry &geom)
 
void spread_forces (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &force, const std::vector< amrex::Real > &epsilon, const amrex::MultiFab *z_phys_nd, const amrex::MultiFab *detJ_cc, const amrex::Geometry &geom, amrex::MultiFab &src_x, amrex::MultiFab &src_y, amrex::MultiFab &src_z)
 
void add_spread_forces (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &force, const std::vector< amrex::Real > &epsilon, const amrex::MultiFab *z_phys_nd, const amrex::MultiFab *detJ_cc, const amrex::Geometry &geom, amrex::MultiFab &src_x, amrex::MultiFab &src_y, amrex::MultiFab &src_z)
 
void spread_forces (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &force, amrex::Real epsilon, const amrex::MultiFab *z_phys_nd, const amrex::MultiFab *detJ_cc, const amrex::Geometry &geom, amrex::MultiFab &src_x, amrex::MultiFab &src_y, amrex::MultiFab &src_z)
 
void spread_partial_norms (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &epsilon, const amrex::MultiFab *z_phys_nd, const amrex::MultiFab *detJ_cc, const amrex::Geometry &geom, const amrex::MultiFab &src_x, std::vector< amrex::Real > &norm, const ZBounds *zb_local=nullptr, ActuatorDeviceBuffers *buffers=nullptr)
 
void add_scaled_kernels (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &f_over_s, const std::vector< amrex::Real > &epsilon, const amrex::MultiFab *z_phys_nd, const amrex::MultiFab *detJ_cc, const amrex::Geometry &geom, amrex::MultiFab &src_x, amrex::MultiFab &src_y, amrex::MultiFab &src_z, const ZBounds *zb_local=nullptr, ActuatorDeviceBuffers *buffers=nullptr)
 
std::string spread_points_problem (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &force, const std::vector< amrex::Real > &epsilon)
 
void check_spread_points (const std::vector< amrex::Real > &pos, const std::vector< amrex::Real > &force, const std::vector< amrex::Real > &epsilon)
 
void check_spread_layout (const amrex::MultiFab *z_phys_nd, const amrex::MultiFab *detJ_cc, const amrex::MultiFab &src_x, const amrex::MultiFab &src_y, const amrex::MultiFab &src_z)
 
amrex::Real integrate_source (int dir, const amrex::MultiFab &src, const amrex::MultiFab *detJ_cc, const amrex::Geometry &geom)
 
bool open_log (std::ofstream &out, const std::string &fname, bool truncate)
 
bool file_exists_everywhere (const std::string &fname)
 

Detailed Description

Device memory that the actuator spreading and sampling kernels keep from one call to the next.

Face heights and face volumes of the staggered grid, shared by the actuator sampler and spreader.

Interpolation of the flow and of the terrain height at arbitrary points, and the check that points lie on a level's grids.

Spreading of point forces onto the face-centered momentum sources with a normalized Gaussian kernel, and the integral check.

Opening of the moving-bodies diagnostics files for a fresh or a restarted run.

Running mean, rms, minimum and maximum of a body's scalar diagnostics, carried across checkpoints.

Wake sampling lines behind one rotor and their running time average.

Enumeration Type Documentation

◆ CoverZ

enum erf_actuator::CoverZ
strong

Return whether every point, with the cells within reach meters around it, lies inside the union of boxes of a level (the anchor level's grids): the sampler needs the neighboring cells and the spreader the kernel's reach, and on a refined level that is not the whole domain. In a periodic direction the needed cells wrap across the seam to the domain's other side; in a non-periodic one they are clamped to the domain. Returns false and names the first point outside in first_outside. On a terrain-following or stretched mesh a point's k is not its height over dz, so z is checked as z says: Reach (the cells within reach of the height over dz, for a flat, uniform mesh), Footprint (the reach across only, at any height: the sampler then finds a point above or below the grids by itself), or Column (for the spreading, whose kernel would otherwise be cut where the grids stop: the reach across over every cell the heights within reach may lie in, over the point's own columns from cells (column_cell_ranges, one range per point), else whatever the column from the mesh's bounds zb (mesh_z_bounds), or over the whole height without either). With cells a point whose range is empty (kb > kt) is not covered. Not with a periodic z.

Enumerator
Reach 
Footprint 
Column 

Function Documentation

◆ add_scaled_kernels() [1/2]

void erf_actuator::add_scaled_kernels ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  f_over_s,
const std::vector< amrex::Real > &  epsilon,
const amrex::MultiFab *  z_phys_nd,
const amrex::MultiFab *  detJ_cc,
const amrex::Geometry &  geom,
amrex::MultiFab &  src_x,
amrex::MultiFab &  src_y,
amrex::MultiFab &  src_z,
const ZBounds *  zb_local = nullptr,
ActuatorDeviceBuffers *  buffers = nullptr 
)

The second pass, on this rank's boxes only: add (F_p / S_p) w_p to every face of every box (both copies of a shared face alike), each point with its own width.

The images are binned to tiles of 8^3 faces of each box: one kernel per box and face grid, one thread per face of the tiles some kernel reaches, which adds its terms in point then image order without atomics on the sources (deterministic, and the order of the point-by-point sum). On the host (no GPU; not from inside an OpenMP parallel region) each image instead adds into the faces of its own reach box, in the same image order: every face takes the same terms in the same order, so the sources are bit-identical, without the tile pass's walk over images that miss the face (with the compilers' standard floating-point contraction: with clang's -ffp-contract=fast, icpx's default, the two loops may fuse a kernel's squared distance differently, so the last bits may differ).

Parameters
[in]f_over_s3*n forces over their whole normalizations (N/m^3), component fastest; a zero skips the point's component
buffersdevice memory kept from call to call (the call uses its own when nullptr)

◆ add_scaled_kernels() [2/2]

void erf_actuator::add_scaled_kernels ( const std::vector< Real > &  pos,
const std::vector< Real > &  f_over_s,
const std::vector< Real > &  epsilon,
const MultiFab *  z_phys_nd,
const MultiFab *  detJ_cc,
const Geometry &  geom,
MultiFab &  src_x,
MultiFab &  src_y,
MultiFab &  src_z,
const ZBounds *  zb_local,
ActuatorDeviceBuffers *  buffers 
)
464 {
465  BL_PROFILE("erf_actuator::add_scaled_kernels()");
466  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(buffers == nullptr || !OpenMP::in_parallel(),
467  "add_scaled_kernels: kept buffers must not be shared by the threads of an OpenMP parallel region");
468  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos.size() == 3 * epsilon.size() && f_over_s.size() == pos.size(),
469  "add_scaled_kernels: one kernel width and one x,y,z scale per x,y,z point");
470  const int npts = static_cast<int>(epsilon.size());
471  if (npts == 0) { return; }
472  ActuatorDeviceBuffers own;
473  ActuatorDeviceBuffers& buf = (buffers != nullptr) ? *buffers : own;
474  KernelFaces kf(geom, z_phys_nd, detJ_cc, zb_local);
475  const auto plo = kf.plo;
476  const auto dx = kf.dx;
477  const auto per = kf.per;
478  const auto dlo = kf.dlo;
479  const auto dhi = kf.dhi;
480  const bool has_znd = kf.has_znd;
481  MultiFab* src[3] = {&src_x, &src_y, &src_z};
482 
483  const KernelImages im(kf, pos, epsilon, buf);
484 #ifndef AMREX_USE_GPU
485  if (!buf.force_face_major && !OpenMP::in_parallel()) {
486  // On the host (where ParallelFor runs its iterations in order, OpenMP or not, as this pass is not called
487  // from a parallel region): each image adds into the faces of its own reach box, in image (point, then
488  // image) order. A face then takes the same terms in the same order as from the face-major pass below,
489  // whose threads (one per face, needed on a device, where images would race on a face) each walk every
490  // image binned to their tile, half of which miss the face
491  const std::vector<Real>& eps_host = buf.point_eps.host;
492  for (MFIter mfi(src_x, false); mfi.isValid(); ++mfi) {
493  Array4<Real const> znd = has_znd ? z_phys_nd->const_array(mfi) : Array4<Real const>{};
494  for (int dir = 0; dir < 3; ++dir) {
495  const Box fbx = mfi.validbox().convert(IntVect::TheDimensionVector(dir));
496  Array4<Real> const& s = src[dir]->array(mfi);
497  for (std::size_t m = 0; m < im.point.size(); ++m) {
498  const int p = im.point[m];
499  const Real scale = f_over_s[3*p+dir];
500  if (scale == Real(0.0)) { continue; }
501  const Box c = im.reach[m][dir] & fbx;
502  if (c.isEmpty()) { continue; }
503  const Real qx = im.pos[m][0], qy = im.pos[m][1], qz = im.pos[m][2];
504  const Real inv_eps2 = eps_host[2*p], reach2 = eps_host[2*p+1];
505  const auto lo = amrex::lbound(c);
506  const auto hi = amrex::ubound(c);
507  for (int k = lo.z; k <= hi.z; ++k) {
508  for (int j = lo.y; j <= hi.y; ++j) {
509  for (int i = lo.x; i <= hi.x; ++i) {
510  if (boundary_face(dir, i, j, k, per, dlo, dhi)) { continue; }
511  const Real w = kernel_weight(dir, i, j, k, qx, qy, qz, plo, dx, znd, has_znd, inv_eps2, reach2);
512  if (w > Real(0.0)) {
513  const Real term = scale * w; // a statement of its own, as in the first pass
514  s(i,j,k) += term;
515  }
516  }
517  }
518  }
519  }
520  }
521  }
522  return;
523  }
524 #endif
525  buf.scales.host.assign(f_over_s.begin(), f_over_s.end());
526  buf.scales.upload();
527  std::vector<std::array<Bin,3>> bin;
528  buf.bins.host.clear();
529  for (MFIter mfi(src_x, false); mfi.isValid(); ++mfi) {
530  std::array<Bin,3> bd;
531  for (int dir = 0; dir < 3; ++dir) {
532  bd[dir] = add_bin(im, f_over_s, dir, mfi.validbox().convert(IntVect::TheDimensionVector(dir)), buf.bins.host);
533  }
534  bin.push_back(bd);
535  }
536  buf.bins.upload();
537  const int* p_bins = buf.bins.data();
538  const Real* p_q = buf.image_pos.data();
539  const int* p_pt = buf.image_point.data();
540  const int* p_reach = buf.image_reach.data();
541  const Real* p_eps = buf.point_eps.data();
542  const Real* p_scale = buf.scales.data();
543 
544  // src(face) += (F_p / S_p) w_p on every face of every box, so that both copies of a shared or
545  // periodic image face carry the same value. One launch per box and face grid over the faces of
546  // the tiles some kernel reaches, one thread per face: a face adds the terms of the images binned
547  // to its tile, in point then image order, with no atomics
548  constexpr int t3 = tile_size * tile_size * tile_size;
549  int ib = 0;
550  for (MFIter mfi(src_x, false); mfi.isValid(); ++mfi, ++ib) {
551  Array4<Real const> znd = has_znd ? z_phys_nd->const_array(mfi) : Array4<Real const>{};
552  for (int dir = 0; dir < 3; ++dir) {
553  const Bin b = bin[ib][dir];
554  if (b.count == 0) { continue; }
555  const Box fbx = mfi.validbox().convert(IntVect::TheDimensionVector(dir));
556  const GpuArray<int,3> fhi{{fbx.bigEnd(0), fbx.bigEnd(1), fbx.bigEnd(2)}};
557  Array4<Real> const& s = src[dir]->array(mfi);
558  const int* corner = p_bins + b.start;
559  const int* offs = p_bins + b.start + 3 * b.count;
560  ++buf.kernel_launches;
561  ParallelFor(b.count * t3, [=] AMREX_GPU_DEVICE (int idx) noexcept
562  {
563  const int t = idx / t3;
564  const int l = idx - t * t3;
565  const int i = corner[3*t] + l % tile_size;
566  const int j = corner[3*t+1] + (l / tile_size) % tile_size;
567  const int k = corner[3*t+2] + l / (tile_size * tile_size);
568  if (i > fhi[0] || j > fhi[1] || k > fhi[2]) { return; }
569  if (boundary_face(dir, i, j, k, per, dlo, dhi)) { return; }
570  Real acc = s(i,j,k);
571  bool added = false;
572  for (int u = offs[t]; u < offs[t+1]; ++u) {
573  const int m = p_bins[u];
574  const int* r = p_reach + 18 * m + 6 * dir;
575  if (i < r[0] || j < r[1] || k < r[2] || i > r[3] || j > r[4] || k > r[5]) { continue; }
576  const int p = p_pt[m];
577  const Real w = kernel_weight(dir, i, j, k, p_q[3*m], p_q[3*m+1], p_q[3*m+2], plo, dx, znd, has_znd,
578  p_eps[2*p], p_eps[2*p+1]);
579  if (w > Real(0.0)) {
580  const Real term = p_scale[3*p+dir] * w; // a statement of its own, as in the first pass
581  acc += term;
582  added = true;
583  }
584  }
585  if (added) { s(i,j,k) = acc; }
586  });
587  }
588  }
589  Gpu::streamSynchronize();
590 }
bool has_znd
Definition: ERF_ActuatorSpreading.cpp:134
GpuArray< int, AMREX_SPACEDIM > per
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dhi
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dlo
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< Real, AMREX_SPACEDIM > plo
Definition: ERF_ActuatorSpreading.cpp:131
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
std::vector< std::array< Real, 3 > > pos
Definition: ERF_ActuatorSpreading.cpp:220
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:404
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);})
@ t
Definition: ERF_WSM6.H:295
@ p
Definition: ERF_WSM6.H:303
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12

Referenced by add_spread_forces().

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

◆ add_spread_forces() [1/2]

void erf_actuator::add_spread_forces ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  force,
const std::vector< amrex::Real > &  epsilon,
const amrex::MultiFab *  z_phys_nd,
const amrex::MultiFab *  detJ_cc,
const amrex::Geometry &  geom,
amrex::MultiFab &  src_x,
amrex::MultiFab &  src_y,
amrex::MultiFab &  src_z 
)

The same, but added to what the sources hold (spread_forces() sets them): for several sets of points spread one after the other. Collective: every rank must pass the same points.

◆ add_spread_forces() [2/2]

void erf_actuator::add_spread_forces ( const std::vector< Real > &  pos,
const std::vector< Real > &  force,
const std::vector< Real > &  epsilon,
const MultiFab *  z_phys_nd,
const MultiFab *  detJ_cc,
const Geometry &  geom,
MultiFab &  src_x,
MultiFab &  src_y,
MultiFab &  src_z 
)
596 {
598  check_spread_layout(z_phys_nd, detJ_cc, src_x, src_y, src_z);
599  const int npts = static_cast<int>(pos.size() / 3);
600  check_same_points_everywhere(npts, "spread_forces");
601  if (npts == 0) { return; }
602 
603  // pass 1: S_p, this rank's part, summed across ranks (this rank's bounds of a fitted mesh, once for both passes)
604  ZBounds zb;
605  if (z_phys_nd != nullptr) { zb = mesh_z_bounds(*z_phys_nd, geom, false); }
606  ActuatorDeviceBuffers buf;
607  std::vector<Real> norm;
608  spread_partial_norms(pos, epsilon, z_phys_nd, detJ_cc, geom, src_x, norm, &zb, &buf);
609  ParallelDescriptor::ReduceRealSum(norm.data(), 3 * npts);
610  for (int p = 0; p < npts; ++p) {
611  for (int dir = 0; dir < 3; ++dir) {
612  if (!(norm[3*p+dir] > Real(0.0))) {
613  Abort("spread_forces: point (" + std::to_string(pos[3*p]) + ", " + std::to_string(pos[3*p+1]) +
614  ", " + std::to_string(pos[3*p+2]) + ") reaches no face of the mesh within 3 epsilon = " +
615  std::to_string(3.0 * epsilon[p]) + " m; actuator points must lie inside the domain");
616  }
617  }
618  }
619  // pass 2: F_p w_p / S_p on the faces
620  std::vector<Real> f_over_s(3 * static_cast<std::size_t>(npts));
621  for (std::size_t q = 0; q < f_over_s.size(); ++q) { f_over_s[q] = force[q] / norm[q]; }
622  add_scaled_kernels(pos, f_over_s, epsilon, z_phys_nd, detJ_cc, geom, src_x, src_y, src_z, &zb, &buf);
623 }
ZBounds zb
Definition: ERF_ActuatorSpreading.cpp:135
@ q
Definition: ERF_WSM6.H:296
void check_spread_layout(const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const MultiFab &src_x, const MultiFab &src_y, const MultiFab &src_z)
Definition: ERF_ActuatorSpreading.cpp:110
void add_scaled_kernels(const std::vector< Real > &pos, const std::vector< Real > &f_over_s, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z, const ZBounds *zb_local, ActuatorDeviceBuffers *buffers)
Definition: ERF_ActuatorSpreading.cpp:460
void spread_partial_norms(const std::vector< Real > &pos, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, const MultiFab &src_x, std::vector< Real > &norm, const ZBounds *zb_local, ActuatorDeviceBuffers *buffers)
Definition: ERF_ActuatorSpreading.cpp:355
ZBounds mesh_z_bounds(const MultiFab &z_phys_nd, const Geometry &geom, bool over_all_ranks)
Definition: ERF_ActuatorSampling.cpp:508
void check_spread_points(const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon)
Definition: ERF_ActuatorSpreading.cpp:103
void check_same_points_everywhere(int npts, const char *who)
Abort, naming who, unless every rank passes the same number of points (one collective).
Definition: ERF_ActuatorSampling.cpp:88

Referenced by spread_forces().

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

◆ check_same_points_everywhere()

void erf_actuator::check_same_points_everywhere ( int  npts,
const char *  who 
)

Abort, naming who, unless every rank passes the same number of points (one collective).

89 {
90  int lo = npts, hi = npts;
91  ParallelDescriptor::ReduceIntMin(lo);
92  ParallelDescriptor::ReduceIntMax(hi);
93  if (lo != hi) {
94  Abort(std::string(who) + ": the ranks disagree on the number of points (" + std::to_string(lo) + " to " +
95  std::to_string(hi) + "); every rank must pass the same point list");
96  }
97 }

Referenced by add_spread_forces(), column_cell_ranges(), sample_cell_scalar(), sample_velocity(), and terrain_heights().

Here is the caller graph for this function:

◆ check_spread_layout() [1/2]

void erf_actuator::check_spread_layout ( const amrex::MultiFab *  z_phys_nd,
const amrex::MultiFab *  detJ_cc,
const amrex::MultiFab &  src_x,
const amrex::MultiFab &  src_y,
const amrex::MultiFab &  src_z 
)

Abort unless the sources are face-centered in x, y and z and share boxes and distribution with z_phys_nd and detJ_cc (one ghost cell).

◆ check_spread_layout() [2/2]

void erf_actuator::check_spread_layout ( const MultiFab *  z_phys_nd,
const MultiFab *  detJ_cc,
const MultiFab &  src_x,
const MultiFab &  src_y,
const MultiFab &  src_z 
)
112 {
113  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(src_x.ixType() == IndexType(IntVect(1,0,0)) && src_y.ixType() == IndexType(IntVect(0,1,0)) &&
114  src_z.ixType() == IndexType(IntVect(0,0,1)),
115  "spread_forces: src_x, src_y and src_z must be face-centred in x, y and z");
116  for (const MultiFab* mf : {&src_y, &src_z, z_phys_nd, detJ_cc}) {
117  if (mf == nullptr) { continue; }
118  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(mf->DistributionMap() == src_x.DistributionMap() && mf->boxArray().CellEqual(src_x.boxArray()),
119  "spread_forces: the sources, z_phys_nd and detJ_cc must share boxes and distribution");
120  }
121  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(detJ_cc == nullptr || detJ_cc->nGrow() >= 1,
122  "spread_forces: detJ_cc needs one ghost cell (the face volumes read the cell below each low face)");
123 }

Referenced by add_spread_forces(), and spread_forces().

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

◆ check_spread_points() [1/2]

void erf_actuator::check_spread_points ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  force,
const std::vector< amrex::Real > &  epsilon 
)

Abort unless the points, forces and widths match in number and are finite, and the widths positive (spread_forces' checks).

◆ check_spread_points() [2/2]

void erf_actuator::check_spread_points ( const std::vector< Real > &  pos,
const std::vector< Real > &  force,
const std::vector< Real > &  epsilon 
)
104 {
105  const std::string problem = spread_points_problem(pos, force, epsilon);
106  if (!problem.empty()) { Abort(problem); }
107 }
std::string spread_points_problem(const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon)
Definition: ERF_ActuatorSpreading.cpp:81

Referenced by add_spread_forces(), and spread_forces().

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

◆ column_cell_ranges() [1/2]

void erf_actuator::column_cell_ranges ( const amrex::MultiFab &  z_phys_nd,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  reach,
ColumnCells &  cells 
)

The cells a point's heights within reach may lie in, over the point's own columns: for point p (pos[3p .. 3p+2], reach[p] across and in height, in meters), the lowest and highest cell k such that, in some node column the reach across touches (a periodic x or y wrapping across the seam, as points_covered_by takes it), the cell's spacing [z(k), z(k+1)] overlaps [z - reach, z + reach] (to a relative 1e-5, as ZBounds::k_range), from every rank's boxes of z_phys_nd. Where those heights go below a box's lowest node in a column (the box's bottom face, unless it is the domain's floor), the cell under that box counts too, and where they go above a box's highest node (unless it is the domain's top), the cell above it: in a column split in z between boxes those cells are held by the next box, and where the level stops they are not, so a kernel the level cuts fails points_covered_by, whatever the boxes. The range is the hull [min k, max k] over the columns: points_covered_by never accepts a kernel some cell of which the level lacks, and accepts every kernel whose cells the level holds when, over the columns within reach, those cells form one run and no column has the heights below its ground under a box that starts above the floor. Elsewhere it is conservative: it also asks for the cells of a gap between two runs, or the cell under such a box (a cliff next to the point on a level aloft). The coverage answer is the same whatever the boxes; the hull itself may differ by cells the level holds in the same run. A reach partly below the floor or above the domain top needs nothing there, as with the mesh's bounds; one wholly beyond them touches no node and is not covered. kb > kt for a point whose reach touches no node of the level. A collective, the same on every rank: every rank passes the same points.

Parameters
[in]pos3*n positions (m)
[in]reachn reaches (m)
[out]cellsthe ranges of the n points

◆ column_cell_ranges() [2/2]

void erf_actuator::column_cell_ranges ( const MultiFab &  z_phys_nd,
const Geometry &  geom,
const std::vector< Real > &  pos,
const std::vector< Real > &  reach,
ColumnCells &  cells 
)
638 {
639  BL_PROFILE("erf_actuator::column_cell_ranges()");
640  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos.size() == 3 * reach.size(), "column_cell_ranges: one reach per point");
641  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!geom.isPeriodic(2), "column_cell_ranges: z must not be periodic");
642  const int npts = static_cast<int>(reach.size());
643  const Box& domain = geom.Domain();
644  const int dlo = domain.smallEnd(2), dhi = domain.bigEnd(2);
645  // nothing touched: an empty range (kb > kt), the domain's cells being dlo .. dhi
646  cells.kb.assign(npts, dhi + 1);
647  cells.kt.assign(npts, dlo - 1);
648  check_same_points_everywhere(npts, "column_cell_ranges");
649  if (npts == 0) { return; }
650  // the pieces: for every point, the node columns its reach across touches in each of this rank's boxes
651  std::vector<ColumnWork> work;
652  std::vector<Box> boxes;
653  std::vector<int> grid; // the box's index in z_phys_nd
654  for (MFIter mfi(z_phys_nd, false); mfi.isValid(); ++mfi) { boxes.push_back(mfi.validbox()); grid.push_back(mfi.index()); }
655  std::array<std::vector<std::array<int,2>>,2> ranges;
656  for (int p = 0; p < npts; ++p) {
657  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(std::isfinite(pos[3*p]) && std::isfinite(pos[3*p+1]) && std::isfinite(pos[3*p+2]) &&
658  std::isfinite(reach[p]) && reach[p] >= Real(0.0),
659  "column_cell_ranges: the points and their reach must be finite, the reach not negative");
660  const Real b[6] = {pos[3*p] - reach[p], pos[3*p+1] - reach[p], Real(0.0), pos[3*p] + reach[p], pos[3*p+1] + reach[p], Real(0.0)};
661  horizontal_node_ranges(b, geom, ranges);
662  for (int ib = 0; ib < static_cast<int>(boxes.size()); ++ib) {
663  const Box& nbx = boxes[ib];
664  for (const auto& ri : ranges[0]) {
665  for (const auto& rj : ranges[1]) {
666  const int i0 = std::max(ri[0], nbx.smallEnd(0)), i1 = std::min(ri[1], nbx.bigEnd(0));
667  const int j0 = std::max(rj[0], nbx.smallEnd(1)), j1 = std::min(rj[1], nbx.bigEnd(1));
668  if (i0 <= i1 && j0 <= j1) { work.push_back({p, ib, i0, i1, j0, j1}); }
669  }
670  }
671  }
672  }
673  if (!work.empty()) {
674  // the pieces sorted by box (one launch per box), with each box's first piece
675  std::stable_sort(work.begin(), work.end(), [] (const ColumnWork& a, const ColumnWork& c) { return a.b < c.b; });
676  std::vector<int> first_of(boxes.size() + 1, static_cast<int>(work.size()));
677  for (int w = static_cast<int>(work.size()) - 1; w >= 0; --w) { first_of[work[w].b] = w; }
678  for (int ib = static_cast<int>(boxes.size()) - 1; ib >= 0; --ib) { first_of[ib] = std::min(first_of[ib], first_of[ib+1]); }
679  // every device vector lives until the synchronization after the last launch
680  Gpu::DeviceVector<ColumnWork> d_work(work.size());
681  Gpu::DeviceVector<Real> d_pos(pos.size()), d_reach(reach.size());
682  Gpu::DeviceVector<int> d_kb(npts, dhi + 1), d_kt(npts, dlo - 1);
683  Gpu::copy(Gpu::hostToDevice, work.begin(), work.end(), d_work.begin());
684  Gpu::copy(Gpu::hostToDevice, pos.begin(), pos.end(), d_pos.begin());
685  Gpu::copy(Gpu::hostToDevice, reach.begin(), reach.end(), d_reach.begin());
686  const ColumnWork* p_work = d_work.data();
687  const Real* p_pos = d_pos.data();
688  const Real* p_reach = d_reach.data();
689  int* p_kb = d_kb.data();
690  int* p_kt = d_kt.data();
691  // the pieces of one box in one launch: each over its node columns, every cell of the column
692  for (int ib = 0; ib < static_cast<int>(boxes.size()); ++ib) {
693  const int w0 = first_of[ib], nw = first_of[ib+1] - first_of[ib];
694  if (nw == 0) { continue; }
695  const Box nbx = boxes[ib];
696  const int kbot = nbx.smallEnd(2), ktop = nbx.bigEnd(2);
697  Array4<Real const> const& znd = z_phys_nd.const_array(grid[ib]);
698  ParallelFor(nw, [=] AMREX_GPU_DEVICE (int m) noexcept
699  {
700  const ColumnWork w = p_work[w0 + m];
701  const Real zl0 = p_pos[3*w.p+2] - p_reach[w.p], zh0 = p_pos[3*w.p+2] + p_reach[w.p];
702  // the slack of ZBounds::k_range: the heights are rounded sums
703  const Real slack = Real(1.0e-5) * amrex::max(amrex::Math::abs(zl0), amrex::max(amrex::Math::abs(zh0), zh0 - zl0));
704  const Real zl = zl0 - slack, zh = zh0 + slack;
705  // from the empty range, not from the point's slot: another piece of the same point (a range
706  // split at a periodic seam) may be updating it
707  int kb = dhi + 1, kt = dlo - 1;
708  for (int j = w.j0; j <= w.j1; ++j) {
709  for (int i = w.i0; i <= w.i1; ++i) {
710  // the cells of this column whose spacing the heights overlap
711  for (int k = kbot; k < ktop; ++k) {
712  if (znd(i,j,k) <= zh && znd(i,j,k+1) >= zl) { kb = amrex::min(kb, k); kt = amrex::max(kt, k); }
713  }
714  // below the box's bottom face (not the floor): the cell under it; above its top (not the domain's): the cell over it
715  if (kbot > dlo && znd(i,j,kbot) > zl) { kb = amrex::min(kb, kbot - 1); }
716  if (ktop < dhi + 1 && znd(i,j,ktop) < zh) { kt = amrex::max(kt, ktop); }
717  }
718  }
719  Gpu::Atomic::Min(p_kb + w.p, kb);
720  Gpu::Atomic::Max(p_kt + w.p, kt);
721  });
722  }
723  Gpu::streamSynchronize();
724  Gpu::copy(Gpu::deviceToHost, d_kb.begin(), d_kb.end(), cells.kb.begin());
725  Gpu::copy(Gpu::deviceToHost, d_kt.begin(), d_kt.end(), cells.kt.begin());
726  }
727  ParallelDescriptor::ReduceIntMin(cells.kb.data(), npts);
728  ParallelDescriptor::ReduceIntMax(cells.kt.data(), npts);
729  // nothing overlapped (a reach wholly above the level's top or below its bottom, or beside it across): one
730  // empty range, whatever the boxes (a box's bottom or top face the heights lie beyond leaves its own cell)
731  for (int p = 0; p < npts; ++p) {
732  if (cells.kb[p] > cells.kt[p]) { cells.kb[p] = dhi + 1; cells.kt[p] = dlo - 1; }
733  }
734 }
std::vector< std::array< Box, 3 > > reach
Definition: ERF_ActuatorSpreading.cpp:222
void horizontal_node_ranges(const Real *b, const Geometry &geom, std::array< std::vector< std::array< int, 2 >>, 2 > &ranges)
Definition: ERF_ActuatorSampling.cpp:585
Here is the call graph for this function:

◆ face_height()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_actuator::face_height ( int  dir,
int  i,
int  j,
int  k,
amrex::Array4< amrex::Real const > const &  znd,
bool  has_znd,
amrex::Real  zlo,
amrex::Real  dz 
)

Return the height (m) of the center of face (i,j,k) of the staggered grid dir (0: x face, 1: y face, 2: z face): the mean of the four mesh nodes of that face on a stretched or terrain-following mesh (znd, has_znd true), or the nominal height on a uniform-dz mesh: zlo + k dz for a z face (z face k is the bottom face of cell k), zlo + (k + 1/2) dz otherwise. Shared by the velocity sampling and the force spreading so that both see the same face positions.

23 {
24  using amrex::Real;
25  if (!has_znd) {
26  return (dir == 2) ? zlo + k * dz : zlo + (k + Real(0.5)) * dz;
27  }
28  if (dir == 0) {
29  return Real(0.25) * (znd(i,j,k) + znd(i,j+1,k) + znd(i,j,k+1) + znd(i,j+1,k+1));
30  }
31  if (dir == 1) {
32  return Real(0.25) * (znd(i,j,k) + znd(i+1,j,k) + znd(i,j,k+1) + znd(i+1,j,k+1));
33  }
34  return Real(0.25) * (znd(i,j,k) + znd(i+1,j,k) + znd(i,j+1,k) + znd(i+1,j+1,k));
35 }
@ dz
Definition: ERF_AdvanceWDM6.cpp:273

Referenced by erf_actuator::erf_detail_ActuatorSampling::centre_height(), erf_actuator::erf_detail_ActuatorSampling::column_value(), sample_cell_scalar(), and sample_velocity_local().

Here is the caller graph for this function:

◆ face_volume()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_actuator::face_volume ( int  dir,
int  i,
int  j,
int  k,
amrex::Array4< amrex::Real const > const &  detj,
bool  has_detj,
amrex::Real  dxdydz 
)

Return the volume (m^3) attached to face (i,j,k) of the staggered grid dir: half of each neighboring cell, with the cell volumes dx dy dz detJ on a stretched or terrain-following mesh (detJ = 1 otherwise). Reads detj at i-1 (x), j-1 (y) or k-1 (z): detJ needs one ghost cell at a box's low faces.

47 {
48  using amrex::Real;
49  if (!has_detj) { return dxdydz; }
50  if (dir == 0) { return Real(0.5) * (detj(i-1,j,k) + detj(i,j,k)) * dxdydz; }
51  if (dir == 1) { return Real(0.5) * (detj(i,j-1,k) + detj(i,j,k)) * dxdydz; }
52  return Real(0.5) * (detj(i,j,k-1) + detj(i,j,k)) * dxdydz;
53 }
Real dxdydz
Definition: ERF_ActuatorSpreading.cpp:133
bool has_detj
Definition: ERF_ActuatorSpreading.cpp:134

Referenced by integrate_source(), and spread_partial_norms().

Here is the caller graph for this function:

◆ file_exists_everywhere()

bool erf_actuator::file_exists_everywhere ( const std::string &  fname)
inline

Whether fname exists, as the I/O rank sees it, on every rank: a checkpoint read that every rank enters (ParallelDescriptor::ReadAndBcastFile) must be decided alike everywhere, whatever a parallel file system shows each node.

44 {
45  int exists = amrex::ParallelDescriptor::IOProcessor() ? (amrex::FileExists(fname) ? 1 : 0) : 0;
46  amrex::ParallelDescriptor::Bcast(&exists, 1, amrex::ParallelDescriptor::IOProcessorNumber());
47  return exists != 0;
48 }

Referenced by erf_actuator::RunningStats::read_state(), and erf_actuator::WakeLines::read_state().

Here is the caller graph for this function:

◆ grow_buffer()

template<class V >
void erf_actuator::grow_buffer ( V &  v,
std::size_t  n 
)

Grow v to hold at least n values; never shrink it, so that a buffer keeps its largest size.

19 {
20  if (v.size() < n) { v.resize(n); }
21 }

Referenced by erf_actuator::StagedBuffer< T >::download_async(), and erf_actuator::StagedBuffer< T >::upload().

Here is the caller graph for this function:

◆ integrate_source() [1/2]

amrex::Real erf_actuator::integrate_source ( int  dir,
const amrex::MultiFab &  src,
const amrex::MultiFab *  detJ_cc,
const amrex::Geometry &  geom 
)

Return the integral of a face-centered source over the mesh, sum of src dV (the total force, N).

Parameters
dir0, 1 or 2: src holds the x-, y- or z-face component

◆ integrate_source() [2/2]

Real erf_actuator::integrate_source ( int  dir,
const MultiFab &  src,
const MultiFab *  detJ_cc,
const Geometry &  geom 
)
640 {
641  BL_PROFILE("erf_actuator::integrate_source()");
642  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(dir >= 0 && dir <= 2 && src.ixType() == IndexType(IntVect::TheDimensionVector(dir)),
643  "integrate_source: src must be face-centred in direction dir (0, 1 or 2)");
644  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(detJ_cc == nullptr || detJ_cc->nGrow() >= 1, "integrate_source: detJ_cc needs one ghost cell");
645  const auto dx = geom.CellSizeArray();
646  const Real dxdydz = dx[0] * dx[1] * dx[2];
647  const bool has_detj = (detJ_cc != nullptr);
648  ReduceOps<ReduceOpSum> reduce_op;
649  ReduceData<Real> reduce_data(reduce_op);
650  using ReduceTuple = typename decltype(reduce_data)::Type;
651  for (MFIter mfi(src, false); mfi.isValid(); ++mfi) {
652  // each physical face once: a box's cells' low faces
653  const Box fbx = owned_faces(dir, amrex::enclosedCells(mfi.validbox()));
654  Array4<Real const> const& s = src.const_array(mfi);
655  Array4<Real const> detj = has_detj ? detJ_cc->const_array(mfi) : Array4<Real const>{};
656  reduce_op.eval(fbx, reduce_data, [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple
657  {
658  return { s(i,j,k) * face_volume(dir, i, j, k, detj, has_detj, dxdydz) };
659  });
660  }
661  Real total = amrex::get<0>(reduce_data.value(reduce_op));
662  ParallelDescriptor::ReduceRealSum(total);
663  return total;
664 }
@ total
Definition: ERF_NOAHMP_Fields.H:137
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real face_volume(int dir, int i, int j, int k, amrex::Array4< amrex::Real const > const &detj, bool has_detj, amrex::Real dxdydz)
Definition: ERF_ActuatorGeometry.H:44
Here is the call graph for this function:

◆ mesh_z_bounds() [1/2]

ZBounds erf_actuator::mesh_z_bounds ( const amrex::MultiFab &  z_phys_nd,
const amrex::Geometry &  geom,
bool  over_all_ranks = true 
)

The bounds of a terrain-following or stretched level's heights, from its z_phys_nd (nodal, one component): a height Z lies in a cell k with floor((Z - ground_hi) / dz_hi) <= k <= floor((Z - ground_lo) / dz_lo). Over every rank's boxes, the same on every rank (a collective), or with over_all_ranks false over this rank's boxes only, which bounds the cells of those boxes alone. With over_all_ranks false and a box of this rank that does not reach the ground (a column split in z, a level aloft), where that formula does not hold, also the table of the boxes' node heights (ZBounds::has_table). The table is this rank's: points_covered_by takes the global bounds only.

◆ mesh_z_bounds() [2/2]

ZBounds erf_actuator::mesh_z_bounds ( const MultiFab &  z_phys_nd,
const Geometry &  geom,
bool  over_all_ranks 
)
509 {
510  BL_PROFILE("erf_actuator::mesh_z_bounds()");
511  const int kground = geom.Domain().smallEnd(2);
512  const Real big = std::numeric_limits<Real>::max();
513  ReduceOps<ReduceOpMin, ReduceOpMax, ReduceOpMin, ReduceOpMax> ops;
514  ReduceData<Real, Real, Real, Real> data(ops);
515  using T = typename decltype(data)::Type;
516  for (MFIter mfi(z_phys_nd, false); mfi.isValid(); ++mfi) {
517  const Box nbx = mfi.validbox();
518  const int ktop = nbx.bigEnd(2);
519  Array4<Real const> const& znd = z_phys_nd.const_array(mfi);
520  ops.eval(nbx, data, [=] AMREX_GPU_DEVICE (int i, int j, int k) -> T
521  {
522  const bool ground = (k == kground);
523  const bool above = (k < ktop); // the spacing to the node above, in this box
524  const Real dz = above ? znd(i,j,k+1) - znd(i,j,k) : Real(0.0);
525  return {ground ? znd(i,j,k) : big, ground ? znd(i,j,k) : -big, above ? dz : big, above ? dz : -big};
526  });
527  }
528  const T r = data.value(ops);
529  ZBounds b;
530  b.ground_lo = amrex::get<0>(r);
531  b.ground_hi = amrex::get<1>(r);
532  b.dz_lo = amrex::get<2>(r);
533  b.dz_hi = amrex::get<3>(r);
534  int all = 1;
535  for (MFIter mfi(z_phys_nd, false); mfi.isValid(); ++mfi) { all = (mfi.validbox().smallEnd(2) == kground) ? all : 0; }
536  if (over_all_ranks) {
537  ParallelDescriptor::ReduceRealMin(b.ground_lo);
538  ParallelDescriptor::ReduceRealMax(b.ground_hi);
539  ParallelDescriptor::ReduceRealMin(b.dz_lo);
540  ParallelDescriptor::ReduceRealMax(b.dz_hi);
541  ParallelDescriptor::ReduceIntMin(all);
542  }
543  b.all_ground = (all != 0);
544  if (!over_all_ranks && !b.all_ground) {
545  // where some of this rank's boxes do not reach the ground
546  node_level_table(z_phys_nd, geom, b);
547  }
548  return b;
549 }
@ T
Definition: ERF_IndexDefines.H:180
void node_level_table(const MultiFab &z_phys_nd, const Geometry &geom, ZBounds &b)
Definition: ERF_ActuatorSampling.cpp:461

Referenced by add_spread_forces(), and mesh_z_levels().

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

◆ mesh_z_levels() [1/2]

ZBounds erf_actuator::mesh_z_levels ( const amrex::MultiFab &  z_phys_nd,
const amrex::Geometry &  geom 
)

mesh_z_bounds over every rank's boxes, with the table of the level's node heights over every rank's boxes too (ZBounds::has_table): each node level's lowest and highest height and the cells the level holds, the same on every rank (a collective). ZBounds::k_range then gives the cells of the level a height range may lie in whether or not the boxes reach the ground. For region_touches_grids; points_covered_by takes the bounds without a table.

◆ mesh_z_levels() [2/2]

ZBounds erf_actuator::mesh_z_levels ( const MultiFab &  z_phys_nd,
const Geometry &  geom 
)
553 {
554  BL_PROFILE("erf_actuator::mesh_z_levels()");
555  ZBounds b = mesh_z_bounds(z_phys_nd, geom, true);
556  node_level_table(z_phys_nd, geom, b);
557  // over every rank's boxes: a rank with no box at a node level holds the largest and the lowest Real there
558  const int nnode = static_cast<int>(b.node_lo.size());
559  ParallelDescriptor::ReduceRealMin(b.node_lo.data(), nnode);
560  ParallelDescriptor::ReduceRealMax(b.node_hi.data(), nnode);
561  std::vector<int> has(b.has_cell.begin(), b.has_cell.end());
562  ParallelDescriptor::ReduceIntMax(has.data(), static_cast<int>(has.size()));
563  for (std::size_t n = 0; n < has.size(); ++n) { b.has_cell[n] = static_cast<char>(has[n]); }
564  return b;
565 }

Referenced by erf_actuator::LevelHeights::get().

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

◆ open_log()

bool erf_actuator::open_log ( std::ofstream &  out,
const std::string &  fname,
bool  truncate 
)
inline

Open a diagnostics text file for writing (the caller writes CSV or space-separated rows), creating its directory. With truncate (a fresh run) the file starts empty; without it (a restarted run) the rows are appended to what the run it continues wrote. Returns whether the caller must write the header: always after truncating, and on append only when the file is new or empty (a restart in a clean directory).

23 {
24  const auto slash = fname.rfind('/');
25  if (slash != std::string::npos && !amrex::UtilCreateDirectory(fname.substr(0, slash), 0755)) {
26  amrex::Abort("cannot create the directory of the diagnostics file '" + fname + "'");
27  }
28  bool need_header = truncate;
29  if (!truncate) {
30  std::ifstream probe(fname, std::ios::ate);
31  need_header = !probe || probe.tellg() <= 0;
32  }
33  out.open(fname, truncate ? std::ios::trunc : std::ios::app);
34  if (!out) { amrex::Abort("cannot open moving-bodies diagnostics file '" + fname + "'"); }
35  return need_header;
36 }

Referenced by erf_actuator::RunningStats::write(), erf_actuator::WakeLines::write_average(), and erf_actuator::WakeLines::write_instantaneous().

Here is the caller graph for this function:

◆ points_covered_by() [1/4]

bool erf_actuator::points_covered_by ( const amrex::BoxArray &  ba,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  pos,
amrex::Real  reach,
std::string &  first_outside,
CoverZ  z = CoverZ::Reach,
const ZBounds *  zb = nullptr,
const ColumnCells *  cells = nullptr 
)

◆ points_covered_by() [2/4]

bool erf_actuator::points_covered_by ( const BoxArray &  ba,
const Geometry &  geom,
const std::vector< Real > &  pos,
Real  reach,
std::string &  first_outside,
CoverZ  z,
const ZBounds *  zb,
const ColumnCells *  cells 
)
754 {
755  const CoverageGrids grids(ba, geom);
756  return points_covered_by(grids, geom, pos, reach, first_outside, z, zb, cells);
757 }
bool points_covered_by(const CoverageGrids &grids, const Geometry &geom, const std::vector< Real > &pos, Real reach, std::string &first_outside, CoverZ z, const ZBounds *zb, const ColumnCells *cells)
Definition: ERF_ActuatorSampling.cpp:760

◆ points_covered_by() [3/4]

bool erf_actuator::points_covered_by ( const CoverageGrids &  grids,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  pos,
amrex::Real  reach,
std::string &  first_outside,
CoverZ  z = CoverZ::Reach,
const ZBounds *  zb = nullptr,
const ColumnCells *  cells = nullptr 
)

points_covered_by on boxes built once (CoverageGrids of the same level and geometry).

◆ points_covered_by() [4/4]

bool erf_actuator::points_covered_by ( const CoverageGrids &  grids,
const Geometry &  geom,
const std::vector< Real > &  pos,
Real  reach,
std::string &  first_outside,
CoverZ  z,
const ZBounds *  zb,
const ColumnCells *  cells 
)
762 {
763  BL_PROFILE("erf_actuator::points_covered_by()");
764  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!geom.isPeriodic(2), "points_covered_by: z must not be periodic");
765  // bounds of a level with no box at the ground (ground_lo > ground_hi) carry no spacing, and the whole
766  // column is asked for; any other bounds must have a positive spacing
767  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(zb == nullptr || zb->ground_lo > zb->ground_hi ||
768  (zb->dz_lo > Real(0.0) && zb->dz_hi >= zb->dz_lo),
769  "points_covered_by: the levels' spacing must be positive");
770  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(zb == nullptr || !zb->has_table(),
771  "points_covered_by: zb must be the bounds over all ranks (mesh_z_bounds with over_all_ranks = true), "
772  "not one rank's table");
773  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(cells == nullptr || (cells->kb.size() == pos.size() / 3 && cells->kt.size() == pos.size() / 3),
774  "points_covered_by: one cell range per point");
775  first_outside.clear();
776  const BoxArray& cc = grids.cells;
777  const BoxArray& footprints = grids.footprints;
778  const auto plo = geom.ProbLoArray();
779  const auto dxi = geom.InvCellSizeArray();
780  const Box& domain = geom.Domain();
781  for (std::size_t p = 0; p < pos.size() / 3; ++p) {
782  // per direction, the index ranges of the cells needed: one range, or two in a periodic
783  // direction whose range crosses the seam (the part beyond it wrapped to the other side)
784  std::array<std::vector<std::array<int,2>>,3> ranges;
785  bool none = false; // the point needs cells the level has nowhere
786  for (int d = 0; d < 3; ++d) {
787  int lo = static_cast<int>(std::floor((pos[3*p+d] - reach - plo[d]) * dxi[d]));
788  int hi = static_cast<int>(std::floor((pos[3*p+d] + reach - plo[d]) * dxi[d]));
789  const int dlo = domain.smallEnd(d), dhi = domain.bigEnd(d), n = dhi - dlo + 1;
790  if (geom.isPeriodic(d)) {
791  // shift by whole periods so that lo lies in the domain
792  const int s = static_cast<int>(std::floor(static_cast<double>(lo - dlo) / n));
793  lo -= s * n;
794  hi -= s * n;
795  if (hi - lo + 1 >= n) {
796  ranges[d].push_back({{dlo, dhi}});
797  } else if (hi <= dhi) {
798  ranges[d].push_back({{lo, hi}});
799  } else {
800  ranges[d].push_back({{lo, dhi}});
801  ranges[d].push_back({{dlo, hi - n}});
802  }
803  } else if (d == 2 && z == CoverZ::Footprint) {
804  ranges[d].push_back({{dlo, dlo}});
805  } else if (d == 2 && z == CoverZ::Column) {
806  if (cells != nullptr) {
807  // the cells of the point's own columns (column_cell_ranges); none: not covered
808  if (cells->kb[p] > cells->kt[p]) { none = true; }
809  ranges[d].push_back({{std::max(cells->kb[p], dlo), std::min(cells->kt[p], dhi)}});
810  } else if (zb == nullptr) {
811  ranges[d].push_back({{dlo, dhi}});
812  } else {
813  // every cell a height within reach may lie in, whatever column it is in (the global
814  // bounds: a rank's own table would make the answer depend on the rank)
815  int kb = dlo, kt = dhi;
816  zb->k_range(pos[3*p+2] - reach, pos[3*p+2] + reach, dlo, dhi, kb, kt);
817  ranges[d].push_back({{std::max(kb, dlo), std::min(kt, dhi)}});
818  }
819  } else {
820  ranges[d].push_back({{std::max(lo, dlo), std::min(hi, dhi)}});
821  }
822  }
823  bool covered = !none;
824  for (const auto& rx : ranges[0]) {
825  if (!covered) { break; }
826  for (const auto& ry : ranges[1]) {
827  for (const auto& rz : ranges[2]) {
828  const Box needed(IntVect(rx[0], ry[0], rz[0]), IntVect(rx[1], ry[1], rz[1]));
829  // a range outside a non-periodic domain gives an empty box, which is not covered
830  if ((z == CoverZ::Footprint) ? !footprints.contains(needed, false) : !cc.contains(needed, true)) { covered = false; }
831  }
832  }
833  }
834  if (!covered) {
835  first_outside = "(" + std::to_string(pos[3*p]) + ", " + std::to_string(pos[3*p+1]) + ", " + std::to_string(pos[3*p+2]) + ") m";
836  return false;
837  }
838  }
839  return true;
840 }
Here is the call graph for this function:

◆ region_touches_grids() [1/2]

bool erf_actuator::region_touches_grids ( const amrex::BoxArray &  ba,
const amrex::Geometry &  geom,
const amrex::RealBox &  region,
const ZBounds *  zb = nullptr 
)

Whether a level's boxes may hold a cell of a region (lo and hi in meters, lo <= hi in every direction, no NaN): the opposite question to points_covered_by, for a caller that must stay clear of a level. x and y use the cells the region's ends fall in (points_covered_by's own); in a periodic direction every image of the region counts, however many periods away, and a region a period or more long takes every cell. z must not be periodic. A region that overlaps a cell of the level is never missed; one that only meets a cell at a face may not count. In z:

  • on a uniform-dz mesh (zb null) the cells are the region's own, from prob_lo and the spacing;
  • with the level's table of node heights (mesh_z_levels), the cells k of the level some bottom node of which is not above the region and some top node not below it (ZBounds::k_range): right wherever the level's boxes are, on the ground or aloft, and no cell where the level has none at those heights;
  • with the level's bounds alone (mesh_z_bounds over all ranks) and every box on the ground, the cells between the ground and spacing extremes allow, which is wider on steep terrain;
  • with the bounds alone and a box that does not stand on the ground (ZBounds::all_ground false: a level or a box aloft, or a column split in z between boxes), the whole column: the answer is then yes wherever a box lies over or under the region's footprint. The cell indices are bounded before they become integers, so a region that reaches far outside the domain (or to infinity) is safe. The answer reads no field data and is the same on every rank when zb is: pass the table or the bounds over all ranks, not one rank's (which this function cannot tell apart).
Parameters
[in]bathe level's boxes (any index type)
[in]geomthe level's geometry
[in]regionthe region (m)
[in]zbthe level's table or bounds on a stretched or terrain-following mesh, or nullptr

◆ region_touches_grids() [2/2]

bool erf_actuator::region_touches_grids ( const BoxArray &  ba,
const Geometry &  geom,
const RealBox &  region,
const ZBounds *  zb 
)
844 {
845  BL_PROFILE("erf_actuator::region_touches_grids()");
846  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!geom.isPeriodic(2), "region_touches_grids: z must not be periodic");
847  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
848  // a NaN first: comparing one may trap (amrex.fpe_trap_invalid) before the message is printed
849  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(!std::isnan(region.lo(d)) && !std::isnan(region.hi(d)) && region.lo(d) <= region.hi(d),
850  "region_touches_grids: the region needs lo <= hi in every direction");
851  }
852  const Box& domain = geom.Domain();
853  const auto plo = geom.ProbLoArray();
854  const auto dxi = geom.InvCellSizeArray();
855  IntVect lo, hi;
856  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
857  // The region's cells, as points_covered_by finds a point's, kept as doubles until they are bounded: an int
858  // holds no infinity and no index far outside the domain
859  const int dlo = domain.smallEnd(d), dhi = domain.bigEnd(d), n = dhi - dlo + 1;
860  double l = std::floor(static_cast<double>((region.lo(d) - plo[d]) * dxi[d]));
861  double h = std::floor(static_cast<double>((region.hi(d) - plo[d]) * dxi[d]));
862  if (geom.isPeriodic(d)) {
863  // an infinite end is replaced by 0 before any difference is taken: inf - inf is invalid and may
864  // trap (amrex.fpe_trap_invalid), also where a compiler evaluates it ahead of a test that skips it
865  const bool unbounded = std::isinf(l) || std::isinf(h);
866  const double fl = unbounded ? 0.0 : l;
867  const double fh = unbounded ? 0.0 : h;
868  if (unbounded || !(fh - fl + 1 < n)) {
869  // a period or more, or an infinite extent: every cell
870  l = dlo;
871  h = dhi;
872  } else {
873  // by whole periods until lo lies in the domain; hi is then less than a period above it, and the
874  // shifts below bring the part beyond the seam back in
875  const double s = std::floor((fl - dlo) / n);
876  l = fl - s * n;
877  h = fh - s * n;
878  }
879  }
880  // one cell outside the domain stays outside it (and bounds what rounding leaves of a far periodic region)
881  lo[d] = static_cast<int>(std::min<double>(static_cast<double>(dhi) + n, std::max<double>(static_cast<double>(dlo) - 1, l)));
882  hi[d] = static_cast<int>(std::min<double>(static_cast<double>(dhi) + n, std::max<double>(static_cast<double>(dlo) - 1, h)));
883  }
884  if (zb != nullptr) {
885  // every cell a height of the region may lie in, whatever its column. With the table, from the level's own
886  // node heights. With the bounds alone, from the ground and the spacing, which describe a column only where
887  // every box stands on the ground; with a box that does not, the column is taken whole
888  int kb = domain.smallEnd(2), kt = domain.bigEnd(2);
889  if (zb->has_table() || zb->all_ground) { zb->k_range(region.lo(2), region.hi(2), domain.smallEnd(2), domain.bigEnd(2), kb, kt); }
890  lo[2] = kb;
891  hi[2] = kt;
892  }
893  if (lo[2] > hi[2]) { return false; } // the table holds no cell at these heights
894  const BoxArray cc = amrex::convert(ba, IntVect::TheZeroVector());
895  const Box needed(lo, hi);
896  for (const IntVect& s : geom.periodicity().shiftIntVect()) {
897  const Box b = (needed + s) & domain;
898  if (b.ok() && cc.intersects(b)) { return true; }
899  }
900  return false;
901 }
Here is the call graph for this function:

◆ sample_cell_scalar() [1/2]

void erf_actuator::sample_cell_scalar ( const amrex::MultiFab &  mf,
int  comp,
const amrex::MultiFab *  z_phys_nd,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  pos,
std::vector< amrex::Real > &  val 
)

Sample one component of a cell-centered field at a set of points: bilinear between the four cell centers around the point in the horizontal and linear in the physical height between the two cell centers that bracket it in the column (cell-center heights from z_phys_nd on a stretched or terrain-following mesh); below the first and above the last cell center the nearest pair extrapolates linearly. Same ownership rules and reduction as sample_velocity(); the field needs one filled ghost cell.

Parameters
[in]mfthe cell-centered field
[in]compthe component of mf (0-based)
[in]pos3*n point coordinates (m, ERF domain coordinates)
[out]valn sampled values, the same on every rank on return

◆ sample_cell_scalar() [2/2]

void erf_actuator::sample_cell_scalar ( const MultiFab &  mf,
int  comp,
const MultiFab *  z_phys_nd,
const Geometry &  geom,
const std::vector< Real > &  pos,
std::vector< Real > &  val 
)
284 {
285  BL_PROFILE("erf_actuator::sample_cell_scalar()");
286  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos.size() % 3 == 0, "sample_cell_scalar: pos holds x,y,z triples");
287  const int npts = static_cast<int>(pos.size() / 3);
288  val.assign(npts, 0.0);
289  check_same_points_everywhere(npts, "sample_cell_scalar");
290  if (npts == 0) { return; }
291  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(mf.nGrow() >= 1, "sample_cell_scalar: the field needs a filled ghost cell");
292  check_layout(mf, z_phys_nd, 1, "sample_cell_scalar: z_phys_nd");
293  const auto plo = geom.ProbLoArray();
294  const auto phi = geom.ProbHiArray();
295  const auto dxi = geom.InvCellSizeArray();
296  const Real dz = geom.CellSize(2);
297  const Box& domain = geom.Domain();
298  const int klo = domain.smallEnd(2), khi = domain.bigEnd(2);
299  const int ihi = domain.bigEnd(0), jhi = domain.bigEnd(1);
300  const bool has_znd = (z_phys_nd != nullptr);
301  const std::vector<Real> wpos = wrap_periodic(pos, geom);
302 
303  Gpu::DeviceVector<Real> d_pos(pos.size()), d_val(npts, 0.0);
304  Gpu::DeviceVector<int> d_cnt(npts, 0);
305  Gpu::copy(Gpu::hostToDevice, wpos.begin(), wpos.end(), d_pos.begin());
306  Real* p_pos = d_pos.data();
307  Real* p_val = d_val.data();
308  int* p_cnt = d_cnt.data();
309 
310  for (MFIter mfi(mf, false); mfi.isValid(); ++mfi) {
311  const Box vbx = mfi.validbox();
312  Array4<Real const> const& f = mf.const_array(mfi);
313  Array4<Real const> znd = has_znd ? z_phys_nd->const_array(mfi) : Array4<Real const>{};
314  ParallelFor(npts, [=] AMREX_GPU_DEVICE (int p) noexcept
315  {
316  const Real x = p_pos[3*p], y = p_pos[3*p+1], z = p_pos[3*p+2];
317  const Real xf = (x - plo[0]) * dxi[0], yf = (y - plo[1]) * dxi[1];
318  int ic = static_cast<int>(std::floor(xf));
319  int jc = static_cast<int>(std::floor(yf));
320  // a point on the domain's upper face belongs to the last cell (decided in space: x/dx can
321  // round one ulp above the cell count, as 1000 m on 88 cells does)
322  if (ic > ihi && x <= phi[0]) { ic = ihi; }
323  if (jc > jhi && y <= phi[1]) { jc = jhi; }
324  // the columns are searched within this box and its first ghost layer (filled), so a box split in z,
325  // or a fine level that stops below the domain's top, reads only its own data. A point the box owns
326  // has its own column's bracketing pair in that range; a neighboring column whose levels a slope
327  // raises or lowers by more than about half a cell, at the box's top or bottom, extrapolates from
328  // the range's end instead (a difference of the field's curvature, none for a field linear in height)
329  const int kb = amrex::max(klo, vbx.smallEnd(2) - 1);
330  const int kt = amrex::min(khi, vbx.bigEnd(2) + 1);
331  if (ic < vbx.smallEnd(0) || ic > vbx.bigEnd(0) ||
332  jc < vbx.smallEnd(1) || jc > vbx.bigEnd(1)) { return; }
333  int kc = -1;
334  if (!has_znd) {
335  int k = static_cast<int>(std::floor((z - plo[2]) * dxi[2]));
336  // a point on the domain's top face belongs to the top cell (the cell, as on the upper x and y faces;
337  // there the value is extrapolated from the top cells, as everywhere in z, not read from a ghost cell)
338  if (k > khi && z <= phi[2]) { k = khi; }
339  if (k >= klo && k <= kt) { kc = k; }
340  } else {
341  for (int k = kb; k <= kt; ++k) {
342  const Real zb = face_height(2, ic, jc, k, znd, true, plo[2], dz);
343  const Real zt = face_height(2, ic, jc, k+1, znd, true, plo[2], dz);
344  // the top cell holds its top face too (the domain's top)
345  if (z >= zb && (z < zt || (k == khi && z <= zt))) { kc = k; break; }
346  }
347  // the bottom face is the mean of its four nodes, but the ground under (x, y) is
348  // bilinear between them: on a slope a point just above the ground can lie below
349  // the face, and belongs to the bottom cell (read by extrapolation) as long as it is
350  // not below the lowest of the four nodes
351  if (kc < 0 && kb == klo && z < face_height(2, ic, jc, klo, znd, true, plo[2], dz) &&
352  z >= amrex::min(amrex::min(znd(ic,jc,klo), znd(ic+1,jc,klo)), amrex::min(znd(ic,jc+1,klo), znd(ic+1,jc+1,klo)))) {
353  kc = klo;
354  }
355  }
356  if (kc < vbx.smallEnd(2) || kc > vbx.bigEnd(2)) { return; }
357  // bilinear between the cell centers around (x, y), each column at height z
358  const Real xi = (x - plo[0]) * dxi[0] - Real(0.5);
359  const Real yi = (y - plo[1]) * dxi[1] - Real(0.5);
360  const int i0 = static_cast<int>(std::floor(xi));
361  const int j0 = static_cast<int>(std::floor(yi));
362  const Real wx = xi - i0, wy = yi - j0;
363  const Real f00 = centre_column_value(f, comp, i0, j0, kb, kt, z, znd, has_znd, plo[2], dz);
364  const Real f10 = centre_column_value(f, comp, i0+1, j0, kb, kt, z, znd, has_znd, plo[2], dz);
365  const Real f01 = centre_column_value(f, comp, i0, j0+1, kb, kt, z, znd, has_znd, plo[2], dz);
366  const Real f11 = centre_column_value(f, comp, i0+1, j0+1, kb, kt, z, znd, has_znd, plo[2], dz);
367  p_val[p] = (Real(1.0) - wy) * ((Real(1.0) - wx) * f00 + wx * f10) + wy * ((Real(1.0) - wx) * f01 + wx * f11);
368  // a count, not a flag: boxes of one rank may run on different streams, and a point held by two of
369  // them must still be refused by the caller
370  Gpu::Atomic::AddNoRet(&p_cnt[p], 1);
371  });
372  }
373  Gpu::streamSynchronize();
374  std::vector<int> cnt(npts, 0);
375  Gpu::copy(Gpu::deviceToHost, d_val.begin(), d_val.end(), val.begin());
376  Gpu::copy(Gpu::deviceToHost, d_cnt.begin(), d_cnt.end(), cnt.begin());
377  ParallelDescriptor::ReduceRealSum(val.data(), npts);
378  ParallelDescriptor::ReduceIntSum(cnt.data(), npts);
379  for (int p = 0; p < npts; ++p) {
380  if (cnt[p] != 1) {
381  Abort("sample_cell_scalar: point (" + std::to_string(pos[3*p]) + ", " + std::to_string(pos[3*p+1]) +
382  ", " + std::to_string(pos[3*p+2]) + ") was sampled by " + std::to_string(cnt[p]) +
383  " boxes; the points must lie inside the domain");
384  }
385  }
386 }
const int khi
Definition: ERF_InitCustomPert_Bubble.H:25
const int klo
Definition: ERF_InitCustomPert_KE.H:9
void check_layout(const MultiFab &ref, const MultiFab *other, int ngrow, const std::string &what)
Definition: ERF_ActuatorSampling.cpp:32
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real centre_column_value(Array4< Real const > const &f, int comp, int i, int j, int klo, int khi, Real z, Array4< Real const > const &znd, bool has_znd, Real zlo, Real dz)
Definition: ERF_ActuatorSampling.cpp:265
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real face_height(int dir, int i, int j, int k, amrex::Array4< amrex::Real const > const &znd, bool has_znd, amrex::Real zlo, amrex::Real dz)
Definition: ERF_ActuatorGeometry.H:20
std::vector< Real > wrap_periodic(const std::vector< Real > &pos, const Geometry &geom)
Definition: ERF_ActuatorSampling.cpp:100
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real phi(amrex::Real d, amrex::Real z0, amrex::Real psi) noexcept
Definition: ERF_ImmersedWallCell.H:176
real(c_double), private f11
Definition: ERF_module_mp_morr_two_moment.F90:232
Here is the call graph for this function:

◆ sample_velocity() [1/2]

void erf_actuator::sample_velocity ( const amrex::MultiFab &  U,
const amrex::MultiFab &  V,
const amrex::MultiFab &  W,
const amrex::MultiFab *  z_phys_nd,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  pos,
std::vector< amrex::Real > &  vel 
)

Sample the velocity at a set of points from the face-centered velocity fields.

Each component is interpolated trilinearly from its own staggered grid: u from the x faces, v from the y faces, w from the z faces, so a field linear in x, y and z is reproduced exactly. The vertical weight uses the physical face heights (z_phys_nd on a stretched or terrain-following mesh, prob_lo + k dz otherwise), so the same holds on those meshes as long as the field is linear in the physical height.

Every rank interpolates the points whose containing cell lies in one of its valid boxes, reading the neighbors from the ghost cells, and the results are summed across ranks; the velocity fields must therefore have at least one filled ghost cell, and z_phys_nd one ghost node, on the velocities' boxes and distribution. The ghost cells across the domain's lateral boundaries are read too (a point within half a cell of a non-periodic face takes up to half its value from them), so the fields must be filled there as well (FillPatch, not only FillBoundary). In a periodic direction a point is moved by whole periods into the domain; a point on the domain's upper x, y or z face belongs to the last cell. A point outside the domain otherwise, or one found by no rank or by more than one, aborts with its coordinates: the caller owns the points and must keep them inside the domain. Every rank must pass the same points: the reduction is collective and the call stops when the ranks disagree on their number. A model whose ranks hold different points (those of the bodies each owns) calls sample_velocity_local() instead, which has no such check.

Parameters
[in]U,V,Wface-centered velocities on one level, ghost cells filled (m/s)
[in]z_phys_ndnodal physical heights (m), or nullptr on a uniform-dz mesh
[in]geomthe level's geometry
[in]pos3*n point coordinates (x0,y0,z0, x1,y1,z1, ...) (m, ERF domain coordinates)
[out]vel3*n sampled velocities (m/s), the same on every rank on return

◆ sample_velocity() [2/2]

void erf_actuator::sample_velocity ( const MultiFab &  U,
const MultiFab &  V,
const MultiFab &  W,
const MultiFab *  z_phys_nd,
const Geometry &  geom,
const std::vector< Real > &  pos,
std::vector< Real > &  vel 
)
233 {
234  BL_PROFILE("erf_actuator::sample_velocity()");
235  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos.size() % 3 == 0, "sample_velocity: pos holds x,y,z triples");
236  const int npts = static_cast<int>(pos.size() / 3);
237  check_same_points_everywhere(npts, "sample_velocity");
238  std::vector<int> cnt;
239  sample_velocity_local(U, V, W, z_phys_nd, geom, wrap_periodic(pos, geom), vel, cnt);
240  if (npts == 0) { return; }
241  ParallelDescriptor::ReduceRealSum(vel.data(), static_cast<int>(vel.size()));
242  ParallelDescriptor::ReduceIntSum(cnt.data(), npts);
243 
244  for (int p = 0; p < npts; ++p) {
245  if (cnt[p] != 1) {
246  Abort("sample_velocity: point (" + std::to_string(pos[3*p]) + ", " + std::to_string(pos[3*p+1]) +
247  ", " + std::to_string(pos[3*p+2]) + ") was sampled by " + std::to_string(cnt[p]) +
248  " boxes; actuator points must lie inside the domain");
249  }
250  }
251 }
@ U
Definition: ERF_IndexDefines.H:178
@ V
Definition: ERF_IndexDefines.H:179
void sample_velocity_local(const MultiFab &U, const MultiFab &V, const MultiFab &W, const MultiFab *z_phys_nd, const Geometry &geom, const std::vector< Real > &wpos, std::vector< Real > &vel, std::vector< int > &cnt, ActuatorDeviceBuffers *buffers)
Definition: ERF_ActuatorSampling.cpp:119
@ W
Definition: ERF_ObsNudgingSeries.H:36
Here is the call graph for this function:

◆ sample_velocity_local() [1/2]

void erf_actuator::sample_velocity_local ( const amrex::MultiFab &  U,
const amrex::MultiFab &  V,
const amrex::MultiFab &  W,
const amrex::MultiFab *  z_phys_nd,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  wpos,
std::vector< amrex::Real > &  vel,
std::vector< int > &  cnt,
ActuatorDeviceBuffers *  buffers = nullptr 
)

The part of sample_velocity() that a rank does on its own: the velocity at each of the points that one of this rank's boxes holds (cnt 1), zero with cnt 0 at the others. No reduction and no check: the caller combines the ranks' answers. The points must already be moved into the domain across its periodic seams (wrap_periodic).

Parameters
[in]wpos3*n point coordinates inside the domain (m)
[out]vel3*n velocities (m/s), zero at the points no box of this rank holds
[out]cntn counts: how many of this rank's boxes hold each point (0 or 1)
buffersdevice memory kept from call to call (the call uses its own when nullptr); the points go to the device and the answers come back with asynchronous copies and one synchronization

◆ sample_velocity_local() [2/2]

void erf_actuator::sample_velocity_local ( const MultiFab &  U,
const MultiFab &  V,
const MultiFab &  W,
const MultiFab *  z_phys_nd,
const Geometry &  geom,
const std::vector< Real > &  wpos,
std::vector< Real > &  vel,
std::vector< int > &  cnt,
ActuatorDeviceBuffers *  buffers 
)
123 {
124  BL_PROFILE("erf_actuator::sample_velocity_local()");
125  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(wpos.size() % 3 == 0, "sample_velocity: pos holds x,y,z triples");
126  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(U.nGrow() >= 1 && V.nGrow() >= 1 && W.nGrow() >= 1,
127  "sample_velocity needs one filled ghost cell on the velocities");
128  check_layout(U, &V, 1, "sample_velocity: V");
129  check_layout(U, &W, 1, "sample_velocity: W");
130  check_layout(U, z_phys_nd, 1, "sample_velocity: z_phys_nd");
131  const int npts = static_cast<int>(wpos.size() / 3);
132  vel.assign(3 * static_cast<std::size_t>(npts), Real(0.0));
133  cnt.assign(static_cast<std::size_t>(npts), 0);
134  if (npts == 0) { return; }
135 
136  const Box& domain = geom.Domain();
137  const auto plo = geom.ProbLoArray();
138  const auto phi = geom.ProbHiArray();
139  const auto dxi = geom.InvCellSizeArray();
140  const Real dz = geom.CellSize(2);
141  const int klo = domain.smallEnd(2);
142  const int khi = domain.bigEnd(2);
143  const int ihi = domain.bigEnd(0), jhi = domain.bigEnd(1);
144  const bool has_znd = (z_phys_nd != nullptr);
145 
146  // the points, and zeros for the answers, to the device in one copy each (into buffers kept from call to call)
147  ActuatorDeviceBuffers own;
148  ActuatorDeviceBuffers& buf = (buffers != nullptr) ? *buffers : own;
149  buf.sample_pos.host.assign(wpos.begin(), wpos.end());
150  buf.sample_vel.host.assign(wpos.size(), Real(0.0));
151  buf.sample_cnt.host.assign(static_cast<std::size_t>(npts), 0);
152  buf.sample_pos.upload();
153  buf.sample_vel.upload();
154  buf.sample_cnt.upload();
155  const Real* p_pos = buf.sample_pos.data();
156  Real* p_vel = buf.sample_vel.data();
157  int* p_cnt = buf.sample_cnt.data();
158 
159  for (MFIter mfi(U, false); mfi.isValid(); ++mfi) {
160  // the cells this rank owns; a point is sampled by the box holding its containing cell
161  const Box vbx = amrex::enclosedCells(mfi.validbox());
162  Array4<Real const> const& u = U.const_array(mfi);
163  Array4<Real const> const& v = V.const_array(mfi);
164  Array4<Real const> const& w = W.const_array(mfi);
165  Array4<Real const> znd = has_znd ? z_phys_nd->const_array(mfi) : Array4<Real const>{};
166 
167  ++buf.kernel_launches;
168  ParallelFor(npts, [=] AMREX_GPU_DEVICE (int p) noexcept
169  {
170  const Real x = p_pos[3*p], y = p_pos[3*p+1], z = p_pos[3*p+2];
171  const Real xf = (x - plo[0]) * dxi[0], yf = (y - plo[1]) * dxi[1];
172  int ic = static_cast<int>(std::floor(xf));
173  int jc = static_cast<int>(std::floor(yf));
174  // a point on the domain's upper face belongs to the last cell (decided in space: x/dx can
175  // round one ulp above the cell count, as 1000 m on 88 cells does)
176  if (ic > ihi && x <= phi[0]) { ic = ihi; }
177  if (jc > jhi && y <= phi[1]) { jc = jhi; }
178  // the columns are searched within this box and its first ghost layer (filled), so a box split in z,
179  // or a fine level that stops below the domain's top, reads only its own data. A point the box owns
180  // has its own column's bracketing pair in that range; a neighboring column whose levels a slope
181  // raises or lowers by more than about half a cell, at the box's top or bottom, extrapolates from
182  // the range's end instead (a difference of the field's curvature, none for a field linear in height)
183  const int kb = amrex::max(klo, vbx.smallEnd(2) - 1);
184  const int kt = amrex::min(khi, vbx.bigEnd(2) + 1);
185  if (ic < vbx.smallEnd(0) || ic > vbx.bigEnd(0) ||
186  jc < vbx.smallEnd(1) || jc > vbx.bigEnd(1)) { return; }
187  // the containing cell in z: between the z faces of column (ic,jc)
188  int kc = -1;
189  if (!has_znd) {
190  int k = static_cast<int>(std::floor((z - plo[2]) * dxi[2]));
191  // a point on the domain's top face belongs to the top cell (the cell, as on the upper x and y faces;
192  // there the value is extrapolated from the top cells, as everywhere in z, not read from a ghost cell)
193  if (k > khi && z <= phi[2]) { k = khi; }
194  if (k >= klo && k <= kt) { kc = k; }
195  } else {
196  for (int k = kb; k <= kt; ++k) {
197  const Real zb = face_height(2, ic, jc, k, znd, true, plo[2], dz);
198  const Real zt = face_height(2, ic, jc, k+1, znd, true, plo[2], dz);
199  // the top cell holds its top face too (the domain's top)
200  if (z >= zb && (z < zt || (k == khi && z <= zt))) { kc = k; break; }
201  }
202  // the bottom face is the mean of its four nodes, but the ground under (x, y) is
203  // bilinear between them: on a slope a point just above the ground can lie below
204  // the face, and belongs to the bottom cell (read by extrapolation) as long as it is
205  // not below the lowest of the four nodes
206  if (kc < 0 && kb == klo && z < face_height(2, ic, jc, klo, znd, true, plo[2], dz) &&
207  z >= amrex::min(amrex::min(znd(ic,jc,klo), znd(ic+1,jc,klo)), amrex::min(znd(ic,jc+1,klo), znd(ic+1,jc+1,klo)))) {
208  kc = klo;
209  }
210  }
211  if (kc < vbx.smallEnd(2) || kc > vbx.bigEnd(2)) { return; }
212 
213  p_vel[3*p] = sample_component(0, u, x, y, z, plo, dxi, kb, kt, znd, has_znd, dz);
214  p_vel[3*p+1] = sample_component(1, v, x, y, z, plo, dxi, kb, kt, znd, has_znd, dz);
215  p_vel[3*p+2] = sample_component(2, w, x, y, z, plo, dxi, kb, kt+1, znd, has_znd, dz);
216  // a count, not a flag: boxes of one rank may run on different streams, and a point held by two of
217  // them must still be refused by the caller
218  Gpu::Atomic::AddNoRet(&p_cnt[p], 1);
219  });
220  }
221  // the answers back with asynchronous copies and one synchronization
222  buf.sample_vel.download_async(vel.size());
223  buf.sample_cnt.download_async(cnt.size());
224  Gpu::streamSynchronize();
225  std::copy(buf.sample_vel.pinned.begin(), buf.sample_vel.pinned.begin() + vel.size(), vel.begin());
226  std::copy(buf.sample_cnt.pinned.begin(), buf.sample_cnt.pinned.begin() + cnt.size(), cnt.begin());
227 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real sample_component(int dir, Array4< Real const > const &f, Real x, Real y, Real z, const GpuArray< Real, AMREX_SPACEDIM > &plo, const GpuArray< Real, AMREX_SPACEDIM > &dxi, int klo, int khi, Array4< Real const > const &znd, bool has_znd, Real dz)
Definition: ERF_ActuatorSampling.cpp:61

Referenced by sample_velocity().

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

◆ spread_forces() [1/4]

void erf_actuator::spread_forces ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  force,
amrex::Real  epsilon,
const amrex::MultiFab *  z_phys_nd,
const amrex::MultiFab *  detJ_cc,
const amrex::Geometry &  geom,
amrex::MultiFab &  src_x,
amrex::MultiFab &  src_y,
amrex::MultiFab &  src_z 
)

The same with one kernel width epsilon (m, > 0 and finite) for every point.

◆ spread_forces() [2/4]

void erf_actuator::spread_forces ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  force,
const std::vector< amrex::Real > &  epsilon,
const amrex::MultiFab *  z_phys_nd,
const amrex::MultiFab *  detJ_cc,
const amrex::Geometry &  geom,
amrex::MultiFab &  src_x,
amrex::MultiFab &  src_y,
amrex::MultiFab &  src_z 
)

Spread point forces onto the face-centered momentum sources with a 3-D Gaussian kernel.

The points are spread where they are given, with the periodic images within reach of the domain: a caller whose points may lie outside a periodic domain moves them inside first (wrap_periodic()).

Each point p carries a force F_p on the fluid (N) and has its own kernel width epsilon_p (the conductor lines and the towers of a run may differ). Its share of the source at a face is F_p w_p(face) / S_p, where w_p = exp(-|x_face - x_p|^2 / epsilon_p^2) is cut off beyond 3 epsilon_p and S_p = sum over the faces of that component of w_p dV is the discrete normalization with the face volumes (dx dy dz detJ on a terrain-following mesh). The sum of src dV over the mesh therefore equals F_p exactly for every point and component, whatever the resolution and however much of the kernel is cut off by the ground or the domain top; the kernel's shape is Gaussian only where it is resolved. The faces sit at prob_lo + i dx with the volumes dx dy dz detJ, with no map factors: a body model must refuse a run whose map factors are not 1 until the core carries them. The normalization sums and the sources are accumulated in a fixed order without atomic adds (spread_partial_norms(), add_scaled_kernels()), so the source does not depend on the device's scheduling.

Every rank accumulates the faces its boxes own (each physical face once, however the boxes share their boundary faces) and the normalizations are summed across ranks, so the result does not depend on the domain decomposition; every rank must pass the same points. Every periodic image of a point within 3 epsilon_p of the domain (in x and y; z is not imaged) adds its kernel, so a kernel wraps across a periodic boundary, and overlaps itself when the period is under 6 epsilon_p. The sources are set (not added to) on every valid face, both copies of a shared face alike; the faces on a non-periodic domain boundary get no source.

Aborts when a position or force is not finite. z_phys_nd and detJ_cc must share the boxes and distribution of src_x; src_x, src_y and src_z are face-centered in x, y and z.

Parameters
[in]pos3*n point coordinates (m, ERF domain coordinates)
[in]force3*n forces ON the fluid BY the body (N, ERF frame): minus the fluid's force on the body
[in]epsilonn kernel widths, one per point (m, > 0 and finite)
[in]z_phys_ndnodal heights (m), or nullptr on a uniform-dz mesh
[in]detJ_cccell Jacobians (1 ghost cell), or nullptr on a uniform-dz mesh
[in]geomthe level's geometry
[out]src_x,src_y,src_zface-centered momentum sources (N/m^3), one component each

◆ spread_forces() [3/4]

void erf_actuator::spread_forces ( const std::vector< Real > &  pos,
const std::vector< Real > &  force,
const std::vector< Real > &  epsilon,
const MultiFab *  z_phys_nd,
const MultiFab *  detJ_cc,
const Geometry &  geom,
MultiFab &  src_x,
MultiFab &  src_y,
MultiFab &  src_z 
)
629 {
631  check_spread_layout(z_phys_nd, detJ_cc, src_x, src_y, src_z);
632  src_x.setVal(0.0);
633  src_y.setVal(0.0);
634  src_z.setVal(0.0);
635  add_spread_forces(pos, force, epsilon, z_phys_nd, detJ_cc, geom, src_x, src_y, src_z);
636 }
void add_spread_forces(const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z)
Definition: ERF_ActuatorSpreading.cpp:593

Referenced by spread_forces().

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

◆ spread_forces() [4/4]

void erf_actuator::spread_forces ( const std::vector< Real > &  pos,
const std::vector< Real > &  force,
Real  epsilon,
const MultiFab *  z_phys_nd,
const MultiFab *  detJ_cc,
const Geometry &  geom,
MultiFab &  src_x,
MultiFab &  src_y,
MultiFab &  src_z 
)
72 {
73  // also without points, where the per-point check of the widths has nothing to refuse
74  if (!(epsilon > 0.0) || !std::isfinite(epsilon)) {
75  Abort("spread_forces: the kernel width " + std::to_string(epsilon) + " m must be positive and finite");
76  }
77  spread_forces(pos, force, std::vector<Real>(pos.size() / 3, epsilon), z_phys_nd, detJ_cc, geom, src_x, src_y, src_z);
78 }
void spread_forces(const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon, const MultiFab *z_phys_nd, const MultiFab *detJ_cc, const Geometry &geom, MultiFab &src_x, MultiFab &src_y, MultiFab &src_z)
Definition: ERF_ActuatorSpreading.cpp:626
Here is the call graph for this function:

◆ spread_partial_norms() [1/2]

void erf_actuator::spread_partial_norms ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  epsilon,
const amrex::MultiFab *  z_phys_nd,
const amrex::MultiFab *  detJ_cc,
const amrex::Geometry &  geom,
const amrex::MultiFab &  src_x,
std::vector< amrex::Real > &  norm,
const ZBounds *  zb_local = nullptr,
ActuatorDeviceBuffers *  buffers = nullptr 
)

The first of spread_forces()'s two passes, on this rank's boxes only: for each point and each component, this rank's part of the normalization S_p (the sum of w_p dV over the faces its boxes own). No reduction: the caller sums the ranks' parts (spread_forces() over every rank; a model whose ranks hold different points, over the ranks a point reaches). The boxes are src_x's, which z_phys_nd and detJ_cc must share. zb_local, when given, is this rank's mesh_z_bounds(z_phys_nd, geom, false), which the call then need not compute; the same for add_scaled_kernels.

The periodic images of the points are made once, and binned to the boxes: one kernel per box and face grid, one thread per point, which adds the point's terms in a fixed order (its images, then their faces k, j, i) without atomics, so the result does not depend on the device's scheduling. The call checks only the arrays' sizes: the caller checks the points, widths and layouts first (check_spread_points(), check_spread_layout()), as spread_forces() does; the same for add_scaled_kernels().

Parameters
[out]norm3*n partial normalizations (m^3), component fastest
buffersdevice memory kept from call to call (the call uses its own when nullptr)

◆ spread_partial_norms() [2/2]

void erf_actuator::spread_partial_norms ( const std::vector< Real > &  pos,
const std::vector< Real > &  epsilon,
const MultiFab *  z_phys_nd,
const MultiFab *  detJ_cc,
const Geometry &  geom,
const MultiFab &  src_x,
std::vector< Real > &  norm,
const ZBounds *  zb_local,
ActuatorDeviceBuffers *  buffers 
)
359 {
360  BL_PROFILE("erf_actuator::spread_partial_norms()");
361  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(buffers == nullptr || !OpenMP::in_parallel(),
362  "spread_partial_norms: kept buffers must not be shared by the threads of an OpenMP parallel region");
363  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos.size() == 3 * epsilon.size(), "spread_partial_norms: one kernel width per x,y,z point");
364  const int npts = static_cast<int>(epsilon.size());
365  norm.assign(3 * static_cast<std::size_t>(npts), Real(0.0));
366  if (npts == 0) { return; }
367  ActuatorDeviceBuffers own;
368  ActuatorDeviceBuffers& buf = (buffers != nullptr) ? *buffers : own;
369  KernelFaces kf(geom, z_phys_nd, detJ_cc, zb_local);
370  const auto plo = kf.plo;
371  const auto dx = kf.dx;
372  const auto per = kf.per;
373  const auto dlo = kf.dlo;
374  const auto dhi = kf.dhi;
375  const Real dxdydz = kf.dxdydz;
376  const bool has_znd = kf.has_znd;
377  const bool has_detj = kf.has_detj;
378 
379  // Work per point over the faces within its reach, so the cost is the sum over the points of
380  // (6 eps_p / dx)^3 ((6 eps_p / dx)^2 x nz with z_phys_nd) rather than nfaces x npts. A point near a periodic
381  // boundary is visited again as its periodic image(s), which is how the kernel wraps.
382  const KernelImages im(kf, pos, epsilon, buf);
383  buf.norms.host.assign(3 * static_cast<std::size_t>(npts), Real(0.0));
384  buf.norms.upload();
385 
386  // the bins of every box and face grid, sent in one copy
387  std::vector<std::array<Bin,3>> bin;
388  buf.bins.host.clear();
389  for (MFIter mfi(src_x, false); mfi.isValid(); ++mfi) {
390  const Box vbx = amrex::enclosedCells(mfi.validbox());
391  bin.push_back({{norm_bin(im, 0, owned_faces(0, vbx), buf.bins.host),
392  norm_bin(im, 1, owned_faces(1, vbx), buf.bins.host),
393  norm_bin(im, 2, owned_faces(2, vbx), buf.bins.host)}});
394  }
395  buf.bins.upload();
396  const int* p_bins = buf.bins.data();
397  const Real* p_q = buf.image_pos.data();
398  const int* p_reach = buf.image_reach.data();
399  const Real* p_eps = buf.point_eps.data();
400  Real* p_norm = buf.norms.data();
401 
402  // S_p = sum over the faces of w_p dV, each physical face once (the owned faces of this rank's
403  // boxes, boundary faces excluded). One launch per box and face grid, one thread per point (a
404  // segment of the images): a thread adds its point's terms in the order of the images and, for an
405  // image, of the faces (k, then j, then i), with no atomics, so that the sum does not depend on
406  // the device's scheduling. A point whose kernel reaches two boxes is updated by both launches,
407  // which read and write its slot of p_norm without atomics: the boxes therefore share one device
408  // stream, so that their launches run one after the other, in box order
409  int ib = 0;
410  for (MFIter mfi(src_x, MFItInfo().SetNumStreams(1)); mfi.isValid(); ++mfi, ++ib) {
411  const Box vbx = amrex::enclosedCells(mfi.validbox());
412  Array4<Real const> znd = has_znd ? z_phys_nd->const_array(mfi) : Array4<Real const>{};
413  Array4<Real const> detj = has_detj ? detJ_cc->const_array(mfi) : Array4<Real const>{};
414  for (int dir = 0; dir < 3; ++dir) {
415  const Bin b = bin[ib][dir];
416  if (b.count == 0) { continue; }
417  const Box fbx = owned_faces(dir, vbx);
418  const GpuArray<int,3> flo{{fbx.smallEnd(0), fbx.smallEnd(1), fbx.smallEnd(2)}};
419  const GpuArray<int,3> fhi{{fbx.bigEnd(0), fbx.bigEnd(1), fbx.bigEnd(2)}};
420  const int* seg_pt = p_bins + b.start;
421  const int* seg_off = p_bins + b.start + b.count;
422  ++buf.kernel_launches;
423  ParallelFor(b.count, [=] AMREX_GPU_DEVICE (int s) noexcept
424  {
425  const int p = seg_pt[s];
426  const Real inv_eps2 = p_eps[2*p], reach2 = p_eps[2*p+1];
427  Real acc = p_norm[3*p+dir];
428  for (int u = seg_off[s]; u < seg_off[s+1]; ++u) {
429  const int m = p_bins[u];
430  const Real qx = p_q[3*m], qy = p_q[3*m+1], qz = p_q[3*m+2];
431  const int* r = p_reach + 18 * m + 6 * dir;
432  const int ilo = amrex::max(r[0], flo[0]), jlo = amrex::max(r[1], flo[1]), klo = amrex::max(r[2], flo[2]);
433  const int ihi = amrex::min(r[3], fhi[0]), jhi = amrex::min(r[4], fhi[1]), khi = amrex::min(r[5], fhi[2]);
434  for (int k = klo; k <= khi; ++k) {
435  for (int j = jlo; j <= jhi; ++j) {
436  for (int i = ilo; i <= ihi; ++i) {
437  if (boundary_face(dir, i, j, k, per, dlo, dhi)) { continue; }
438  const Real w = kernel_weight(dir, i, j, k, qx, qy, qz, plo, dx, znd, has_znd, inv_eps2, reach2);
439  // the product in a statement of its own: fused with the addition it would
440  // round differently on a target with a fused multiply-add
441  if (w > Real(0.0)) {
442  const Real term = w * face_volume(dir, i, j, k, detj, has_detj, dxdydz);
443  acc += term;
444  }
445  }
446  }
447  }
448  }
449  p_norm[3*p+dir] = acc;
450  });
451  }
452  }
453  buf.norms.download_async(norm.size());
454  Gpu::streamSynchronize();
455  std::copy(buf.norms.pinned.begin(), buf.norms.pinned.begin() + norm.size(), norm.begin());
456 }

Referenced by add_spread_forces().

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

◆ spread_points_problem() [1/2]

std::string erf_actuator::spread_points_problem ( const std::vector< amrex::Real > &  pos,
const std::vector< amrex::Real > &  force,
const std::vector< amrex::Real > &  epsilon 
)

What is wrong with the points, forces and widths for spread_forces (an empty string when nothing is).

◆ spread_points_problem() [2/2]

std::string erf_actuator::spread_points_problem ( const std::vector< Real > &  pos,
const std::vector< Real > &  force,
const std::vector< Real > &  epsilon 
)
82 {
83  if (!(pos.size() == force.size() && pos.size() % 3 == 0)) { return "spread_forces: one x,y,z force per x,y,z point"; }
84  if (3 * epsilon.size() != pos.size()) { return "spread_forces: one kernel width per point"; }
85  for (std::size_t p = 0; p < epsilon.size(); ++p) {
86  if (!(epsilon[p] > 0.0) || !std::isfinite(epsilon[p])) {
87  return "spread_forces: point " + std::to_string(p) + " (0-based) has the kernel width " + std::to_string(epsilon[p]) +
88  " m; it must be positive and finite";
89  }
90  }
91  for (std::size_t q = 0; q < pos.size(); ++q) {
92  if (!std::isfinite(pos[q]) || !std::isfinite(force[q])) {
93  const std::size_t p = q / 3;
94  return "spread_forces: point " + std::to_string(p) + " (0-based) has a non-finite position or force: (" +
95  std::to_string(pos[3*p]) + ", " + std::to_string(pos[3*p+1]) + ", " + std::to_string(pos[3*p+2]) + ") m, (" +
96  std::to_string(force[3*p]) + ", " + std::to_string(force[3*p+1]) + ", " + std::to_string(force[3*p+2]) + ") N";
97  }
98  }
99  return {};
100 }

Referenced by check_spread_points().

Here is the caller graph for this function:

◆ terrain_heights() [1/2]

void erf_actuator::terrain_heights ( const amrex::MultiFab *  z_phys_nd,
const amrex::Geometry &  geom,
const std::vector< amrex::Real > &  pos,
std::vector< amrex::Real > &  h 
)

Return the height of the terrain surface (the k = 0 node plane of z_phys_nd, bilinear between the four mesh nodes around each point's (x, y)) under a set of points; prob_lo z everywhere on a uniform-dz mesh (z_phys_nd null), where no point is checked. Otherwise the same ownership rules, periodic images and reduction as sample_velocity(): the points must lie inside the domain horizontally, a point on the domain's upper x or y face belonging to the last cell; z is ignored.

Parameters
[in]pos3*n positions (m)
[out]hn terrain heights (m), the same on every rank on return

◆ terrain_heights() [2/2]

void erf_actuator::terrain_heights ( const MultiFab *  z_phys_nd,
const Geometry &  geom,
const std::vector< Real > &  pos,
std::vector< Real > &  h 
)
390 {
391  BL_PROFILE("erf_actuator::terrain_heights()");
392  const int npts = static_cast<int>(pos.size() / 3);
393  h.assign(npts, static_cast<Real>(geom.ProbLo(2)));
394  check_same_points_everywhere(npts, "terrain_heights");
395  if (npts == 0 || z_phys_nd == nullptr) { return; }
396  const auto plo = geom.ProbLoArray();
397  const auto phi = geom.ProbHiArray();
398  const auto dxi = geom.InvCellSizeArray();
399  const Box& domain = geom.Domain();
400  const int ilo = domain.smallEnd(0), ihi = domain.bigEnd(0);
401  const int jlo = domain.smallEnd(1), jhi = domain.bigEnd(1);
402  const int klo = domain.smallEnd(2);
403  const std::vector<Real> wpos = wrap_periodic(pos, geom);
404 
405  Gpu::DeviceVector<Real> d_pos(pos.size()), d_h(npts, 0.0);
406  Gpu::DeviceVector<int> d_cnt(npts, 0);
407  Gpu::copy(Gpu::hostToDevice, wpos.begin(), wpos.end(), d_pos.begin());
408  Real* p_pos = d_pos.data();
409  Real* p_h = d_h.data();
410  int* p_cnt = d_cnt.data();
411 
412  for (MFIter mfi(*z_phys_nd, false); mfi.isValid(); ++mfi) {
413  const Box nbx = mfi.validbox();
414  if (nbx.smallEnd(2) > klo || nbx.bigEnd(2) < klo) { continue; }
415  // the cells whose four lower nodes this nodal box holds; cell boxes are disjoint, so
416  // every point has exactly one owner
417  const Box cbx = amrex::enclosedCells(nbx);
418  Array4<Real const> const& znd = z_phys_nd->const_array(mfi);
419  ParallelFor(npts, [=] AMREX_GPU_DEVICE (int p) noexcept
420  {
421  const Real xi = (p_pos[3*p] - plo[0]) * dxi[0];
422  const Real yi = (p_pos[3*p+1] - plo[1]) * dxi[1];
423  int ic = static_cast<int>(std::floor(xi));
424  int jc = static_cast<int>(std::floor(yi));
425  // a point on the domain's upper face belongs to the last cell
426  if (ic > ihi && p_pos[3*p] <= phi[0]) { ic = ihi; }
427  if (jc > jhi && p_pos[3*p+1] <= phi[1]) { jc = jhi; }
428  if (ic < ilo || ic > ihi || jc < jlo || jc > jhi) { return; }
429  if (ic < cbx.smallEnd(0) || ic > cbx.bigEnd(0) || jc < cbx.smallEnd(1) || jc > cbx.bigEnd(1)) { return; }
430  const Real wx = xi - static_cast<Real>(ic), wy = yi - static_cast<Real>(jc);
431  const Real z00 = znd(ic, jc, klo), z10 = znd(ic+1, jc, klo);
432  const Real z01 = znd(ic, jc+1, klo), z11 = znd(ic+1, jc+1, klo);
433  p_h[p] = (Real(1.0) - wy) * ((Real(1.0) - wx) * z00 + wx * z10) + wy * ((Real(1.0) - wx) * z01 + wx * z11);
434  // a count, not a flag: boxes of one rank may run on different streams, and a point held by two of
435  // them must still be refused by the caller
436  Gpu::Atomic::AddNoRet(&p_cnt[p], 1);
437  });
438  }
439  Gpu::streamSynchronize();
440  std::vector<int> cnt(npts, 0);
441  Gpu::copy(Gpu::deviceToHost, d_h.begin(), d_h.end(), h.begin());
442  Gpu::copy(Gpu::deviceToHost, d_cnt.begin(), d_cnt.end(), cnt.begin());
443  ParallelDescriptor::ReduceRealSum(h.data(), npts);
444  ParallelDescriptor::ReduceIntSum(cnt.data(), npts);
445  for (int p = 0; p < npts; ++p) {
446  if (cnt[p] != 1) {
447  Abort("terrain_heights: the point (" + std::to_string(pos[3*p]) + ", " + std::to_string(pos[3*p+1]) +
448  ") was found in " + std::to_string(cnt[p]) + " boxes: the point must lie inside the domain, over grids of "
449  "this level that reach the ground (a refined level aloft holds no ground)");
450  }
451  }
452 }
Here is the call graph for this function:

◆ wrap_periodic() [1/2]

std::vector<amrex::Real> erf_actuator::wrap_periodic ( const std::vector< amrex::Real > &  pos,
const amrex::Geometry &  geom 
)

The points (3 coordinates each, m) with their coordinates in every periodic direction moved by whole periods into [prob_lo, prob_hi): the image the boxes hold, so that a point that has crossed a periodic seam is sampled on the far side.

◆ wrap_periodic() [2/2]

std::vector<Real> erf_actuator::wrap_periodic ( const std::vector< Real > &  pos,
const Geometry &  geom 
)
101 {
102  std::vector<Real> p(pos);
103  for (int d = 0; d < AMREX_SPACEDIM; ++d) {
104  if (!geom.isPeriodic(d)) { continue; }
105  const Real lo = static_cast<Real>(geom.ProbLo(d)), len = static_cast<Real>(geom.ProbLength(d));
106  const Real hi = static_cast<Real>(geom.ProbHi(d));
107  for (std::size_t i = static_cast<std::size_t>(d); i < p.size(); i += 3) {
108  Real x = std::fmod(p[i] - lo, len);
109  if (x < Real(0.0)) { x += len; }
110  p[i] = lo + x;
111  // a point a few ulps below lo can round onto hi or past it (with lo != 0), which no box owns
112  if (p[i] >= hi) { p[i] = lo; }
113  }
114  }
115  return p;
116 }

Referenced by sample_cell_scalar(), sample_velocity(), and terrain_heights().

Here is the caller graph for this function: