14b9ad928SBarry Smith /* 24b9ad928SBarry Smith Defines the multigrid preconditioner interface. 34b9ad928SBarry Smith */ 4af0996ceSBarry Smith #include <petsc/private/pcmgimpl.h> /*I "petscksp.h" I*/ 541b6fd38SMatthew G. Knepley #include <petsc/private/kspimpl.h> 61e25c274SJed Brown #include <petscdm.h> 7391689abSStefano Zampini PETSC_INTERN PetscErrorCode PCPreSolveChangeRHS(PC, PetscBool *); 84b9ad928SBarry Smith 9f3b08a26SMatthew G. Knepley /* 10f3b08a26SMatthew G. Knepley Contains the list of registered coarse space construction routines 11f3b08a26SMatthew G. Knepley */ 12f3b08a26SMatthew G. Knepley PetscFunctionList PCMGCoarseList = NULL; 13f3b08a26SMatthew G. Knepley 14d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGMCycle_Private(PC pc, PC_MG_Levels **mglevelsin, PetscBool transpose, PetscBool matapp, PCRichardsonConvergedReason *reason) 15d71ae5a4SJacob Faibussowitsch { 1631567311SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 1731567311SBarry Smith PC_MG_Levels *mgc, *mglevels = *mglevelsin; 18835f2295SStefano Zampini PetscInt cycles = (mglevels->level == 1) ? 1 : mglevels->cycles; 194b9ad928SBarry Smith 204b9ad928SBarry Smith PetscFunctionBegin; 219566063dSJacob Faibussowitsch if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventBegin(mglevels->eventsmoothsolve, 0, 0, 0, 0)); 22fcb023d4SJed Brown if (!transpose) { 2330b0564aSStefano Zampini if (matapp) { 249566063dSJacob Faibussowitsch PetscCall(KSPMatSolve(mglevels->smoothd, mglevels->B, mglevels->X)); /* pre-smooth */ 259566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->smoothd, pc, NULL)); 2630b0564aSStefano Zampini } else { 279566063dSJacob Faibussowitsch PetscCall(KSPSolve(mglevels->smoothd, mglevels->b, mglevels->x)); /* pre-smooth */ 289566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->smoothd, pc, mglevels->x)); 2930b0564aSStefano Zampini } 30fcb023d4SJed Brown } else { 3128b400f6SJacob Faibussowitsch PetscCheck(!matapp, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not supported"); 329566063dSJacob Faibussowitsch PetscCall(KSPSolveTranspose(mglevels->smoothu, mglevels->b, mglevels->x)); /* transpose of post-smooth */ 339566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->smoothu, pc, mglevels->x)); 34fcb023d4SJed Brown } 359566063dSJacob Faibussowitsch if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventEnd(mglevels->eventsmoothsolve, 0, 0, 0, 0)); 3631567311SBarry Smith if (mglevels->level) { /* not the coarsest grid */ 379566063dSJacob Faibussowitsch if (mglevels->eventresidual) PetscCall(PetscLogEventBegin(mglevels->eventresidual, 0, 0, 0, 0)); 3848a46eb9SPierre Jolivet if (matapp && !mglevels->R) PetscCall(MatDuplicate(mglevels->B, MAT_DO_NOT_COPY_VALUES, &mglevels->R)); 39fcb023d4SJed Brown if (!transpose) { 409566063dSJacob Faibussowitsch if (matapp) PetscCall((*mglevels->matresidual)(mglevels->A, mglevels->B, mglevels->X, mglevels->R)); 419566063dSJacob Faibussowitsch else PetscCall((*mglevels->residual)(mglevels->A, mglevels->b, mglevels->x, mglevels->r)); 42fcb023d4SJed Brown } else { 439566063dSJacob Faibussowitsch if (matapp) PetscCall((*mglevels->matresidualtranspose)(mglevels->A, mglevels->B, mglevels->X, mglevels->R)); 449566063dSJacob Faibussowitsch else PetscCall((*mglevels->residualtranspose)(mglevels->A, mglevels->b, mglevels->x, mglevels->r)); 45fcb023d4SJed Brown } 469566063dSJacob Faibussowitsch if (mglevels->eventresidual) PetscCall(PetscLogEventEnd(mglevels->eventresidual, 0, 0, 0, 0)); 474b9ad928SBarry Smith 484b9ad928SBarry Smith /* if on finest level and have convergence criteria set */ 4931567311SBarry Smith if (mglevels->level == mglevels->levels - 1 && mg->ttol && reason) { 504b9ad928SBarry Smith PetscReal rnorm; 51218e2965SBarry Smith 529566063dSJacob Faibussowitsch PetscCall(VecNorm(mglevels->r, NORM_2, &rnorm)); 534b9ad928SBarry Smith if (rnorm <= mg->ttol) { 5470441072SBarry Smith if (rnorm < mg->abstol) { 554d0a8057SBarry Smith *reason = PCRICHARDSON_CONVERGED_ATOL; 569566063dSJacob Faibussowitsch PetscCall(PetscInfo(pc, "Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n", (double)rnorm, (double)mg->abstol)); 574b9ad928SBarry Smith } else { 584d0a8057SBarry Smith *reason = PCRICHARDSON_CONVERGED_RTOL; 599566063dSJacob Faibussowitsch PetscCall(PetscInfo(pc, "Linear solver has converged. Residual norm %g is less than relative tolerance times initial residual norm %g\n", (double)rnorm, (double)mg->ttol)); 604b9ad928SBarry Smith } 613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 624b9ad928SBarry Smith } 634b9ad928SBarry Smith } 644b9ad928SBarry Smith 6531567311SBarry Smith mgc = *(mglevelsin - 1); 669566063dSJacob Faibussowitsch if (mglevels->eventinterprestrict) PetscCall(PetscLogEventBegin(mglevels->eventinterprestrict, 0, 0, 0, 0)); 67fcb023d4SJed Brown if (!transpose) { 689566063dSJacob Faibussowitsch if (matapp) PetscCall(MatMatRestrict(mglevels->restrct, mglevels->R, &mgc->B)); 699566063dSJacob Faibussowitsch else PetscCall(MatRestrict(mglevels->restrct, mglevels->r, mgc->b)); 70fcb023d4SJed Brown } else { 719566063dSJacob Faibussowitsch if (matapp) PetscCall(MatMatRestrict(mglevels->interpolate, mglevels->R, &mgc->B)); 729566063dSJacob Faibussowitsch else PetscCall(MatRestrict(mglevels->interpolate, mglevels->r, mgc->b)); 73fcb023d4SJed Brown } 749566063dSJacob Faibussowitsch if (mglevels->eventinterprestrict) PetscCall(PetscLogEventEnd(mglevels->eventinterprestrict, 0, 0, 0, 0)); 7530b0564aSStefano Zampini if (matapp) { 7630b0564aSStefano Zampini if (!mgc->X) { 779566063dSJacob Faibussowitsch PetscCall(MatDuplicate(mgc->B, MAT_DO_NOT_COPY_VALUES, &mgc->X)); 7830b0564aSStefano Zampini } else { 799566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(mgc->X)); 8030b0564aSStefano Zampini } 8130b0564aSStefano Zampini } else { 829566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(mgc->x)); 8330b0564aSStefano Zampini } 8448a46eb9SPierre Jolivet while (cycles--) PetscCall(PCMGMCycle_Private(pc, mglevelsin - 1, transpose, matapp, reason)); 859566063dSJacob Faibussowitsch if (mglevels->eventinterprestrict) PetscCall(PetscLogEventBegin(mglevels->eventinterprestrict, 0, 0, 0, 0)); 86fcb023d4SJed Brown if (!transpose) { 879566063dSJacob Faibussowitsch if (matapp) PetscCall(MatMatInterpolateAdd(mglevels->interpolate, mgc->X, mglevels->X, &mglevels->X)); 889566063dSJacob Faibussowitsch else PetscCall(MatInterpolateAdd(mglevels->interpolate, mgc->x, mglevels->x, mglevels->x)); 89fcb023d4SJed Brown } else { 909566063dSJacob Faibussowitsch PetscCall(MatInterpolateAdd(mglevels->restrct, mgc->x, mglevels->x, mglevels->x)); 91fcb023d4SJed Brown } 929566063dSJacob Faibussowitsch if (mglevels->eventinterprestrict) PetscCall(PetscLogEventEnd(mglevels->eventinterprestrict, 0, 0, 0, 0)); 939566063dSJacob Faibussowitsch if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventBegin(mglevels->eventsmoothsolve, 0, 0, 0, 0)); 94fcb023d4SJed Brown if (!transpose) { 9530b0564aSStefano Zampini if (matapp) { 969566063dSJacob Faibussowitsch PetscCall(KSPMatSolve(mglevels->smoothu, mglevels->B, mglevels->X)); /* post smooth */ 979566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->smoothu, pc, NULL)); 9830b0564aSStefano Zampini } else { 999566063dSJacob Faibussowitsch PetscCall(KSPSolve(mglevels->smoothu, mglevels->b, mglevels->x)); /* post smooth */ 1009566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->smoothu, pc, mglevels->x)); 10130b0564aSStefano Zampini } 102fcb023d4SJed Brown } else { 10328b400f6SJacob Faibussowitsch PetscCheck(!matapp, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not supported"); 1049566063dSJacob Faibussowitsch PetscCall(KSPSolveTranspose(mglevels->smoothd, mglevels->b, mglevels->x)); /* post smooth */ 1059566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->smoothd, pc, mglevels->x)); 106fcb023d4SJed Brown } 10741b6fd38SMatthew G. Knepley if (mglevels->cr) { 10820654ebbSStefano Zampini Mat crA; 10920654ebbSStefano Zampini 11028b400f6SJacob Faibussowitsch PetscCheck(!matapp, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not supported"); 11141b6fd38SMatthew G. Knepley /* TODO Turn on copy and turn off noisy if we have an exact solution 1129566063dSJacob Faibussowitsch PetscCall(VecCopy(mglevels->x, mglevels->crx)); 1139566063dSJacob Faibussowitsch PetscCall(VecCopy(mglevels->b, mglevels->crb)); */ 11420654ebbSStefano Zampini PetscCall(KSPGetOperators(mglevels->cr, &crA, NULL)); 11520654ebbSStefano Zampini PetscCall(KSPSetNoisy_Private(crA, mglevels->crx)); 1169566063dSJacob Faibussowitsch PetscCall(KSPSolve(mglevels->cr, mglevels->crb, mglevels->crx)); /* compatible relaxation */ 1179566063dSJacob Faibussowitsch PetscCall(KSPCheckSolve(mglevels->cr, pc, mglevels->crx)); 11841b6fd38SMatthew G. Knepley } 1199566063dSJacob Faibussowitsch if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventEnd(mglevels->eventsmoothsolve, 0, 0, 0, 0)); 1204b9ad928SBarry Smith } 1213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1224b9ad928SBarry Smith } 1234b9ad928SBarry Smith 124d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyRichardson_MG(PC pc, Vec b, Vec x, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool zeroguess, PetscInt *outits, PCRichardsonConvergedReason *reason) 125d71ae5a4SJacob Faibussowitsch { 126f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 127f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 128391689abSStefano Zampini PC tpc; 129391689abSStefano Zampini PetscBool changeu, changed; 130f3fbd535SBarry Smith PetscInt levels = mglevels[0]->levels, i; 1314b9ad928SBarry Smith 1324b9ad928SBarry Smith PetscFunctionBegin; 133a762d673SBarry Smith /* When the DM is supplying the matrix then it will not exist until here */ 134a762d673SBarry Smith for (i = 0; i < levels; i++) { 135a762d673SBarry Smith if (!mglevels[i]->A) { 1369566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothu, &mglevels[i]->A, NULL)); 1379566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)mglevels[i]->A)); 138a762d673SBarry Smith } 139a762d673SBarry Smith } 140391689abSStefano Zampini 1419566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[levels - 1]->smoothd, &tpc)); 1429566063dSJacob Faibussowitsch PetscCall(PCPreSolveChangeRHS(tpc, &changed)); 1439566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[levels - 1]->smoothu, &tpc)); 1449566063dSJacob Faibussowitsch PetscCall(PCPreSolveChangeRHS(tpc, &changeu)); 145391689abSStefano Zampini if (!changed && !changeu) { 1469566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[levels - 1]->b)); 147f3fbd535SBarry Smith mglevels[levels - 1]->b = b; 148391689abSStefano Zampini } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */ 149391689abSStefano Zampini if (!mglevels[levels - 1]->b) { 150391689abSStefano Zampini Vec *vec; 151391689abSStefano Zampini 1529566063dSJacob Faibussowitsch PetscCall(KSPCreateVecs(mglevels[levels - 1]->smoothd, 1, &vec, 0, NULL)); 153391689abSStefano Zampini mglevels[levels - 1]->b = *vec; 1549566063dSJacob Faibussowitsch PetscCall(PetscFree(vec)); 155391689abSStefano Zampini } 1569566063dSJacob Faibussowitsch PetscCall(VecCopy(b, mglevels[levels - 1]->b)); 157391689abSStefano Zampini } 158f3fbd535SBarry Smith mglevels[levels - 1]->x = x; 1594b9ad928SBarry Smith 16031567311SBarry Smith mg->rtol = rtol; 16131567311SBarry Smith mg->abstol = abstol; 16231567311SBarry Smith mg->dtol = dtol; 1634b9ad928SBarry Smith if (rtol) { 1644b9ad928SBarry Smith /* compute initial residual norm for relative convergence test */ 1654b9ad928SBarry Smith PetscReal rnorm; 166218e2965SBarry Smith 1677319c654SBarry Smith if (zeroguess) { 1689566063dSJacob Faibussowitsch PetscCall(VecNorm(b, NORM_2, &rnorm)); 1697319c654SBarry Smith } else { 1709566063dSJacob Faibussowitsch PetscCall((*mglevels[levels - 1]->residual)(mglevels[levels - 1]->A, b, x, w)); 1719566063dSJacob Faibussowitsch PetscCall(VecNorm(w, NORM_2, &rnorm)); 1727319c654SBarry Smith } 17331567311SBarry Smith mg->ttol = PetscMax(rtol * rnorm, abstol); 1742fa5cd67SKarl Rupp } else if (abstol) mg->ttol = abstol; 1752fa5cd67SKarl Rupp else mg->ttol = 0.0; 1764b9ad928SBarry Smith 1774d0a8057SBarry Smith /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't 178336babb1SJed Brown stop prematurely due to small residual */ 1794d0a8057SBarry Smith for (i = 1; i < levels; i++) { 180fb842aefSJose E. Roman PetscCall(KSPSetTolerances(mglevels[i]->smoothu, 0, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT)); 181f3fbd535SBarry Smith if (mglevels[i]->smoothu != mglevels[i]->smoothd) { 18223067569SBarry Smith /* For Richardson the initial guess is nonzero since it is solving in each cycle the original system not just applying as a preconditioner */ 1839566063dSJacob Faibussowitsch PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->smoothd, PETSC_TRUE)); 184fb842aefSJose E. Roman PetscCall(KSPSetTolerances(mglevels[i]->smoothd, 0, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT)); 1854b9ad928SBarry Smith } 1864d0a8057SBarry Smith } 1874d0a8057SBarry Smith 188dd460d27SBarry Smith *reason = PCRICHARDSON_NOT_SET; 1894d0a8057SBarry Smith for (i = 0; i < its; i++) { 1909566063dSJacob Faibussowitsch PetscCall(PCMGMCycle_Private(pc, mglevels + levels - 1, PETSC_FALSE, PETSC_FALSE, reason)); 1914d0a8057SBarry Smith if (*reason) break; 1924d0a8057SBarry Smith } 193dd460d27SBarry Smith if (*reason == PCRICHARDSON_NOT_SET) *reason = PCRICHARDSON_CONVERGED_ITS; 1944d0a8057SBarry Smith *outits = i; 195391689abSStefano Zampini if (!changed && !changeu) mglevels[levels - 1]->b = NULL; 1963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1974b9ad928SBarry Smith } 1984b9ad928SBarry Smith 199d71ae5a4SJacob Faibussowitsch PetscErrorCode PCReset_MG(PC pc) 200d71ae5a4SJacob Faibussowitsch { 2013aeaf226SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 2023aeaf226SBarry Smith PC_MG_Levels **mglevels = mg->levels; 2032b3cbbdaSStefano Zampini PetscInt i, n; 2043aeaf226SBarry Smith 2053aeaf226SBarry Smith PetscFunctionBegin; 2063aeaf226SBarry Smith if (mglevels) { 2073aeaf226SBarry Smith n = mglevels[0]->levels; 2083aeaf226SBarry Smith for (i = 0; i < n - 1; i++) { 2099566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[i + 1]->r)); 2109566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[i]->b)); 2119566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[i]->x)); 2129566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i + 1]->R)); 2139566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i]->B)); 2149566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i]->X)); 2159566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[i]->crx)); 2169566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[i]->crb)); 2179566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i + 1]->restrct)); 2189566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i + 1]->interpolate)); 2199566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i + 1]->inject)); 2209566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[i + 1]->rscale)); 2213aeaf226SBarry Smith } 2229566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[n - 1]->crx)); 2239566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[n - 1]->crb)); 224391689abSStefano Zampini /* this is not null only if the smoother on the finest level 225391689abSStefano Zampini changes the rhs during PreSolve */ 2269566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[n - 1]->b)); 2279566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[n - 1]->B)); 2283aeaf226SBarry Smith 2293aeaf226SBarry Smith for (i = 0; i < n; i++) { 2302b3cbbdaSStefano Zampini PetscCall(MatDestroy(&mglevels[i]->coarseSpace)); 2319566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i]->A)); 23248a46eb9SPierre Jolivet if (mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPReset(mglevels[i]->smoothd)); 2339566063dSJacob Faibussowitsch PetscCall(KSPReset(mglevels[i]->smoothu)); 2349566063dSJacob Faibussowitsch if (mglevels[i]->cr) PetscCall(KSPReset(mglevels[i]->cr)); 2353aeaf226SBarry Smith } 236f3b08a26SMatthew G. Knepley mg->Nc = 0; 2373aeaf226SBarry Smith } 2383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2393aeaf226SBarry Smith } 2403aeaf226SBarry Smith 24141b6fd38SMatthew G. Knepley /* Implementing CR 24241b6fd38SMatthew G. Knepley 24341b6fd38SMatthew G. Knepley We only want to make corrections that ``do not change'' the coarse solution. What we mean by not changing is that if I prolong my coarse solution to the fine grid and then inject that fine solution back to the coarse grid, I get the same answer. Injection is what Brannick calls R. We want the complementary projector to Inj, which we will call S, after Brannick, so that Inj S = 0. Now the orthogonal projector onto the range of Inj^T is 24441b6fd38SMatthew G. Knepley 24541b6fd38SMatthew G. Knepley Inj^T (Inj Inj^T)^{-1} Inj 24641b6fd38SMatthew G. Knepley 24741b6fd38SMatthew G. Knepley and if Inj is a VecScatter, as it is now in PETSc, we have 24841b6fd38SMatthew G. Knepley 24941b6fd38SMatthew G. Knepley Inj^T Inj 25041b6fd38SMatthew G. Knepley 25141b6fd38SMatthew G. Knepley and 25241b6fd38SMatthew G. Knepley 25341b6fd38SMatthew G. Knepley S = I - Inj^T Inj 25441b6fd38SMatthew G. Knepley 25541b6fd38SMatthew G. Knepley since 25641b6fd38SMatthew G. Knepley 25741b6fd38SMatthew G. Knepley Inj S = Inj - (Inj Inj^T) Inj = 0. 25841b6fd38SMatthew G. Knepley 25941b6fd38SMatthew G. Knepley Brannick suggests 26041b6fd38SMatthew G. Knepley 26141b6fd38SMatthew G. Knepley A \to S^T A S \qquad\mathrm{and}\qquad M \to S^T M S 26241b6fd38SMatthew G. Knepley 26341b6fd38SMatthew G. Knepley but I do not think his :math:`S^T S = I` is correct. Our S is an orthogonal projector, so :math:`S^T S = S^2 = S`. We will use 26441b6fd38SMatthew G. Knepley 26541b6fd38SMatthew G. Knepley M^{-1} A \to S M^{-1} A S 26641b6fd38SMatthew G. Knepley 26741b6fd38SMatthew G. Knepley In fact, since it is somewhat hard in PETSc to do the symmetric application, we will just apply S on the left. 26841b6fd38SMatthew G. Knepley 26941b6fd38SMatthew G. Knepley Check: || Inj P - I ||_F < tol 27041b6fd38SMatthew G. Knepley Check: In general, Inj Inj^T = I 27141b6fd38SMatthew G. Knepley */ 27241b6fd38SMatthew G. Knepley 27341b6fd38SMatthew G. Knepley typedef struct { 27441b6fd38SMatthew G. Knepley PC mg; /* The PCMG object */ 27541b6fd38SMatthew G. Knepley PetscInt l; /* The multigrid level for this solver */ 27641b6fd38SMatthew G. Knepley Mat Inj; /* The injection matrix */ 27741b6fd38SMatthew G. Knepley Mat S; /* I - Inj^T Inj */ 27841b6fd38SMatthew G. Knepley } CRContext; 27941b6fd38SMatthew G. Knepley 280d71ae5a4SJacob Faibussowitsch static PetscErrorCode CRSetup_Private(PC pc) 281d71ae5a4SJacob Faibussowitsch { 28241b6fd38SMatthew G. Knepley CRContext *ctx; 28341b6fd38SMatthew G. Knepley Mat It; 28441b6fd38SMatthew G. Knepley 28541b6fd38SMatthew G. Knepley PetscFunctionBeginUser; 2869566063dSJacob Faibussowitsch PetscCall(PCShellGetContext(pc, &ctx)); 2879566063dSJacob Faibussowitsch PetscCall(PCMGGetInjection(ctx->mg, ctx->l, &It)); 28828b400f6SJacob Faibussowitsch PetscCheck(It, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "CR requires that injection be defined for this PCMG"); 2899566063dSJacob Faibussowitsch PetscCall(MatCreateTranspose(It, &ctx->Inj)); 2909566063dSJacob Faibussowitsch PetscCall(MatCreateNormal(ctx->Inj, &ctx->S)); 2919566063dSJacob Faibussowitsch PetscCall(MatScale(ctx->S, -1.0)); 2929566063dSJacob Faibussowitsch PetscCall(MatShift(ctx->S, 1.0)); 2933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 29441b6fd38SMatthew G. Knepley } 29541b6fd38SMatthew G. Knepley 296d71ae5a4SJacob Faibussowitsch static PetscErrorCode CRApply_Private(PC pc, Vec x, Vec y) 297d71ae5a4SJacob Faibussowitsch { 29841b6fd38SMatthew G. Knepley CRContext *ctx; 29941b6fd38SMatthew G. Knepley 30041b6fd38SMatthew G. Knepley PetscFunctionBeginUser; 3019566063dSJacob Faibussowitsch PetscCall(PCShellGetContext(pc, &ctx)); 3029566063dSJacob Faibussowitsch PetscCall(MatMult(ctx->S, x, y)); 3033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30441b6fd38SMatthew G. Knepley } 30541b6fd38SMatthew G. Knepley 306d71ae5a4SJacob Faibussowitsch static PetscErrorCode CRDestroy_Private(PC pc) 307d71ae5a4SJacob Faibussowitsch { 30841b6fd38SMatthew G. Knepley CRContext *ctx; 30941b6fd38SMatthew G. Knepley 31041b6fd38SMatthew G. Knepley PetscFunctionBeginUser; 3119566063dSJacob Faibussowitsch PetscCall(PCShellGetContext(pc, &ctx)); 3129566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ctx->Inj)); 3139566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ctx->S)); 3149566063dSJacob Faibussowitsch PetscCall(PetscFree(ctx)); 3159566063dSJacob Faibussowitsch PetscCall(PCShellSetContext(pc, NULL)); 3163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31741b6fd38SMatthew G. Knepley } 31841b6fd38SMatthew G. Knepley 319d71ae5a4SJacob Faibussowitsch static PetscErrorCode CreateCR_Private(PC pc, PetscInt l, PC *cr) 320d71ae5a4SJacob Faibussowitsch { 32141b6fd38SMatthew G. Knepley CRContext *ctx; 32241b6fd38SMatthew G. Knepley 32341b6fd38SMatthew G. Knepley PetscFunctionBeginUser; 3249566063dSJacob Faibussowitsch PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), cr)); 3259566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*cr, "S (complementary projector to injection)")); 3269566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(1, &ctx)); 32741b6fd38SMatthew G. Knepley ctx->mg = pc; 32841b6fd38SMatthew G. Knepley ctx->l = l; 3299566063dSJacob Faibussowitsch PetscCall(PCSetType(*cr, PCSHELL)); 3309566063dSJacob Faibussowitsch PetscCall(PCShellSetContext(*cr, ctx)); 3319566063dSJacob Faibussowitsch PetscCall(PCShellSetApply(*cr, CRApply_Private)); 3329566063dSJacob Faibussowitsch PetscCall(PCShellSetSetUp(*cr, CRSetup_Private)); 3339566063dSJacob Faibussowitsch PetscCall(PCShellSetDestroy(*cr, CRDestroy_Private)); 3343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33541b6fd38SMatthew G. Knepley } 33641b6fd38SMatthew G. Knepley 3378f4fb22eSMark Adams PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char[], const char[], const char *[], const char *[], PetscBool *); 3388f4fb22eSMark Adams 339d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetLevels_MG(PC pc, PetscInt levels, MPI_Comm *comms) 340d71ae5a4SJacob Faibussowitsch { 341f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 342ce94432eSBarry Smith MPI_Comm comm; 3433aeaf226SBarry Smith PC_MG_Levels **mglevels = mg->levels; 34410eca3edSLisandro Dalcin PCMGType mgtype = mg->am; 34510167fecSBarry Smith PetscInt mgctype = (PetscInt)PC_MG_CYCLE_V; 346f3fbd535SBarry Smith PetscInt i; 347f3fbd535SBarry Smith PetscMPIInt size; 348f3fbd535SBarry Smith const char *prefix; 349f3fbd535SBarry Smith PC ipc; 3503aeaf226SBarry Smith PetscInt n; 3514b9ad928SBarry Smith 3524b9ad928SBarry Smith PetscFunctionBegin; 3530700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 354548767e0SJed Brown PetscValidLogicalCollectiveInt(pc, levels, 2); 3553ba16761SJacob Faibussowitsch if (mg->nlevels == levels) PetscFunctionReturn(PETSC_SUCCESS); 3569566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)pc, &comm)); 3573aeaf226SBarry Smith if (mglevels) { 35810eca3edSLisandro Dalcin mgctype = mglevels[0]->cycles; 3593aeaf226SBarry Smith /* changing the number of levels so free up the previous stuff */ 3609566063dSJacob Faibussowitsch PetscCall(PCReset_MG(pc)); 3613aeaf226SBarry Smith n = mglevels[0]->levels; 3623aeaf226SBarry Smith for (i = 0; i < n; i++) { 36348a46eb9SPierre Jolivet if (mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPDestroy(&mglevels[i]->smoothd)); 3649566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&mglevels[i]->smoothu)); 3659566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&mglevels[i]->cr)); 3669566063dSJacob Faibussowitsch PetscCall(PetscFree(mglevels[i])); 3673aeaf226SBarry Smith } 3689566063dSJacob Faibussowitsch PetscCall(PetscFree(mg->levels)); 3693aeaf226SBarry Smith } 370f3fbd535SBarry Smith 371f3fbd535SBarry Smith mg->nlevels = levels; 372f3fbd535SBarry Smith 3739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(levels, &mglevels)); 374f3fbd535SBarry Smith 3759566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 376f3fbd535SBarry Smith 377a9db87a2SMatthew G Knepley mg->stageApply = 0; 378f3fbd535SBarry Smith for (i = 0; i < levels; i++) { 3794dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&mglevels[i])); 3802fa5cd67SKarl Rupp 381f3fbd535SBarry Smith mglevels[i]->level = i; 382f3fbd535SBarry Smith mglevels[i]->levels = levels; 38310eca3edSLisandro Dalcin mglevels[i]->cycles = mgctype; 384336babb1SJed Brown mg->default_smoothu = 2; 385336babb1SJed Brown mg->default_smoothd = 2; 38663e6d426SJed Brown mglevels[i]->eventsmoothsetup = 0; 38763e6d426SJed Brown mglevels[i]->eventsmoothsolve = 0; 38863e6d426SJed Brown mglevels[i]->eventresidual = 0; 38963e6d426SJed Brown mglevels[i]->eventinterprestrict = 0; 390f3fbd535SBarry Smith 391f3fbd535SBarry Smith if (comms) comm = comms[i]; 392d5a8f7e6SBarry Smith if (comm != MPI_COMM_NULL) { 3939566063dSJacob Faibussowitsch PetscCall(KSPCreate(comm, &mglevels[i]->smoothd)); 3943821be0aSBarry Smith PetscCall(KSPSetNestLevel(mglevels[i]->smoothd, pc->kspnestlevel)); 3959566063dSJacob Faibussowitsch PetscCall(KSPSetErrorIfNotConverged(mglevels[i]->smoothd, pc->erroriffailure)); 3969566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd, (PetscObject)pc, levels - i)); 3979566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(mglevels[i]->smoothd, prefix)); 3989566063dSJacob Faibussowitsch PetscCall(PetscObjectComposedDataSetInt((PetscObject)mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level)); 3998f4fb22eSMark Adams if (i == 0 && levels > 1) { // coarse grid 4009566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(mglevels[0]->smoothd, "mg_coarse_")); 401f3fbd535SBarry Smith 4029dbfc187SHong Zhang /* coarse solve is (redundant) LU by default; set shifttype NONZERO to avoid annoying zero-pivot in LU preconditioner */ 4039566063dSJacob Faibussowitsch PetscCall(KSPSetType(mglevels[0]->smoothd, KSPPREONLY)); 4049566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[0]->smoothd, &ipc)); 4059566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 406f3fbd535SBarry Smith if (size > 1) { 4079566063dSJacob Faibussowitsch PetscCall(PCSetType(ipc, PCREDUNDANT)); 408f3fbd535SBarry Smith } else { 4099566063dSJacob Faibussowitsch PetscCall(PCSetType(ipc, PCLU)); 410f3fbd535SBarry Smith } 4119566063dSJacob Faibussowitsch PetscCall(PCFactorSetShiftType(ipc, MAT_SHIFT_INBLOCKS)); 4128f4fb22eSMark Adams } else { 4138f4fb22eSMark Adams char tprefix[128]; 4148f4fb22eSMark Adams 4158f4fb22eSMark Adams PetscCall(KSPSetType(mglevels[i]->smoothd, KSPCHEBYSHEV)); 4168f4fb22eSMark Adams PetscCall(KSPSetConvergenceTest(mglevels[i]->smoothd, KSPConvergedSkip, NULL, NULL)); 4178f4fb22eSMark Adams PetscCall(KSPSetNormType(mglevels[i]->smoothd, KSP_NORM_NONE)); 4188f4fb22eSMark Adams PetscCall(KSPGetPC(mglevels[i]->smoothd, &ipc)); 4198f4fb22eSMark Adams PetscCall(PCSetType(ipc, PCSOR)); 420fb842aefSJose E. Roman PetscCall(KSPSetTolerances(mglevels[i]->smoothd, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, mg->default_smoothd)); 4218f4fb22eSMark Adams 4228f4fb22eSMark Adams if (i == levels - 1 && levels > 1) { // replace 'mg_finegrid_' with 'mg_levels_X_' 4238f4fb22eSMark Adams PetscBool set; 424218e2965SBarry Smith 4258f4fb22eSMark Adams PetscCall(PetscOptionsFindPairPrefix_Private(((PetscObject)mglevels[i]->smoothd)->options, ((PetscObject)mglevels[i]->smoothd)->prefix, "-mg_fine_", NULL, NULL, &set)); 4268f4fb22eSMark Adams if (set) { 4278f4fb22eSMark Adams if (prefix) PetscCall(PetscSNPrintf(tprefix, 128, "%smg_fine_", prefix)); 4288f4fb22eSMark Adams else PetscCall(PetscSNPrintf(tprefix, 128, "mg_fine_")); 4298f4fb22eSMark Adams PetscCall(KSPSetOptionsPrefix(mglevels[i]->smoothd, tprefix)); 4308f4fb22eSMark Adams } else { 431835f2295SStefano Zampini PetscCall(PetscSNPrintf(tprefix, 128, "mg_levels_%" PetscInt_FMT "_", i)); 4328f4fb22eSMark Adams PetscCall(KSPAppendOptionsPrefix(mglevels[i]->smoothd, tprefix)); 4338f4fb22eSMark Adams } 4348f4fb22eSMark Adams } else { 435835f2295SStefano Zampini PetscCall(PetscSNPrintf(tprefix, 128, "mg_levels_%" PetscInt_FMT "_", i)); 4368f4fb22eSMark Adams PetscCall(KSPAppendOptionsPrefix(mglevels[i]->smoothd, tprefix)); 4378f4fb22eSMark Adams } 438f3fbd535SBarry Smith } 439d5a8f7e6SBarry Smith } 440f3fbd535SBarry Smith mglevels[i]->smoothu = mglevels[i]->smoothd; 44131567311SBarry Smith mg->rtol = 0.0; 44231567311SBarry Smith mg->abstol = 0.0; 44331567311SBarry Smith mg->dtol = 0.0; 44431567311SBarry Smith mg->ttol = 0.0; 44531567311SBarry Smith mg->cyclesperpcapply = 1; 446f3fbd535SBarry Smith } 447f3fbd535SBarry Smith mg->levels = mglevels; 4489566063dSJacob Faibussowitsch PetscCall(PCMGSetType(pc, mgtype)); 4493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4504b9ad928SBarry Smith } 4514b9ad928SBarry Smith 45297d33e41SMatthew G. Knepley /*@C 453f1580f4eSBarry Smith PCMGSetLevels - Sets the number of levels to use with `PCMG`. 454f1580f4eSBarry Smith Must be called before any other `PCMG` routine. 45597d33e41SMatthew G. Knepley 456c3339decSBarry Smith Logically Collective 45797d33e41SMatthew G. Knepley 45897d33e41SMatthew G. Knepley Input Parameters: 45997d33e41SMatthew G. Knepley + pc - the preconditioner context 46097d33e41SMatthew G. Knepley . levels - the number of levels 46197d33e41SMatthew G. Knepley - comms - optional communicators for each level; this is to allow solving the coarser problems 462d5a8f7e6SBarry Smith on smaller sets of processes. For processes that are not included in the computation 46320f4b53cSBarry Smith you must pass `MPI_COMM_NULL`. Use comms = `NULL` to specify that all processes 46405552d4bSJunchao Zhang should participate in each level of problem. 46597d33e41SMatthew G. Knepley 466*efba3485SBarry Smith Options Database Key: 467*efba3485SBarry Smith . -pc_mg_levels <levels> - set the number of levels to use 468*efba3485SBarry Smith 46997d33e41SMatthew G. Knepley Level: intermediate 47097d33e41SMatthew G. Knepley 47197d33e41SMatthew G. Knepley Notes: 47220f4b53cSBarry Smith If the number of levels is one then the multigrid uses the `-mg_levels` prefix 4738f4fb22eSMark Adams for setting the level options rather than the `-mg_coarse` or `-mg_fine` prefix. 47497d33e41SMatthew G. Knepley 475d5a8f7e6SBarry Smith You can free the information in comms after this routine is called. 476d5a8f7e6SBarry Smith 477f1580f4eSBarry Smith The array of MPI communicators must contain `MPI_COMM_NULL` for those ranks that at each level 478d5a8f7e6SBarry Smith are not participating in the coarser solve. For example, with 2 levels and 1 and 2 ranks on 479d5a8f7e6SBarry Smith the two levels, rank 0 in the original communicator will pass in an array of 2 communicators 480d5a8f7e6SBarry Smith of size 2 and 1, while rank 1 in the original communicator will pass in array of 2 communicators 481f1580f4eSBarry Smith the first of size 2 and the second of value `MPI_COMM_NULL` since the rank 1 does not participate 482d5a8f7e6SBarry Smith in the coarse grid solve. 483d5a8f7e6SBarry Smith 484f1580f4eSBarry Smith Since each coarser level may have a new `MPI_Comm` with fewer ranks than the previous, one 485d5a8f7e6SBarry Smith must take special care in providing the restriction and interpolation operation. We recommend 486d5a8f7e6SBarry Smith providing these as two step operations; first perform a standard restriction or interpolation on 487d5a8f7e6SBarry Smith the full number of ranks for that level and then use an MPI call to copy the resulting vector 48805552d4bSJunchao Zhang array entries (after calls to VecGetArray()) to the smaller or larger number of ranks, note in both 489d5a8f7e6SBarry Smith cases the MPI calls must be made on the larger of the two communicators. Traditional MPI send and 49020f4b53cSBarry Smith receives or `MPI_AlltoAllv()` could be used to do the reshuffling of the vector entries. 491d5a8f7e6SBarry Smith 492feefa0e1SJacob Faibussowitsch Fortran Notes: 49320f4b53cSBarry Smith Use comms = `PETSC_NULL_MPI_COMM` as the equivalent of `NULL` in the C interface. Note `PETSC_NULL_MPI_COMM` 494f1580f4eSBarry Smith is not `MPI_COMM_NULL`. It is more like `PETSC_NULL_INTEGER`, `PETSC_NULL_REAL` etc. 495d5a8f7e6SBarry Smith 496562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMGSetType()`, `PCMGGetLevels()` 49797d33e41SMatthew G. Knepley @*/ 498d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetLevels(PC pc, PetscInt levels, MPI_Comm *comms) 499d71ae5a4SJacob Faibussowitsch { 50097d33e41SMatthew G. Knepley PetscFunctionBegin; 50197d33e41SMatthew G. Knepley PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 5024f572ea9SToby Isaac if (comms) PetscAssertPointer(comms, 3); 503cac4c232SBarry Smith PetscTryMethod(pc, "PCMGSetLevels_C", (PC, PetscInt, MPI_Comm *), (pc, levels, comms)); 5043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 50597d33e41SMatthew G. Knepley } 50697d33e41SMatthew G. Knepley 507d71ae5a4SJacob Faibussowitsch PetscErrorCode PCDestroy_MG(PC pc) 508d71ae5a4SJacob Faibussowitsch { 509a06653b4SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 510a06653b4SBarry Smith PC_MG_Levels **mglevels = mg->levels; 511a06653b4SBarry Smith PetscInt i, n; 512c07bf074SBarry Smith 513c07bf074SBarry Smith PetscFunctionBegin; 5149566063dSJacob Faibussowitsch PetscCall(PCReset_MG(pc)); 515a06653b4SBarry Smith if (mglevels) { 516a06653b4SBarry Smith n = mglevels[0]->levels; 517a06653b4SBarry Smith for (i = 0; i < n; i++) { 51848a46eb9SPierre Jolivet if (mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPDestroy(&mglevels[i]->smoothd)); 5199566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&mglevels[i]->smoothu)); 5209566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&mglevels[i]->cr)); 5219566063dSJacob Faibussowitsch PetscCall(PetscFree(mglevels[i])); 522a06653b4SBarry Smith } 5239566063dSJacob Faibussowitsch PetscCall(PetscFree(mg->levels)); 524a06653b4SBarry Smith } 5259566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data)); 5269566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", NULL)); 5279566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", NULL)); 5282b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetGalerkin_C", NULL)); 529bbbcb081SMark Adams PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetReusePreconditioner_C", NULL)); 5302b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetLevels_C", NULL)); 5312b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetLevels_C", NULL)); 5322b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", NULL)); 5332b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", NULL)); 5342b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptInterpolation_C", NULL)); 5352b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptInterpolation_C", NULL)); 5362b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCR_C", NULL)); 5372b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCR_C", NULL)); 5382b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCoarseSpaceType_C", NULL)); 5392b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCoarseSpaceType_C", NULL)); 5403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 541f3fbd535SBarry Smith } 542f3fbd535SBarry Smith 543f3fbd535SBarry Smith /* 544f3fbd535SBarry Smith PCApply_MG - Runs either an additive, multiplicative, Kaskadic 545f3fbd535SBarry Smith or full cycle of multigrid. 546f3fbd535SBarry Smith 547f3fbd535SBarry Smith Note: 548f3fbd535SBarry Smith A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle(). 549f3fbd535SBarry Smith */ 550d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_MG_Internal(PC pc, Vec b, Vec x, Mat B, Mat X, PetscBool transpose) 551d71ae5a4SJacob Faibussowitsch { 552f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 553f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 554391689abSStefano Zampini PC tpc; 555f3fbd535SBarry Smith PetscInt levels = mglevels[0]->levels, i; 55630b0564aSStefano Zampini PetscBool changeu, changed, matapp; 557f3fbd535SBarry Smith 558f3fbd535SBarry Smith PetscFunctionBegin; 55930b0564aSStefano Zampini matapp = (PetscBool)(B && X); 5609566063dSJacob Faibussowitsch if (mg->stageApply) PetscCall(PetscLogStagePush(mg->stageApply)); 561e1d8e5deSBarry Smith /* When the DM is supplying the matrix then it will not exist until here */ 562298cc208SBarry Smith for (i = 0; i < levels; i++) { 563e1d8e5deSBarry Smith if (!mglevels[i]->A) { 5649566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothu, &mglevels[i]->A, NULL)); 5659566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)mglevels[i]->A)); 566e1d8e5deSBarry Smith } 567e1d8e5deSBarry Smith } 568e1d8e5deSBarry Smith 5699566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[levels - 1]->smoothd, &tpc)); 5709566063dSJacob Faibussowitsch PetscCall(PCPreSolveChangeRHS(tpc, &changed)); 5719566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[levels - 1]->smoothu, &tpc)); 5729566063dSJacob Faibussowitsch PetscCall(PCPreSolveChangeRHS(tpc, &changeu)); 573391689abSStefano Zampini if (!changeu && !changed) { 57430b0564aSStefano Zampini if (matapp) { 5759566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[levels - 1]->B)); 57630b0564aSStefano Zampini mglevels[levels - 1]->B = B; 57730b0564aSStefano Zampini } else { 5789566063dSJacob Faibussowitsch PetscCall(VecDestroy(&mglevels[levels - 1]->b)); 579f3fbd535SBarry Smith mglevels[levels - 1]->b = b; 58030b0564aSStefano Zampini } 581391689abSStefano Zampini } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */ 58230b0564aSStefano Zampini if (matapp) { 58330b0564aSStefano Zampini if (mglevels[levels - 1]->B) { 58430b0564aSStefano Zampini PetscInt N1, N2; 58530b0564aSStefano Zampini PetscBool flg; 58630b0564aSStefano Zampini 5879566063dSJacob Faibussowitsch PetscCall(MatGetSize(mglevels[levels - 1]->B, NULL, &N1)); 5889566063dSJacob Faibussowitsch PetscCall(MatGetSize(B, NULL, &N2)); 5899566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mglevels[levels - 1]->B, ((PetscObject)B)->type_name, &flg)); 59048a46eb9SPierre Jolivet if (N1 != N2 || !flg) PetscCall(MatDestroy(&mglevels[levels - 1]->B)); 59130b0564aSStefano Zampini } 59230b0564aSStefano Zampini if (!mglevels[levels - 1]->B) { 5939566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &mglevels[levels - 1]->B)); 59430b0564aSStefano Zampini } else { 5959566063dSJacob Faibussowitsch PetscCall(MatCopy(B, mglevels[levels - 1]->B, SAME_NONZERO_PATTERN)); 59630b0564aSStefano Zampini } 59730b0564aSStefano Zampini } else { 598391689abSStefano Zampini if (!mglevels[levels - 1]->b) { 599391689abSStefano Zampini Vec *vec; 600391689abSStefano Zampini 6019566063dSJacob Faibussowitsch PetscCall(KSPCreateVecs(mglevels[levels - 1]->smoothd, 1, &vec, 0, NULL)); 602391689abSStefano Zampini mglevels[levels - 1]->b = *vec; 6039566063dSJacob Faibussowitsch PetscCall(PetscFree(vec)); 604391689abSStefano Zampini } 6059566063dSJacob Faibussowitsch PetscCall(VecCopy(b, mglevels[levels - 1]->b)); 606391689abSStefano Zampini } 60730b0564aSStefano Zampini } 6089371c9d4SSatish Balay if (matapp) { 6099371c9d4SSatish Balay mglevels[levels - 1]->X = X; 6109371c9d4SSatish Balay } else { 6119371c9d4SSatish Balay mglevels[levels - 1]->x = x; 6129371c9d4SSatish Balay } 61330b0564aSStefano Zampini 61430b0564aSStefano Zampini /* If coarser Xs are present, it means we have already block applied the PC at least once 61530b0564aSStefano Zampini Reset operators if sizes/type do no match */ 61630b0564aSStefano Zampini if (matapp && levels > 1 && mglevels[levels - 2]->X) { 61730b0564aSStefano Zampini PetscInt Xc, Bc; 61830b0564aSStefano Zampini PetscBool flg; 61930b0564aSStefano Zampini 6209566063dSJacob Faibussowitsch PetscCall(MatGetSize(mglevels[levels - 2]->X, NULL, &Xc)); 6219566063dSJacob Faibussowitsch PetscCall(MatGetSize(mglevels[levels - 1]->B, NULL, &Bc)); 6229566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)mglevels[levels - 2]->X, ((PetscObject)mglevels[levels - 1]->X)->type_name, &flg)); 62330b0564aSStefano Zampini if (Xc != Bc || !flg) { 6249566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[levels - 1]->R)); 62530b0564aSStefano Zampini for (i = 0; i < levels - 1; i++) { 6269566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i]->R)); 6279566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i]->B)); 6289566063dSJacob Faibussowitsch PetscCall(MatDestroy(&mglevels[i]->X)); 62930b0564aSStefano Zampini } 63030b0564aSStefano Zampini } 63130b0564aSStefano Zampini } 632391689abSStefano Zampini 63331567311SBarry Smith if (mg->am == PC_MG_MULTIPLICATIVE) { 6349566063dSJacob Faibussowitsch if (matapp) PetscCall(MatZeroEntries(X)); 6359566063dSJacob Faibussowitsch else PetscCall(VecZeroEntries(x)); 63648a46eb9SPierre Jolivet for (i = 0; i < mg->cyclesperpcapply; i++) PetscCall(PCMGMCycle_Private(pc, mglevels + levels - 1, transpose, matapp, NULL)); 6372fa5cd67SKarl Rupp } else if (mg->am == PC_MG_ADDITIVE) { 6389566063dSJacob Faibussowitsch PetscCall(PCMGACycle_Private(pc, mglevels, transpose, matapp)); 6392fa5cd67SKarl Rupp } else if (mg->am == PC_MG_KASKADE) { 6409566063dSJacob Faibussowitsch PetscCall(PCMGKCycle_Private(pc, mglevels, transpose, matapp)); 6412fa5cd67SKarl Rupp } else { 6429566063dSJacob Faibussowitsch PetscCall(PCMGFCycle_Private(pc, mglevels, transpose, matapp)); 643f3fbd535SBarry Smith } 6449566063dSJacob Faibussowitsch if (mg->stageApply) PetscCall(PetscLogStagePop()); 64530b0564aSStefano Zampini if (!changeu && !changed) { 6469371c9d4SSatish Balay if (matapp) { 6479371c9d4SSatish Balay mglevels[levels - 1]->B = NULL; 6489371c9d4SSatish Balay } else { 6499371c9d4SSatish Balay mglevels[levels - 1]->b = NULL; 6509371c9d4SSatish Balay } 65130b0564aSStefano Zampini } 6523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 653f3fbd535SBarry Smith } 654f3fbd535SBarry Smith 655d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_MG(PC pc, Vec b, Vec x) 656d71ae5a4SJacob Faibussowitsch { 657fcb023d4SJed Brown PetscFunctionBegin; 6589566063dSJacob Faibussowitsch PetscCall(PCApply_MG_Internal(pc, b, x, NULL, NULL, PETSC_FALSE)); 6593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 660fcb023d4SJed Brown } 661fcb023d4SJed Brown 662d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyTranspose_MG(PC pc, Vec b, Vec x) 663d71ae5a4SJacob Faibussowitsch { 664fcb023d4SJed Brown PetscFunctionBegin; 6659566063dSJacob Faibussowitsch PetscCall(PCApply_MG_Internal(pc, b, x, NULL, NULL, PETSC_TRUE)); 6663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 66730b0564aSStefano Zampini } 66830b0564aSStefano Zampini 669d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_MG(PC pc, Mat b, Mat x) 670d71ae5a4SJacob Faibussowitsch { 67130b0564aSStefano Zampini PetscFunctionBegin; 6729566063dSJacob Faibussowitsch PetscCall(PCApply_MG_Internal(pc, NULL, NULL, b, x, PETSC_FALSE)); 6733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 674fcb023d4SJed Brown } 675f3fbd535SBarry Smith 6764dbf25a8SPierre Jolivet static PetscErrorCode PCMatApplyTranspose_MG(PC pc, Mat b, Mat x) 6774dbf25a8SPierre Jolivet { 6784dbf25a8SPierre Jolivet PetscFunctionBegin; 6794dbf25a8SPierre Jolivet PetscCall(PCApply_MG_Internal(pc, NULL, NULL, b, x, PETSC_TRUE)); 6804dbf25a8SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 6814dbf25a8SPierre Jolivet } 6824dbf25a8SPierre Jolivet 683ce78bad3SBarry Smith PetscErrorCode PCSetFromOptions_MG(PC pc, PetscOptionItems PetscOptionsObject) 684d71ae5a4SJacob Faibussowitsch { 685710315b6SLawrence Mitchell PetscInt levels, cycles; 686f3b08a26SMatthew G. Knepley PetscBool flg, flg2; 687f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 6883d3eaba7SBarry Smith PC_MG_Levels **mglevels; 689f3fbd535SBarry Smith PCMGType mgtype; 690f3fbd535SBarry Smith PCMGCycleType mgctype; 6912134b1e4SBarry Smith PCMGGalerkinType gtype; 6922b3cbbdaSStefano Zampini PCMGCoarseSpaceType coarseSpaceType; 693f3fbd535SBarry Smith 694f3fbd535SBarry Smith PetscFunctionBegin; 6951d743356SStefano Zampini levels = PetscMax(mg->nlevels, 1); 696d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "Multigrid options"); 6979566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-pc_mg_levels", "Number of Levels", "PCMGSetLevels", levels, &levels, &flg)); 6981a97d7b6SStefano Zampini if (!flg && !mg->levels && pc->dm) { 6999566063dSJacob Faibussowitsch PetscCall(DMGetRefineLevel(pc->dm, &levels)); 700eb3f98d2SBarry Smith levels++; 7013aeaf226SBarry Smith mg->usedmfornumberoflevels = PETSC_TRUE; 702eb3f98d2SBarry Smith } 7039566063dSJacob Faibussowitsch PetscCall(PCMGSetLevels(pc, levels, NULL)); 7043aeaf226SBarry Smith mglevels = mg->levels; 7053aeaf226SBarry Smith 706f3fbd535SBarry Smith mgctype = (PCMGCycleType)mglevels[0]->cycles; 7079566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-pc_mg_cycle_type", "V cycle or for W-cycle", "PCMGSetCycleType", PCMGCycleTypes, (PetscEnum)mgctype, (PetscEnum *)&mgctype, &flg)); 7081baa6e33SBarry Smith if (flg) PetscCall(PCMGSetCycleType(pc, mgctype)); 7092b3cbbdaSStefano Zampini coarseSpaceType = mg->coarseSpaceType; 7102b3cbbdaSStefano Zampini PetscCall(PetscOptionsEnum("-pc_mg_adapt_interp_coarse_space", "Type of adaptive coarse space: none, polynomial, harmonic, eigenvector, generalized_eigenvector, gdsw", "PCMGSetAdaptCoarseSpaceType", PCMGCoarseSpaceTypes, (PetscEnum)coarseSpaceType, (PetscEnum *)&coarseSpaceType, &flg)); 7112b3cbbdaSStefano Zampini if (flg) PetscCall(PCMGSetAdaptCoarseSpaceType(pc, coarseSpaceType)); 7129566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-pc_mg_adapt_interp_n", "Size of the coarse space for adaptive interpolation", "PCMGSetCoarseSpace", mg->Nc, &mg->Nc, &flg)); 7139566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-pc_mg_mesp_monitor", "Monitor the multilevel eigensolver", "PCMGSetAdaptInterpolation", PETSC_FALSE, &mg->mespMonitor, &flg)); 71441b6fd38SMatthew G. Knepley flg2 = PETSC_FALSE; 7159566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-pc_mg_adapt_cr", "Monitor coarse space quality using Compatible Relaxation (CR)", "PCMGSetAdaptCR", PETSC_FALSE, &flg2, &flg)); 7169566063dSJacob Faibussowitsch if (flg) PetscCall(PCMGSetAdaptCR(pc, flg2)); 717f56b1265SBarry Smith flg = PETSC_FALSE; 7189566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-pc_mg_distinct_smoothup", "Create separate smoothup KSP and append the prefix _up", "PCMGSetDistinctSmoothUp", PETSC_FALSE, &flg, NULL)); 7191baa6e33SBarry Smith if (flg) PetscCall(PCMGSetDistinctSmoothUp(pc)); 7205544a5bcSStefano Zampini PetscCall(PetscOptionsEnum("-pc_mg_galerkin", "Use Galerkin process to compute coarser operators", "PCMGSetGalerkin", PCMGGalerkinTypes, (PetscEnum)mg->galerkin, (PetscEnum *)>ype, &flg)); 7215544a5bcSStefano Zampini if (flg) PetscCall(PCMGSetGalerkin(pc, gtype)); 72231567311SBarry Smith mgtype = mg->am; 7239566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-pc_mg_type", "Multigrid type", "PCMGSetType", PCMGTypes, (PetscEnum)mgtype, (PetscEnum *)&mgtype, &flg)); 7241baa6e33SBarry Smith if (flg) PetscCall(PCMGSetType(pc, mgtype)); 72531567311SBarry Smith if (mg->am == PC_MG_MULTIPLICATIVE) { 7269566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-pc_mg_multiplicative_cycles", "Number of cycles for each preconditioner step", "PCMGMultiplicativeSetCycles", mg->cyclesperpcapply, &cycles, &flg)); 7271baa6e33SBarry Smith if (flg) PetscCall(PCMGMultiplicativeSetCycles(pc, cycles)); 728f3fbd535SBarry Smith } 729f3fbd535SBarry Smith flg = PETSC_FALSE; 7309566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-pc_mg_log", "Log times for each multigrid level", "None", flg, &flg, NULL)); 731f3fbd535SBarry Smith if (flg) { 732f3fbd535SBarry Smith PetscInt i; 733f3fbd535SBarry Smith char eventname[128]; 7341a97d7b6SStefano Zampini 735f3fbd535SBarry Smith levels = mglevels[0]->levels; 736f3fbd535SBarry Smith for (i = 0; i < levels; i++) { 737835f2295SStefano Zampini PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGSetup Level %" PetscInt_FMT, i)); 7389566063dSJacob Faibussowitsch PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventsmoothsetup)); 739835f2295SStefano Zampini PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGSmooth Level %" PetscInt_FMT, i)); 7409566063dSJacob Faibussowitsch PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventsmoothsolve)); 741f3fbd535SBarry Smith if (i) { 742835f2295SStefano Zampini PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGResid Level %" PetscInt_FMT, i)); 7439566063dSJacob Faibussowitsch PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventresidual)); 744835f2295SStefano Zampini PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGInterp Level %" PetscInt_FMT, i)); 7459566063dSJacob Faibussowitsch PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventinterprestrict)); 746f3fbd535SBarry Smith } 747f3fbd535SBarry Smith } 748eec35531SMatthew G Knepley 7492611ad71SToby Isaac if (PetscDefined(USE_LOG)) { 7502611ad71SToby Isaac const char sname[] = "MG Apply"; 751eec35531SMatthew G Knepley 7522611ad71SToby Isaac PetscCall(PetscLogStageGetId(sname, &mg->stageApply)); 7532611ad71SToby Isaac if (mg->stageApply < 0) PetscCall(PetscLogStageRegister(sname, &mg->stageApply)); 754eec35531SMatthew G Knepley } 755f3fbd535SBarry Smith } 756d0609cedSBarry Smith PetscOptionsHeadEnd(); 757f3b08a26SMatthew G. Knepley /* Check option consistency */ 7589566063dSJacob Faibussowitsch PetscCall(PCMGGetGalerkin(pc, >ype)); 7599566063dSJacob Faibussowitsch PetscCall(PCMGGetAdaptInterpolation(pc, &flg)); 76008401ef6SPierre Jolivet PetscCheck(!flg || !(gtype >= PC_MG_GALERKIN_NONE), PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use Galerkin coarse operators when adapting the interpolator"); 7613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 762f3fbd535SBarry Smith } 763f3fbd535SBarry Smith 7640a545947SLisandro Dalcin const char *const PCMGTypes[] = {"MULTIPLICATIVE", "ADDITIVE", "FULL", "KASKADE", "PCMGType", "PC_MG", NULL}; 7650a545947SLisandro Dalcin const char *const PCMGCycleTypes[] = {"invalid", "v", "w", "PCMGCycleType", "PC_MG_CYCLE", NULL}; 7660a545947SLisandro Dalcin const char *const PCMGGalerkinTypes[] = {"both", "pmat", "mat", "none", "external", "PCMGGalerkinType", "PC_MG_GALERKIN", NULL}; 7672b3cbbdaSStefano Zampini const char *const PCMGCoarseSpaceTypes[] = {"none", "polynomial", "harmonic", "eigenvector", "generalized_eigenvector", "gdsw", "PCMGCoarseSpaceType", "PCMG_ADAPT_NONE", NULL}; 768f3fbd535SBarry Smith 7699804daf3SBarry Smith #include <petscdraw.h> 770d71ae5a4SJacob Faibussowitsch PetscErrorCode PCView_MG(PC pc, PetscViewer viewer) 771d71ae5a4SJacob Faibussowitsch { 772f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 773f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 774e3deeeafSJed Brown PetscInt levels = mglevels ? mglevels[0]->levels : 0, i; 7759f196a02SMartin Diehl PetscBool isascii, isbinary, isdraw; 776f3fbd535SBarry Smith 777f3fbd535SBarry Smith PetscFunctionBegin; 7789f196a02SMartin Diehl PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 7799566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 7809566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 7819f196a02SMartin Diehl if (isascii) { 782e3deeeafSJed Brown const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown"; 78363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " type is %s, levels=%" PetscInt_FMT " cycles=%s\n", PCMGTypes[mg->am], levels, cyclename)); 78448a46eb9SPierre Jolivet if (mg->am == PC_MG_MULTIPLICATIVE) PetscCall(PetscViewerASCIIPrintf(viewer, " Cycles per PCApply=%" PetscInt_FMT "\n", mg->cyclesperpcapply)); 7852134b1e4SBarry Smith if (mg->galerkin == PC_MG_GALERKIN_BOTH) { 7869566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Using Galerkin computed coarse grid matrices\n")); 7872134b1e4SBarry Smith } else if (mg->galerkin == PC_MG_GALERKIN_PMAT) { 7889566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Using Galerkin computed coarse grid matrices for pmat\n")); 7892134b1e4SBarry Smith } else if (mg->galerkin == PC_MG_GALERKIN_MAT) { 7909566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Using Galerkin computed coarse grid matrices for mat\n")); 7912134b1e4SBarry Smith } else if (mg->galerkin == PC_MG_GALERKIN_EXTERNAL) { 7929566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Using externally compute Galerkin coarse grid matrices\n")); 7934f66f45eSBarry Smith } else { 7949566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Not using Galerkin computed coarse grid matrices\n")); 795f3fbd535SBarry Smith } 7961baa6e33SBarry Smith if (mg->view) PetscCall((*mg->view)(pc, viewer)); 797f3fbd535SBarry Smith for (i = 0; i < levels; i++) { 79863a3b9bcSJacob Faibussowitsch if (i) { 79963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Down solver (pre-smoother) on level %" PetscInt_FMT " -------------------------------\n", i)); 800f3fbd535SBarry Smith } else { 80163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Coarse grid solver -- level %" PetscInt_FMT " -------------------------------\n", i)); 802f3fbd535SBarry Smith } 8039566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 8049566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->smoothd, viewer)); 8059566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 806f3fbd535SBarry Smith if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) { 8079566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Up solver (post-smoother) same as down solver (pre-smoother)\n")); 808f3fbd535SBarry Smith } else if (i) { 80963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Up solver (post-smoother) on level %" PetscInt_FMT " -------------------------------\n", i)); 8109566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 8119566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->smoothu, viewer)); 8129566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 813f3fbd535SBarry Smith } 81441b6fd38SMatthew G. Knepley if (i && mglevels[i]->cr) { 81563a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "CR solver on level %" PetscInt_FMT " -------------------------------\n", i)); 8169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 8179566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->cr, viewer)); 8189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 81941b6fd38SMatthew G. Knepley } 820f3fbd535SBarry Smith } 8215b0b0462SBarry Smith } else if (isbinary) { 8225b0b0462SBarry Smith for (i = levels - 1; i >= 0; i--) { 8239566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->smoothd, viewer)); 82448a46eb9SPierre Jolivet if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPView(mglevels[i]->smoothu, viewer)); 8255b0b0462SBarry Smith } 826219b1045SBarry Smith } else if (isdraw) { 827219b1045SBarry Smith PetscDraw draw; 828b4375e8dSPeter Brune PetscReal x, w, y, bottom, th; 8299566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 8309566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 8319566063dSJacob Faibussowitsch PetscCall(PetscDrawStringGetSize(draw, NULL, &th)); 832219b1045SBarry Smith bottom = y - th; 833219b1045SBarry Smith for (i = levels - 1; i >= 0; i--) { 834b4375e8dSPeter Brune if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) { 8359566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 8369566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->smoothd, viewer)); 8379566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 838b4375e8dSPeter Brune } else { 839b4375e8dSPeter Brune w = 0.5 * PetscMin(1.0 - x, x); 8409566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x + w, bottom)); 8419566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->smoothd, viewer)); 8429566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 8439566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x - w, bottom)); 8449566063dSJacob Faibussowitsch PetscCall(KSPView(mglevels[i]->smoothu, viewer)); 8459566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 846b4375e8dSPeter Brune } 8479566063dSJacob Faibussowitsch PetscCall(PetscDrawGetBoundingBox(draw, NULL, &bottom, NULL, NULL)); 8481cd381d6SBarry Smith bottom -= th; 849219b1045SBarry Smith } 8505b0b0462SBarry Smith } 8513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 852f3fbd535SBarry Smith } 853f3fbd535SBarry Smith 854af0996ceSBarry Smith #include <petsc/private/kspimpl.h> 855cab2e9ccSBarry Smith 856f3fbd535SBarry Smith /* 857f3fbd535SBarry Smith Calls setup for the KSP on each level 858f3fbd535SBarry Smith */ 859d71ae5a4SJacob Faibussowitsch PetscErrorCode PCSetUp_MG(PC pc) 860d71ae5a4SJacob Faibussowitsch { 861f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 862f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 8631a97d7b6SStefano Zampini PetscInt i, n; 86498e04984SBarry Smith PC cpc; 8653db492dfSBarry Smith PetscBool dump = PETSC_FALSE, opsset, use_amat, missinginterpolate = PETSC_FALSE; 866f3fbd535SBarry Smith Mat dA, dB; 867f3fbd535SBarry Smith Vec tvec; 868218a07d4SBarry Smith DM *dms; 8690a545947SLisandro Dalcin PetscViewer viewer = NULL; 87041b6fd38SMatthew G. Knepley PetscBool dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE, doCR = PETSC_FALSE; 8712b3cbbdaSStefano Zampini PetscBool adaptInterpolation = mg->adaptInterpolation; 872f3fbd535SBarry Smith 873f3fbd535SBarry Smith PetscFunctionBegin; 87428b400f6SJacob Faibussowitsch PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must set MG levels with PCMGSetLevels() before setting up"); 8751a97d7b6SStefano Zampini n = mglevels[0]->levels; 87601bc414fSDmitry Karpeev /* FIX: Move this to PCSetFromOptions_MG? */ 8773aeaf226SBarry Smith if (mg->usedmfornumberoflevels) { 8783aeaf226SBarry Smith PetscInt levels; 879*efba3485SBarry Smith 8809566063dSJacob Faibussowitsch PetscCall(DMGetRefineLevel(pc->dm, &levels)); 8813aeaf226SBarry Smith levels++; 8823aeaf226SBarry Smith if (levels > n) { /* the problem is now being solved on a finer grid */ 8839566063dSJacob Faibussowitsch PetscCall(PCMGSetLevels(pc, levels, NULL)); 8843aeaf226SBarry Smith n = levels; 8859566063dSJacob Faibussowitsch PetscCall(PCSetFromOptions(pc)); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */ 8863aeaf226SBarry Smith mglevels = mg->levels; 8873aeaf226SBarry Smith } 8883aeaf226SBarry Smith } 8893aeaf226SBarry Smith 890f3fbd535SBarry Smith /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */ 891f3fbd535SBarry Smith /* so use those from global PC */ 892f3fbd535SBarry Smith /* Is this what we always want? What if user wants to keep old one? */ 8939566063dSJacob Faibussowitsch PetscCall(KSPGetOperatorsSet(mglevels[n - 1]->smoothd, NULL, &opsset)); 89498e04984SBarry Smith if (opsset) { 89598e04984SBarry Smith Mat mmat; 8969566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[n - 1]->smoothd, NULL, &mmat)); 89798e04984SBarry Smith if (mmat == pc->pmat) opsset = PETSC_FALSE; 89898e04984SBarry Smith } 899bbbcb081SMark Adams /* fine grid smoother inherits the reuse-pc flag */ 900bbbcb081SMark Adams PetscCall(KSPGetPC(mglevels[n - 1]->smoothd, &cpc)); 901bbbcb081SMark Adams cpc->reusepreconditioner = pc->reusepreconditioner; 902bbbcb081SMark Adams PetscCall(KSPGetPC(mglevels[n - 1]->smoothu, &cpc)); 903bbbcb081SMark Adams cpc->reusepreconditioner = pc->reusepreconditioner; 9044a5f07a5SMark F. Adams 90541b6fd38SMatthew G. Knepley /* Create CR solvers */ 9069566063dSJacob Faibussowitsch PetscCall(PCMGGetAdaptCR(pc, &doCR)); 90741b6fd38SMatthew G. Knepley if (doCR) { 90841b6fd38SMatthew G. Knepley const char *prefix; 90941b6fd38SMatthew G. Knepley 9109566063dSJacob Faibussowitsch PetscCall(PCGetOptionsPrefix(pc, &prefix)); 91141b6fd38SMatthew G. Knepley for (i = 1; i < n; ++i) { 91241b6fd38SMatthew G. Knepley PC ipc, cr; 91341b6fd38SMatthew G. Knepley char crprefix[128]; 91441b6fd38SMatthew G. Knepley 9159566063dSJacob Faibussowitsch PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &mglevels[i]->cr)); 9163821be0aSBarry Smith PetscCall(KSPSetNestLevel(mglevels[i]->cr, pc->kspnestlevel)); 9179566063dSJacob Faibussowitsch PetscCall(KSPSetErrorIfNotConverged(mglevels[i]->cr, PETSC_FALSE)); 9189566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->cr, (PetscObject)pc, n - i)); 9199566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(mglevels[i]->cr, prefix)); 9209566063dSJacob Faibussowitsch PetscCall(PetscObjectComposedDataSetInt((PetscObject)mglevels[i]->cr, PetscMGLevelId, mglevels[i]->level)); 9219566063dSJacob Faibussowitsch PetscCall(KSPSetType(mglevels[i]->cr, KSPCHEBYSHEV)); 9229566063dSJacob Faibussowitsch PetscCall(KSPSetConvergenceTest(mglevels[i]->cr, KSPConvergedSkip, NULL, NULL)); 9239566063dSJacob Faibussowitsch PetscCall(KSPSetNormType(mglevels[i]->cr, KSP_NORM_PRECONDITIONED)); 9249566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[i]->cr, &ipc)); 92541b6fd38SMatthew G. Knepley 9269566063dSJacob Faibussowitsch PetscCall(PCSetType(ipc, PCCOMPOSITE)); 9279566063dSJacob Faibussowitsch PetscCall(PCCompositeSetType(ipc, PC_COMPOSITE_MULTIPLICATIVE)); 9289566063dSJacob Faibussowitsch PetscCall(PCCompositeAddPCType(ipc, PCSOR)); 9299566063dSJacob Faibussowitsch PetscCall(CreateCR_Private(pc, i, &cr)); 9309566063dSJacob Faibussowitsch PetscCall(PCCompositeAddPC(ipc, cr)); 9319566063dSJacob Faibussowitsch PetscCall(PCDestroy(&cr)); 93241b6fd38SMatthew G. Knepley 933fb842aefSJose E. Roman PetscCall(KSPSetTolerances(mglevels[i]->cr, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, mg->default_smoothd)); 9349566063dSJacob Faibussowitsch PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->cr, PETSC_TRUE)); 935835f2295SStefano Zampini PetscCall(PetscSNPrintf(crprefix, 128, "mg_levels_%" PetscInt_FMT "_cr_", i)); 9369566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(mglevels[i]->cr, crprefix)); 93741b6fd38SMatthew G. Knepley } 93841b6fd38SMatthew G. Knepley } 93941b6fd38SMatthew G. Knepley 9404a5f07a5SMark F. Adams if (!opsset) { 9419566063dSJacob Faibussowitsch PetscCall(PCGetUseAmat(pc, &use_amat)); 9424a5f07a5SMark F. Adams if (use_amat) { 9439566063dSJacob Faibussowitsch PetscCall(PetscInfo(pc, "Using outer operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n")); 9449566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(mglevels[n - 1]->smoothd, pc->mat, pc->pmat)); 94569858f1bSStefano Zampini } else { 9469566063dSJacob Faibussowitsch PetscCall(PetscInfo(pc, "Using matrix (pmat) operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n")); 9479566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(mglevels[n - 1]->smoothd, pc->pmat, pc->pmat)); 9484a5f07a5SMark F. Adams } 9494a5f07a5SMark F. Adams } 950f3fbd535SBarry Smith 9519c7ffb39SBarry Smith for (i = n - 1; i > 0; i--) { 9529c7ffb39SBarry Smith if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) { 9539c7ffb39SBarry Smith missinginterpolate = PETSC_TRUE; 9542b3cbbdaSStefano Zampini break; 9559c7ffb39SBarry Smith } 9569c7ffb39SBarry Smith } 9572134b1e4SBarry Smith 9589566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[n - 1]->smoothd, &dA, &dB)); 9592134b1e4SBarry Smith if (dA == dB) dAeqdB = PETSC_TRUE; 9603a7d0413SPierre Jolivet if (mg->galerkin == PC_MG_GALERKIN_NONE || ((mg->galerkin == PC_MG_GALERKIN_PMAT || mg->galerkin == PC_MG_GALERKIN_MAT) && !dAeqdB)) needRestricts = PETSC_TRUE; /* user must compute either mat, pmat, or both so must restrict x to coarser levels */ 9612134b1e4SBarry Smith 9622b3cbbdaSStefano Zampini if (pc->dm && !pc->setupcalled) { 9632b3cbbdaSStefano Zampini /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */ 9642b3cbbdaSStefano Zampini PetscCall(KSPSetDM(mglevels[n - 1]->smoothd, pc->dm)); 9652b3cbbdaSStefano Zampini PetscCall(KSPSetDMActive(mglevels[n - 1]->smoothd, PETSC_FALSE)); 9662b3cbbdaSStefano Zampini if (mglevels[n - 1]->smoothd != mglevels[n - 1]->smoothu) { 9672b3cbbdaSStefano Zampini PetscCall(KSPSetDM(mglevels[n - 1]->smoothu, pc->dm)); 9682b3cbbdaSStefano Zampini PetscCall(KSPSetDMActive(mglevels[n - 1]->smoothu, PETSC_FALSE)); 9692b3cbbdaSStefano Zampini } 9702b3cbbdaSStefano Zampini if (mglevels[n - 1]->cr) { 9712b3cbbdaSStefano Zampini PetscCall(KSPSetDM(mglevels[n - 1]->cr, pc->dm)); 9722b3cbbdaSStefano Zampini PetscCall(KSPSetDMActive(mglevels[n - 1]->cr, PETSC_FALSE)); 9732b3cbbdaSStefano Zampini } 9742b3cbbdaSStefano Zampini } 9752b3cbbdaSStefano Zampini 9769c7ffb39SBarry Smith /* 9779c7ffb39SBarry Smith Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS) 9782b3cbbdaSStefano Zampini Skipping for externally managed hierarchy (such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs? 9799c7ffb39SBarry Smith */ 98032fe681dSStefano Zampini if (missinginterpolate && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) { 98132fe681dSStefano Zampini /* first see if we can compute a coarse space */ 98232fe681dSStefano Zampini if (mg->coarseSpaceType == PCMG_ADAPT_GDSW) { 98332fe681dSStefano Zampini for (i = n - 2; i > -1; i--) { 98432fe681dSStefano Zampini if (!mglevels[i + 1]->restrct && !mglevels[i + 1]->interpolate) { 98532fe681dSStefano Zampini PetscCall(PCMGComputeCoarseSpace_Internal(pc, i + 1, mg->coarseSpaceType, mg->Nc, NULL, &mglevels[i + 1]->coarseSpace)); 98632fe681dSStefano Zampini PetscCall(PCMGSetInterpolation(pc, i + 1, mglevels[i + 1]->coarseSpace)); 98732fe681dSStefano Zampini } 98832fe681dSStefano Zampini } 98932fe681dSStefano Zampini } else { /* construct the interpolation from the DMs */ 9902e499ae9SBarry Smith Mat p; 99173250ac0SBarry Smith Vec rscale; 99264da7896SBarry Smith 99364da7896SBarry Smith PetscCheck(n == 1 || pc->dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PC lacks a DM so cannot automatically construct a multigrid hierarchy. Number of levels requested %" PetscInt_FMT, n); 9949566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &dms)); 995218a07d4SBarry Smith dms[n - 1] = pc->dm; 996ef1267afSMatthew G. Knepley /* Separately create them so we do not get DMKSP interference between levels */ 9979566063dSJacob Faibussowitsch for (i = n - 2; i > -1; i--) PetscCall(DMCoarsen(dms[i + 1], MPI_COMM_NULL, &dms[i])); 998218a07d4SBarry Smith for (i = n - 2; i > -1; i--) { 999942e3340SBarry Smith DMKSP kdm; 1000eab52d2bSLawrence Mitchell PetscBool dmhasrestrict, dmhasinject; 1001218e2965SBarry Smith 10029566063dSJacob Faibussowitsch PetscCall(KSPSetDM(mglevels[i]->smoothd, dms[i])); 10039566063dSJacob Faibussowitsch if (!needRestricts) PetscCall(KSPSetDMActive(mglevels[i]->smoothd, PETSC_FALSE)); 1004c27ee7a3SPatrick Farrell if (mglevels[i]->smoothd != mglevels[i]->smoothu) { 10059566063dSJacob Faibussowitsch PetscCall(KSPSetDM(mglevels[i]->smoothu, dms[i])); 10069566063dSJacob Faibussowitsch if (!needRestricts) PetscCall(KSPSetDMActive(mglevels[i]->smoothu, PETSC_FALSE)); 1007c27ee7a3SPatrick Farrell } 100841b6fd38SMatthew G. Knepley if (mglevels[i]->cr) { 10099566063dSJacob Faibussowitsch PetscCall(KSPSetDM(mglevels[i]->cr, dms[i])); 10109566063dSJacob Faibussowitsch if (!needRestricts) PetscCall(KSPSetDMActive(mglevels[i]->cr, PETSC_FALSE)); 101141b6fd38SMatthew G. Knepley } 10129566063dSJacob Faibussowitsch PetscCall(DMGetDMKSPWrite(dms[i], &kdm)); 1013d1a3e677SJed Brown /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take 1014d1a3e677SJed Brown * a bitwise OR of computing the matrix, RHS, and initial iterate. */ 10150298fd71SBarry Smith kdm->ops->computerhs = NULL; 10160298fd71SBarry Smith kdm->rhsctx = NULL; 101724c3aa18SBarry Smith if (!mglevels[i + 1]->interpolate) { 10189566063dSJacob Faibussowitsch PetscCall(DMCreateInterpolation(dms[i], dms[i + 1], &p, &rscale)); 10199566063dSJacob Faibussowitsch PetscCall(PCMGSetInterpolation(pc, i + 1, p)); 10209566063dSJacob Faibussowitsch if (rscale) PetscCall(PCMGSetRScale(pc, i + 1, rscale)); 10219566063dSJacob Faibussowitsch PetscCall(VecDestroy(&rscale)); 10229566063dSJacob Faibussowitsch PetscCall(MatDestroy(&p)); 1023218a07d4SBarry Smith } 10249566063dSJacob Faibussowitsch PetscCall(DMHasCreateRestriction(dms[i], &dmhasrestrict)); 10253ad4599aSBarry Smith if (dmhasrestrict && !mglevels[i + 1]->restrct) { 10269566063dSJacob Faibussowitsch PetscCall(DMCreateRestriction(dms[i], dms[i + 1], &p)); 10279566063dSJacob Faibussowitsch PetscCall(PCMGSetRestriction(pc, i + 1, p)); 10289566063dSJacob Faibussowitsch PetscCall(MatDestroy(&p)); 10293ad4599aSBarry Smith } 10309566063dSJacob Faibussowitsch PetscCall(DMHasCreateInjection(dms[i], &dmhasinject)); 1031eab52d2bSLawrence Mitchell if (dmhasinject && !mglevels[i + 1]->inject) { 10329566063dSJacob Faibussowitsch PetscCall(DMCreateInjection(dms[i], dms[i + 1], &p)); 10339566063dSJacob Faibussowitsch PetscCall(PCMGSetInjection(pc, i + 1, p)); 10349566063dSJacob Faibussowitsch PetscCall(MatDestroy(&p)); 1035eab52d2bSLawrence Mitchell } 103624c3aa18SBarry Smith } 10372d2b81a6SBarry Smith 10389566063dSJacob Faibussowitsch for (i = n - 2; i > -1; i--) PetscCall(DMDestroy(&dms[i])); 10399566063dSJacob Faibussowitsch PetscCall(PetscFree(dms)); 1040ce4cda84SJed Brown } 104132fe681dSStefano Zampini } 1042cab2e9ccSBarry Smith 10432134b1e4SBarry Smith if (mg->galerkin < PC_MG_GALERKIN_NONE) { 10442134b1e4SBarry Smith Mat A, B; 10452134b1e4SBarry Smith PetscBool doA = PETSC_FALSE, doB = PETSC_FALSE; 10462134b1e4SBarry Smith MatReuse reuse = MAT_INITIAL_MATRIX; 10472134b1e4SBarry Smith 10482b3cbbdaSStefano Zampini if (mg->galerkin == PC_MG_GALERKIN_PMAT || mg->galerkin == PC_MG_GALERKIN_BOTH) doB = PETSC_TRUE; 10492b3cbbdaSStefano Zampini if (mg->galerkin == PC_MG_GALERKIN_MAT || (mg->galerkin == PC_MG_GALERKIN_BOTH && dA != dB)) doA = PETSC_TRUE; 10502134b1e4SBarry Smith if (pc->setupcalled) reuse = MAT_REUSE_MATRIX; 1051f3fbd535SBarry Smith for (i = n - 2; i > -1; i--) { 10527827d75bSBarry Smith PetscCheck(mglevels[i + 1]->restrct || mglevels[i + 1]->interpolate, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must provide interpolation or restriction for each MG level except level 0"); 105348a46eb9SPierre Jolivet if (!mglevels[i + 1]->interpolate) PetscCall(PCMGSetInterpolation(pc, i + 1, mglevels[i + 1]->restrct)); 105448a46eb9SPierre Jolivet if (!mglevels[i + 1]->restrct) PetscCall(PCMGSetRestriction(pc, i + 1, mglevels[i + 1]->interpolate)); 105548a46eb9SPierre Jolivet if (reuse == MAT_REUSE_MATRIX) PetscCall(KSPGetOperators(mglevels[i]->smoothd, &A, &B)); 105648a46eb9SPierre Jolivet if (doA) PetscCall(MatGalerkin(mglevels[i + 1]->restrct, dA, mglevels[i + 1]->interpolate, reuse, 1.0, &A)); 105748a46eb9SPierre Jolivet if (doB) PetscCall(MatGalerkin(mglevels[i + 1]->restrct, dB, mglevels[i + 1]->interpolate, reuse, 1.0, &B)); 10582134b1e4SBarry Smith /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */ 10592134b1e4SBarry Smith if (!doA && dAeqdB) { 10609566063dSJacob Faibussowitsch if (reuse == MAT_INITIAL_MATRIX) PetscCall(PetscObjectReference((PetscObject)B)); 10612134b1e4SBarry Smith A = B; 10622134b1e4SBarry Smith } else if (!doA && reuse == MAT_INITIAL_MATRIX) { 10639566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothd, &A, NULL)); 10649566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 1065b9367914SBarry Smith } 10662134b1e4SBarry Smith if (!doB && dAeqdB) { 10679566063dSJacob Faibussowitsch if (reuse == MAT_INITIAL_MATRIX) PetscCall(PetscObjectReference((PetscObject)A)); 10682134b1e4SBarry Smith B = A; 10692134b1e4SBarry Smith } else if (!doB && reuse == MAT_INITIAL_MATRIX) { 10709566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothd, NULL, &B)); 10719566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 10727d537d92SJed Brown } 10732134b1e4SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 10749566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(mglevels[i]->smoothd, A, B)); 10759566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)A)); 10769566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)B)); 10772134b1e4SBarry Smith } 10782134b1e4SBarry Smith dA = A; 1079f3fbd535SBarry Smith dB = B; 1080f3fbd535SBarry Smith } 1081f3fbd535SBarry Smith } 1082f3b08a26SMatthew G. Knepley 1083f3b08a26SMatthew G. Knepley /* Adapt interpolation matrices */ 10842b3cbbdaSStefano Zampini if (adaptInterpolation) { 1085f3b08a26SMatthew G. Knepley for (i = 0; i < n; ++i) { 108648a46eb9SPierre Jolivet if (!mglevels[i]->coarseSpace) PetscCall(PCMGComputeCoarseSpace_Internal(pc, i, mg->coarseSpaceType, mg->Nc, !i ? NULL : mglevels[i - 1]->coarseSpace, &mglevels[i]->coarseSpace)); 10872b3cbbdaSStefano Zampini if (i) PetscCall(PCMGAdaptInterpolator_Internal(pc, i, mglevels[i - 1]->smoothu, mglevels[i]->smoothu, mglevels[i - 1]->coarseSpace, mglevels[i]->coarseSpace)); 1088f3b08a26SMatthew G. Knepley } 108948a46eb9SPierre Jolivet for (i = n - 2; i > -1; --i) PetscCall(PCMGRecomputeLevelOperators_Internal(pc, i)); 1090f3b08a26SMatthew G. Knepley } 1091f3b08a26SMatthew G. Knepley 10922134b1e4SBarry Smith if (needRestricts && pc->dm) { 1093caa4e7f2SJed Brown for (i = n - 2; i >= 0; i--) { 1094ccceb50cSJed Brown DM dmfine, dmcoarse; 1095caa4e7f2SJed Brown Mat Restrict, Inject; 1096caa4e7f2SJed Brown Vec rscale; 1097218e2965SBarry Smith 10989566063dSJacob Faibussowitsch PetscCall(KSPGetDM(mglevels[i + 1]->smoothd, &dmfine)); 10999566063dSJacob Faibussowitsch PetscCall(KSPGetDM(mglevels[i]->smoothd, &dmcoarse)); 11009566063dSJacob Faibussowitsch PetscCall(PCMGGetRestriction(pc, i + 1, &Restrict)); 11019566063dSJacob Faibussowitsch PetscCall(PCMGGetRScale(pc, i + 1, &rscale)); 11029566063dSJacob Faibussowitsch PetscCall(PCMGGetInjection(pc, i + 1, &Inject)); 11039566063dSJacob Faibussowitsch PetscCall(DMRestrict(dmfine, Restrict, rscale, Inject, dmcoarse)); 1104caa4e7f2SJed Brown } 1105caa4e7f2SJed Brown } 1106f3fbd535SBarry Smith 1107f3fbd535SBarry Smith if (!pc->setupcalled) { 110848a46eb9SPierre Jolivet for (i = 0; i < n; i++) PetscCall(KSPSetFromOptions(mglevels[i]->smoothd)); 1109f3fbd535SBarry Smith for (i = 1; i < n; i++) { 111048a46eb9SPierre Jolivet if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) PetscCall(KSPSetFromOptions(mglevels[i]->smoothu)); 111148a46eb9SPierre Jolivet if (mglevels[i]->cr) PetscCall(KSPSetFromOptions(mglevels[i]->cr)); 1112f3fbd535SBarry Smith } 111315229ffcSPierre Jolivet /* insure that if either interpolation or restriction is set the other one is set */ 1114f3fbd535SBarry Smith for (i = 1; i < n; i++) { 11159566063dSJacob Faibussowitsch PetscCall(PCMGGetInterpolation(pc, i, NULL)); 11169566063dSJacob Faibussowitsch PetscCall(PCMGGetRestriction(pc, i, NULL)); 1117f3fbd535SBarry Smith } 1118f3fbd535SBarry Smith for (i = 0; i < n - 1; i++) { 1119f3fbd535SBarry Smith if (!mglevels[i]->b) { 1120f3fbd535SBarry Smith Vec *vec; 11219566063dSJacob Faibussowitsch PetscCall(KSPCreateVecs(mglevels[i]->smoothd, 1, &vec, 0, NULL)); 11229566063dSJacob Faibussowitsch PetscCall(PCMGSetRhs(pc, i, *vec)); 11239566063dSJacob Faibussowitsch PetscCall(VecDestroy(vec)); 11249566063dSJacob Faibussowitsch PetscCall(PetscFree(vec)); 1125f3fbd535SBarry Smith } 1126f3fbd535SBarry Smith if (!mglevels[i]->r && i) { 11279566063dSJacob Faibussowitsch PetscCall(VecDuplicate(mglevels[i]->b, &tvec)); 11289566063dSJacob Faibussowitsch PetscCall(PCMGSetR(pc, i, tvec)); 11299566063dSJacob Faibussowitsch PetscCall(VecDestroy(&tvec)); 1130f3fbd535SBarry Smith } 1131f3fbd535SBarry Smith if (!mglevels[i]->x) { 11329566063dSJacob Faibussowitsch PetscCall(VecDuplicate(mglevels[i]->b, &tvec)); 11339566063dSJacob Faibussowitsch PetscCall(PCMGSetX(pc, i, tvec)); 11349566063dSJacob Faibussowitsch PetscCall(VecDestroy(&tvec)); 1135f3fbd535SBarry Smith } 113641b6fd38SMatthew G. Knepley if (doCR) { 11379566063dSJacob Faibussowitsch PetscCall(VecDuplicate(mglevels[i]->b, &mglevels[i]->crx)); 11389566063dSJacob Faibussowitsch PetscCall(VecDuplicate(mglevels[i]->b, &mglevels[i]->crb)); 11399566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(mglevels[i]->crb)); 114041b6fd38SMatthew G. Knepley } 1141f3fbd535SBarry Smith } 1142f3fbd535SBarry Smith if (n != 1 && !mglevels[n - 1]->r) { 1143f3fbd535SBarry Smith /* PCMGSetR() on the finest level if user did not supply it */ 1144f3fbd535SBarry Smith Vec *vec; 1145218e2965SBarry Smith 11469566063dSJacob Faibussowitsch PetscCall(KSPCreateVecs(mglevels[n - 1]->smoothd, 1, &vec, 0, NULL)); 11479566063dSJacob Faibussowitsch PetscCall(PCMGSetR(pc, n - 1, *vec)); 11489566063dSJacob Faibussowitsch PetscCall(VecDestroy(vec)); 11499566063dSJacob Faibussowitsch PetscCall(PetscFree(vec)); 1150f3fbd535SBarry Smith } 115141b6fd38SMatthew G. Knepley if (doCR) { 11529566063dSJacob Faibussowitsch PetscCall(VecDuplicate(mglevels[n - 1]->r, &mglevels[n - 1]->crx)); 11539566063dSJacob Faibussowitsch PetscCall(VecDuplicate(mglevels[n - 1]->r, &mglevels[n - 1]->crb)); 11549566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(mglevels[n - 1]->crb)); 115541b6fd38SMatthew G. Knepley } 1156f3fbd535SBarry Smith } 1157f3fbd535SBarry Smith 115898e04984SBarry Smith if (pc->dm) { 115998e04984SBarry Smith /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */ 116098e04984SBarry Smith for (i = 0; i < n - 1; i++) { 116198e04984SBarry Smith if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX; 116298e04984SBarry Smith } 116398e04984SBarry Smith } 116408549ed4SJed Brown // We got here (PCSetUp_MG) because the matrix has changed, which means the smoother needs to be set up again (e.g., 116508549ed4SJed Brown // new diagonal for Jacobi). Setting it here allows it to be logged under PCSetUp rather than deep inside a PCApply. 116608549ed4SJed Brown if (mglevels[n - 1]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[n - 1]->smoothd->setupstage = KSP_SETUP_NEWMATRIX; 1167f3fbd535SBarry Smith 1168f3fbd535SBarry Smith for (i = 1; i < n; i++) { 11692a21e185SBarry Smith if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1) { 1170f3fbd535SBarry Smith /* if doing only down then initial guess is zero */ 11719566063dSJacob Faibussowitsch PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->smoothd, PETSC_TRUE)); 1172f3fbd535SBarry Smith } 11739566063dSJacob Faibussowitsch if (mglevels[i]->cr) PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->cr, PETSC_TRUE)); 11749566063dSJacob Faibussowitsch if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0)); 11759566063dSJacob Faibussowitsch PetscCall(KSPSetUp(mglevels[i]->smoothd)); 1176d4645a75SStefano Zampini if (mglevels[i]->smoothd->reason) pc->failedreason = PC_SUBPC_ERROR; 11779566063dSJacob Faibussowitsch if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0)); 1178d42688cbSBarry Smith if (!mglevels[i]->residual) { 1179d42688cbSBarry Smith Mat mat; 1180218e2965SBarry Smith 11819566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothd, &mat, NULL)); 11829566063dSJacob Faibussowitsch PetscCall(PCMGSetResidual(pc, i, PCMGResidualDefault, mat)); 1183d42688cbSBarry Smith } 1184fcb023d4SJed Brown if (!mglevels[i]->residualtranspose) { 1185fcb023d4SJed Brown Mat mat; 1186218e2965SBarry Smith 11879566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothd, &mat, NULL)); 11889566063dSJacob Faibussowitsch PetscCall(PCMGSetResidualTranspose(pc, i, PCMGResidualTransposeDefault, mat)); 1189fcb023d4SJed Brown } 1190f3fbd535SBarry Smith } 1191f3fbd535SBarry Smith for (i = 1; i < n; i++) { 1192f3fbd535SBarry Smith if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) { 1193f3fbd535SBarry Smith Mat downmat, downpmat; 1194f3fbd535SBarry Smith 1195f3fbd535SBarry Smith /* check if operators have been set for up, if not use down operators to set them */ 11969566063dSJacob Faibussowitsch PetscCall(KSPGetOperatorsSet(mglevels[i]->smoothu, &opsset, NULL)); 1197f3fbd535SBarry Smith if (!opsset) { 11989566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothd, &downmat, &downpmat)); 11999566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(mglevels[i]->smoothu, downmat, downpmat)); 1200f3fbd535SBarry Smith } 1201f3fbd535SBarry Smith 12029566063dSJacob Faibussowitsch PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->smoothu, PETSC_TRUE)); 12039566063dSJacob Faibussowitsch if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0)); 12049566063dSJacob Faibussowitsch PetscCall(KSPSetUp(mglevels[i]->smoothu)); 1205d4645a75SStefano Zampini if (mglevels[i]->smoothu->reason) pc->failedreason = PC_SUBPC_ERROR; 12069566063dSJacob Faibussowitsch if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0)); 1207f3fbd535SBarry Smith } 120841b6fd38SMatthew G. Knepley if (mglevels[i]->cr) { 120941b6fd38SMatthew G. Knepley Mat downmat, downpmat; 121041b6fd38SMatthew G. Knepley 121141b6fd38SMatthew G. Knepley /* check if operators have been set for up, if not use down operators to set them */ 12129566063dSJacob Faibussowitsch PetscCall(KSPGetOperatorsSet(mglevels[i]->cr, &opsset, NULL)); 121341b6fd38SMatthew G. Knepley if (!opsset) { 12149566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[i]->smoothd, &downmat, &downpmat)); 12159566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(mglevels[i]->cr, downmat, downpmat)); 121641b6fd38SMatthew G. Knepley } 121741b6fd38SMatthew G. Knepley 12189566063dSJacob Faibussowitsch PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->cr, PETSC_TRUE)); 12199566063dSJacob Faibussowitsch if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0)); 12209566063dSJacob Faibussowitsch PetscCall(KSPSetUp(mglevels[i]->cr)); 1221d4645a75SStefano Zampini if (mglevels[i]->cr->reason) pc->failedreason = PC_SUBPC_ERROR; 12229566063dSJacob Faibussowitsch if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0)); 122341b6fd38SMatthew G. Knepley } 1224f3fbd535SBarry Smith } 1225f3fbd535SBarry Smith 12269566063dSJacob Faibussowitsch if (mglevels[0]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[0]->eventsmoothsetup, 0, 0, 0, 0)); 12279566063dSJacob Faibussowitsch PetscCall(KSPSetUp(mglevels[0]->smoothd)); 1228d4645a75SStefano Zampini if (mglevels[0]->smoothd->reason) pc->failedreason = PC_SUBPC_ERROR; 12299566063dSJacob Faibussowitsch if (mglevels[0]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[0]->eventsmoothsetup, 0, 0, 0, 0)); 1230f3fbd535SBarry Smith 1231f3fbd535SBarry Smith /* 1232f3fbd535SBarry Smith Dump the interpolation/restriction matrices plus the 1233e3c5b3baSBarry Smith Jacobian/stiffness on each level. This allows MATLAB users to 1234f3fbd535SBarry Smith easily check if the Galerkin condition A_c = R A_f R^T is satisfied. 1235f3fbd535SBarry Smith 1236f3fbd535SBarry Smith Only support one or the other at the same time. 1237f3fbd535SBarry Smith */ 1238f3fbd535SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER) 12399566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_mg_dump_matlab", &dump, NULL)); 1240ce94432eSBarry Smith if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc)); 1241f3fbd535SBarry Smith dump = PETSC_FALSE; 1242f3fbd535SBarry Smith #endif 12439566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_mg_dump_binary", &dump, NULL)); 1244ce94432eSBarry Smith if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc)); 1245f3fbd535SBarry Smith 1246f3fbd535SBarry Smith if (viewer) { 124748a46eb9SPierre Jolivet for (i = 1; i < n; i++) PetscCall(MatView(mglevels[i]->restrct, viewer)); 1248f3fbd535SBarry Smith for (i = 0; i < n; i++) { 12499566063dSJacob Faibussowitsch PetscCall(KSPGetPC(mglevels[i]->smoothd, &pc)); 12509566063dSJacob Faibussowitsch PetscCall(MatView(pc->mat, viewer)); 1251f3fbd535SBarry Smith } 1252f3fbd535SBarry Smith } 12533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1254f3fbd535SBarry Smith } 1255f3fbd535SBarry Smith 1256d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetLevels_MG(PC pc, PetscInt *levels) 1257d71ae5a4SJacob Faibussowitsch { 125897d33e41SMatthew G. Knepley PC_MG *mg = (PC_MG *)pc->data; 125997d33e41SMatthew G. Knepley 126097d33e41SMatthew G. Knepley PetscFunctionBegin; 126197d33e41SMatthew G. Knepley *levels = mg->nlevels; 12623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 126397d33e41SMatthew G. Knepley } 126497d33e41SMatthew G. Knepley 12654b9ad928SBarry Smith /*@ 1266f1580f4eSBarry Smith PCMGGetLevels - Gets the number of levels to use with `PCMG`. 12674b9ad928SBarry Smith 12684b9ad928SBarry Smith Not Collective 12694b9ad928SBarry Smith 12704b9ad928SBarry Smith Input Parameter: 12714b9ad928SBarry Smith . pc - the preconditioner context 12724b9ad928SBarry Smith 1273feefa0e1SJacob Faibussowitsch Output Parameter: 12744b9ad928SBarry Smith . levels - the number of levels 12754b9ad928SBarry Smith 12764b9ad928SBarry Smith Level: advanced 12774b9ad928SBarry Smith 1278562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGSetLevels()` 12794b9ad928SBarry Smith @*/ 1280d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetLevels(PC pc, PetscInt *levels) 1281d71ae5a4SJacob Faibussowitsch { 12824b9ad928SBarry Smith PetscFunctionBegin; 12830700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 12844f572ea9SToby Isaac PetscAssertPointer(levels, 2); 128597d33e41SMatthew G. Knepley *levels = 0; 1286cac4c232SBarry Smith PetscTryMethod(pc, "PCMGGetLevels_C", (PC, PetscInt *), (pc, levels)); 12873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12884b9ad928SBarry Smith } 12894b9ad928SBarry Smith 12904b9ad928SBarry Smith /*@ 1291f1580f4eSBarry Smith PCMGGetGridComplexity - compute operator and grid complexity of the `PCMG` hierarchy 1292e7d4b4cbSMark Adams 1293e7d4b4cbSMark Adams Input Parameter: 1294e7d4b4cbSMark Adams . pc - the preconditioner context 1295e7d4b4cbSMark Adams 129690db8557SMark Adams Output Parameters: 129790db8557SMark Adams + gc - grid complexity = sum_i(n_i) / n_0 129890db8557SMark Adams - oc - operator complexity = sum_i(nnz_i) / nnz_0 1299e7d4b4cbSMark Adams 1300e7d4b4cbSMark Adams Level: advanced 1301e7d4b4cbSMark Adams 1302f1580f4eSBarry Smith Note: 1303f1580f4eSBarry Smith This is often call the operator complexity in multigrid literature 1304f1580f4eSBarry Smith 1305562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGGetLevels()`, `PCMGSetLevels()` 1306e7d4b4cbSMark Adams @*/ 1307d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetGridComplexity(PC pc, PetscReal *gc, PetscReal *oc) 1308d71ae5a4SJacob Faibussowitsch { 1309e7d4b4cbSMark Adams PC_MG *mg = (PC_MG *)pc->data; 1310e7d4b4cbSMark Adams PC_MG_Levels **mglevels = mg->levels; 1311e7d4b4cbSMark Adams PetscInt lev, N; 1312e7d4b4cbSMark Adams PetscLogDouble nnz0 = 0, sgc = 0, soc = 0, n0 = 0; 1313e7d4b4cbSMark Adams MatInfo info; 1314e7d4b4cbSMark Adams 1315e7d4b4cbSMark Adams PetscFunctionBegin; 131690db8557SMark Adams PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 13174f572ea9SToby Isaac if (gc) PetscAssertPointer(gc, 2); 13184f572ea9SToby Isaac if (oc) PetscAssertPointer(oc, 3); 1319e7d4b4cbSMark Adams if (!pc->setupcalled) { 132090db8557SMark Adams if (gc) *gc = 0; 132190db8557SMark Adams if (oc) *oc = 0; 13223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1323e7d4b4cbSMark Adams } 1324e7d4b4cbSMark Adams PetscCheck(mg->nlevels > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MG has no levels"); 1325e7d4b4cbSMark Adams for (lev = 0; lev < mg->nlevels; lev++) { 1326e7d4b4cbSMark Adams Mat dB; 13279566063dSJacob Faibussowitsch PetscCall(KSPGetOperators(mglevels[lev]->smoothd, NULL, &dB)); 13289566063dSJacob Faibussowitsch PetscCall(MatGetInfo(dB, MAT_GLOBAL_SUM, &info)); /* global reduction */ 13299566063dSJacob Faibussowitsch PetscCall(MatGetSize(dB, &N, NULL)); 1330e7d4b4cbSMark Adams sgc += N; 1331e7d4b4cbSMark Adams soc += info.nz_used; 13329371c9d4SSatish Balay if (lev == mg->nlevels - 1) { 13339371c9d4SSatish Balay nnz0 = info.nz_used; 13349371c9d4SSatish Balay n0 = N; 13359371c9d4SSatish Balay } 1336e7d4b4cbSMark Adams } 13370fdf79fbSJacob Faibussowitsch PetscCheck(n0 > 0 && gc, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number for grid points on finest level is not available"); 13380fdf79fbSJacob Faibussowitsch *gc = (PetscReal)(sgc / n0); 133990db8557SMark Adams if (nnz0 > 0 && oc) *oc = (PetscReal)(soc / nnz0); 13403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1341e7d4b4cbSMark Adams } 1342e7d4b4cbSMark Adams 1343e7d4b4cbSMark Adams /*@ 134404c3f3b8SBarry Smith PCMGSetType - Determines the form of multigrid to use, either 13454b9ad928SBarry Smith multiplicative, additive, full, or the Kaskade algorithm. 13464b9ad928SBarry Smith 1347c3339decSBarry Smith Logically Collective 13484b9ad928SBarry Smith 13494b9ad928SBarry Smith Input Parameters: 13504b9ad928SBarry Smith + pc - the preconditioner context 1351f1580f4eSBarry Smith - form - multigrid form, one of `PC_MG_MULTIPLICATIVE`, `PC_MG_ADDITIVE`, `PC_MG_FULL`, `PC_MG_KASKADE` 13524b9ad928SBarry Smith 13534b9ad928SBarry Smith Options Database Key: 135420f4b53cSBarry Smith . -pc_mg_type <form> - Sets <form>, one of multiplicative, additive, full, kaskade 13554b9ad928SBarry Smith 13564b9ad928SBarry Smith Level: advanced 13574b9ad928SBarry Smith 1358562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMGType`, `PCMG`, `PCMGGetLevels()`, `PCMGSetLevels()`, `PCMGGetType()`, `PCMGCycleType` 13594b9ad928SBarry Smith @*/ 1360d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetType(PC pc, PCMGType form) 1361d71ae5a4SJacob Faibussowitsch { 1362f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 13634b9ad928SBarry Smith 13644b9ad928SBarry Smith PetscFunctionBegin; 13650700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1366c5eb9154SBarry Smith PetscValidLogicalCollectiveEnum(pc, form, 2); 136731567311SBarry Smith mg->am = form; 13689dcbbd2bSBarry Smith if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG; 1369c60c7ad4SBarry Smith else pc->ops->applyrichardson = NULL; 13703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1371c60c7ad4SBarry Smith } 1372c60c7ad4SBarry Smith 1373c60c7ad4SBarry Smith /*@ 1374f1580f4eSBarry Smith PCMGGetType - Finds the form of multigrid the `PCMG` is using multiplicative, additive, full, or the Kaskade algorithm. 1375c60c7ad4SBarry Smith 1376c3339decSBarry Smith Logically Collective 1377c60c7ad4SBarry Smith 1378c60c7ad4SBarry Smith Input Parameter: 1379c60c7ad4SBarry Smith . pc - the preconditioner context 1380c60c7ad4SBarry Smith 1381c60c7ad4SBarry Smith Output Parameter: 1382f1580f4eSBarry Smith . type - one of `PC_MG_MULTIPLICATIVE`, `PC_MG_ADDITIVE`, `PC_MG_FULL`, `PC_MG_KASKADE`, `PCMGCycleType` 1383c60c7ad4SBarry Smith 1384c60c7ad4SBarry Smith Level: advanced 1385c60c7ad4SBarry Smith 1386562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMGType`, `PCMG`, `PCMGGetLevels()`, `PCMGSetLevels()`, `PCMGSetType()` 1387c60c7ad4SBarry Smith @*/ 1388d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetType(PC pc, PCMGType *type) 1389d71ae5a4SJacob Faibussowitsch { 1390c60c7ad4SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 1391c60c7ad4SBarry Smith 1392c60c7ad4SBarry Smith PetscFunctionBegin; 1393c60c7ad4SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1394c60c7ad4SBarry Smith *type = mg->am; 13953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 13964b9ad928SBarry Smith } 13974b9ad928SBarry Smith 13984b9ad928SBarry Smith /*@ 1399f1580f4eSBarry Smith PCMGSetCycleType - Sets the type cycles to use. Use `PCMGSetCycleTypeOnLevel()` for more 14004b9ad928SBarry Smith complicated cycling. 14014b9ad928SBarry Smith 1402c3339decSBarry Smith Logically Collective 14034b9ad928SBarry Smith 14044b9ad928SBarry Smith Input Parameters: 1405c2be2410SBarry Smith + pc - the multigrid context 1406f1580f4eSBarry Smith - n - either `PC_MG_CYCLE_V` or `PC_MG_CYCLE_W` 14074b9ad928SBarry Smith 14084b9ad928SBarry Smith Options Database Key: 1409c1cbb1deSBarry Smith . -pc_mg_cycle_type <v,w> - provide the cycle desired 14104b9ad928SBarry Smith 14114b9ad928SBarry Smith Level: advanced 14124b9ad928SBarry Smith 1413562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGSetCycleTypeOnLevel()`, `PCMGType`, `PCMGCycleType` 14144b9ad928SBarry Smith @*/ 1415d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetCycleType(PC pc, PCMGCycleType n) 1416d71ae5a4SJacob Faibussowitsch { 1417f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 1418f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 141979416396SBarry Smith PetscInt i, levels; 14204b9ad928SBarry Smith 14214b9ad928SBarry Smith PetscFunctionBegin; 14220700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 142369fbec6eSBarry Smith PetscValidLogicalCollectiveEnum(pc, n, 2); 142428b400f6SJacob Faibussowitsch PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Must set MG levels with PCMGSetLevels() before calling"); 1425f3fbd535SBarry Smith levels = mglevels[0]->levels; 14262fa5cd67SKarl Rupp for (i = 0; i < levels; i++) mglevels[i]->cycles = n; 14273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 14284b9ad928SBarry Smith } 14294b9ad928SBarry Smith 14308cc2d5dfSBarry Smith /*@ 14318cc2d5dfSBarry Smith PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step 1432f1580f4eSBarry Smith of multigrid when `PCMGType` is `PC_MG_MULTIPLICATIVE` 14338cc2d5dfSBarry Smith 1434c3339decSBarry Smith Logically Collective 14358cc2d5dfSBarry Smith 14368cc2d5dfSBarry Smith Input Parameters: 14378cc2d5dfSBarry Smith + pc - the multigrid context 14388cc2d5dfSBarry Smith - n - number of cycles (default is 1) 14398cc2d5dfSBarry Smith 14408cc2d5dfSBarry Smith Options Database Key: 1441147403d9SBarry Smith . -pc_mg_multiplicative_cycles n - set the number of cycles 14428cc2d5dfSBarry Smith 14438cc2d5dfSBarry Smith Level: advanced 14448cc2d5dfSBarry Smith 1445f1580f4eSBarry Smith Note: 1446f1580f4eSBarry Smith This is not associated with setting a v or w cycle, that is set with `PCMGSetCycleType()` 14478cc2d5dfSBarry Smith 1448562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMGSetCycleTypeOnLevel()`, `PCMGSetCycleType()`, `PCMGCycleType`, `PCMGType` 14498cc2d5dfSBarry Smith @*/ 1450d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGMultiplicativeSetCycles(PC pc, PetscInt n) 1451d71ae5a4SJacob Faibussowitsch { 1452f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 14538cc2d5dfSBarry Smith 14548cc2d5dfSBarry Smith PetscFunctionBegin; 14550700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1456c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(pc, n, 2); 14572a21e185SBarry Smith mg->cyclesperpcapply = n; 14583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 14598cc2d5dfSBarry Smith } 14608cc2d5dfSBarry Smith 1461bbbcb081SMark Adams /* 1462bbbcb081SMark Adams Since the finest level KSP shares the original matrix (of the entire system), it's preconditioner 1463bbbcb081SMark Adams should not be updated if the whole PC is supposed to reuse the preconditioner 1464bbbcb081SMark Adams */ 1465bbbcb081SMark Adams static PetscErrorCode PCSetReusePreconditioner_MG(PC pc, PetscBool flag) 1466bbbcb081SMark Adams { 1467bbbcb081SMark Adams PC_MG *mg = (PC_MG *)pc->data; 1468bbbcb081SMark Adams PC_MG_Levels **mglevels = mg->levels; 1469bbbcb081SMark Adams PetscInt levels; 1470bbbcb081SMark Adams PC tpc; 1471bbbcb081SMark Adams 1472bbbcb081SMark Adams PetscFunctionBegin; 1473bbbcb081SMark Adams PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1474bbbcb081SMark Adams PetscValidLogicalCollectiveBool(pc, flag, 2); 1475bbbcb081SMark Adams if (mglevels) { 1476bbbcb081SMark Adams levels = mglevels[0]->levels; 1477bbbcb081SMark Adams PetscCall(KSPGetPC(mglevels[levels - 1]->smoothd, &tpc)); 1478bbbcb081SMark Adams tpc->reusepreconditioner = flag; 1479bbbcb081SMark Adams PetscCall(KSPGetPC(mglevels[levels - 1]->smoothu, &tpc)); 1480bbbcb081SMark Adams tpc->reusepreconditioner = flag; 1481bbbcb081SMark Adams } 1482bbbcb081SMark Adams PetscFunctionReturn(PETSC_SUCCESS); 1483bbbcb081SMark Adams } 1484bbbcb081SMark Adams 148566976f2fSJacob Faibussowitsch static PetscErrorCode PCMGSetGalerkin_MG(PC pc, PCMGGalerkinType use) 1486d71ae5a4SJacob Faibussowitsch { 1487fb15c04eSBarry Smith PC_MG *mg = (PC_MG *)pc->data; 1488fb15c04eSBarry Smith 1489fb15c04eSBarry Smith PetscFunctionBegin; 14902134b1e4SBarry Smith mg->galerkin = use; 14913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1492fb15c04eSBarry Smith } 1493fb15c04eSBarry Smith 1494c2be2410SBarry Smith /*@ 149597177400SBarry Smith PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the 149682b87d87SMatthew G. Knepley finest grid via the Galerkin process: A_i-1 = r_i * A_i * p_i 1497c2be2410SBarry Smith 1498c3339decSBarry Smith Logically Collective 1499c2be2410SBarry Smith 1500c2be2410SBarry Smith Input Parameters: 1501c91913d3SJed Brown + pc - the multigrid context 1502f1580f4eSBarry Smith - use - one of `PC_MG_GALERKIN_BOTH`, `PC_MG_GALERKIN_PMAT`, `PC_MG_GALERKIN_MAT`, or `PC_MG_GALERKIN_NONE` 1503c2be2410SBarry Smith 1504c2be2410SBarry Smith Options Database Key: 1505147403d9SBarry Smith . -pc_mg_galerkin <both,pmat,mat,none> - set the matrices to form via the Galerkin process 1506c2be2410SBarry Smith 1507c2be2410SBarry Smith Level: intermediate 1508c2be2410SBarry Smith 1509f1580f4eSBarry Smith Note: 1510f1580f4eSBarry Smith Some codes that use `PCMG` such as `PCGAMG` use Galerkin internally while constructing the hierarchy and thus do not 1511f1580f4eSBarry Smith use the `PCMG` construction of the coarser grids. 15121c1aac46SBarry Smith 1513562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGGetGalerkin()`, `PCMGGalerkinType` 1514c2be2410SBarry Smith @*/ 1515d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetGalerkin(PC pc, PCMGGalerkinType use) 1516d71ae5a4SJacob Faibussowitsch { 1517c2be2410SBarry Smith PetscFunctionBegin; 15180700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1519cac4c232SBarry Smith PetscTryMethod(pc, "PCMGSetGalerkin_C", (PC, PCMGGalerkinType), (pc, use)); 15203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1521c2be2410SBarry Smith } 1522c2be2410SBarry Smith 15233fc8bf9cSBarry Smith /*@ 1524f1580f4eSBarry Smith PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e. A_i-1 = r_i * A_i * p_i 15253fc8bf9cSBarry Smith 15263fc8bf9cSBarry Smith Not Collective 15273fc8bf9cSBarry Smith 15283fc8bf9cSBarry Smith Input Parameter: 15293fc8bf9cSBarry Smith . pc - the multigrid context 15303fc8bf9cSBarry Smith 15313fc8bf9cSBarry Smith Output Parameter: 1532f1580f4eSBarry Smith . galerkin - one of `PC_MG_GALERKIN_BOTH`,`PC_MG_GALERKIN_PMAT`,`PC_MG_GALERKIN_MAT`, `PC_MG_GALERKIN_NONE`, or `PC_MG_GALERKIN_EXTERNAL` 15333fc8bf9cSBarry Smith 15343fc8bf9cSBarry Smith Level: intermediate 15353fc8bf9cSBarry Smith 1536562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGSetGalerkin()`, `PCMGGalerkinType` 15373fc8bf9cSBarry Smith @*/ 1538d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetGalerkin(PC pc, PCMGGalerkinType *galerkin) 1539d71ae5a4SJacob Faibussowitsch { 1540f3fbd535SBarry Smith PC_MG *mg = (PC_MG *)pc->data; 15413fc8bf9cSBarry Smith 15423fc8bf9cSBarry Smith PetscFunctionBegin; 15430700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 15442134b1e4SBarry Smith *galerkin = mg->galerkin; 15453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15463fc8bf9cSBarry Smith } 15473fc8bf9cSBarry Smith 154866976f2fSJacob Faibussowitsch static PetscErrorCode PCMGSetAdaptInterpolation_MG(PC pc, PetscBool adapt) 1549d71ae5a4SJacob Faibussowitsch { 1550f3b08a26SMatthew G. Knepley PC_MG *mg = (PC_MG *)pc->data; 1551f3b08a26SMatthew G. Knepley 1552f3b08a26SMatthew G. Knepley PetscFunctionBegin; 1553f3b08a26SMatthew G. Knepley mg->adaptInterpolation = adapt; 15543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1555f3b08a26SMatthew G. Knepley } 1556f3b08a26SMatthew G. Knepley 155766976f2fSJacob Faibussowitsch static PetscErrorCode PCMGGetAdaptInterpolation_MG(PC pc, PetscBool *adapt) 1558d71ae5a4SJacob Faibussowitsch { 1559f3b08a26SMatthew G. Knepley PC_MG *mg = (PC_MG *)pc->data; 1560f3b08a26SMatthew G. Knepley 1561f3b08a26SMatthew G. Knepley PetscFunctionBegin; 1562f3b08a26SMatthew G. Knepley *adapt = mg->adaptInterpolation; 15633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1564f3b08a26SMatthew G. Knepley } 1565f3b08a26SMatthew G. Knepley 156666976f2fSJacob Faibussowitsch static PetscErrorCode PCMGSetAdaptCoarseSpaceType_MG(PC pc, PCMGCoarseSpaceType ctype) 1567d71ae5a4SJacob Faibussowitsch { 15682b3cbbdaSStefano Zampini PC_MG *mg = (PC_MG *)pc->data; 15692b3cbbdaSStefano Zampini 15702b3cbbdaSStefano Zampini PetscFunctionBegin; 15712b3cbbdaSStefano Zampini mg->adaptInterpolation = ctype != PCMG_ADAPT_NONE ? PETSC_TRUE : PETSC_FALSE; 15722b3cbbdaSStefano Zampini mg->coarseSpaceType = ctype; 1573a077d33dSBarry Smith PetscCall(PCMGSetGalerkin(pc, PC_MG_GALERKIN_BOTH)); 15743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15752b3cbbdaSStefano Zampini } 15762b3cbbdaSStefano Zampini 157766976f2fSJacob Faibussowitsch static PetscErrorCode PCMGGetAdaptCoarseSpaceType_MG(PC pc, PCMGCoarseSpaceType *ctype) 1578d71ae5a4SJacob Faibussowitsch { 15792b3cbbdaSStefano Zampini PC_MG *mg = (PC_MG *)pc->data; 15802b3cbbdaSStefano Zampini 15812b3cbbdaSStefano Zampini PetscFunctionBegin; 15822b3cbbdaSStefano Zampini *ctype = mg->coarseSpaceType; 15833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15842b3cbbdaSStefano Zampini } 15852b3cbbdaSStefano Zampini 158666976f2fSJacob Faibussowitsch static PetscErrorCode PCMGSetAdaptCR_MG(PC pc, PetscBool cr) 1587d71ae5a4SJacob Faibussowitsch { 158841b6fd38SMatthew G. Knepley PC_MG *mg = (PC_MG *)pc->data; 158941b6fd38SMatthew G. Knepley 159041b6fd38SMatthew G. Knepley PetscFunctionBegin; 159141b6fd38SMatthew G. Knepley mg->compatibleRelaxation = cr; 15923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 159341b6fd38SMatthew G. Knepley } 159441b6fd38SMatthew G. Knepley 159566976f2fSJacob Faibussowitsch static PetscErrorCode PCMGGetAdaptCR_MG(PC pc, PetscBool *cr) 1596d71ae5a4SJacob Faibussowitsch { 159741b6fd38SMatthew G. Knepley PC_MG *mg = (PC_MG *)pc->data; 159841b6fd38SMatthew G. Knepley 159941b6fd38SMatthew G. Knepley PetscFunctionBegin; 160041b6fd38SMatthew G. Knepley *cr = mg->compatibleRelaxation; 16013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 160241b6fd38SMatthew G. Knepley } 160341b6fd38SMatthew G. Knepley 1604cc4c1da9SBarry Smith /*@ 16052b3cbbdaSStefano Zampini PCMGSetAdaptCoarseSpaceType - Set the type of adaptive coarse space. 16062b3cbbdaSStefano Zampini 16072b3cbbdaSStefano Zampini Adapts or creates the interpolator based upon a vector space which should be accurately captured by the next coarser mesh, and thus accurately interpolated. 16082b3cbbdaSStefano Zampini 1609c3339decSBarry Smith Logically Collective 16102b3cbbdaSStefano Zampini 16112b3cbbdaSStefano Zampini Input Parameters: 16122b3cbbdaSStefano Zampini + pc - the multigrid context 16132b3cbbdaSStefano Zampini - ctype - the type of coarse space 16142b3cbbdaSStefano Zampini 16152b3cbbdaSStefano Zampini Options Database Keys: 16162b3cbbdaSStefano Zampini + -pc_mg_adapt_interp_n <int> - The number of modes to use 1617a3b724e8SBarry Smith - -pc_mg_adapt_interp_coarse_space <type> - The type of coarse space: none, `polynomial`, `harmonic`, `eigenvector`, `generalized_eigenvector`, `gdsw` 16182b3cbbdaSStefano Zampini 16192b3cbbdaSStefano Zampini Level: intermediate 16202b3cbbdaSStefano Zampini 1621a077d33dSBarry Smith Note: 1622a077d33dSBarry Smith Requires a `DM` with specific functionality be attached to the `PC`. 1623a077d33dSBarry Smith 1624a077d33dSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGCoarseSpaceType`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetGalerkin()`, `PCMGSetAdaptInterpolation()`, `DM` 16252b3cbbdaSStefano Zampini @*/ 1626d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetAdaptCoarseSpaceType(PC pc, PCMGCoarseSpaceType ctype) 1627d71ae5a4SJacob Faibussowitsch { 16282b3cbbdaSStefano Zampini PetscFunctionBegin; 16292b3cbbdaSStefano Zampini PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 16302b3cbbdaSStefano Zampini PetscValidLogicalCollectiveEnum(pc, ctype, 2); 16312b3cbbdaSStefano Zampini PetscTryMethod(pc, "PCMGSetAdaptCoarseSpaceType_C", (PC, PCMGCoarseSpaceType), (pc, ctype)); 16323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16332b3cbbdaSStefano Zampini } 16342b3cbbdaSStefano Zampini 1635cc4c1da9SBarry Smith /*@ 16362b3cbbdaSStefano Zampini PCMGGetAdaptCoarseSpaceType - Get the type of adaptive coarse space. 16372b3cbbdaSStefano Zampini 16382b3cbbdaSStefano Zampini Not Collective 16392b3cbbdaSStefano Zampini 16402b3cbbdaSStefano Zampini Input Parameter: 16412b3cbbdaSStefano Zampini . pc - the multigrid context 16422b3cbbdaSStefano Zampini 16432b3cbbdaSStefano Zampini Output Parameter: 16442b3cbbdaSStefano Zampini . ctype - the type of coarse space 16452b3cbbdaSStefano Zampini 16462b3cbbdaSStefano Zampini Level: intermediate 16472b3cbbdaSStefano Zampini 1648562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGCoarseSpaceType`, `PCMGSetAdaptCoarseSpaceType()`, `PCMGSetGalerkin()`, `PCMGSetAdaptInterpolation()` 16492b3cbbdaSStefano Zampini @*/ 1650d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetAdaptCoarseSpaceType(PC pc, PCMGCoarseSpaceType *ctype) 1651d71ae5a4SJacob Faibussowitsch { 16522b3cbbdaSStefano Zampini PetscFunctionBegin; 16532b3cbbdaSStefano Zampini PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 16544f572ea9SToby Isaac PetscAssertPointer(ctype, 2); 16552b3cbbdaSStefano Zampini PetscUseMethod(pc, "PCMGGetAdaptCoarseSpaceType_C", (PC, PCMGCoarseSpaceType *), (pc, ctype)); 16563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16572b3cbbdaSStefano Zampini } 16582b3cbbdaSStefano Zampini 16592b3cbbdaSStefano Zampini /* MATT: REMOVE? */ 1660f3b08a26SMatthew G. Knepley /*@ 1661f3b08a26SMatthew G. Knepley PCMGSetAdaptInterpolation - Adapt the interpolator based upon a vector space which should be accurately captured by the next coarser mesh, and thus accurately interpolated. 1662f3b08a26SMatthew G. Knepley 1663c3339decSBarry Smith Logically Collective 1664f3b08a26SMatthew G. Knepley 1665f3b08a26SMatthew G. Knepley Input Parameters: 1666f3b08a26SMatthew G. Knepley + pc - the multigrid context 1667f3b08a26SMatthew G. Knepley - adapt - flag for adaptation of the interpolator 1668f3b08a26SMatthew G. Knepley 1669f3b08a26SMatthew G. Knepley Options Database Keys: 1670f3b08a26SMatthew G. Knepley + -pc_mg_adapt_interp - Turn on adaptation 1671f3b08a26SMatthew G. Knepley . -pc_mg_adapt_interp_n <int> - The number of modes to use, should be divisible by dimension 1672f3b08a26SMatthew G. Knepley - -pc_mg_adapt_interp_coarse_space <type> - The type of coarse space: polynomial, harmonic, eigenvector, generalized_eigenvector 1673f3b08a26SMatthew G. Knepley 1674f3b08a26SMatthew G. Knepley Level: intermediate 1675f3b08a26SMatthew G. Knepley 1676562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGGetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()` 1677f3b08a26SMatthew G. Knepley @*/ 1678d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetAdaptInterpolation(PC pc, PetscBool adapt) 1679d71ae5a4SJacob Faibussowitsch { 1680f3b08a26SMatthew G. Knepley PetscFunctionBegin; 1681f3b08a26SMatthew G. Knepley PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1682cac4c232SBarry Smith PetscTryMethod(pc, "PCMGSetAdaptInterpolation_C", (PC, PetscBool), (pc, adapt)); 16833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1684f3b08a26SMatthew G. Knepley } 1685f3b08a26SMatthew G. Knepley 1686f3b08a26SMatthew G. Knepley /*@ 1687f1580f4eSBarry Smith PCMGGetAdaptInterpolation - Get the flag to adapt the interpolator based upon a vector space which should be accurately captured by the next coarser mesh, 1688f1580f4eSBarry Smith and thus accurately interpolated. 1689f3b08a26SMatthew G. Knepley 169020f4b53cSBarry Smith Not Collective 1691f3b08a26SMatthew G. Knepley 1692f3b08a26SMatthew G. Knepley Input Parameter: 1693f3b08a26SMatthew G. Knepley . pc - the multigrid context 1694f3b08a26SMatthew G. Knepley 1695f3b08a26SMatthew G. Knepley Output Parameter: 1696f3b08a26SMatthew G. Knepley . adapt - flag for adaptation of the interpolator 1697f3b08a26SMatthew G. Knepley 1698f3b08a26SMatthew G. Knepley Level: intermediate 1699f3b08a26SMatthew G. Knepley 1700562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGSetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()` 1701f3b08a26SMatthew G. Knepley @*/ 1702d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetAdaptInterpolation(PC pc, PetscBool *adapt) 1703d71ae5a4SJacob Faibussowitsch { 1704f3b08a26SMatthew G. Knepley PetscFunctionBegin; 1705f3b08a26SMatthew G. Knepley PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 17064f572ea9SToby Isaac PetscAssertPointer(adapt, 2); 1707cac4c232SBarry Smith PetscUseMethod(pc, "PCMGGetAdaptInterpolation_C", (PC, PetscBool *), (pc, adapt)); 17083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1709f3b08a26SMatthew G. Knepley } 1710f3b08a26SMatthew G. Knepley 17114b9ad928SBarry Smith /*@ 171241b6fd38SMatthew G. Knepley PCMGSetAdaptCR - Monitor the coarse space quality using an auxiliary solve with compatible relaxation. 171341b6fd38SMatthew G. Knepley 1714c3339decSBarry Smith Logically Collective 171541b6fd38SMatthew G. Knepley 171641b6fd38SMatthew G. Knepley Input Parameters: 171741b6fd38SMatthew G. Knepley + pc - the multigrid context 171841b6fd38SMatthew G. Knepley - cr - flag for compatible relaxation 171941b6fd38SMatthew G. Knepley 1720f1580f4eSBarry Smith Options Database Key: 172141b6fd38SMatthew G. Knepley . -pc_mg_adapt_cr - Turn on compatible relaxation 172241b6fd38SMatthew G. Knepley 172341b6fd38SMatthew G. Knepley Level: intermediate 172441b6fd38SMatthew G. Knepley 1725562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGGetAdaptCR()`, `PCMGSetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()` 172641b6fd38SMatthew G. Knepley @*/ 1727d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetAdaptCR(PC pc, PetscBool cr) 1728d71ae5a4SJacob Faibussowitsch { 172941b6fd38SMatthew G. Knepley PetscFunctionBegin; 173041b6fd38SMatthew G. Knepley PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 1731cac4c232SBarry Smith PetscTryMethod(pc, "PCMGSetAdaptCR_C", (PC, PetscBool), (pc, cr)); 17323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 173341b6fd38SMatthew G. Knepley } 173441b6fd38SMatthew G. Knepley 173541b6fd38SMatthew G. Knepley /*@ 173641b6fd38SMatthew G. Knepley PCMGGetAdaptCR - Get the flag to monitor coarse space quality using an auxiliary solve with compatible relaxation. 173741b6fd38SMatthew G. Knepley 173820f4b53cSBarry Smith Not Collective 173941b6fd38SMatthew G. Knepley 174041b6fd38SMatthew G. Knepley Input Parameter: 174141b6fd38SMatthew G. Knepley . pc - the multigrid context 174241b6fd38SMatthew G. Knepley 174341b6fd38SMatthew G. Knepley Output Parameter: 174441b6fd38SMatthew G. Knepley . cr - flag for compatible relaxaion 174541b6fd38SMatthew G. Knepley 174641b6fd38SMatthew G. Knepley Level: intermediate 174741b6fd38SMatthew G. Knepley 1748562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMGSetAdaptCR()`, `PCMGGetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()` 174941b6fd38SMatthew G. Knepley @*/ 1750d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGGetAdaptCR(PC pc, PetscBool *cr) 1751d71ae5a4SJacob Faibussowitsch { 175241b6fd38SMatthew G. Knepley PetscFunctionBegin; 175341b6fd38SMatthew G. Knepley PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 17544f572ea9SToby Isaac PetscAssertPointer(cr, 2); 1755cac4c232SBarry Smith PetscUseMethod(pc, "PCMGGetAdaptCR_C", (PC, PetscBool *), (pc, cr)); 17563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 175741b6fd38SMatthew G. Knepley } 175841b6fd38SMatthew G. Knepley 175941b6fd38SMatthew G. Knepley /*@ 176006792cafSBarry Smith PCMGSetNumberSmooth - Sets the number of pre and post-smoothing steps to use 1761f1580f4eSBarry Smith on all levels. Use `PCMGDistinctSmoothUp()` to create separate up and down smoothers if you want different numbers of 1762710315b6SLawrence Mitchell pre- and post-smoothing steps. 176306792cafSBarry Smith 1764c3339decSBarry Smith Logically Collective 176506792cafSBarry Smith 176606792cafSBarry Smith Input Parameters: 1767feefa0e1SJacob Faibussowitsch + pc - the multigrid context 176806792cafSBarry Smith - n - the number of smoothing steps 176906792cafSBarry Smith 177006792cafSBarry Smith Options Database Key: 1771a2b725a8SWilliam Gropp . -mg_levels_ksp_max_it <n> - Sets number of pre and post-smoothing steps 177206792cafSBarry Smith 177306792cafSBarry Smith Level: advanced 177406792cafSBarry Smith 1775f1580f4eSBarry Smith Note: 1776f1580f4eSBarry Smith This does not set a value on the coarsest grid, since we assume that there is no separate smooth up on the coarsest grid. 177706792cafSBarry Smith 1778562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGSetDistinctSmoothUp()` 177906792cafSBarry Smith @*/ 1780d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetNumberSmooth(PC pc, PetscInt n) 1781d71ae5a4SJacob Faibussowitsch { 178206792cafSBarry Smith PC_MG *mg = (PC_MG *)pc->data; 178306792cafSBarry Smith PC_MG_Levels **mglevels = mg->levels; 178406792cafSBarry Smith PetscInt i, levels; 178506792cafSBarry Smith 178606792cafSBarry Smith PetscFunctionBegin; 178706792cafSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 178806792cafSBarry Smith PetscValidLogicalCollectiveInt(pc, n, 2); 178928b400f6SJacob Faibussowitsch PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Must set MG levels with PCMGSetLevels() before calling"); 179006792cafSBarry Smith levels = mglevels[0]->levels; 179106792cafSBarry Smith 179206792cafSBarry Smith for (i = 1; i < levels; i++) { 1793fb842aefSJose E. Roman PetscCall(KSPSetTolerances(mglevels[i]->smoothu, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, n)); 1794fb842aefSJose E. Roman PetscCall(KSPSetTolerances(mglevels[i]->smoothd, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, n)); 179506792cafSBarry Smith mg->default_smoothu = n; 179606792cafSBarry Smith mg->default_smoothd = n; 179706792cafSBarry Smith } 17983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 179906792cafSBarry Smith } 180006792cafSBarry Smith 1801f442ab6aSBarry Smith /*@ 1802f1580f4eSBarry Smith PCMGSetDistinctSmoothUp - sets the up (post) smoother to be a separate `KSP` from the down (pre) smoother on all levels 1803710315b6SLawrence Mitchell and adds the suffix _up to the options name 1804f442ab6aSBarry Smith 1805c3339decSBarry Smith Logically Collective 1806f442ab6aSBarry Smith 1807f1580f4eSBarry Smith Input Parameter: 1808f442ab6aSBarry Smith . pc - the preconditioner context 1809f442ab6aSBarry Smith 1810f442ab6aSBarry Smith Options Database Key: 1811147403d9SBarry Smith . -pc_mg_distinct_smoothup <bool> - use distinct smoothing objects 1812f442ab6aSBarry Smith 1813f442ab6aSBarry Smith Level: advanced 1814f442ab6aSBarry Smith 1815f1580f4eSBarry Smith Note: 1816f1580f4eSBarry Smith This does not set a value on the coarsest grid, since we assume that there is no separate smooth up on the coarsest grid. 1817f442ab6aSBarry Smith 1818562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMG`, `PCMGSetNumberSmooth()` 1819f442ab6aSBarry Smith @*/ 1820d71ae5a4SJacob Faibussowitsch PetscErrorCode PCMGSetDistinctSmoothUp(PC pc) 1821d71ae5a4SJacob Faibussowitsch { 1822f442ab6aSBarry Smith PC_MG *mg = (PC_MG *)pc->data; 1823f442ab6aSBarry Smith PC_MG_Levels **mglevels = mg->levels; 1824f442ab6aSBarry Smith PetscInt i, levels; 1825f442ab6aSBarry Smith KSP subksp; 1826f442ab6aSBarry Smith 1827f442ab6aSBarry Smith PetscFunctionBegin; 1828f442ab6aSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 182928b400f6SJacob Faibussowitsch PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Must set MG levels with PCMGSetLevels() before calling"); 1830f442ab6aSBarry Smith levels = mglevels[0]->levels; 1831f442ab6aSBarry Smith 1832f442ab6aSBarry Smith for (i = 1; i < levels; i++) { 1833710315b6SLawrence Mitchell const char *prefix = NULL; 1834f442ab6aSBarry Smith /* make sure smoother up and down are different */ 18359566063dSJacob Faibussowitsch PetscCall(PCMGGetSmootherUp(pc, i, &subksp)); 18369566063dSJacob Faibussowitsch PetscCall(KSPGetOptionsPrefix(mglevels[i]->smoothd, &prefix)); 18379566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(subksp, prefix)); 18389566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(subksp, "up_")); 1839f442ab6aSBarry Smith } 18403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1841f442ab6aSBarry Smith } 1842f442ab6aSBarry Smith 184307a4832bSFande Kong /* No new matrices are created, and the coarse operator matrices are the references to the original ones */ 184466976f2fSJacob Faibussowitsch static PetscErrorCode PCGetInterpolations_MG(PC pc, PetscInt *num_levels, Mat *interpolations[]) 1845d71ae5a4SJacob Faibussowitsch { 184607a4832bSFande Kong PC_MG *mg = (PC_MG *)pc->data; 184707a4832bSFande Kong PC_MG_Levels **mglevels = mg->levels; 184807a4832bSFande Kong Mat *mat; 184907a4832bSFande Kong PetscInt l; 185007a4832bSFande Kong 185107a4832bSFande Kong PetscFunctionBegin; 185228b400f6SJacob Faibussowitsch PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must set MG levels before calling"); 18539566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(mg->nlevels, &mat)); 185407a4832bSFande Kong for (l = 1; l < mg->nlevels; l++) { 185507a4832bSFande Kong mat[l - 1] = mglevels[l]->interpolate; 18569566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)mat[l - 1])); 185707a4832bSFande Kong } 185807a4832bSFande Kong *num_levels = mg->nlevels; 185907a4832bSFande Kong *interpolations = mat; 18603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 186107a4832bSFande Kong } 186207a4832bSFande Kong 186307a4832bSFande Kong /* No new matrices are created, and the coarse operator matrices are the references to the original ones */ 186466976f2fSJacob Faibussowitsch static PetscErrorCode PCGetCoarseOperators_MG(PC pc, PetscInt *num_levels, Mat *coarseOperators[]) 1865d71ae5a4SJacob Faibussowitsch { 186607a4832bSFande Kong PC_MG *mg = (PC_MG *)pc->data; 186707a4832bSFande Kong PC_MG_Levels **mglevels = mg->levels; 186807a4832bSFande Kong PetscInt l; 186907a4832bSFande Kong Mat *mat; 187007a4832bSFande Kong 187107a4832bSFande Kong PetscFunctionBegin; 187228b400f6SJacob Faibussowitsch PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must set MG levels before calling"); 18739566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(mg->nlevels, &mat)); 187407a4832bSFande Kong for (l = 0; l < mg->nlevels - 1; l++) { 1875f4f49eeaSPierre Jolivet PetscCall(KSPGetOperators(mglevels[l]->smoothd, NULL, &mat[l])); 18769566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)mat[l])); 187707a4832bSFande Kong } 187807a4832bSFande Kong *num_levels = mg->nlevels; 187907a4832bSFande Kong *coarseOperators = mat; 18803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 188107a4832bSFande Kong } 188207a4832bSFande Kong 1883f3b08a26SMatthew G. Knepley /*@C 1884f1580f4eSBarry Smith PCMGRegisterCoarseSpaceConstructor - Adds a method to the `PCMG` package for coarse space construction. 1885f3b08a26SMatthew G. Knepley 1886cc4c1da9SBarry Smith Not Collective, No Fortran Support 1887f3b08a26SMatthew G. Knepley 1888f3b08a26SMatthew G. Knepley Input Parameters: 1889f3b08a26SMatthew G. Knepley + name - name of the constructor 18904d4d2bdcSBarry Smith - function - constructor routine, see `PCMGCoarseSpaceConstructorFn` 1891f3b08a26SMatthew G. Knepley 1892f3b08a26SMatthew G. Knepley Level: advanced 1893f3b08a26SMatthew G. Knepley 1894feefa0e1SJacob Faibussowitsch Developer Notes: 18954d4d2bdcSBarry Smith This does not appear to be used anywhere 1896f1580f4eSBarry Smith 18974d4d2bdcSBarry Smith .seealso: [](ch_ksp), `PCMGCoarseSpaceConstructorFn`, `PCMG`, `PCMGGetCoarseSpaceConstructor()`, `PCRegister()` 1898f3b08a26SMatthew G. Knepley @*/ 18994d4d2bdcSBarry Smith PetscErrorCode PCMGRegisterCoarseSpaceConstructor(const char name[], PCMGCoarseSpaceConstructorFn *function) 1900d71ae5a4SJacob Faibussowitsch { 1901f3b08a26SMatthew G. Knepley PetscFunctionBegin; 19029566063dSJacob Faibussowitsch PetscCall(PCInitializePackage()); 19039566063dSJacob Faibussowitsch PetscCall(PetscFunctionListAdd(&PCMGCoarseList, name, function)); 19043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1905f3b08a26SMatthew G. Knepley } 1906f3b08a26SMatthew G. Knepley 1907f3b08a26SMatthew G. Knepley /*@C 1908f3b08a26SMatthew G. Knepley PCMGGetCoarseSpaceConstructor - Returns the given coarse space construction method. 1909f3b08a26SMatthew G. Knepley 1910cc4c1da9SBarry Smith Not Collective, No Fortran Support 1911f3b08a26SMatthew G. Knepley 1912f3b08a26SMatthew G. Knepley Input Parameter: 1913f3b08a26SMatthew G. Knepley . name - name of the constructor 1914f3b08a26SMatthew G. Knepley 1915f3b08a26SMatthew G. Knepley Output Parameter: 1916f3b08a26SMatthew G. Knepley . function - constructor routine 1917f3b08a26SMatthew G. Knepley 1918f3b08a26SMatthew G. Knepley Level: advanced 1919f3b08a26SMatthew G. Knepley 19204d4d2bdcSBarry Smith .seealso: [](ch_ksp), `PCMGCoarseSpaceConstructorFn`, `PCMG`, `PCMGRegisterCoarseSpaceConstructor()`, `PCRegister()` 1921f3b08a26SMatthew G. Knepley @*/ 19224d4d2bdcSBarry Smith PetscErrorCode PCMGGetCoarseSpaceConstructor(const char name[], PCMGCoarseSpaceConstructorFn **function) 1923d71ae5a4SJacob Faibussowitsch { 1924f3b08a26SMatthew G. Knepley PetscFunctionBegin; 19259566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(PCMGCoarseList, name, function)); 19263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1927f3b08a26SMatthew G. Knepley } 1928f3b08a26SMatthew G. Knepley 19293b09bd56SBarry Smith /*MC 1930*efba3485SBarry Smith PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional information about the restriction/interpolation 1931*efba3485SBarry Smith operators using `PCMGSetInterpolation()` and/or `PCMGSetRestriction()`(and possibly the coarser grid matrices) or a `DM` that can provide such information. 19323b09bd56SBarry Smith 19333b09bd56SBarry Smith Options Database Keys: 19343b09bd56SBarry Smith + -pc_mg_levels <nlevels> - number of levels including finest 1935391689abSStefano Zampini . -pc_mg_cycle_type <v,w> - provide the cycle desired 19368c1c2452SJed Brown . -pc_mg_type <additive,multiplicative,full,kaskade> - multiplicative is the default 19373b09bd56SBarry Smith . -pc_mg_log - log information about time spent on each level of the solver 1938710315b6SLawrence Mitchell . -pc_mg_distinct_smoothup - configure up (after interpolation) and down (before restriction) smoothers separately (with different options prefixes) 1939*efba3485SBarry Smith . -pc_mg_galerkin <both,pmat,mat,none> - use the Galerkin process to compute coarser operators, i.e., $A_{coarse} = R A_{fine} R^T$ 19408cf6037eSBarry Smith . -pc_mg_multiplicative_cycles - number of cycles to use as the preconditioner (defaults to 1) 19418cf6037eSBarry Smith . -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices 1942a3b724e8SBarry Smith to a `PETSCVIEWERSOCKET` for reading from MATLAB. 19438cf6037eSBarry Smith - -pc_mg_dump_binary -dumps the matrices for each level and the restriction/interpolation matrices 19448cf6037eSBarry Smith to the binary output file called binaryoutput 19453b09bd56SBarry Smith 194620f4b53cSBarry Smith Level: intermediate 194720f4b53cSBarry Smith 194895452b02SPatrick Sanan Notes: 1949*efba3485SBarry Smith `PCMG` provides a general framework for implementing multigrid methods. Use `PCGAMG` for PETSc's algebraic multigrid preconditioner, `PCHYPRE` for hypre's 1950*efba3485SBarry Smith BoomerAMG algebraic multigrid, and `PCML` for Trilinos's ML preconditioner. `PCAMGX` provides access to NVIDIA's AmgX algebraic multigrid. 1951*efba3485SBarry Smith 1952*efba3485SBarry Smith If you use `KSPSetDM()` (or `SNESSetDM()` or `TSSetDM()`) with an appropriate `DM`, such as `DMDA`, then `PCMG` will use the geometric information 1953*efba3485SBarry Smith from the `DM` to generate appropriate restriction and interpolation information and construct a geometric multigrid. 1954*efba3485SBarry Smith 1955*efba3485SBarry Smith If you do not provide an appropriate `DM` and do not provide restriction or interpolation operators with `PCMGSetInterpolation()` and/or `PCMGSetRestriction()`, 1956*efba3485SBarry Smith then `PCMG` will run multigrid with only a single level (so not really multigrid). 1957*efba3485SBarry Smith 195804c3f3b8SBarry Smith The Krylov solver (if any) and preconditioner (smoother) and their parameters are controlled from the options database with the standard 19598f4fb22eSMark Adams options database keywords prefixed with `-mg_levels_` to affect all the levels but the coarsest, which is controlled with `-mg_coarse_`, 19608f4fb22eSMark Adams and the finest where `-mg_fine_` can override `-mg_levels_`. One can set different preconditioners etc on specific levels with the prefix 19618f4fb22eSMark Adams `-mg_levels_n_` where `n` is the level number (zero being the coarse level. For example 196204c3f3b8SBarry Smith .vb 196304c3f3b8SBarry Smith -mg_levels_ksp_type gmres -mg_levels_pc_type bjacobi -mg_coarse_pc_type svd -mg_levels_2_pc_type sor 196404c3f3b8SBarry Smith .ve 196504c3f3b8SBarry Smith These options also work for controlling the smoothers etc inside `PCGAMG` 196604c3f3b8SBarry Smith 1967e87b5d96SPierre Jolivet If one uses a Krylov method such `KSPGMRES` or `KSPCG` as the smoother than one must use `KSPFGMRES`, `KSPGCR`, or `KSPRICHARDSON` as the outer Krylov method 19683b09bd56SBarry Smith 19698cf6037eSBarry Smith When run with a single level the smoother options are used on that level NOT the coarse grid solver options 19708cf6037eSBarry Smith 1971f1580f4eSBarry Smith When run with `KSPRICHARDSON` the convergence test changes slightly if monitor is turned on. The iteration count may change slightly. This 197223067569SBarry Smith is because without monitoring the residual norm is computed WITHIN each multigrid cycle on the finest level after the pre-smoothing 197323067569SBarry Smith (because the residual has just been computed for the multigrid algorithm and is hence available for free) while with monitoring the 197423067569SBarry Smith residual is computed at the end of each cycle. 197523067569SBarry Smith 197604c3f3b8SBarry Smith .seealso: [](sec_mg), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCMGType`, `PCEXOTIC`, `PCGAMG`, `PCML`, `PCHYPRE` 1977db781477SPatrick Sanan `PCMGSetLevels()`, `PCMGGetLevels()`, `PCMGSetType()`, `PCMGSetCycleType()`, 1978db781477SPatrick Sanan `PCMGSetDistinctSmoothUp()`, `PCMGGetCoarseSolve()`, `PCMGSetResidual()`, `PCMGSetInterpolation()`, 1979db781477SPatrick Sanan `PCMGSetRestriction()`, `PCMGGetSmoother()`, `PCMGGetSmootherUp()`, `PCMGGetSmootherDown()`, 1980f1580f4eSBarry Smith `PCMGSetCycleTypeOnLevel()`, `PCMGSetRhs()`, `PCMGSetX()`, `PCMGSetR()`, 1981f1580f4eSBarry Smith `PCMGSetAdaptCR()`, `PCMGGetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()` 19823b09bd56SBarry Smith M*/ 19833b09bd56SBarry Smith 1984d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc) 1985d71ae5a4SJacob Faibussowitsch { 1986f3fbd535SBarry Smith PC_MG *mg; 1987f3fbd535SBarry Smith 19884b9ad928SBarry Smith PetscFunctionBegin; 19894dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&mg)); 19903ec1f749SStefano Zampini pc->data = mg; 1991f3fbd535SBarry Smith mg->nlevels = -1; 199210eca3edSLisandro Dalcin mg->am = PC_MG_MULTIPLICATIVE; 19932134b1e4SBarry Smith mg->galerkin = PC_MG_GALERKIN_NONE; 1994f3b08a26SMatthew G. Knepley mg->adaptInterpolation = PETSC_FALSE; 1995f3b08a26SMatthew G. Knepley mg->Nc = -1; 1996f3b08a26SMatthew G. Knepley mg->eigenvalue = -1; 1997f3fbd535SBarry Smith 199837a44384SMark Adams pc->useAmat = PETSC_TRUE; 199937a44384SMark Adams 20004b9ad928SBarry Smith pc->ops->apply = PCApply_MG; 2001fcb023d4SJed Brown pc->ops->applytranspose = PCApplyTranspose_MG; 200230b0564aSStefano Zampini pc->ops->matapply = PCMatApply_MG; 20034dbf25a8SPierre Jolivet pc->ops->matapplytranspose = PCMatApplyTranspose_MG; 20044b9ad928SBarry Smith pc->ops->setup = PCSetUp_MG; 2005a06653b4SBarry Smith pc->ops->reset = PCReset_MG; 20064b9ad928SBarry Smith pc->ops->destroy = PCDestroy_MG; 20074b9ad928SBarry Smith pc->ops->setfromoptions = PCSetFromOptions_MG; 20084b9ad928SBarry Smith pc->ops->view = PCView_MG; 2009fb15c04eSBarry Smith 20109566063dSJacob Faibussowitsch PetscCall(PetscObjectComposedDataRegister(&mg->eigenvalue)); 20119566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetGalerkin_C", PCMGSetGalerkin_MG)); 2012bbbcb081SMark Adams PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetReusePreconditioner_C", PCSetReusePreconditioner_MG)); 20139566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetLevels_C", PCMGGetLevels_MG)); 20149566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetLevels_C", PCMGSetLevels_MG)); 20159566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", PCGetInterpolations_MG)); 20169566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", PCGetCoarseOperators_MG)); 20179566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptInterpolation_C", PCMGSetAdaptInterpolation_MG)); 20189566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptInterpolation_C", PCMGGetAdaptInterpolation_MG)); 20199566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCR_C", PCMGSetAdaptCR_MG)); 20209566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCR_C", PCMGGetAdaptCR_MG)); 20212b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCoarseSpaceType_C", PCMGSetAdaptCoarseSpaceType_MG)); 20222b3cbbdaSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCoarseSpaceType_C", PCMGGetAdaptCoarseSpaceType_MG)); 20233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 20244b9ad928SBarry Smith } 2025