74 static const char* fn[3] = {
"force_x",
"force_y",
"force_z"};
78 s.setAdvection(
false);
80 s.setDomainBc(2 * c.comp + 0, 1, 0, 0, 0);
81 s.setDomainBc(2 * c.comp + 1, 1, 0, 0, 0);
83 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
84 s.setDensityMode(
true);
87 c.walls ? std::vector<double>{0.0, -GRAV} : std::vector<double>{FORCE, 0.0});
90 s.setField(fn[c.comp], std::vector<double>((std::size_t)lnx * lny * lnz,
FORCE));
95static std::vector<double>
gatherGlobal(
const std::vector<double>& local,
int ox,
int oy,
int oz,
96 int lnx,
int lny,
int lnz,
int rank,
int size) {
97 std::vector<double> global;
99 global.assign(
GCELLS, 0.0);
100 for (
int r = 0; r < size; ++r) {
101 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
104 for (
int z = 0; z < lnz; ++z)
105 for (
int y = 0; y < lny; ++y)
106 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) *
NX +
107 (std::size_t)(z + oz) *
NX *
NY],
108 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
109 (std::size_t)lnx *
sizeof(
double));
113 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
114 MPI_Send(local.data(), (
int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
115 }
else if (rank == 0) {
116 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
117 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
118 MPI_Recv(buf.data(), (
int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
120 for (
int z = 0; z < meta[5]; ++z)
121 for (
int y = 0; y < meta[4]; ++y)
122 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) *
NX +
123 (std::size_t)(z + meta[2]) *
NX *
NY],
124 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
125 (std::size_t)meta[3] *
sizeof(
double));
170int main(
int argc,
char** argv) {
171 MPI_Init(&argc, &argv);
172 Kokkos::initialize(argc, argv);
175 int rank = 0, size = 1;
176 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
177 MPI_Comm_size(MPI_COMM_WORLD, &size);
179 auto dec = peclet::flow::CutcellMG::decomposition(
static_cast<std::size_t
>(size),
NX,
NY,
NZ);
180 auto blk = dec.block(rank);
181 const int ox = (int)blk.origin[0], oy = (
int)blk.origin[1], oz = (int)blk.origin[2];
182 const int lnx = (int)blk.size[0], lny = (
int)blk.size[1], lnz = (int)blk.size[2];
183 const int gn[3] = {
NX,
NY,
NZ};
184 bool cut[3] = {
false,
false,
false};
185 for (
const auto& sz : dec.sizes())
186 for (
int a = 0; a < 3; ++a)
187 if ((
int)sz[a] != gn[a])
190 std::printf(
"BODYFORCE-GHOST MPI np=%d grid %dx%dx%d block %dx%dx%d cut axes: %s%s%s\n",
191 size,
NX,
NY,
NZ, lnx, lny, lnz, cut[0] ?
"x" :
"", cut[1] ?
"y" :
"",
197 const Config configs[] = {{
"per-z", 2,
false,
true,
true},
198 {
"per-x", 0,
false,
false,
false},
199 {
"walls-z", 2,
true,
true,
true}};
201 for (
const Config& c : configs) {
202 if (c.needCut && size > 1 && !cut[c.comp]) {
205 " [%-7s np=%d] FAIL — the decomposition does NOT cut the forced axis %d; "
206 "this test exists to gate the force ghost ACROSS a rank boundary (WO-G)\n",
207 c.name, size, c.comp);
212 sd.initMpi(dec, MPI_COMM_WORLD);
214 for (
int it = 0; it <
STEPS; ++it)
217 std::vector<double> gu[3];
218 for (
int comp = 0; comp < 3; ++comp)
220 const std::vector<double> gp =
226 for (
int it = 0; it <
STEPS; ++it)
228 double du = 0, umag = 0;
229 for (
int comp = 0; comp < 3; ++comp) {
230 du = std::fmax(du,
maxAbsDiff(gu[comp], ref.getVelocity(comp)));
231 umag = std::fmax(umag,
maxAbs(ref.getVelocity(comp)));
233 const double dp =
maxAbsDiff(gp, ref.getPressure());
234 const double pmag =
maxAbs(ref.getPressure());
235 const double utol = (size == 1) ? 0.0 : std::fmax(1e-15, 1e-11 * umag);
236 const double ptol = (size == 1) ? 0.0 : std::fmax(1e-12, 1e-11 * pmag);
237 bool ok = du <= utol && dp <= ptol;
239 char extra[160] =
"";
242 const std::vector<double>& uf = gu[c.comp];
243 double lo = uf[0], hi = uf[0];
244 for (
double v : uf) {
245 lo = std::fmin(lo, v);
246 hi = std::fmax(hi, v);
249 const double spread = hi - lo, verr = std::fabs(0.5 * (hi + lo) - want) / want;
252 for (
int comp = 0; comp < 3; ++comp)
254 cross = std::fmax(cross,
maxAbs(gu[comp]));
255 ok = ok && spread <= 1e-13 * want && verr <= 1e-13 && cross <= 1e-13 * want;
256 std::snprintf(extra,
sizeof(extra),
257 " | u_f spread=%.3e (want 0) value=%.17g (want %.17g) cross=%.2e", spread,
258 0.5 * (hi + lo), want, cross);
262 const double umax = std::fmax(
maxAbs(gu[0]), std::fmax(
maxAbs(gu[1]),
maxAbs(gu[2])));
263 ok = ok && umax < 1e-13 && perr < 1e-11;
264 std::snprintf(extra,
sizeof(extra),
" | max|u|=%.2e dP/dz err=%.3e (worst plane z=%d)",
267 std::printf(
" [%-7s np=%d] du=%.3e dp=%.3e (tol %.1e/%.1e)%s %s\n", c.name, size, du, dp,
268 utol, ptol, extra, ok ?
"OK" :
"FAIL");
272 MPI_Bcast(&fail, 1, MPI_INT, 0, MPI_COMM_WORLD);
275 std::printf(
"BODYFORCE-GHOST MPI (np=%d): %s\n", size, fail ?
"FAIL" :
"PASS");
static double maxAbs(const std::vector< double > &a)
static std::vector< double > gatherGlobal(const std::vector< double > &local, int ox, int oy, int oz, int lnx, int lny, int lnz, int rank, int size)
static constexpr double FORCE
static double maxAbsDiff(const std::vector< double > &a, const std::vector< double > &b)
static void configure(IbmSolver &s, const Config &c, int lnx, int lny, int lnz)
static double pressureGradientError(const std::vector< double > &p, int *worstPlane)