ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ActuatorSpreading.H
Go to the documentation of this file.
1 #ifndef ERF_ACTUATOR_SPREADING_H
2 #define ERF_ACTUATOR_SPREADING_H
3 
4 /** Spreading of point forces onto the face-centered momentum sources with a normalized Gaussian kernel, and the integral check. */
5 
6 #include <string>
7 #include <vector>
8 
9 #include <AMReX_Geometry.H>
10 #include <AMReX_MultiFab.H>
11 #include <AMReX_REAL.H>
12 
14 #include "ERF_ActuatorSampling.H"
15 
16 namespace erf_actuator {
17 
18 /**
19  * Spread point forces onto the face-centered momentum sources with a 3-D Gaussian kernel.
20  *
21  * The points are spread where they are given, with the periodic images within reach of the domain: a
22  * caller whose points may lie outside a periodic domain moves them inside first (wrap_periodic()).
23  *
24  * Each point p carries a force F_p on the fluid (N) and has its own kernel width epsilon_p
25  * (the conductor lines and the towers of a run may differ). Its share of the source at a face is
26  * F_p w_p(face) / S_p, where w_p = exp(-|x_face - x_p|^2 / epsilon_p^2) is cut off beyond
27  * 3 epsilon_p and S_p = sum over the faces of that component of w_p dV is the discrete
28  * normalization with the face volumes (dx dy dz detJ on a terrain-following mesh). The sum
29  * of src dV over the mesh therefore equals F_p exactly for every point and component,
30  * whatever the resolution and however much of the kernel is cut off by the ground or the
31  * domain top; the kernel's shape is Gaussian only where it is resolved. The faces sit at
32  * prob_lo + i dx with the volumes dx dy dz detJ, with no map factors: a body model must refuse a
33  * run whose map factors are not 1 until the core carries them. The normalization sums and the
34  * sources are accumulated in a fixed order without atomic adds (spread_partial_norms(),
35  * add_scaled_kernels()), so the source does not depend on the device's scheduling.
36  *
37  * Every rank accumulates the faces its boxes own (each physical face once, however the boxes
38  * share their boundary faces) and the normalizations are summed across ranks, so the result
39  * does not depend on the domain decomposition; every rank must pass the same points. Every periodic image of a point within
40  * 3 epsilon_p of the domain (in x and y; z is not imaged) adds its kernel, so a kernel wraps across
41  * a periodic boundary, and overlaps itself when the period is under 6 epsilon_p. The sources are
42  * set (not added to) on every valid face, both copies of a shared face alike; the faces on a
43  * non-periodic domain boundary get no source.
44  *
45  * Aborts when a position or force is not finite. z_phys_nd and detJ_cc must share the boxes and
46  * distribution of src_x; src_x, src_y and src_z are face-centered in x, y and z.
47  *
48  * @param[in] pos 3*n point coordinates (m, ERF domain coordinates)
49  * @param[in] force 3*n forces ON the fluid BY the body (N, ERF frame): minus the fluid's force on the body
50  * @param[in] epsilon n kernel widths, one per point (m, > 0 and finite)
51  * @param[in] z_phys_nd nodal heights (m), or nullptr on a uniform-dz mesh
52  * @param[in] detJ_cc cell Jacobians (1 ghost cell), or nullptr on a uniform-dz mesh
53  * @param[in] geom the level's geometry
54  * @param[out] src_x,src_y,src_z face-centered momentum sources (N/m^3), one component each
55  */
56 void spread_forces (const std::vector<amrex::Real>& pos,
57  const std::vector<amrex::Real>& force,
58  const std::vector<amrex::Real>& epsilon,
59  const amrex::MultiFab* z_phys_nd,
60  const amrex::MultiFab* detJ_cc,
61  const amrex::Geometry& geom,
62  amrex::MultiFab& src_x,
63  amrex::MultiFab& src_y,
64  amrex::MultiFab& src_z);
65 
66 /**
67  * The same, but added to what the sources hold (spread_forces() sets them): for several sets of
68  * points spread one after the other. Collective: every rank must pass the same points.
69  */
70 void add_spread_forces (const std::vector<amrex::Real>& pos,
71  const std::vector<amrex::Real>& force,
72  const std::vector<amrex::Real>& epsilon,
73  const amrex::MultiFab* z_phys_nd,
74  const amrex::MultiFab* detJ_cc,
75  const amrex::Geometry& geom,
76  amrex::MultiFab& src_x,
77  amrex::MultiFab& src_y,
78  amrex::MultiFab& src_z);
79 
80 /** The same with one kernel width epsilon (m, > 0 and finite) for every point. */
81 void spread_forces (const std::vector<amrex::Real>& pos,
82  const std::vector<amrex::Real>& force,
83  amrex::Real epsilon,
84  const amrex::MultiFab* z_phys_nd,
85  const amrex::MultiFab* detJ_cc,
86  const amrex::Geometry& geom,
87  amrex::MultiFab& src_x,
88  amrex::MultiFab& src_y,
89  amrex::MultiFab& src_z);
90 
91 /**
92  * The first of spread_forces()'s two passes, on this rank's boxes only: for each point and each
93  * component, this rank's part of the normalization S_p (the sum of w_p dV over the faces its boxes
94  * own). No reduction: the caller sums the ranks' parts (spread_forces() over every rank; a model
95  * whose ranks hold different points, over the ranks a point reaches). The boxes are src_x's, which
96  * z_phys_nd and detJ_cc must share.
97  * zb_local, when given, is this rank's mesh_z_bounds(z_phys_nd, geom, false), which the call then
98  * need not compute; the same for add_scaled_kernels.
99  *
100  * The periodic images of the points are made once, and binned to the boxes: one kernel per box and
101  * face grid, one thread per point, which adds the point's terms in a fixed order (its images, then
102  * their faces k, j, i) without atomics, so the result does not depend on the device's scheduling.
103  * The call checks only the arrays' sizes: the caller checks the points, widths and layouts first
104  * (check_spread_points(), check_spread_layout()), as spread_forces() does; the same for add_scaled_kernels().
105  * @param[out] norm 3*n partial normalizations (m^3), component fastest
106  * @param buffers device memory kept from call to call (the call uses its own when nullptr)
107  */
108 void spread_partial_norms (const std::vector<amrex::Real>& pos,
109  const std::vector<amrex::Real>& epsilon,
110  const amrex::MultiFab* z_phys_nd,
111  const amrex::MultiFab* detJ_cc,
112  const amrex::Geometry& geom,
113  const amrex::MultiFab& src_x,
114  std::vector<amrex::Real>& norm,
115  const ZBounds* zb_local = nullptr,
116  ActuatorDeviceBuffers* buffers = nullptr);
117 
118 /**
119  * The second pass, on this rank's boxes only: add (F_p / S_p) w_p to every face of every box (both
120  * copies of a shared face alike), each point with its own width.
121  *
122  * The images are binned to tiles of 8^3 faces of each box: one kernel per box and face grid, one
123  * thread per face of the tiles some kernel reaches, which adds its terms in point then image order
124  * without atomics on the sources (deterministic, and the order of the point-by-point sum). On the
125  * host (no GPU; not from inside an OpenMP parallel region) each image instead adds into the faces of
126  * its own reach box, in the same image order: every face takes the same terms in the same order, so the
127  * sources are bit-identical, without the tile pass's walk over images that miss the face (with the
128  * compilers' standard floating-point contraction: with clang's -ffp-contract=fast, icpx's default, the
129  * two loops may fuse a kernel's squared distance differently, so the last bits may differ).
130  * @param[in] f_over_s 3*n forces over their whole normalizations (N/m^3), component fastest; a zero skips the point's component
131  * @param buffers device memory kept from call to call (the call uses its own when nullptr)
132  */
133 void add_scaled_kernels (const std::vector<amrex::Real>& pos,
134  const std::vector<amrex::Real>& f_over_s,
135  const std::vector<amrex::Real>& epsilon,
136  const amrex::MultiFab* z_phys_nd,
137  const amrex::MultiFab* detJ_cc,
138  const amrex::Geometry& geom,
139  amrex::MultiFab& src_x,
140  amrex::MultiFab& src_y,
141  amrex::MultiFab& src_z,
142  const ZBounds* zb_local = nullptr,
143  ActuatorDeviceBuffers* buffers = nullptr);
144 
145 /** What is wrong with the points, forces and widths for spread_forces (an empty string when nothing is). */
146 std::string spread_points_problem (const std::vector<amrex::Real>& pos, const std::vector<amrex::Real>& force,
147  const std::vector<amrex::Real>& epsilon);
148 
149 /** Abort unless the points, forces and widths match in number and are finite, and the widths positive (spread_forces' checks). */
150 void check_spread_points (const std::vector<amrex::Real>& pos, const std::vector<amrex::Real>& force,
151  const std::vector<amrex::Real>& epsilon);
152 
153 /** 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). */
154 void check_spread_layout (const amrex::MultiFab* z_phys_nd, const amrex::MultiFab* detJ_cc, const amrex::MultiFab& src_x,
155  const amrex::MultiFab& src_y, const amrex::MultiFab& src_z);
156 
157 /**
158  * Return the integral of a face-centered source over the mesh, sum of src dV (the total force, N).
159  *
160  * @param dir 0, 1 or 2: src holds the x-, y- or z-face component
161  */
162 amrex::Real integrate_source (int dir,
163  const amrex::MultiFab& src,
164  const amrex::MultiFab* detJ_cc,
165  const amrex::Geometry& geom);
166 
167 } // namespace erf_actuator
168 
169 #endif
std::vector< std::array< Real, 3 > > pos
Definition: ERF_ActuatorSpreading.cpp:220
Definition: ERF_ActuatorDeviceBuffers.H:14
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
Real integrate_source(int dir, const MultiFab &src, const MultiFab *detJ_cc, const Geometry &geom)
Definition: ERF_ActuatorSpreading.cpp:639
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)
Definition: ERF_ActuatorSpreading.cpp:69
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
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
void check_spread_points(const std::vector< Real > &pos, const std::vector< Real > &force, const std::vector< Real > &epsilon)
Definition: ERF_ActuatorSpreading.cpp:103
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12
Definition: ERF_ActuatorDeviceBuffers.H:61
Definition: ERF_ActuatorSampling.H:146