19 PetscFunctionBeginUser;
20 if (!user->
Phi) PetscCall(DMCreateGlobalVector(user->
da, &user->
Phi));
21 if (!user->
lPhi) PetscCall(DMCreateLocalVector(user->
da, &user->
lPhi));
22 if (!user->
Aj) PetscCall(DMCreateGlobalVector(user->
da, &user->
Aj));
23 if (!user->
lAj) PetscCall(DMCreateLocalVector(user->
da, &user->
lAj));
26 PetscFunctionReturn(0);
37 PetscFunctionBeginUser;
40 PetscCall(VecSet(user->
P, 1.0));
41 PetscCall(VecSet(user->
Phi, 0.25));
47 PetscFunctionReturn(0);
59 PetscFunctionBeginUser;
64 PetscCall(VecSet(user->
Ucont, 0.0));
65 PetscCall(VecSet(user->
Nvert, 0.0));
66 PetscCall(VecSet(user->
Aj, 1.0));
67 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
68 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
69 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
70 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
71 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Aj, INSERT_VALUES, user->
lAj));
72 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Aj, INSERT_VALUES, user->
lAj));
74 PetscCall(VecDuplicate(user->
P, &B));
76 PetscCall(
PicurvAssertVecConstant(B, 0.0, 1.0e-12,
"zero velocity divergence should produce zero Poisson RHS"));
79 PetscCall(VecDestroy(&B));
81 PetscFunctionReturn(0);
97 PetscFunctionBeginUser;
99 PetscCall(VecDuplicate(user->
Ucont, &rct));
100 PetscCall(VecZeroEntries(rct));
112 PetscCall(VecSet(user->
Nvert, 0.0));
113 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
114 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
115 PetscCall(DMDAVecGetArray(user->
fda, user->
lCsi, &l_csi));
116 PetscCall(DMDAVecGetArray(user->
fda, user->
lEta, &l_eta));
117 PetscCall(DMDAVecGetArray(user->
fda, user->
lZet, &l_zet));
118 l_csi[1][1][1].
x = 1.0; l_csi[1][1][1].
y = 0.0; l_csi[1][1][1].
z = 0.0;
119 l_eta[1][1][1].
x = 0.0; l_eta[1][1][1].
y = 1.0; l_eta[1][1][1].
z = 0.0;
120 l_zet[1][1][1].
x = 0.0; l_zet[1][1][1].
y = 0.0; l_zet[1][1][1].
z = 1.0;
121 PetscCall(DMDAVecRestoreArray(user->
fda, user->
lZet, &l_zet));
122 PetscCall(DMDAVecRestoreArray(user->
fda, user->
lEta, &l_eta));
123 PetscCall(DMDAVecRestoreArray(user->
fda, user->
lCsi, &l_csi));
126 PetscCall(DMDAVecGetArrayRead(user->
fda, rct, &rct_arr));
128 "ComputeBodyForces should update the driven-flow controller magnitude"));
129 PetscCall(
PicurvAssertRealNear(0.0, rct_arr[1][1][1].y, 1.0e-12,
"ComputeBodyForces should keep y unchanged"));
130 PetscCall(
PicurvAssertRealNear(0.0, rct_arr[1][1][1].z, 1.0e-12,
"ComputeBodyForces should keep z unchanged"));
131 PetscCall(DMDAVecRestoreArrayRead(user->
fda, rct, &rct_arr));
133 PetscCall(VecDestroy(&rct));
135 PetscFunctionReturn(0);
145 PetscReal ***diff_arr = NULL;
147 PetscFunctionBeginUser;
153 simCtx->
les = PETSC_FALSE;
157 PetscCall(DMDAVecGetArrayRead(user->
da, user->
Diffusivity, &diff_arr));
158 PetscCall(
PicurvAssertRealNear(0.125, diff_arr[1][1][1], 1.0e-12,
"molecular diffusivity interior value"));
159 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
Diffusivity, &diff_arr));
162 PetscFunctionReturn(0);
174 PetscFunctionBeginUser;
176 PetscCall(VecDuplicate(user->
lUcont, &conv));
177 PetscCall(VecSet(user->
Ucont, 0.0));
178 PetscCall(VecSet(user->
Ucat, 0.0));
179 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
180 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
181 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
182 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
187 PetscCall(VecDestroy(&conv));
189 PetscFunctionReturn(0);
201 PetscFunctionBeginUser;
203 PetscCall(VecDuplicate(user->
lUcont, &visc));
204 PetscCall(VecSet(user->
Ucont, 1.0));
205 PetscCall(VecSet(user->
Ucat, 1.0));
206 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
207 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
208 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
209 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
214 PetscCall(VecDestroy(&visc));
216 PetscFunctionReturn(0);
231 Cmpnts ***stress = NULL, ***v = NULL;
232 const PetscReal tau = 0.37;
234 PetscFunctionBeginUser;
236 PetscCall(VecDuplicate(user->
lUcont, &visc));
237 PetscCall(VecSet(user->
Ucat, 1.0));
238 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
239 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
246 PetscCall(DMDAVecGetArray(user->
fda, user->
Wall_Stress, &stress));
247 for (PetscInt k = user->
info.zs; k < user->
info.zs + user->
info.zm; ++k)
248 for (PetscInt j = user->
info.ys; j < user->
info.ys + user->
info.ym; ++j)
249 if (user->
info.xs == 0) stress[k][j][0].
y = tau;
250 PetscCall(DMDAVecRestoreArray(user->
fda, user->
Wall_Stress, &stress));
256 PetscCall(DMDAVecGetArrayRead(user->
fda, visc, &v));
258 "the first cell must receive minus the stored stress times the unit face area"));
260 "a cell away from the wall must not see the wall stress"));
261 PetscCall(DMDAVecRestoreArrayRead(user->
fda, visc, &v));
265 PetscCall(DMDAVecGetArrayRead(user->
fda, visc, &v));
267 "without a wall model the face must carry its gradient flux, zero here"));
268 PetscCall(DMDAVecRestoreArrayRead(user->
fda, visc, &v));
272 PetscCall(VecDestroy(&visc));
274 PetscFunctionReturn(0);
286 Vec jacobians[4] = {user->
Aj, user->
IAj, user->
JAj, user->
KAj};
289 PetscFunctionBeginUser;
290 for (PetscInt v = 0; v < 12; ++v) {
291 PetscCall(VecScale(area_vectors[v], h * h));
292 PetscCall(DMGlobalToLocalBegin(user->
fda, area_vectors[v], INSERT_VALUES, local_area[v]));
293 PetscCall(DMGlobalToLocalEnd(user->
fda, area_vectors[v], INSERT_VALUES, local_area[v]));
295 for (PetscInt v = 0; v < 4; ++v) {
296 PetscCall(VecSet(jacobians[v], 1.0 / (h * h * h)));
297 PetscCall(DMGlobalToLocalBegin(user->
da, jacobians[v], INSERT_VALUES, local_jac[v]));
298 PetscCall(DMGlobalToLocalEnd(user->
da, jacobians[v], INSERT_VALUES, local_jac[v]));
300 PetscFunctionReturn(0);
316 Vec molecular = NULL, with_clark = NULL;
317 Cmpnts ***ucat = NULL, ***visc0 = NULL, ***visc1 = NULL;
318 const PetscInt n = 10, mid = n / 2;
320 PetscFunctionBeginUser;
326 PetscCall(DMDAVecGetArray(user->
fda, user->
Ucat, &ucat));
327 for (PetscInt k = user->
info.zs; k < user->
info.zs + user->
info.zm; ++k)
328 for (PetscInt j = user->
info.ys; j < user->
info.ys + user->
info.ym; ++j)
329 for (PetscInt i = user->
info.xs; i < user->
info.xs + user->
info.xm; ++i) {
330 const PetscReal x = (i - 0.5) * h;
331 ucat[k][j][i].
x = x * x; ucat[k][j][i].
y = 0.0; ucat[k][j][i].
z = 0.0;
333 PetscCall(DMDAVecRestoreArray(user->
fda, user->
Ucat, &ucat));
334 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
335 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
336 PetscCall(VecSet(user->
lUcont, 0.0));
338 PetscCall(VecDuplicate(user->
lUcont, &molecular));
339 PetscCall(VecDuplicate(user->
lUcont, &with_clark));
345 PetscCall(DMDAVecGetArrayRead(user->
fda, molecular, &visc0));
346 PetscCall(DMDAVecGetArrayRead(user->
fda, with_clark, &visc1));
348 const PetscReal v_molecular = visc0[mid][mid][probe].x;
349 const PetscReal v_clark = visc1[mid][mid][probe].x - v_molecular;
350 PetscCheck(PetscAbsReal(v_molecular) > 0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB,
351 "The molecular viscous term vanished at the probe; the fixture is wrong.");
352 *ratio_per_x = (v_clark / v_molecular) / ((probe - 0.5) * h);
354 PetscCall(DMDAVecRestoreArrayRead(user->
fda, with_clark, &visc1));
355 PetscCall(DMDAVecRestoreArrayRead(user->
fda, molecular, &visc0));
356 PetscCall(VecDestroy(&with_clark));
357 PetscCall(VecDestroy(&molecular));
359 PetscFunctionReturn(0);
373 PetscReal coarse = 0.0, fine = 0.0;
375 PetscFunctionBeginUser;
381 "the gradient model removes momentum where du/dx grows along x"));
383 "halving the filter width divides the gradient-model force by four"));
389 "gradient-model force matches (Delta^2/12) du/dx du/dx at h = 0.2"));
391 "gradient-model force matches (Delta^2/12) du/dx du/dx at h = 0.1"));
392 PetscFunctionReturn(0);
404 PetscFunctionBeginUser;
406 PetscCall(VecSet(user->
Ucont, 0.0));
407 PetscCall(VecSet(user->
Ucat, 0.0));
408 PetscCall(VecSet(user->
Nvert, 0.0));
409 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
410 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
411 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
412 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucat, INSERT_VALUES, user->
lUcat));
413 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
414 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
417 PetscCall(
PicurvAssertVecConstant(user->
Rhs, 0.0, 1.0e-12,
"ComputeRHS should remain zero without forcing on a quiescent field"));
420 PetscFunctionReturn(0);
431 PetscFunctionBeginUser;
441 PetscFunctionReturn(0);
451 PetscReal ***diff_arr = NULL;
452 Cmpnts ***grad_arr = NULL;
454 const PetscReal gamma0 = 0.5;
455 const PetscReal slope_x = 0.25;
457 PetscFunctionBeginUser;
463 "verification diffusivity override should report as active"));
467 PetscCall(DMDAVecGetArrayRead(user->
da, user->
Diffusivity, &diff_arr));
468 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
Cent, ¢));
470 "verification override should populate the linear diffusivity field"));
471 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
Cent, ¢));
472 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
Diffusivity, &diff_arr));
476 "linear verification diffusivity should yield the current finite-difference x gradient"));
478 "linear verification diffusivity should keep y gradient zero"));
480 "linear verification diffusivity should keep z gradient zero"));
484 PetscFunctionReturn(0);
495 PetscReal ***diff = NULL;
499 PetscFunctionBeginUser;
503 const DMDALocalInfo info = user->
info;
504 PetscCall(DMDAVecGetArray(user->
da, user->
Diffusivity, &diff));
505 for (PetscInt k = info.zs; k < info.zs + info.zm; ++k)
506 for (PetscInt j = info.ys; j < info.ys + info.ym; ++j)
507 for (PetscInt i = info.xs; i < info.xs + info.xm; ++i) diff[k][j][i] = 1.0 + 0.1 * (PetscReal)(i * i);
508 PetscCall(DMDAVecRestoreArray(user->
da, user->
Diffusivity, &diff));
515 PetscCall(
PicurvAssertBool((PetscBool)(PetscAbsReal(grad[2][2][1].x) > 1.0e-6),
"the first cell should carry a gradient"));
517 "the low dummy cell should image the last cell's gradient"));
519 "the high dummy cell should image the first cell's gradient"));
523 PetscFunctionReturn(0);
532 PetscInt rows = 0, cols = 0;
534 const PetscInt *col_idx = NULL;
535 const PetscScalar *values = NULL;
536 PetscInt interior_row = 0;
538 PetscFunctionBeginUser;
540 PetscCall(VecSet(user->
Nvert, 0.0));
541 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
542 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
545 PetscCall(
PicurvAssertBool((PetscBool)(user->
A != NULL),
"AssemblePoissonOperator should allocate the Poisson operator"));
547 PetscCall(MatGetSize(user->
A, &rows, &cols));
551 interior_row = (2 * user->
info.my + 2) * user->
info.mx + 2;
552 PetscCall(MatGetRow(user->
A, interior_row, &ncols, &col_idx, &values));
553 PetscCall(
PicurvAssertBool((PetscBool)(ncols > 1),
"Interior Poisson row should couple to neighboring nodes"));
554 PetscCall(
PicurvAssertBool((PetscBool)(values != NULL),
"Interior Poisson row should expose non-null coefficients"));
555 PetscCall(MatRestoreRow(user->
A, interior_row, &ncols, &col_idx, &values));
558 PetscFunctionReturn(0);
570 Vec jacobians[4] = {user->
Aj, user->
IAj, user->
JAj, user->
KAj};
574 PetscFunctionBeginUser;
575 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rnd));
576 PetscCall(PetscRandomSetInterval(rnd, 0.5, 1.5));
577 PetscCall(PetscRandomSetSeed(rnd, 2026));
578 PetscCall(PetscRandomSeed(rnd));
579 for (PetscInt v = 0; v < 9; ++v) {
580 PetscCall(VecSetRandom(face_metrics[v], rnd));
581 PetscCall(DMGlobalToLocalBegin(user->
fda, face_metrics[v], INSERT_VALUES, local_metrics[v]));
582 PetscCall(DMGlobalToLocalEnd(user->
fda, face_metrics[v], INSERT_VALUES, local_metrics[v]));
584 for (PetscInt v = 0; v < 4; ++v) {
585 PetscCall(VecSetRandom(jacobians[v], rnd));
586 PetscCall(DMGlobalToLocalBegin(user->
da, jacobians[v], INSERT_VALUES, local_jac[v]));
587 PetscCall(DMGlobalToLocalEnd(user->
da, jacobians[v], INSERT_VALUES, local_jac[v]));
589 PetscCall(PetscRandomDestroy(&rnd));
590 PetscFunctionReturn(0);
605 Vec B = NULL, APhi = NULL;
608 const PetscReal ***b, ***aphi;
609 PetscReal worst = 0.0;
611 PetscFunctionBeginUser;
617 PetscCall(VecSet(user->
Nvert, 0.0));
618 PetscCall(DMDAVecGetArray(user->
da, user->
Nvert, &nvert));
619 if (user->
info.xs <= 3 && 3 < user->
info.xs + user->
info.xm &&
620 user->
info.ys <= 3 && 3 < user->
info.ys + user->
info.ym &&
621 user->
info.zs <= 3 && 3 < user->
info.zs + user->
info.zm) nvert[3][3][3] = 1.0;
622 PetscCall(DMDAVecRestoreArray(user->
da, user->
Nvert, &nvert));
623 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
624 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
626 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rnd));
627 PetscCall(PetscRandomSetSeed(rnd, 7));
628 PetscCall(PetscRandomSeed(rnd));
629 PetscCall(VecSetRandom(user->
Phi, rnd));
630 PetscCall(PetscRandomDestroy(&rnd));
631 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Phi, INSERT_VALUES, user->
lPhi));
632 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Phi, INSERT_VALUES, user->
lPhi));
633 PetscCall(VecSet(user->
Ucont, 0.0));
634 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
635 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
639 PetscCall(VecDuplicate(user->
P, &B));
641 PetscCall(VecDuplicate(user->
Phi, &APhi));
642 PetscCall(MatMult(user->
A, user->
Phi, APhi));
644 PetscCall(DMDAVecGetArrayRead(user->
da, B, &b));
645 PetscCall(DMDAVecGetArrayRead(user->
da, APhi, &aphi));
646 PetscCall(DMDAVecGetArray(user->
da, user->
Nvert, &nvert));
647 for (PetscInt k = PetscMax(user->
info.zs, 1); k < PetscMin(user->
info.zs + user->
info.zm, user->
info.mz - 1); k++) {
648 for (PetscInt j = PetscMax(user->
info.ys, 1); j < PetscMin(user->
info.ys + user->
info.ym, user->
info.my - 1); j++) {
649 for (PetscInt i = PetscMax(user->
info.xs, 1); i < PetscMin(user->
info.xs + user->
info.xm, user->
info.mx - 1); i++) {
650 if (nvert[k][j][i] > 0.1)
continue;
651 worst = PetscMax(worst, PetscAbsReal(b[k][j][i] + aphi[k][j][i]) / (1.0 + PetscAbsReal(aphi[k][j][i])));
655 PetscCall(DMDAVecRestoreArray(user->
da, user->
Nvert, &nvert));
656 PetscCall(DMDAVecRestoreArrayRead(user->
da, APhi, &aphi));
657 PetscCall(DMDAVecRestoreArrayRead(user->
da, B, &b));
659 "the right-hand side of the projected flux should equal -A Phi on every fluid row"));
661 PetscCall(VecDestroy(&APhi));
662 PetscCall(VecDestroy(&B));
664 PetscFunctionReturn(0);
675 PetscFunctionBeginUser;
677 PetscCall(VecSet(user->
Ucont, 0.0));
678 PetscCall(VecSet(user->
Phi, 0.0));
679 PetscCall(VecSet(user->
Nvert, 0.0));
680 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
681 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
682 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Phi, INSERT_VALUES, user->
lPhi));
683 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Phi, INSERT_VALUES, user->
lPhi));
684 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
685 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
688 PetscCall(
PicurvAssertVecConstant(user->
Ucont, 0.0, 1.0e-12,
"ProjectVelocity should leave a zero-velocity field unchanged when Phi is zero"));
691 PetscFunctionReturn(0);
700 PetscReal ***phi = NULL;
703 PetscFunctionBeginUser;
706 PetscCall(VecSet(user->
Ucont, 0.0));
707 PetscCall(VecSet(user->
Nvert, 0.0));
708 PetscCall(DMDAVecGetArray(user->
da, user->
Phi, &phi));
709 for (PetscInt k = user->
info.zs; k < user->
info.zs + user->
info.zm; ++k) {
710 for (PetscInt j = user->
info.ys; j < user->
info.ys + user->
info.ym; ++j) {
711 for (PetscInt i = user->
info.xs; i < user->
info.xs + user->
info.xm; ++i) {
712 phi[k][j][i] = (PetscReal)i;
716 PetscCall(DMDAVecRestoreArray(user->
da, user->
Phi, &phi));
717 PetscCall(DMGlobalToLocalBegin(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
718 PetscCall(DMGlobalToLocalEnd(user->
fda, user->
Ucont, INSERT_VALUES, user->
lUcont));
719 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Phi, INSERT_VALUES, user->
lPhi));
720 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Phi, INSERT_VALUES, user->
lPhi));
721 PetscCall(DMGlobalToLocalBegin(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
722 PetscCall(DMGlobalToLocalEnd(user->
da, user->
Nvert, INSERT_VALUES, user->
lNvert));
726 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
Ucont, &ucont));
727 PetscCall(
PicurvAssertRealNear(-1.0, ucont[2][2][2].x, 1.0e-10,
"ProjectVelocity should subtract the x pressure gradient under identity metrics"));
728 PetscCall(
PicurvAssertRealNear(0.0, ucont[2][2][2].y, 1.0e-10,
"ProjectVelocity should leave the y component unchanged for an x-only gradient"));
729 PetscCall(
PicurvAssertRealNear(0.0, ucont[2][2][2].z, 1.0e-10,
"ProjectVelocity should leave the z component unchanged for an x-only gradient"));
730 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
Ucont, &ucont));
733 PetscFunctionReturn(0);
742 DMDALocalInfo info = user->
info;
745 PetscFunctionBeginUser;
746 PetscCall(DMDAVecGetArray(user->
fda, user->
Ucont, &ucont));
747 for (PetscInt k = PetscMax(info.zs, 1); k < PetscMin(info.zs + info.zm, info.mz - 1); k++) {
748 for (PetscInt j = PetscMax(info.ys, 1); j < PetscMin(info.ys + info.ym, info.my - 1); j++) {
749 for (PetscInt i = PetscMax(info.xs, 1); i < PetscMin(info.xs + info.xm, info.mx - 1); i++) {
750 if (i <= info.mx - 3) ucont[k][j][i].
x += amplitude * PetscSinReal(1.3 * i + 0.7 * j + 0.4 * k);
751 if (j <= info.my - 3) ucont[k][j][i].
y += amplitude * PetscCosReal(0.9 * i + 1.1 * j + 0.3 * k);
752 if (k <= info.mz - 3) ucont[k][j][i].
z += amplitude * PetscSinReal(0.5 * i + 0.8 * j + 1.7 * k);
756 PetscCall(DMDAVecRestoreArray(user->
fda, user->
Ucont, &ucont));
758 PetscFunctionReturn(0);
771 char tmpdir[PETSC_MAX_PATH_LEN];
774 PetscFunctionBeginUser;
777 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n"
778 "-ps_ksp_rtol 1.0e-12\n-ps_ksp_atol 1.0e-14\n-ps_ksp_max_it 200\n",
779 &simCtx, &user, tmpdir,
sizeof(tmpdir)));
785 PetscCall(
PicurvAssertBool((PetscBool)(before > 1.0e-2),
"perturbation should make the field divergent"));
793 "multigrid solve plus projection should leave no divergence above the solve tolerance"));
797 PetscFunctionReturn(0);
811 char tmpdir[PETSC_MAX_PATH_LEN];
812 PetscReal ***phi, local[2] = {0.0, 0.0}, peak;
814 PetscFunctionBeginUser;
816 NULL, PETSC_FALSE,
"-im 17\n-jm 17\n-km 17\n-mg_level 3\n", &simCtx, &user, tmpdir,
sizeof(tmpdir)));
819 PetscCall(KSPSetInitialGuessNonzero(user->
ksp, PETSC_TRUE));
820 PetscCall(VecSet(user->
Phi, 5.0));
823 const DMDALocalInfo info = user->
info;
824 PetscCall(DMDAVecGetArrayRead(user->
da, user->
Phi, (
void *)&phi));
825 for (PetscInt k = PetscMax(info.zs, 1); k < PetscMin(info.zs + info.zm, info.mz - 1); k++)
826 for (PetscInt j = PetscMax(info.ys, 1); j < PetscMin(info.ys + info.ym, info.my - 1); j++)
827 for (PetscInt i = PetscMax(info.xs, 1); i < PetscMin(info.xs + info.xm, info.mx - 1); i++) {
828 local[0] += phi[k][j][i];
831 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
Phi, (
void *)&phi));
832 PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, local, 2, MPIU_REAL, MPI_SUM, PETSC_COMM_WORLD));
833 PetscCall(VecNorm(user->
Phi, NORM_INFINITY, &peak));
834 PetscCall(
PicurvAssertBool((PetscBool)(peak > 1.0e-6),
"the perturbation should need a correction"));
836 "Phi should carry no constant over the interior"));
840 PetscFunctionReturn(0);
851 PetscFunctionBeginUser;
853 file = fopen(path,
"r");
854 if (!file) PetscFunctionReturn(0);
855 while (fgets(line,
sizeof(line), file)) {
856 if (strstr(line, needle)) (*count)++;
859 PetscFunctionReturn(0);
871 char tmpdir[PETSC_MAX_PATH_LEN], log_path[PETSC_MAX_PATH_LEN + 64];
874 KSPConvergedReason reason;
875 PetscInt headers = 0;
877 PetscFunctionBeginUser;
880 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n"
881 "-ps_ksp_rtol 1.0e-12\n-ps_ksp_atol 1.0e-14\n-ps_ksp_max_it 200\n",
882 &simCtx, &user, tmpdir,
sizeof(tmpdir)));
887 first_ksp = user->
ksp;
888 first_operator = user->
A;
889 PetscCall(
PicurvAssertBool((PetscBool)(first_ksp != NULL),
"the first solve should build and keep the solver"));
890 PetscCall(KSPGetConvergedReason(user->
ksp, &reason));
891 PetscCall(
PicurvAssertBool((PetscBool)(reason > 0),
"the first solve should converge"));
896 PetscCall(
PicurvAssertBool((PetscBool)(user->
ksp == first_ksp),
"the second solve should reuse the solver"));
897 PetscCall(
PicurvAssertBool((PetscBool)(user->
A == first_operator),
"the second solve should reuse the operator"));
898 PetscCall(KSPGetConvergedReason(user->
ksp, &reason));
899 PetscCall(
PicurvAssertBool((PetscBool)(reason > 0),
"the reused solver should converge"));
901 PetscCall(PetscSNPrintf(log_path,
sizeof(log_path),
"%s/Poisson_Solver_Convergence_History_Block_0.csv", simCtx->
log_dir));
903 PetscCall(
PicurvAssertIntEqual(1, headers,
"the convergence CSV should carry its header once"));
905 PetscCall(
PicurvAssertIntEqual(2, headers,
"the convergence CSV should hold an iteration-0 row per solve"));
909 PetscFunctionReturn(0);
921 const char *
const configurations[2] = {
922 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n-mg_pre_it 1\n-mg_post_it 3\n"
923 "-ps_mg_levels_1_ksp_type richardson\n-ps_mg_levels_2_ksp_type richardson\n"
924 "-ps_ksp_rtol 1.0e-12\n-ps_ksp_atol 1.0e-14\n-ps_ksp_max_it 200\n",
925 "-im 17\n-jm 17\n-km 17\n-mg_level 3\n-mg_pre_it 2\n-mg_post_it 2\n",
928 PetscFunctionBeginUser;
929 for (PetscInt c = 0; c < 2; c++) {
932 char tmpdir[PETSC_MAX_PATH_LEN];
937 &simCtx, &user, tmpdir,
sizeof(tmpdir)));
942 PetscCall(KSPGetPC(user->
ksp, &pc));
945 KSP pre_smoother, post_smoother;
946 PetscInt pre_its, post_its;
947 PetscBool is_richardson;
949 PetscCall(PCMGGetSmootherDown(pc, l, &pre_smoother));
950 PetscCall(KSPGetTolerances(pre_smoother, NULL, NULL, NULL, &pre_its));
952 PetscCall(PCMGGetSmootherUp(pc, l, &post_smoother));
953 PetscCall(KSPGetTolerances(post_smoother, NULL, NULL, NULL, &post_its));
954 PetscCall(
PicurvAssertBool((PetscBool)(pre_smoother != post_smoother),
"unequal sweeps should give a separate post-smoother"));
956 PetscCall(
PicurvAssertIntEqual(3, post_its,
"post-smoothing should run post_sweeps iterations"));
957 PetscCall(PetscObjectTypeCompare((PetscObject)post_smoother, KSPRICHARDSON, &is_richardson));
958 PetscCall(
PicurvAssertBool(is_richardson,
"the post-smoother should keep the configured smoother type"));
962 PetscCall(
PicurvAssertIntEqual(2, pre_its,
"the shared smoother should run the configured sweeps"));
972 "the solve with distinct sweeps should still remove the divergence"));
977 PetscFunctionReturn(0);
989 char tmpdir[PETSC_MAX_PATH_LEN];
990 MatNullSpace nullsp = NULL;
992 PetscReal ***xa, norm, sum;
994 PetscFunctionBeginUser;
996 NULL, PETSC_FALSE,
"-im 9\n-jm 9\n-km 9\n-mg_level 2\n", &simCtx, &user, tmpdir,
sizeof(tmpdir)));
998 PetscCall(MatGetNullSpace(user->
A, &nullsp));
999 PetscCall(
PicurvAssertBool((PetscBool)(nullsp != NULL),
"the operator should carry the Neumann null space"));
1001 PetscCall(VecDuplicate(user->
Phi, &x));
1002 PetscCall(VecSet(x, 3.0));
1003 PetscCall(MatNullSpaceRemove(nullsp, x));
1004 PetscCall(VecNorm(x, NORM_INFINITY, &norm));
1005 PetscCall(
PicurvAssertRealNear(0.0, norm, 1.0e-12,
"removing the null space should annihilate a constant"));
1007 PetscCall(DMDAVecGetArray(user->
da, x, &xa));
1008 for (PetscInt k = user->
info.zs; k < user->
info.zs + user->
info.zm; k++)
1009 for (PetscInt j = user->
info.ys; j < user->
info.ys + user->
info.ym; j++)
1010 for (PetscInt i = user->
info.xs; i < user->
info.xs + user->
info.xm; i++) xa[k][j][i] = (PetscReal)i;
1011 PetscCall(DMDAVecRestoreArray(user->
da, x, &xa));
1012 PetscCall(MatNullSpaceRemove(nullsp, x));
1013 PetscCall(VecSum(x, &sum));
1015 PetscCall(DMDAVecGetArray(user->
da, x, &xa));
1017 PetscCall(DMDAVecRestoreArray(user->
da, x, &xa));
1019 PetscCall(VecDestroy(&x));
1022 PetscFunctionReturn(0);
1035 char tmpdir[PETSC_MAX_PATH_LEN];
1036 PetscErrorCode solve_error;
1038 PetscFunctionBeginUser;
1041 "-im 9\n-jm 9\n-km 9\n-mg_level 3\n"
1043 "-ps_ksp_type fgmres\n-ps_pc_type mg\n"
1044 "-ps_mg_coarse_ksp_type preonly\n-ps_mg_coarse_pc_type redundant\n",
1045 &simCtx, &user, tmpdir,
sizeof(tmpdir)));
1048 PetscCall(PetscPushErrorHandler(PetscReturnErrorHandler, NULL));
1050 PetscCall(PetscPopErrorHandler());
1052 "an unfactorable coarse level should stop the solve, not reach the projection"));
1056 PetscFunctionReturn(0);
1064 PetscErrorCode ierr;
1090 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv Poisson/RHS tests");
1095 ierr =
PicurvRunTests(
"unit-poisson-rhs", cases,
sizeof(cases) /
sizeof(cases[0]));
1101 ierr = PetscFinalize();
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
Header file for Particle related physics modules.
PetscErrorCode ComputeEulerianDiffusivity(UserCtx *user)
Computes the effective diffusivity scalar field (Gamma_eff) on the Eulerian grid.
PetscErrorCode ComputeEulerianDiffusivityGradient(UserCtx *user)
Computes the Eulerian gradient of the effective diffusivity field.
FieldId
Compile-time identity for a catalogued Eulerian field.
Pressure-Poisson projection: the multigrid pressure-correction solve, the pressure update,...
PetscErrorCode AssemblePoissonOperator(UserCtx *user)
Assembles the pressure-correction operator on one multigrid level.
PetscErrorCode UpdatePressure(UserCtx *user)
Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images an...
PetscErrorCode ProjectVelocity(UserCtx *user)
Corrects the contravariant flux with the gradient of Phi.
PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg)
Solves the pressure-correction equation for every block with geometric multigrid.
PetscErrorCode ComputePoissonRHS(UserCtx *user, Vec B)
Forms the right-hand side of the pressure-correction equation.
PetscErrorCode ComputeBodyForces(UserCtx *user, Vec Rct)
General dispatcher for applying all active body forces (momentum sources).
PetscErrorCode ComputeConvection(UserCtx *user, Vec Ucont, Vec Ucat, Vec Conv)
Computes the convective contribution to the contravariant momentum RHS.
PetscErrorCode ComputeViscous(UserCtx *user, Vec Ucat, Vec Visc)
Computes the viscous contribution to the contravariant momentum RHS.
PetscErrorCode ComputeRHS(UserCtx *user, Vec Rhs)
Computes the Right-Hand Side (RHS) of the momentum equations.
PetscErrorCode ComputeDivergence(UserCtx *user)
Computes the discrete divergence of the contravariant velocity field.
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
static PetscErrorCode TestPoissonSolverMultigridReusesItsSolver(void)
Tests that the multigrid solver is built once and reused on later steps.
static PetscErrorCode TestClarkGradientModelScalesWithFilterWidthSquared(void)
Tests that the Clark gradient model scales as the square of the filter width.
static PetscErrorCode TestComputeEulerianDiffusivityMolecularOnly(void)
Tests Eulerian diffusivity for the molecular-only configuration.
static PetscErrorCode TestComputeRHSZeroFieldNoForcing(void)
Tests that the full RHS remains zero without forcing on a quiescent field.
static PetscErrorCode TestViscousUniformField(void)
Tests that viscous terms vanish for a uniform field.
int main(int argc, char **argv)
Runs the unit-poisson-rhs PETSc test binary.
static PetscErrorCode TestConvectionZeroField(void)
Tests that convection vanishes for a quiescent field.
static PetscErrorCode TestComputeBodyForcesDispatcher(void)
Tests the body-force dispatcher across supported source terms.
static PetscErrorCode PerturbInteriorFaceFluxes(UserCtx *user, PetscReal amplitude)
Adds a smooth divergent perturbation to every interior face flux.
static PetscErrorCode RandomizeMinimalFaceMetrics(UserCtx *user)
Fills every face metric and Jacobian of the minimal fixture with reproducible random values,...
static PetscErrorCode TestUpdatePressureAddsPhi(void)
Tests that pressure updates add the correction potential.
static PetscErrorCode TestProjectionZeroPhiLeavesVelocityUnchanged(void)
Tests that projection leaves a zero pressure-correction field unchanged.
static PetscErrorCode TestOperatorAndProjectionShareOneFaceGradient(void)
Tests that the operator is exactly the divergence of the projection's gradient.
static PetscErrorCode TestPoissonSolverMultigridRefusesAnOvercoarsenedHierarchy(void)
Tests that an over-coarsened hierarchy stops the run at the Poisson solve.
static PetscErrorCode TestPoissonSolverMultigridProjectsToDivergenceFree(void)
Tests that the production multigrid Poisson solve and projection remove a divergence.
static PetscErrorCode TestPoissonSolverMultigridHonoursDistinctSweeps(void)
Tests that unequal pre- and post-smoothing counts give two distinct smoothers.
static PetscErrorCode ScaleMinimalMetricsToSpacing(UserCtx *user, PetscReal h)
Rescales the minimal fixture's unit metrics to cubic cells of edge h.
static PetscErrorCode TestPoissonRHSZeroDivergence(void)
Tests that the Poisson RHS is zero for zero-divergence velocity.
static PetscErrorCode TestDiffusivityGradientFillsPeriodicDummyCells(void)
Tests that the diffusivity gradient fills its periodic dummy cells.
static PetscErrorCode TestComputeEulerianDiffusivityVerificationLinearX(void)
Tests verification-driven linear diffusivity override and its gradient.
static PetscErrorCode EnsurePoissonAndRhsVectors(UserCtx *user)
Allocates Poisson/RHS support vectors required by the tests.
static PetscErrorCode TestPoissonNullSpaceRemovesTheInteriorMean(void)
Tests the null space attached to the operator.
static PetscErrorCode TestAssemblePoissonOperatorPopulatesRows(void)
Tests that Poisson matrix assembly produces a populated operator on a tiny Cartesian grid.
static PetscErrorCode TestProjectionLinearPhiCorrectsVelocity(void)
Tests that projection applies the expected x-direction correction for a linear pressure field.
static PetscErrorCode CountFileOccurrences(const char *path, const char *needle, PetscInt *count)
Counts the occurrences of needle in a text file; a missing file counts zero.
static PetscErrorCode ClarkToMolecularRatioPerX(PetscReal h, PetscInt probe, PetscReal *ratio_per_x)
Returns the Clark contribution over the molecular viscous one, per unit x, at one cell.
static PetscErrorCode TestPoissonSolverMultigridKeepsThePressureLevel(void)
Tests that the multigrid solve returns Phi without a constant, so P keeps its level.
static PetscErrorCode TestViscousWallModelledFaceCarriesTheStoredStress(void)
Tests that a wall-modelled face carries the stored wall stress and no gradient flux.
static PetscErrorCode TestComputeEulerianDiffusivityGradientConstantField(void)
Tests diffusivity-gradient computation on a constant field.
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Builds minimal SimCtx and UserCtx fixtures for C unit tests.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Asserts that two real values agree within tolerance.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Destroys minimal SimCtx/UserCtx fixtures and all owned PETSc objects.
PetscErrorCode PicurvCreateMinimalContextsWithPeriodicity(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz, PetscBool x_periodic, PetscBool y_periodic, PetscBool z_periodic)
Builds minimal SimCtx and UserCtx fixtures for C unit tests with configurable periodicity.
PetscErrorCode PicurvDestroyRuntimeContext(SimCtx **simCtx_ptr)
Finalizes and frees a runtime context built by PicurvBuildTinyRuntimeContext.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Runs a named C test suite and prints pass/fail progress markers.
PetscErrorCode PicurvBuildTinyRuntimeContextWithOptions(const char *bcs_contents, PetscBool enable_particles, const char *extra_options, SimCtx **simCtx_out, UserCtx **user_out, char *tmpdir, size_t tmpdir_len)
Builds the tiny runtime context with extra control-file options appended.
PetscErrorCode PicurvAssertVecConstant(Vec vec, PetscScalar expected, PetscReal tol, const char *context)
Asserts that a PETSc vector is spatially constant within tolerance.
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Asserts that two integer values are equal.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Asserts that one boolean condition is true.
PetscErrorCode PicurvRemoveTempDir(const char *path)
Recursively removes a temporary directory created by PicurvMakeTempDir.
Shared declarations for the PICurv C test fixture and assertion layer.
Named test case descriptor consumed by PicurvRunTests.
BoundaryFaceConfig boundary_faces[6]
PetscReal forceScalingFactor
@ BC_HANDLER_PERIODIC_DRIVEN_CONSTANT_FLUX
BCHandlerType handler_type
PetscReal poissonSourceImbalance
PetscReal bulkVelocityCorrection
VerificationFieldConfig verificationDiffusivity
char log_dir[PETSC_MAX_PATH_LEN]
PetscInt les_gradient_model
Add the Clark gradient (tensor-diffusivity) term to the viscous flux.
char eulerianSource[PETSC_MAX_PATH_LEN]
PetscInt wallfunction
Enable wall functions on WALL faces.
PetscInt les
Active LES closure; an LESModelType value.
#define COEF_TIME_ACCURACY
Coefficient controlling the temporal accuracy scheme (e.g., 1.5 for 2nd Order Backward Difference).
PetscReal drivingForceMagnitude
A 3D point or vector with PetscScalar components.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.
PetscErrorCode VerificationFieldCompile(const char *text, VerificationFieldConfig *field)
Compiles a verification field's expression, in physical x, y, z and t, and enables the field.
PetscBool VerificationDiffusivityOverrideActive(const SimCtx *simCtx)
Reports whether a verification-only diffusivity override is active.