29 { 1, 0, 0}, {-1, 0, 0}, { 0, 1, 0}, { 0, -1, 0},
30 { 0, 0, 1}, { 0, 0, -1},
31 { 1, 1, 0}, { 1, -1, 0}, {-1, 1, 0}, {-1, -1, 0},
32 { 0, 1, 1}, { 0, 1, -1}, { 0, -1, 1}, { 0, -1, -1},
33 { 1, 0, 1}, { 1, 0, -1}, {-1, 0, 1}, {-1, 0, -1},
63 return field[c[2] + d[2]][c[1] + d[1]][c[0] + d[0]];
79 const PetscReal ***nvert,
const PetscInt c[3], PetscInt n, PetscInt t,
80 const PetscInt m[3],
const PetscBool periodic[3])
107 PetscInt n,
const PetscInt m[3],
const PetscBool periodic[3])
110 const Cmpnts normal = metrics->
metric[n][n][c[2]][c[1]][c[0]];
112 face.
aj = metrics->
aj[n][c[2]][c[1]][c[0]];
113 for (PetscInt b = 0; b < 3; b++) {
114 const Cmpnts base = metrics->
metric[n][b][c[2]][c[1]][c[0]];
115 face.
dot[b] = base.
x * normal.
x + base.
y * normal.
y + base.
z * normal.
z;
130 for (PetscInt s = 0; s < 19; s++) {
151 PetscFunctionBeginUser;
153 for (PetscInt n = 0; n < 3; n++) {
154 for (PetscInt b = 0; b < 3; b++) {
156 PetscCall(DMDAVecGetArrayRead(view.
dm, view.
local_vec, (
void *)&metrics->
metric[n][b]));
159 PetscCall(DMDAVecGetArrayRead(view.
dm, view.
local_vec, (
void *)&metrics->
aj[n]));
162 PetscFunctionReturn(0);
175 PetscFunctionBeginUser;
177 for (PetscInt n = 0; n < 3; n++) {
178 for (PetscInt b = 0; b < 3; b++) {
180 PetscCall(DMDAVecRestoreArrayRead(view.
dm, view.
local_vec, (
void *)&metrics->
metric[n][b]));
183 PetscCall(DMDAVecRestoreArrayRead(view.
dm, view.
local_vec, (
void *)&metrics->
aj[n]));
186 PetscFunctionReturn(0);
195 if (periodic && d == 1 && v == m - 2)
return 1;
196 if (periodic && d == -1 && v == 1)
return m - 2;
210 PetscInt n, PetscReal sign, PetscScalar coefficients[19])
212 for (PetscInt b = 0; b < 3; b++) {
213 const PetscReal g = face->
dot[b] * face->
aj;
214 PetscInt lower[3] = {own[0], own[1], own[2]};
215 PetscInt upper[3] = {own[0], own[1], own[2]};
225 if (diff.
weight == 0.0)
continue;
226 const PetscReal term = g * diff.
weight;
227 PetscInt hi_lower[3] = {lower[0], lower[1], lower[2]}, hi_upper[3] = {upper[0], upper[1], upper[2]};
228 PetscInt lo_lower[3] = {lower[0], lower[1], lower[2]}, lo_upper[3] = {upper[0], upper[1], upper[2]};
229 hi_lower[b] += diff.
hi; hi_upper[b] += diff.
hi;
230 lo_lower[b] += diff.
lo; lo_upper[b] += diff.
lo;
246 const DMDALocalInfo info = user->
info;
247 const PetscInt m[3] = {info.mx, info.my, info.mz};
248 PetscBool periodic[3];
250 const PetscReal ***nvert, ***aj;
253 PetscFunctionBeginUser;
258 PetscCall(VecGetLocalSize(user->
Phi, &local_rows));
259 PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, local_rows, local_rows, m[0] * m[1] * m[2], m[0] * m[1] * m[2],
260 19, NULL, 19, NULL, &user->
A));
262 PetscCall(MatZeroEntries(user->
A));
265 PetscCall(DMDAGetAO(user->
da, &ao));
267 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
268 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lAj, (
void *)&aj));
270 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
271 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
272 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
273 const PetscInt c[3] = {i, j, k};
274 PetscInt row = i + j * m[0] + k * m[0] * m[1];
275 PetscInt columns[19];
276 PetscScalar coefficients[19] = {0.0};
278 PetscCall(AOApplicationToPetsc(ao, 1, &row));
279 if (i == 0 || i == m[0] - 1 || j == 0 || j == m[1] - 1 || k == 0 || k == m[2] - 1) {
280 const PetscScalar one = 1.0;
281 PetscCall(MatSetValues(user->
A, 1, &row, 1, &row, &one, INSERT_VALUES));
285 for (PetscInt s = 0; s < 19; s++) {
291 PetscCall(AOApplicationToPetsc(ao, 19, columns));
296 coefficients[0] = 1.0;
297 PetscCall(MatSetValues(user->
A, 1, &row, 19, columns, coefficients, INSERT_VALUES));
303 for (PetscInt n = 0; n < 3; n++) {
304 const PetscInt first = periodic[n] ? 0 : 1;
305 const PetscInt last = periodic[n] ? m[n] - 1 : m[n] - 2;
306 for (PetscInt side = 0; side < 2; side++) {
307 PetscInt across[3] = {0, 0, 0}, own[3] = {0, 0, 0}, face_cell[3] = {i, j, k};
308 const PetscBool upper = (PetscBool)(side == 0);
310 across[n] = upper ? 1 : -1;
312 if (c[n] == (upper ? last : first))
continue;
313 if (!upper) { own[n] = -1; face_cell[n] -= 1; }
321 for (PetscInt s = 0; s < 19; s++) coefficients[s] *= -aj[k][j][i];
322 PetscCall(MatSetValues(user->
A, 1, &row, 19, columns, coefficients, INSERT_VALUES));
327 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lAj, (
void *)&aj));
328 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
330 PetscCall(MatAssemblyBegin(user->
A, MAT_FINAL_ASSEMBLY));
331 PetscCall(MatAssemblyEnd(user->
A, MAT_FINAL_ASSEMBLY));
335 PetscFunctionReturn(0);
347 const DMDALocalInfo info = user->
info;
348 const PetscInt mx = info.mx, my = info.my, mz = info.mz;
349 const PetscReal dt = simCtx->
dt;
351 const PetscReal ***nvert, ***aj;
353 PetscReal local_sum = 0.0, global_sum = 0.0;
355 PetscFunctionBeginUser;
357 PetscCall(DMDAVecGetArray(user->
da, B, &rhs));
358 PetscCall(DMDAVecGetArrayRead(user->
fda, user->
lUcont, (
void *)&ucont));
359 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
360 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lAj, (
void *)&aj));
362 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
363 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
364 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
365 if (i == 0 || i == mx - 1 || j == 0 || j == my - 1 || k == 0 || k == mz - 1 ||
369 rhs[k][j][i] = -(ucont[k][j][i].
x - ucont[k][j][i-1].
x +
370 ucont[k][j][i].
y - ucont[k][j-1][i].
y +
380 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
381 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
382 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
387 PetscCallMPI(MPI_Allreduce(&local_sum, &global_sum, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)B)));
391 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lAj, (
void *)&aj));
392 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
393 PetscCall(DMDAVecRestoreArrayRead(user->
fda, user->
lUcont, (
void *)&ucont));
394 PetscCall(DMDAVecRestoreArray(user->
da, B, &rhs));
396 PetscFunctionReturn(0);
409 PetscFunctionBeginUser;
411 PetscCall(VecAXPY(user->
P, 1.0, user->
Phi));
416 PetscFunctionReturn(0);
428 const DMDALocalInfo info = user->
info;
429 const PetscInt m[3] = {info.mx, info.my, info.mz};
430 const PetscInt start[3] = {info.xs, info.ys, info.zs};
433 PetscBool periodic[3];
434 PetscInt interior_start[3], interior_end[3];
436 const PetscReal ***nvert, ***phi;
439 PetscFunctionBeginUser;
445 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
446 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lPhi, (
void *)&phi));
447 PetscCall(DMDAVecGetArray(user->
fda, user->
Ucont, &ucont));
451 for (PetscInt n = 0; n < 3; n++) {
452 PetscInt lo[3], hi[3];
453 for (PetscInt a = 0; a < 3; a++) { lo[a] = interior_start[a]; hi[a] = interior_end[a]; }
454 if (periodic[n] && start[n] == 0) lo[n] = 0;
455 hi[n] = PetscMin(hi[n], periodic[n] ? m[n] - 1 : m[n] - 2);
457 for (PetscInt k = lo[2]; k < hi[2]; k++) {
458 for (PetscInt j = lo[1]; j < hi[1]; j++) {
459 for (PetscInt i = lo[0]; i < hi[0]; i++) {
460 const PetscInt c[3] = {i, j, k};
461 PetscInt across[3] = {0, 0, 0};
468 const PetscReal correction = flux * scale;
469 if (n == 0) ucont[k][j][i].
x -= correction;
470 else if (n == 1) ucont[k][j][i].
y -= correction;
471 else ucont[k][j][i].
z -= correction;
477 PetscCall(DMDAVecRestoreArray(user->
fda, user->
Ucont, &ucont));
478 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lPhi, (
void *)&phi));
479 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
486 PetscFunctionReturn(0);
499 const DMDALocalInfo info = user->
info;
500 const PetscInt mx = info.mx, my = info.my, mz = info.mz;
501 const PetscInt xs = info.xs, xe = info.xs + info.xm;
502 const PetscInt ys = info.ys, ye = info.ys + info.ym;
503 const PetscInt zs = info.zs, ze = info.zs + info.zm;
504 PetscInt lo[3], hi[3];
506 const PetscInt lxs = lo[0], lxe = hi[0], lys = lo[1], lye = hi[1], lzs = lo[2], lze = hi[2];
507 const PetscReal ***nvert;
509 PetscReal local[2] = {0.0, 0.0}, global[2];
510 MPI_Comm comm = PetscObjectComm((PetscObject)X);
512 PetscFunctionBeginUser;
515 PetscCall(DMDAVecGetArray(user->
da, X, &x));
516 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
518 for (PetscInt k = lzs; k < lze; k++) {
519 for (PetscInt j = lys; j < lye; j++) {
520 for (PetscInt i = lxs; i < lxe; i++) {
522 local[0] += x[k][j][i];
528 PetscCallMPI(MPI_Allreduce(&local[0], &global[0], 1, MPIU_REAL, MPI_SUM, comm));
529 PetscCallMPI(MPI_Allreduce(&local[1], &global[1], 1, MPIU_REAL, MPI_SUM, comm));
530 const PetscReal shift = global[0] / (-1.0 * global[1]);
531 for (PetscInt k = lzs; k < lze; k++) {
532 for (PetscInt j = lys; j < lye; j++) {
533 for (PetscInt i = lxs; i < lxe; i++) {
539 for (PetscInt k = zs; k < ze; k++) {
540 for (PetscInt j = ys; j < ye; j++) {
541 for (PetscInt i = xs; i < xe; i++) {
542 if (i == 0 || i == mx - 1 || j == 0 || j == my - 1 || k == 0 || k == mz - 1 ||
548 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
549 PetscCall(DMDAVecRestoreArray(user->
da, X, &x));
551 PetscFunctionReturn(0);
562 PetscInt *coarse, PetscInt *direction)
569 *coarse = (f + 1) / 2;
570 *direction = (f - 2 * (*coarse)) == 0 ? 1 : -1;
571 if (f == 1 || f == m - 2) *direction = 0;
583 const PetscReal ***x, ***nvert, ***nvert_c;
586 PetscFunctionBeginUser;
588 PetscCall(MatShellGetContext(P, &user));
591 const PetscInt mx = info.mx, my = info.my, mz = info.mz;
592 const PetscInt xs = info.xs, xe = info.xs + info.xm;
593 const PetscInt ys = info.ys, ye = info.ys + info.ym;
594 const PetscInt zs = info.zs, ze = info.zs + info.zm;
595 PetscInt lo[3], hi[3];
597 const PetscInt lxs = lo[0], lxe = hi[0], lys = lo[1], lye = hi[1], lzs = lo[2], lze = hi[2];
599 PetscCall(DMGetLocalVector(coarse->
da, &lX));
600 PetscCall(DMGlobalToLocalBegin(coarse->
da, X, INSERT_VALUES, lX));
601 PetscCall(DMGlobalToLocalEnd(coarse->
da, X, INSERT_VALUES, lX));
602 PetscCall(DMDAVecGetArrayRead(coarse->
da, lX, (
void *)&x));
603 PetscCall(DMDAVecGetArrayRead(coarse->
da, coarse->
lNvert, (
void *)&nvert_c));
604 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
605 PetscCall(DMDAVecGetArray(user->
da, F, &f));
607 for (PetscInt k = lzs; k < lze; k++) {
608 for (PetscInt j = lys; j < lye; j++) {
609 for (PetscInt i = lxs; i < lxe; i++) {
610 PetscInt ic, jc, kc, ia, ja, ka;
622 f[k][j][i] = (x[kc ][jc ][ic ] * 9 +
623 x[kc ][jc+ja][ic ] * 3 +
624 x[kc ][jc ][ic+ia] * 3 +
625 x[kc ][jc+ja][ic+ia]) * 3./64. +
626 (x[kc+ka][jc ][ic ] * 9 +
627 x[kc+ka][jc+ja][ic ] * 3 +
628 x[kc+ka][jc ][ic+ia] * 3 +
629 x[kc+ka][jc+ja][ic+ia]) / 64.;
633 for (PetscInt k = zs; k < ze; k++) {
634 for (PetscInt j = ys; j < ye; j++) {
635 for (PetscInt i = xs; i < xe; i++) {
636 if (i == 0 || i == mx - 1 || j == 0 || j == my - 1 || k == 0 || k == mz - 1 ||
642 PetscCall(DMDAVecRestoreArray(user->
da, F, &f));
643 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
644 PetscCall(DMDAVecRestoreArrayRead(coarse->
da, coarse->
lNvert, (
void *)&nvert_c));
645 PetscCall(DMDAVecRestoreArrayRead(coarse->
da, lX, (
void *)&x));
646 PetscCall(DMRestoreLocalVector(coarse->
da, &lX));
648 PetscFunctionReturn(0);
663 const PetscReal ***x, ***nvert, ***nvert_f;
666 PetscFunctionBeginUser;
668 PetscCall(MatShellGetContext(R, &user));
671 const PetscInt mx = info.mx, my = info.my, mz = info.mz;
672 const PetscInt ia = user->
isc ? 0 : 1, ja = user->
jsc ? 0 : 1, ka = user->
ksc ? 0 : 1;
674 PetscCall(DMGetLocalVector(fine->
da, &lX));
675 PetscCall(DMGlobalToLocalBegin(fine->
da, X, INSERT_VALUES, lX));
676 PetscCall(DMGlobalToLocalEnd(fine->
da, X, INSERT_VALUES, lX));
677 PetscCall(DMDAVecGetArrayRead(fine->
da, lX, (
void *)&x));
678 PetscCall(DMDAVecGetArrayRead(fine->
da, fine->
lNvert, (
void *)&nvert_f));
679 PetscCall(DMDAVecGetArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
680 PetscCall(DMDAVecGetArray(user->
da, F, &f));
682 for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
683 for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
684 for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
685 if (i == 0 || i == mx - 1 || j == 0 || j == my - 1 || k == 0 || k == mz - 1 ||
690 const PetscInt ih = user->
isc ? i : 2 * i;
691 const PetscInt jh = user->
jsc ? j : 2 * j;
692 const PetscInt kh = user->
ksc ? k : 2 * k;
694 (x[kh ][jh ][ih ] * PetscMax(0., 1 - nvert_f[kh ][jh ][ih ]) +
695 x[kh ][jh ][ih-ia] * PetscMax(0., 1 - nvert_f[kh ][jh ][ih-ia]) +
696 x[kh ][jh-ja][ih ] * PetscMax(0., 1 - nvert_f[kh ][jh-ja][ih ]) +
697 x[kh-ka][jh ][ih ] * PetscMax(0., 1 - nvert_f[kh-ka][jh ][ih ]) +
698 x[kh ][jh-ja][ih-ia] * PetscMax(0., 1 - nvert_f[kh ][jh-ja][ih-ia]) +
699 x[kh-ka][jh-ja][ih ] * PetscMax(0., 1 - nvert_f[kh-ka][jh-ja][ih ]) +
700 x[kh-ka][jh ][ih-ia] * PetscMax(0., 1 - nvert_f[kh-ka][jh ][ih-ia]) +
701 x[kh-ka][jh-ja][ih-ia] * PetscMax(0., 1 - nvert_f[kh-ka][jh-ja][ih-ia]));
706 PetscCall(DMDAVecRestoreArray(user->
da, F, &f));
707 PetscCall(DMDAVecRestoreArrayRead(user->
da, user->
lNvert, (
void *)&nvert));
708 PetscCall(DMDAVecRestoreArrayRead(fine->
da, fine->
lNvert, (
void *)&nvert_f));
709 PetscCall(DMDAVecRestoreArrayRead(fine->
da, lX, (
void *)&x));
710 PetscCall(DMRestoreLocalVector(fine->
da, &lX));
712 PetscFunctionReturn(0);
723 PCType level_pc_type;
724 PetscBool is_bjacobi = PETSC_FALSE;
728 PetscFunctionBeginUser;
730 PetscCall(KSPGetPC(level_ksp, &level_pc));
731 PetscCall(PCGetType(level_pc, &level_pc_type));
732 if (level_pc_type) PetscCall(PetscStrcmp(level_pc_type, PCBJACOBI, &is_bjacobi));
735 PetscCall(KSPSetUp(level_ksp));
736 PetscCall(PCBJacobiGetSubKSP(level_pc, &nblocks, NULL, &block_ksp));
737 for (PetscInt b = 0; b < nblocks; b++) {
739 PetscCall(KSPGetPC(block_ksp[b], &block_pc));
740 PetscCall(PCFactorSetShiftAmount(block_pc, 1.e-10));
743 PetscFunctionReturn(0);
765 const PetscInt levels = usermg->
mglevels;
772 PetscFunctionBeginUser;
774 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Block %" PetscInt_FMT
": building the multigrid Poisson solver on %" PetscInt_FMT
" levels.\n", bi, levels);
778 PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
779 PetscCall(KSPAppendOptionsPrefix(ksp,
"ps_"));
783 PetscCall(PetscNew(&monitor));
788 PetscCall(KSPGetPC(ksp, &pc));
789 PetscCall(PCSetType(pc, PCMG));
790 PetscCall(PCMGSetLevels(pc, levels, NULL));
791 PetscCall(PCMGSetCycleType(pc, PC_MG_CYCLE_V));
792 PetscCall(PCMGSetType(pc, PC_MG_MULTIPLICATIVE));
793 PetscCall(PCMGSetNumberSmooth(pc, simCtx->
mg_preItr));
795 for (PetscInt l = levels - 1; l > 0; l--) {
798 const PetscInt m_c = coarse->
info.xm * coarse->
info.ym * coarse->
info.zm;
799 const PetscInt m_f = fine->
info.xm * fine->
info.ym * fine->
info.zm;
800 const PetscInt M_c = coarse->
info.mx * coarse->
info.my * coarse->
info.mz;
801 const PetscInt M_f = fine->
info.mx * fine->
info.my * fine->
info.mz;
803 PetscCall(MatCreateShell(PETSC_COMM_WORLD, m_c, m_f, M_c, M_f, coarse, &fine->
MR));
804 PetscCall(MatCreateShell(PETSC_COMM_WORLD, m_f, m_c, M_f, M_c, fine, &fine->
MP));
807 PetscCall(PCMGSetRestriction(pc, l, fine->
MR));
808 PetscCall(PCMGSetInterpolation(pc, l, fine->
MP));
811 for (PetscInt l = levels - 1; l >= 0; l--) {
817 PetscCall(PCMGGetSmoother(pc, l, &level_ksp));
819 PetscCall(PCMGGetCoarseSolve(pc, &level_ksp));
820 PetscCall(KSPSetTolerances(level_ksp, 1.e-8, PETSC_DEFAULT, PETSC_DEFAULT, 40));
822 PetscCall(KSPSetOperators(level_ksp, level->
A, level->
A));
823 PetscCall(KSPGetPC(level_ksp, &level_pc));
824 PetscCall(PCSetType(level_pc, PCBJACOBI));
825 PetscCall(KSPSetFromOptions(level_ksp));
828 PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, NULL, &level->
nullsp));
830 PetscCall(MatSetNullSpace(level->
A, level->
nullsp));
831 PetscCall(PCMGSetResidual(pc, l, PCMGResidualDefault, level->
A));
832 PetscCall(KSPSetUp(level_ksp));
839 PetscCall(PCMGGetSmootherUp(pc, l, &post_smoother));
840 PetscCall(KSPAppendOptionsPrefix(post_smoother,
"up_"));
841 PetscCall(KSPSetOperators(post_smoother, level->
A, level->
A));
842 PetscCall(KSPSetTolerances(post_smoother, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, simCtx->
mg_poItr));
843 PetscCall(KSPSetFromOptions(post_smoother));
845 PetscCall(KSPSetUp(post_smoother));
848 if (l < levels - 1) {
849 PetscCall(MatCreateVecs(level->
A, &level->
R, NULL));
850 PetscCall(PCMGSetRhs(pc, l, level->
R));
854 PetscCall(KSPSetOperators(ksp, finest->
A, finest->
A));
855 PetscCall(MatSetNullSpace(finest->
A, finest->
nullsp));
856 PetscCall(KSPSetFromOptions(ksp));
857 PetscCall(KSPSetUp(ksp));
858 PetscCall(VecDuplicate(finest->
P, &finest->
B));
862 PetscFunctionReturn(0);
876 PetscFunctionBeginUser;
878 PetscCall(KSPGetMonitorContext(ksp, &monitor));
886 if (simCtx->
rank == 0) {
889 PetscCall(PetscSNPrintf(filename,
sizeof(filename),
890 "Poisson_Solver_Convergence_History_Block_%" PetscInt_FMT
".csv", bi));
893 "step,block,iter,unpreconditioned_norm,true_norm,relative_norm",
897 PetscFunctionReturn(0);
905 PetscFunctionBeginUser;
907 PetscCall(KSPGetMonitorContext(ksp, &monitor));
913 PetscFunctionReturn(0);
927 PetscFunctionBeginUser;
931 for (PetscInt bi = 0; bi < simCtx->
block_number; bi++) {
933 KSPConvergedReason reason;
941 PetscCall(KSPSolve(user->
ksp, user->
B, user->
Phi));
946 PetscCall(MatNullSpaceRemove(user->
nullsp, user->
Phi));
953 PetscCall(KSPGetConvergedReason(user->
ksp, &reason));
954 PetscCheck(reason != KSP_DIVERGED_NANORINF && reason != KSP_DIVERGED_PC_FAILED,
955 PETSC_COMM_WORLD, PETSC_ERR_NOT_CONVERGED,
956 "Pressure Poisson solve on block %" PetscInt_FMT
" failed at step %" PetscInt_FMT
957 " (KSP reason %s). Known causes: a multigrid hierarchy coarsened too far "
958 "(reduce poisson_solver.multigrid.levels or refine the grid), or a momentum "
959 "field that has already diverged, such as an explicit time step beyond its "
961 bi, simCtx->
step, KSPConvergedReasons[reason]);
962 if (reason < 0 && reason != KSP_DIVERGED_ITS) {
964 " diverged at step %" PetscInt_FMT
" (KSP reason %s); the projection uses the last iterate.\n",
965 bi, simCtx->
step, KSPConvergedReasons[reason]);
971 PetscFunctionReturn(0);
PetscErrorCode SynchronizePeriodicStaggeredFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes persistent component-staggered vector fields.
PetscErrorCode FinalizePostProjectionCellFields(UserCtx *user)
Finalizes cell-centered fields after the projection step.
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
PetscErrorCode FieldGetView(UserCtx *user, FieldId field_id, FieldView *view)
Resolve the existing DM and global/local vectors for one field.
FieldId
Compile-time identity for a catalogued Eulerian field.
Non-owning runtime objects resolved for one field and UserCtx.
Logging utilities and macros for PETSc-based applications.
PetscErrorCode PicurvOpenRunCsv(SimCtx *simCtx, const char *dir, const char *filename, const char *header, PicurvCsvLifecycle lifecycle, PetscBool required, FILE **file)
Opens one of the run's CSV records (diagnostics, solver history, profiling) for writing.
@ PICURV_CSV_TRUNCATE_ON_FRESH
A fresh run's first write empties the file; a continued run appends.
PetscErrorCode DualMonitorDestroy(void **ctx)
Destroys the DualMonitorCtx.
#define GLOBAL
Scope for global logging across all processes.
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
#define LOG(scope, level, fmt,...)
Logging macro for PETSc-based applications with scope control.
PetscErrorCode DualKSPMonitor(KSP ksp, PetscInt it, PetscReal rnorm, void *ctx)
A custom KSP monitor that logs to a file and optionally to the console.
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
static LogLevel get_log_level(void)
Retrieves the current logging level from the environment variable LOG_LEVEL.
Context for a dual-purpose KSP monitor.
PetscErrorCode PoissonOperator_RestoreFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Implementation of PoissonOperator_RestoreFaceMetrics().
static PetscErrorCode PoissonMultigrid_Restrict(Mat R, Vec X, Vec F)
Restricts a fine-level residual to the next coarser level (MatShell multiply).
static PetscInt PoissonOperator_NeighborIndex(PetscInt v, PetscInt d, PetscInt m, PetscBool periodic)
Index of the neighbour at offset d (-1, 0, +1) from v on an axis of m points, wrapping between the in...
PetscReal aj
Inverse Jacobian on the face.
PoissonTransverseDifference diff[3]
Transverse differences; diff[n] is unused.
static PICURV_ALWAYS_INLINE PetscReal PoissonOperator_At(const PetscReal ***field, const PetscInt c[3], const PetscInt d[3])
Value of a cell-centred array at cell c displaced by d.
static PetscErrorCode PoissonMultigrid_CloseConvergenceLog(KSP ksp)
Closes the log file opened by PoissonMultigrid_OpenConvergenceLog().
PetscErrorCode AssemblePoissonOperator(UserCtx *user)
Implementation of AssemblePoissonOperator().
static const PetscInt POISSON_STENCIL_OFFSETS[19][3]
Offsets of the 19 stencil points, in the column order used to insert each row.
static void PoissonOperator_AddFaceFlux(const PoissonFaceGradient *face, const PetscInt own[3], PetscInt n, PetscReal sign, PetscScalar coefficients[19])
Adds the signed gradient flux through one face to a row's stencil coefficients.
PetscErrorCode PoissonOperator_GetFaceMetrics(UserCtx *user, PoissonFaceMetrics *metrics)
Implementation of PoissonOperator_GetFaceMetrics().
static const FieldId POISSON_FACE_AJ_FIELDS[3]
static void PoissonMultigrid_InterpolationParent(PetscInt f, PetscInt m, PetscInt semi, PetscInt *coarse, PetscInt *direction)
Coarse cell and interpolation direction along one axis for fine index f.
static PetscErrorCode PoissonMultigrid_ShiftBlockFactors(KSP level_ksp)
Gives each block factor of a block-Jacobi level solver a small diagonal shift, so the factorization o...
PetscErrorCode UpdatePressure(UserCtx *user)
Implementation of UpdatePressure().
static PetscErrorCode PoissonMultigrid_Interpolate(Mat P, Vec X, Vec F)
Prolongs a coarse-level correction to the next finer level (MatShell multiply).
PetscErrorCode ProjectVelocity(UserCtx *user)
Implementation of ProjectVelocity().
static PetscInt PoissonOperator_StencilSlot(const PetscInt d[3])
Maps the 19 stencil offsets to their slots; corners of the 3x3x3 block are -1.
static PetscErrorCode PoissonMultigrid_RemoveNullSpace(MatNullSpace nullsp, Vec X, void *ctx)
Removes the null space of the Neumann pressure problem from a level vector.
PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg)
Implementation of PoissonSolver_Multigrid().
static const FieldId POISSON_FACE_METRIC_FIELDS[3][3]
Field IDs of the face metric vectors, indexed as PoissonFaceMetrics.
static PICURV_ALWAYS_INLINE PoissonTransverseDifference PoissonOperator_TransverseDifference(const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscInt t, const PetscInt m[3], const PetscBool periodic[3])
The rows and weight of the transverse difference along axis t at the face between cell c and c + e_n,...
static PetscErrorCode PoissonMultigrid_OpenConvergenceLog(KSP ksp, SimCtx *simCtx, PetscInt bi)
Prepares the convergence monitor for one solve and opens its log file on rank 0.
PetscErrorCode ComputePoissonRHS(UserCtx *user, Vec B)
Implementation of ComputePoissonRHS().
static PICURV_ALWAYS_INLINE PoissonFaceGradient PoissonOperator_FaceGradientStencil(const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
Gathers the metric coefficients and transverse differences of the gradient flux through the face betw...
static PetscErrorCode PoissonMultigrid_Build(UserMG *usermg, PetscInt bi)
Builds the multigrid solver for block bi and stores it in the finest level.
Everything needed to evaluate the gradient flux through one face.
Pressure-Poisson projection: the multigrid pressure-correction solve, the pressure update,...
const PetscReal *** aj[3]
PetscReal weight
0.25 central, 0.5 one-sided, 0 when no fluid side remains.
const Cmpnts *** metric[3][3]
static PoissonTransverseKind PoissonOperator_TransverseKind(const PetscReal ***nvert, const PetscInt c[3], PetscInt n, PetscInt t, const PetscInt m[3], const PetscBool periodic[3])
Chooses the transverse stencil along axis t at the face between cell c and c + e_n.
@ POISSON_TRANSVERSE_NONE
Neither side available: the transverse term vanishes.
@ POISSON_TRANSVERSE_CENTRAL
Central over rows -1 and +1, weight 0.25.
@ POISSON_TRANSVERSE_LOWER
One-sided over rows -1 and 0, weight 0.5.
@ POISSON_TRANSVERSE_UPPER
One-sided over rows 0 and +1, weight 0.5.
PetscInt hi
Transverse offset of the added row pair.
static PetscReal PoissonOperator_FaceGradientFlux(const PoissonFaceMetrics *metrics, const PetscReal ***nvert, const PetscReal ***field, const PetscInt c[3], PetscInt n, const PetscInt m[3], const PetscBool periodic[3])
Gradient flux of a cell-centred field through the face between cell c and c + e_n: sum_b g_nb D_b(fie...
#define POISSON_SOLID_THRESHOLD
Cells whose nvert exceeds this value are solid.
PetscInt lo
Transverse offset of the subtracted row pair.
Read-only face metric arrays: metric[n][b] is F_b on the n-faces.
Transverse difference used at one face.
static void GetOwnedInteriorCellBounds(const DMDALocalInfo *info, PetscInt lo[3], PetscInt hi[3])
Index bounds of the interior cells this rank owns in the solver's cell-centred layout: the owned rang...
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
static void GetPeriodicAxes(const SimCtx *simCtx, PetscBool periodic[3])
Which grid axes are periodic.
#define PICURV_ALWAYS_INLINE
Forces inlining of a per-cell or per-face kernel whose size defeats the compiler's inlining heuristic...
SimCtx * simCtx
Back-pointer to the master simulation context.
PetscReal poissonSourceImbalance
char log_dir[PETSC_MAX_PATH_LEN]
PetscBool ps_ksp_pic_monitor_true_residual
#define COEF_TIME_ACCURACY
Coefficient controlling the temporal accuracy scheme (e.g., 1.5 for 2nd Order Backward Difference).
A 3D point or vector with PetscScalar components.
Context for Multigrid operations.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.
User-level context for managing the entire multigrid hierarchy.