88static std::vector<double>
gatherGlobal(
const std::vector<double>& local,
int ox,
int oy,
int oz,
89 int lnx,
int lny,
int lnz,
int rank,
int size) {
90 std::vector<double> global;
92 global.assign(
GCELLS, 0.0);
93 for (
int r = 0; r < size; ++r) {
94 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
97 for (
int z = 0; z < lnz; ++z)
98 for (
int y = 0; y < lny; ++y)
99 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) *
NX +
100 (std::size_t)(z + oz) *
NX *
NY],
101 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
102 (std::size_t)lnx *
sizeof(
double));
106 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
107 MPI_Send(local.data(), (
int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
108 }
else if (rank == 0) {
109 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
110 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
111 MPI_Recv(buf.data(), (
int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
113 for (
int z = 0; z < meta[5]; ++z)
114 for (
int y = 0; y < meta[4]; ++y)
115 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) *
NX +
116 (std::size_t)(z + meta[2]) *
NX *
NY],
117 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
118 (std::size_t)meta[3] *
sizeof(
double));
144int main(
int argc,
char** argv) {
145 std::setvbuf(stdout,
nullptr, _IONBF, 0);
146 MPI_Init(&argc, &argv);
147 Kokkos::initialize(argc, argv);
150 int rank = 0, size = 1;
151 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
152 MPI_Comm_size(MPI_COMM_WORLD, &size);
153 auto dec = peclet::flow::CutcellMG::decomposition(
static_cast<std::size_t
>(size),
NX,
NY,
NZ);
154 auto blk = dec.block(rank);
155 const int ox = (int)blk.origin[0], oy = (
int)blk.origin[1], oz = (int)blk.origin[2];
156 const int lnx = (int)blk.size[0], lny = (
int)blk.size[1], lnz = (int)blk.size[2];
157 const int gn[3] = {
NX,
NY,
NZ};
158 bool cut[3] = {
false,
false,
false};
159 for (
const auto& sz : dec.sizes())
160 for (
int a = 0; a < 3; ++a)
161 if ((
int)sz[a] != gn[a])
164 std::printf(
"VOF CUTCELL MPI np=%d grid %dx%dx%d block %dx%dx%d cut axes: %s%s%s\n", size,
165 NX,
NY,
NZ, lnx, lny, lnz, cut[0] ?
"x" :
"", cut[1] ?
"y" :
"",
167 if (size > 1 && !cut[2]) {
169 std::printf(
" FAIL — the decomposition does not cut z, so no sphere is cut\n");
173 const std::vector<double> gSdf =
blockOf(
174 [](
int x,
int y,
int z) {
return sdfAt(x + 0.5, y + 0.5, z + 0.5); }, 0, 0, 0,
NX,
NY,
NZ);
180 std::vector<double> gU[3];
181 std::vector<double> refColour;
182 double refDrift = 0.0, refDiv = 0.0;
183 const int kinSteps = 40;
186 ref.setAdvection(
false);
190 ref.setBodyForce(2e-3, 1e-3, 5e-4);
191 ref.setSolid(gSdf,
true);
192 for (
int i = 0; i < 40; ++i)
194 refDiv = ref.maxOpenDivergence();
195 for (
int c = 0; c < 3; ++c)
196 gU[c] = ref.getVelocity(c);
199 const double v0 = ref.vofDiagnostics().volume;
200 const double dtA = 0.2 / ref.vofMaxCourant();
201 for (
int i = 0; i < kinSteps; ++i)
202 ref.advectVofKinematic(dtA);
203 const auto d1 = ref.vofDiagnostics();
204 refDrift = (d1.volume - v0) / v0;
205 refColour = ref.getVof();
208 " reference (full grid, every rank): max|div(open u)| %.3e, %d kinematic steps at "
209 "dt %.4g, drift %.3e, clipped %.3e\n",
210 refDiv, kinSteps, dtA, refDrift, d1.clippedVolume);
217 sd.initMpi(dec, MPI_COMM_WORLD);
233 for (
int w = 0; w < 5; ++w) {
239 for (
int c = 0; c < 3; ++c)
240 sd.
setField(c == 0 ?
"u" : (c == 1 ?
"v" :
"w"),
sliceOf(gU[c], ox, oy, oz, lnx, lny, lnz));
261 MPI_Allreduce(&locV0, &v0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
263 const double dtA = 0.2 / cflLoc;
264 for (
int i = 0; i < kinSteps; ++i)
268 MPI_Allreduce(&locV1, &v1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
272 const double drift = (v1 - v0) / v0;
274 " [kinematic np=%d] geometry %.3e band fill %.3e colour %.3e "
275 "drift %.3e (ref %.3e) max|div| %.3e\n",
276 size, dg, dfill, dc, drift, refDrift, divD);
277 if (!(dg == 0.0) || !(dfill == 0.0) || !(dc == 0.0)) {
278 std::printf(
" [kinematic np=%d] FAIL — not bitwise\n", size);
281 if (!(std::fabs(drift) <= 1e-11)) {
282 std::printf(
" [kinematic np=%d] FAIL — colour volume drift %.3e\n", size, drift);
290 const int steps = 30;
291 const double ratio = 10.0, grav = 2e-3;
292 auto setup = [&](
IbmSolver& s,
int aox,
int aoy,
int aoz,
int anx,
int any,
int anz) {
296 s.setAdvection(
true);
297 s.setSolid(
sliceOf(gSdf, aox, aoy, aoz, anx, any, anz),
true);
306 sd.initMpi(dec, MPI_COMM_WORLD);
307 setup(sd, ox, oy, oz, lnx, lny, lnz);
310 MPI_Allreduce(&lv0, &v0, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
312 for (
int i = 0; i < steps; ++i) {
318 MPI_Allreduce(&lv1, &v1, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
321 MPI_Allreduce(&lsolid, &solid, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
324 std::vector<double> gu[3];
325 for (
int c = 0; c < 3; ++c)
329 setup(ref, 0, 0, 0,
NX,
NY,
NZ);
330 for (
int i = 0; i < steps; ++i)
333 for (
int c = 0; c < 3; ++c)
334 du = std::fmax(du,
maxAbsDiff(gu[c], ref.getVelocity(c)));
335 const double dc =
maxAbsDiff(gc, ref.getVof());
336 const double drift = (v1 - v0) / v0;
337 const double tol = size == 1 ? 0.0 : 1e-9;
339 " [coupled np=%d] colour %.3e velocity %.3e drift %.3e solid sum C %.3e "
340 "max|div| %.3e pressure iters max %ld\n",
341 size, dc, du, drift, solid, divD, maxIt);
342 if (!(dc <= tol) || !(du <= tol)) {
343 std::printf(
" [coupled np=%d] FAIL — colour %.3e velocity %.3e above tol %.1e\n", size,
347 if (!(solid == 0.0)) {
348 std::printf(
" [coupled np=%d] FAIL — colour in solid cells %.3e\n", size, solid);
351 if (!(std::fabs(drift) <= 1e-8)) {
352 std::printf(
" [coupled np=%d] FAIL — colour drift %.3e\n", size, drift);
358 MPI_Allreduce(&fail, &g, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
361 std::printf(fail ?
"VOF CUTCELL MPI: FAILED\n" :
"VOF CUTCELL MPI: all checks passed\n");