1dba47a55SKris Buschelman 24b9ad928SBarry Smith /* 34b9ad928SBarry Smith Defines the multigrid preconditioner interface. 44b9ad928SBarry Smith */ 5af0996ceSBarry Smith #include <petsc/private/pcmgimpl.h> /*I "petscksp.h" I*/ 61e25c274SJed Brown #include <petscdm.h> 74b9ad928SBarry Smith 84b9ad928SBarry Smith #undef __FUNCT__ 99dcbbd2bSBarry Smith #define __FUNCT__ "PCMGMCycle_Private" 1031567311SBarry Smith PetscErrorCode PCMGMCycle_Private(PC pc,PC_MG_Levels **mglevelsin,PCRichardsonConvergedReason *reason) 114b9ad928SBarry Smith { 1231567311SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 1331567311SBarry Smith PC_MG_Levels *mgc,*mglevels = *mglevelsin; 146849ba73SBarry Smith PetscErrorCode ierr; 1531567311SBarry Smith PetscInt cycles = (mglevels->level == 1) ? 1 : (PetscInt) mglevels->cycles; 16a0808db4SHong Zhang PC subpc; 17a0808db4SHong Zhang PCFailedReason pcreason; 184b9ad928SBarry Smith 194b9ad928SBarry Smith PetscFunctionBegin; 2063e6d426SJed Brown if (mglevels->eventsmoothsolve) {ierr = PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);} 2131567311SBarry Smith ierr = KSPSolve(mglevels->smoothd,mglevels->b,mglevels->x);CHKERRQ(ierr); /* pre-smooth */ 22a0808db4SHong Zhang ierr = KSPGetPC(mglevels->smoothd,&subpc);CHKERRQ(ierr); 23a0808db4SHong Zhang ierr = PCGetSetUpFailedReason(subpc,&pcreason);CHKERRQ(ierr); 24a0808db4SHong Zhang if (pcreason) { 25a0808db4SHong Zhang pc->failedreason = PC_SUBPC_ERROR; 26a0808db4SHong Zhang } 2763e6d426SJed Brown if (mglevels->eventsmoothsolve) {ierr = PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);} 2831567311SBarry Smith if (mglevels->level) { /* not the coarsest grid */ 2963e6d426SJed Brown if (mglevels->eventresidual) {ierr = PetscLogEventBegin(mglevels->eventresidual,0,0,0,0);CHKERRQ(ierr);} 3031567311SBarry Smith ierr = (*mglevels->residual)(mglevels->A,mglevels->b,mglevels->x,mglevels->r);CHKERRQ(ierr); 3163e6d426SJed Brown if (mglevels->eventresidual) {ierr = PetscLogEventEnd(mglevels->eventresidual,0,0,0,0);CHKERRQ(ierr);} 324b9ad928SBarry Smith 334b9ad928SBarry Smith /* if on finest level and have convergence criteria set */ 3431567311SBarry Smith if (mglevels->level == mglevels->levels-1 && mg->ttol && reason) { 354b9ad928SBarry Smith PetscReal rnorm; 3631567311SBarry Smith ierr = VecNorm(mglevels->r,NORM_2,&rnorm);CHKERRQ(ierr); 374b9ad928SBarry Smith if (rnorm <= mg->ttol) { 3870441072SBarry Smith if (rnorm < mg->abstol) { 394d0a8057SBarry Smith *reason = PCRICHARDSON_CONVERGED_ATOL; 4057622a8eSBarry Smith ierr = PetscInfo2(pc,"Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n",(double)rnorm,(double)mg->abstol);CHKERRQ(ierr); 414b9ad928SBarry Smith } else { 424d0a8057SBarry Smith *reason = PCRICHARDSON_CONVERGED_RTOL; 4357622a8eSBarry Smith ierr = PetscInfo2(pc,"Linear solver has converged. Residual norm %g is less than relative tolerance times initial residual norm %g\n",(double)rnorm,(double)mg->ttol);CHKERRQ(ierr); 444b9ad928SBarry Smith } 454b9ad928SBarry Smith PetscFunctionReturn(0); 464b9ad928SBarry Smith } 474b9ad928SBarry Smith } 484b9ad928SBarry Smith 4931567311SBarry Smith mgc = *(mglevelsin - 1); 5063e6d426SJed Brown if (mglevels->eventinterprestrict) {ierr = PetscLogEventBegin(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);} 5131567311SBarry Smith ierr = MatRestrict(mglevels->restrct,mglevels->r,mgc->b);CHKERRQ(ierr); 5263e6d426SJed Brown if (mglevels->eventinterprestrict) {ierr = PetscLogEventEnd(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);} 53efb30889SBarry Smith ierr = VecSet(mgc->x,0.0);CHKERRQ(ierr); 544b9ad928SBarry Smith while (cycles--) { 5531567311SBarry Smith ierr = PCMGMCycle_Private(pc,mglevelsin-1,reason);CHKERRQ(ierr); 564b9ad928SBarry Smith } 5763e6d426SJed Brown if (mglevels->eventinterprestrict) {ierr = PetscLogEventBegin(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);} 5831567311SBarry Smith ierr = MatInterpolateAdd(mglevels->interpolate,mgc->x,mglevels->x,mglevels->x);CHKERRQ(ierr); 5963e6d426SJed Brown if (mglevels->eventinterprestrict) {ierr = PetscLogEventEnd(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);} 6063e6d426SJed Brown if (mglevels->eventsmoothsolve) {ierr = PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);} 6131567311SBarry Smith ierr = KSPSolve(mglevels->smoothu,mglevels->b,mglevels->x);CHKERRQ(ierr); /* post smooth */ 6263e6d426SJed Brown if (mglevels->eventsmoothsolve) {ierr = PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);} 634b9ad928SBarry Smith } 644b9ad928SBarry Smith PetscFunctionReturn(0); 654b9ad928SBarry Smith } 664b9ad928SBarry Smith 674b9ad928SBarry Smith #undef __FUNCT__ 684b9ad928SBarry Smith #define __FUNCT__ "PCApplyRichardson_MG" 69ace3abfcSBarry Smith 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) 704b9ad928SBarry Smith { 71f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 72f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 73dfbe8321SBarry Smith PetscErrorCode ierr; 74f3fbd535SBarry Smith PetscInt levels = mglevels[0]->levels,i; 754b9ad928SBarry Smith 764b9ad928SBarry Smith PetscFunctionBegin; 77a762d673SBarry Smith /* When the DM is supplying the matrix then it will not exist until here */ 78a762d673SBarry Smith for (i=0; i<levels; i++) { 79a762d673SBarry Smith if (!mglevels[i]->A) { 80a762d673SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);CHKERRQ(ierr); 81a762d673SBarry Smith ierr = PetscObjectReference((PetscObject)mglevels[i]->A);CHKERRQ(ierr); 82a762d673SBarry Smith } 83a762d673SBarry Smith } 84f3fbd535SBarry Smith mglevels[levels-1]->b = b; 85f3fbd535SBarry Smith mglevels[levels-1]->x = x; 864b9ad928SBarry Smith 8731567311SBarry Smith mg->rtol = rtol; 8831567311SBarry Smith mg->abstol = abstol; 8931567311SBarry Smith mg->dtol = dtol; 904b9ad928SBarry Smith if (rtol) { 914b9ad928SBarry Smith /* compute initial residual norm for relative convergence test */ 924b9ad928SBarry Smith PetscReal rnorm; 937319c654SBarry Smith if (zeroguess) { 947319c654SBarry Smith ierr = VecNorm(b,NORM_2,&rnorm);CHKERRQ(ierr); 957319c654SBarry Smith } else { 96f3fbd535SBarry Smith ierr = (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);CHKERRQ(ierr); 974b9ad928SBarry Smith ierr = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr); 987319c654SBarry Smith } 9931567311SBarry Smith mg->ttol = PetscMax(rtol*rnorm,abstol); 1002fa5cd67SKarl Rupp } else if (abstol) mg->ttol = abstol; 1012fa5cd67SKarl Rupp else mg->ttol = 0.0; 1024b9ad928SBarry Smith 1034d0a8057SBarry Smith /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't 104336babb1SJed Brown stop prematurely due to small residual */ 1054d0a8057SBarry Smith for (i=1; i<levels; i++) { 106f3fbd535SBarry Smith ierr = KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 107f3fbd535SBarry Smith if (mglevels[i]->smoothu != mglevels[i]->smoothd) { 108f3fbd535SBarry Smith ierr = KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 1094b9ad928SBarry Smith } 1104d0a8057SBarry Smith } 1114d0a8057SBarry Smith 1124d0a8057SBarry Smith *reason = (PCRichardsonConvergedReason)0; 1134d0a8057SBarry Smith for (i=0; i<its; i++) { 114f3fbd535SBarry Smith ierr = PCMGMCycle_Private(pc,mglevels+levels-1,reason);CHKERRQ(ierr); 1154d0a8057SBarry Smith if (*reason) break; 1164d0a8057SBarry Smith } 1174d0a8057SBarry Smith if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS; 1184d0a8057SBarry Smith *outits = i; 1194b9ad928SBarry Smith PetscFunctionReturn(0); 1204b9ad928SBarry Smith } 1214b9ad928SBarry Smith 1224b9ad928SBarry Smith #undef __FUNCT__ 1233aeaf226SBarry Smith #define __FUNCT__ "PCReset_MG" 1243aeaf226SBarry Smith PetscErrorCode PCReset_MG(PC pc) 1253aeaf226SBarry Smith { 1263aeaf226SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 1273aeaf226SBarry Smith PC_MG_Levels **mglevels = mg->levels; 1283aeaf226SBarry Smith PetscErrorCode ierr; 1293aeaf226SBarry Smith PetscInt i,n; 1303aeaf226SBarry Smith 1313aeaf226SBarry Smith PetscFunctionBegin; 1323aeaf226SBarry Smith if (mglevels) { 1333aeaf226SBarry Smith n = mglevels[0]->levels; 1343aeaf226SBarry Smith for (i=0; i<n-1; i++) { 1353aeaf226SBarry Smith ierr = VecDestroy(&mglevels[i+1]->r);CHKERRQ(ierr); 1363aeaf226SBarry Smith ierr = VecDestroy(&mglevels[i]->b);CHKERRQ(ierr); 1373aeaf226SBarry Smith ierr = VecDestroy(&mglevels[i]->x);CHKERRQ(ierr); 1383aeaf226SBarry Smith ierr = MatDestroy(&mglevels[i+1]->restrct);CHKERRQ(ierr); 1393aeaf226SBarry Smith ierr = MatDestroy(&mglevels[i+1]->interpolate);CHKERRQ(ierr); 14073250ac0SBarry Smith ierr = VecDestroy(&mglevels[i+1]->rscale);CHKERRQ(ierr); 1413aeaf226SBarry Smith } 1423aeaf226SBarry Smith 1433aeaf226SBarry Smith for (i=0; i<n; i++) { 1443aeaf226SBarry Smith ierr = MatDestroy(&mglevels[i]->A);CHKERRQ(ierr); 1453aeaf226SBarry Smith if (mglevels[i]->smoothd != mglevels[i]->smoothu) { 1463aeaf226SBarry Smith ierr = KSPReset(mglevels[i]->smoothd);CHKERRQ(ierr); 1473aeaf226SBarry Smith } 1483aeaf226SBarry Smith ierr = KSPReset(mglevels[i]->smoothu);CHKERRQ(ierr); 1493aeaf226SBarry Smith } 1503aeaf226SBarry Smith } 1513aeaf226SBarry Smith PetscFunctionReturn(0); 1523aeaf226SBarry Smith } 1533aeaf226SBarry Smith 1543aeaf226SBarry Smith #undef __FUNCT__ 1559dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetLevels" 1564b9ad928SBarry Smith /*@C 15797177400SBarry Smith PCMGSetLevels - Sets the number of levels to use with MG. 1584b9ad928SBarry Smith Must be called before any other MG routine. 1594b9ad928SBarry Smith 160ad4df100SBarry Smith Logically Collective on PC 1614b9ad928SBarry Smith 1624b9ad928SBarry Smith Input Parameters: 1634b9ad928SBarry Smith + pc - the preconditioner context 1644b9ad928SBarry Smith . levels - the number of levels 1654b9ad928SBarry Smith - comms - optional communicators for each level; this is to allow solving the coarser problems 1660298fd71SBarry Smith on smaller sets of processors. Use NULL_OBJECT for default in Fortran 1674b9ad928SBarry Smith 1684b9ad928SBarry Smith Level: intermediate 1694b9ad928SBarry Smith 1704b9ad928SBarry Smith Notes: 1714b9ad928SBarry Smith If the number of levels is one then the multigrid uses the -mg_levels prefix 1724b9ad928SBarry Smith for setting the level options rather than the -mg_coarse prefix. 1734b9ad928SBarry Smith 1744b9ad928SBarry Smith .keywords: MG, set, levels, multigrid 1754b9ad928SBarry Smith 17697177400SBarry Smith .seealso: PCMGSetType(), PCMGGetLevels() 1774b9ad928SBarry Smith @*/ 1787087cfbeSBarry Smith PetscErrorCode PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms) 1794b9ad928SBarry Smith { 180dfbe8321SBarry Smith PetscErrorCode ierr; 181f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 182ce94432eSBarry Smith MPI_Comm comm; 1833aeaf226SBarry Smith PC_MG_Levels **mglevels = mg->levels; 18410eca3edSLisandro Dalcin PCMGType mgtype = mg->am; 18510167fecSBarry Smith PetscInt mgctype = (PetscInt) PC_MG_CYCLE_V; 186f3fbd535SBarry Smith PetscInt i; 187f3fbd535SBarry Smith PetscMPIInt size; 188f3fbd535SBarry Smith const char *prefix; 189f3fbd535SBarry Smith PC ipc; 1903aeaf226SBarry Smith PetscInt n; 1914b9ad928SBarry Smith 1924b9ad928SBarry Smith PetscFunctionBegin; 1930700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 194548767e0SJed Brown PetscValidLogicalCollectiveInt(pc,levels,2); 195ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr); 196548767e0SJed Brown if (mg->nlevels == levels) PetscFunctionReturn(0); 1973aeaf226SBarry Smith if (mglevels) { 19810eca3edSLisandro Dalcin mgctype = mglevels[0]->cycles; 1993aeaf226SBarry Smith /* changing the number of levels so free up the previous stuff */ 2003aeaf226SBarry Smith ierr = PCReset_MG(pc);CHKERRQ(ierr); 2013aeaf226SBarry Smith n = mglevels[0]->levels; 2023aeaf226SBarry Smith for (i=0; i<n; i++) { 2033aeaf226SBarry Smith if (mglevels[i]->smoothd != mglevels[i]->smoothu) { 2043aeaf226SBarry Smith ierr = KSPDestroy(&mglevels[i]->smoothd);CHKERRQ(ierr); 2053aeaf226SBarry Smith } 2063aeaf226SBarry Smith ierr = KSPDestroy(&mglevels[i]->smoothu);CHKERRQ(ierr); 2073aeaf226SBarry Smith ierr = PetscFree(mglevels[i]);CHKERRQ(ierr); 2083aeaf226SBarry Smith } 2093aeaf226SBarry Smith ierr = PetscFree(mg->levels);CHKERRQ(ierr); 2103aeaf226SBarry Smith } 211f3fbd535SBarry Smith 212f3fbd535SBarry Smith mg->nlevels = levels; 213f3fbd535SBarry Smith 214785e854fSJed Brown ierr = PetscMalloc1(levels,&mglevels);CHKERRQ(ierr); 2153bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)pc,levels*(sizeof(PC_MG*)));CHKERRQ(ierr); 216f3fbd535SBarry Smith 217f3fbd535SBarry Smith ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 218f3fbd535SBarry Smith 219a9db87a2SMatthew G Knepley mg->stageApply = 0; 220f3fbd535SBarry Smith for (i=0; i<levels; i++) { 221b00a9115SJed Brown ierr = PetscNewLog(pc,&mglevels[i]);CHKERRQ(ierr); 2222fa5cd67SKarl Rupp 223f3fbd535SBarry Smith mglevels[i]->level = i; 224f3fbd535SBarry Smith mglevels[i]->levels = levels; 22510eca3edSLisandro Dalcin mglevels[i]->cycles = mgctype; 226336babb1SJed Brown mg->default_smoothu = 2; 227336babb1SJed Brown mg->default_smoothd = 2; 22863e6d426SJed Brown mglevels[i]->eventsmoothsetup = 0; 22963e6d426SJed Brown mglevels[i]->eventsmoothsolve = 0; 23063e6d426SJed Brown mglevels[i]->eventresidual = 0; 23163e6d426SJed Brown mglevels[i]->eventinterprestrict = 0; 232f3fbd535SBarry Smith 233f3fbd535SBarry Smith if (comms) comm = comms[i]; 234f3fbd535SBarry Smith ierr = KSPCreate(comm,&mglevels[i]->smoothd);CHKERRQ(ierr); 235422a814eSBarry Smith ierr = KSPSetErrorIfNotConverged(mglevels[i]->smoothd,pc->erroriffailure);CHKERRQ(ierr); 2360ee9a668SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);CHKERRQ(ierr); 2370ee9a668SBarry Smith ierr = KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);CHKERRQ(ierr); 2380ee9a668SBarry Smith ierr = PetscObjectComposedDataSetInt((PetscObject) mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level);CHKERRQ(ierr); 2390ee9a668SBarry Smith if (i || levels == 1) { 2400ee9a668SBarry Smith char tprefix[128]; 2410ee9a668SBarry Smith 242336babb1SJed Brown ierr = KSPSetType(mglevels[i]->smoothd,KSPCHEBYSHEV);CHKERRQ(ierr); 2430059c7bdSJed Brown ierr = KSPSetConvergenceTest(mglevels[i]->smoothd,KSPConvergedSkip,NULL,NULL);CHKERRQ(ierr); 244336babb1SJed Brown ierr = KSPSetNormType(mglevels[i]->smoothd,KSP_NORM_NONE);CHKERRQ(ierr); 245336babb1SJed Brown ierr = KSPGetPC(mglevels[i]->smoothd,&ipc);CHKERRQ(ierr); 246336babb1SJed Brown ierr = PCSetType(ipc,PCSOR);CHKERRQ(ierr); 2470ee9a668SBarry Smith ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mg->default_smoothd);CHKERRQ(ierr); 248f3fbd535SBarry Smith 2490ee9a668SBarry Smith sprintf(tprefix,"mg_levels_%d_",(int)i); 2500ee9a668SBarry Smith ierr = KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);CHKERRQ(ierr); 2510ee9a668SBarry Smith } else { 252f3fbd535SBarry Smith ierr = KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");CHKERRQ(ierr); 253f3fbd535SBarry Smith 2549dbfc187SHong Zhang /* coarse solve is (redundant) LU by default; set shifttype NONZERO to avoid annoying zero-pivot in LU preconditioner */ 255f3fbd535SBarry Smith ierr = KSPSetType(mglevels[0]->smoothd,KSPPREONLY);CHKERRQ(ierr); 256f3fbd535SBarry Smith ierr = KSPGetPC(mglevels[0]->smoothd,&ipc);CHKERRQ(ierr); 257f3fbd535SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 258f3fbd535SBarry Smith if (size > 1) { 259f3fbd535SBarry Smith ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr); 260f3fbd535SBarry Smith } else { 261f3fbd535SBarry Smith ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr); 262f3fbd535SBarry Smith } 263753b7fb9SBarry Smith ierr = PCFactorSetShiftType(ipc,MAT_SHIFT_INBLOCKS);CHKERRQ(ierr); 264f3fbd535SBarry Smith } 2653bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[i]->smoothd);CHKERRQ(ierr); 2662fa5cd67SKarl Rupp 267f3fbd535SBarry Smith mglevels[i]->smoothu = mglevels[i]->smoothd; 26831567311SBarry Smith mg->rtol = 0.0; 26931567311SBarry Smith mg->abstol = 0.0; 27031567311SBarry Smith mg->dtol = 0.0; 27131567311SBarry Smith mg->ttol = 0.0; 27231567311SBarry Smith mg->cyclesperpcapply = 1; 273f3fbd535SBarry Smith } 274f3fbd535SBarry Smith mg->levels = mglevels; 27510eca3edSLisandro Dalcin ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr); 2764b9ad928SBarry Smith PetscFunctionReturn(0); 2774b9ad928SBarry Smith } 2784b9ad928SBarry Smith 279c07bf074SBarry Smith 280c07bf074SBarry Smith #undef __FUNCT__ 281c07bf074SBarry Smith #define __FUNCT__ "PCDestroy_MG" 282c07bf074SBarry Smith PetscErrorCode PCDestroy_MG(PC pc) 283c07bf074SBarry Smith { 284c07bf074SBarry Smith PetscErrorCode ierr; 285a06653b4SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 286a06653b4SBarry Smith PC_MG_Levels **mglevels = mg->levels; 287a06653b4SBarry Smith PetscInt i,n; 288c07bf074SBarry Smith 289c07bf074SBarry Smith PetscFunctionBegin; 290a06653b4SBarry Smith ierr = PCReset_MG(pc);CHKERRQ(ierr); 291a06653b4SBarry Smith if (mglevels) { 292a06653b4SBarry Smith n = mglevels[0]->levels; 293a06653b4SBarry Smith for (i=0; i<n; i++) { 294a06653b4SBarry Smith if (mglevels[i]->smoothd != mglevels[i]->smoothu) { 2956bf464f9SBarry Smith ierr = KSPDestroy(&mglevels[i]->smoothd);CHKERRQ(ierr); 296a06653b4SBarry Smith } 2976bf464f9SBarry Smith ierr = KSPDestroy(&mglevels[i]->smoothu);CHKERRQ(ierr); 298a06653b4SBarry Smith ierr = PetscFree(mglevels[i]);CHKERRQ(ierr); 299a06653b4SBarry Smith } 300a06653b4SBarry Smith ierr = PetscFree(mg->levels);CHKERRQ(ierr); 301a06653b4SBarry Smith } 302c31cb41cSBarry Smith ierr = PetscFree(pc->data);CHKERRQ(ierr); 303f3fbd535SBarry Smith PetscFunctionReturn(0); 304f3fbd535SBarry Smith } 305f3fbd535SBarry Smith 306f3fbd535SBarry Smith 307f3fbd535SBarry Smith 30809573ac7SBarry Smith extern PetscErrorCode PCMGACycle_Private(PC,PC_MG_Levels**); 30909573ac7SBarry Smith extern PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**); 31009573ac7SBarry Smith extern PetscErrorCode PCMGKCycle_Private(PC,PC_MG_Levels**); 311f3fbd535SBarry Smith 312f3fbd535SBarry Smith /* 313f3fbd535SBarry Smith PCApply_MG - Runs either an additive, multiplicative, Kaskadic 314f3fbd535SBarry Smith or full cycle of multigrid. 315f3fbd535SBarry Smith 316f3fbd535SBarry Smith Note: 317f3fbd535SBarry Smith A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle(). 318f3fbd535SBarry Smith */ 319f3fbd535SBarry Smith #undef __FUNCT__ 320f3fbd535SBarry Smith #define __FUNCT__ "PCApply_MG" 321f3fbd535SBarry Smith static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x) 322f3fbd535SBarry Smith { 323f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 324f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 325f3fbd535SBarry Smith PetscErrorCode ierr; 326f3fbd535SBarry Smith PetscInt levels = mglevels[0]->levels,i; 327f3fbd535SBarry Smith 328f3fbd535SBarry Smith PetscFunctionBegin; 329a9db87a2SMatthew G Knepley if (mg->stageApply) {ierr = PetscLogStagePush(mg->stageApply);CHKERRQ(ierr);} 330e1d8e5deSBarry Smith /* When the DM is supplying the matrix then it will not exist until here */ 331298cc208SBarry Smith for (i=0; i<levels; i++) { 332e1d8e5deSBarry Smith if (!mglevels[i]->A) { 33323ee1639SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);CHKERRQ(ierr); 334298cc208SBarry Smith ierr = PetscObjectReference((PetscObject)mglevels[i]->A);CHKERRQ(ierr); 335e1d8e5deSBarry Smith } 336e1d8e5deSBarry Smith } 337e1d8e5deSBarry Smith 338f3fbd535SBarry Smith mglevels[levels-1]->b = b; 339f3fbd535SBarry Smith mglevels[levels-1]->x = x; 34031567311SBarry Smith if (mg->am == PC_MG_MULTIPLICATIVE) { 341f3fbd535SBarry Smith ierr = VecSet(x,0.0);CHKERRQ(ierr); 34231567311SBarry Smith for (i=0; i<mg->cyclesperpcapply; i++) { 3430298fd71SBarry Smith ierr = PCMGMCycle_Private(pc,mglevels+levels-1,NULL);CHKERRQ(ierr); 344f3fbd535SBarry Smith } 3452fa5cd67SKarl Rupp } else if (mg->am == PC_MG_ADDITIVE) { 34631567311SBarry Smith ierr = PCMGACycle_Private(pc,mglevels);CHKERRQ(ierr); 3472fa5cd67SKarl Rupp } else if (mg->am == PC_MG_KASKADE) { 34831567311SBarry Smith ierr = PCMGKCycle_Private(pc,mglevels);CHKERRQ(ierr); 3492fa5cd67SKarl Rupp } else { 350f3fbd535SBarry Smith ierr = PCMGFCycle_Private(pc,mglevels);CHKERRQ(ierr); 351f3fbd535SBarry Smith } 352a9db87a2SMatthew G Knepley if (mg->stageApply) {ierr = PetscLogStagePop();CHKERRQ(ierr);} 353f3fbd535SBarry Smith PetscFunctionReturn(0); 354f3fbd535SBarry Smith } 355f3fbd535SBarry Smith 356f3fbd535SBarry Smith 357f3fbd535SBarry Smith #undef __FUNCT__ 358f3fbd535SBarry Smith #define __FUNCT__ "PCSetFromOptions_MG" 3594416b707SBarry Smith PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC pc) 360f3fbd535SBarry Smith { 361f3fbd535SBarry Smith PetscErrorCode ierr; 362f3fbd535SBarry Smith PetscInt m,levels = 1,cycles; 363*2134b1e4SBarry Smith PetscBool flg; 364f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 3653d3eaba7SBarry Smith PC_MG_Levels **mglevels; 366f3fbd535SBarry Smith PCMGType mgtype; 367f3fbd535SBarry Smith PCMGCycleType mgctype; 368*2134b1e4SBarry Smith PCMGGalerkinType gtype; 369f3fbd535SBarry Smith 370f3fbd535SBarry Smith PetscFunctionBegin; 371e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"Multigrid options");CHKERRQ(ierr); 3723aeaf226SBarry Smith if (!mg->levels) { 373f3fbd535SBarry Smith ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);CHKERRQ(ierr); 374eb3f98d2SBarry Smith if (!flg && pc->dm) { 375eb3f98d2SBarry Smith ierr = DMGetRefineLevel(pc->dm,&levels);CHKERRQ(ierr); 376eb3f98d2SBarry Smith levels++; 3773aeaf226SBarry Smith mg->usedmfornumberoflevels = PETSC_TRUE; 378eb3f98d2SBarry Smith } 3790298fd71SBarry Smith ierr = PCMGSetLevels(pc,levels,NULL);CHKERRQ(ierr); 380f3fbd535SBarry Smith } 3813aeaf226SBarry Smith mglevels = mg->levels; 3823aeaf226SBarry Smith 383f3fbd535SBarry Smith mgctype = (PCMGCycleType) mglevels[0]->cycles; 384f3fbd535SBarry Smith ierr = PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);CHKERRQ(ierr); 385f3fbd535SBarry Smith if (flg) { 386f3fbd535SBarry Smith ierr = PCMGSetCycleType(pc,mgctype);CHKERRQ(ierr); 3872fa5cd67SKarl Rupp } 388*2134b1e4SBarry Smith gtype = mg->galerkin; 389*2134b1e4SBarry Smith ierr = PetscOptionsEnum("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",PCMGGalerkinTypes,(PetscEnum)gtype,(PetscEnum*)>ype,&flg);CHKERRQ(ierr); 390*2134b1e4SBarry Smith if (flg) { 391*2134b1e4SBarry Smith ierr = PCMGSetGalerkin(pc,gtype);CHKERRQ(ierr); 392f3fbd535SBarry Smith } 39394ae4db5SBarry Smith ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",mg->default_smoothu,&m,&flg);CHKERRQ(ierr); 394f3fbd535SBarry Smith if (flg) { 395f3fbd535SBarry Smith ierr = PCMGSetNumberSmoothUp(pc,m);CHKERRQ(ierr); 396f3fbd535SBarry Smith } 39794ae4db5SBarry Smith ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",mg->default_smoothd,&m,&flg);CHKERRQ(ierr); 398f3fbd535SBarry Smith if (flg) { 399f3fbd535SBarry Smith ierr = PCMGSetNumberSmoothDown(pc,m);CHKERRQ(ierr); 400f3fbd535SBarry Smith } 40131567311SBarry Smith mgtype = mg->am; 402f3fbd535SBarry Smith ierr = PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);CHKERRQ(ierr); 403f3fbd535SBarry Smith if (flg) { 404f3fbd535SBarry Smith ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr); 405f3fbd535SBarry Smith } 40631567311SBarry Smith if (mg->am == PC_MG_MULTIPLICATIVE) { 4075363c1e0SLisandro Dalcin ierr = PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGMultiplicativeSetCycles",mg->cyclesperpcapply,&cycles,&flg);CHKERRQ(ierr); 408f3fbd535SBarry Smith if (flg) { 409f3fbd535SBarry Smith ierr = PCMGMultiplicativeSetCycles(pc,cycles);CHKERRQ(ierr); 410f3fbd535SBarry Smith } 411f3fbd535SBarry Smith } 412f3fbd535SBarry Smith flg = PETSC_FALSE; 4130298fd71SBarry Smith ierr = PetscOptionsBool("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,NULL);CHKERRQ(ierr); 414f3fbd535SBarry Smith if (flg) { 415f3fbd535SBarry Smith PetscInt i; 416f3fbd535SBarry Smith char eventname[128]; 417ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 418f3fbd535SBarry Smith levels = mglevels[0]->levels; 419f3fbd535SBarry Smith for (i=0; i<levels; i++) { 420f3fbd535SBarry Smith sprintf(eventname,"MGSetup Level %d",(int)i); 42163e6d426SJed Brown ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsetup);CHKERRQ(ierr); 422f3fbd535SBarry Smith sprintf(eventname,"MGSmooth Level %d",(int)i); 42363e6d426SJed Brown ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsolve);CHKERRQ(ierr); 424f3fbd535SBarry Smith if (i) { 425f3fbd535SBarry Smith sprintf(eventname,"MGResid Level %d",(int)i); 42663e6d426SJed Brown ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventresidual);CHKERRQ(ierr); 427f3fbd535SBarry Smith sprintf(eventname,"MGInterp Level %d",(int)i); 42863e6d426SJed Brown ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventinterprestrict);CHKERRQ(ierr); 429f3fbd535SBarry Smith } 430f3fbd535SBarry Smith } 431eec35531SMatthew G Knepley 432ec60ca73SSatish Balay #if defined(PETSC_USE_LOG) 433eec35531SMatthew G Knepley { 434eec35531SMatthew G Knepley const char *sname = "MG Apply"; 435eec35531SMatthew G Knepley PetscStageLog stageLog; 436eec35531SMatthew G Knepley PetscInt st; 437eec35531SMatthew G Knepley 438eec35531SMatthew G Knepley PetscFunctionBegin; 439eec35531SMatthew G Knepley ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr); 440eec35531SMatthew G Knepley for (st = 0; st < stageLog->numStages; ++st) { 441eec35531SMatthew G Knepley PetscBool same; 442eec35531SMatthew G Knepley 443eec35531SMatthew G Knepley ierr = PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);CHKERRQ(ierr); 4442fa5cd67SKarl Rupp if (same) mg->stageApply = st; 445eec35531SMatthew G Knepley } 446eec35531SMatthew G Knepley if (!mg->stageApply) { 447eec35531SMatthew G Knepley ierr = PetscLogStageRegister(sname, &mg->stageApply);CHKERRQ(ierr); 448eec35531SMatthew G Knepley } 449eec35531SMatthew G Knepley } 450ec60ca73SSatish Balay #endif 451f3fbd535SBarry Smith } 452f3fbd535SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 453f3fbd535SBarry Smith PetscFunctionReturn(0); 454f3fbd535SBarry Smith } 455f3fbd535SBarry Smith 4566a6fc655SJed Brown const char *const PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0}; 4576a6fc655SJed Brown const char *const PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0}; 458*2134b1e4SBarry Smith const char *const PCMGGalerkinTypes[] = {"both","pmat","mat","none","external","PCMGGalerkinType","PC_MG_GALERKIN",0}; 459f3fbd535SBarry Smith 4609804daf3SBarry Smith #include <petscdraw.h> 461f3fbd535SBarry Smith #undef __FUNCT__ 462f3fbd535SBarry Smith #define __FUNCT__ "PCView_MG" 463f3fbd535SBarry Smith PetscErrorCode PCView_MG(PC pc,PetscViewer viewer) 464f3fbd535SBarry Smith { 465f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 466f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 467f3fbd535SBarry Smith PetscErrorCode ierr; 468e3deeeafSJed Brown PetscInt levels = mglevels ? mglevels[0]->levels : 0,i; 469219b1045SBarry Smith PetscBool iascii,isbinary,isdraw; 470f3fbd535SBarry Smith 471f3fbd535SBarry Smith PetscFunctionBegin; 472251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 4735b0b0462SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 474219b1045SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 475f3fbd535SBarry Smith if (iascii) { 476e3deeeafSJed Brown const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown"; 477e3deeeafSJed Brown ierr = PetscViewerASCIIPrintf(viewer," MG: type is %s, levels=%D cycles=%s\n", PCMGTypes[mg->am],levels,cyclename);CHKERRQ(ierr); 47831567311SBarry Smith if (mg->am == PC_MG_MULTIPLICATIVE) { 47931567311SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Cycles per PCApply=%d\n",mg->cyclesperpcapply);CHKERRQ(ierr); 480f3fbd535SBarry Smith } 481*2134b1e4SBarry Smith if (mg->galerkin == PC_MG_GALERKIN_BOTH) { 482f3fbd535SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr); 483*2134b1e4SBarry Smith } else if (mg->galerkin == PC_MG_GALERKIN_PMAT) { 484*2134b1e4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using Galerkin computed coarse grid matrices for pmat\n");CHKERRQ(ierr); 485*2134b1e4SBarry Smith } else if (mg->galerkin == PC_MG_GALERKIN_MAT) { 486*2134b1e4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using Galerkin computed coarse grid matrices for mat\n");CHKERRQ(ierr); 487*2134b1e4SBarry Smith } else if (mg->galerkin == PC_MG_GALERKIN_EXTERNAL) { 488*2134b1e4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Using externally compute Galerkin coarse grid matrices\n");CHKERRQ(ierr); 4894f66f45eSBarry Smith } else { 4904f66f45eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Not using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr); 491f3fbd535SBarry Smith } 4925adeb434SBarry Smith if (mg->view){ 4935adeb434SBarry Smith ierr = (*mg->view)(pc,viewer);CHKERRQ(ierr); 4945adeb434SBarry Smith } 495f3fbd535SBarry Smith for (i=0; i<levels; i++) { 496f3fbd535SBarry Smith if (!i) { 49789cce641SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level -------------------------------\n",i);CHKERRQ(ierr); 498f3fbd535SBarry Smith } else { 49989cce641SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr); 500f3fbd535SBarry Smith } 501f3fbd535SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 502f3fbd535SBarry Smith ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr); 503f3fbd535SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 504f3fbd535SBarry Smith if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) { 505f3fbd535SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr); 506f3fbd535SBarry Smith } else if (i) { 50789cce641SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr); 508f3fbd535SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 509f3fbd535SBarry Smith ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr); 510f3fbd535SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 511f3fbd535SBarry Smith } 512f3fbd535SBarry Smith } 5135b0b0462SBarry Smith } else if (isbinary) { 5145b0b0462SBarry Smith for (i=levels-1; i>=0; i--) { 5155b0b0462SBarry Smith ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr); 5165b0b0462SBarry Smith if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) { 5175b0b0462SBarry Smith ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr); 5185b0b0462SBarry Smith } 5195b0b0462SBarry Smith } 520219b1045SBarry Smith } else if (isdraw) { 521219b1045SBarry Smith PetscDraw draw; 522b4375e8dSPeter Brune PetscReal x,w,y,bottom,th; 523219b1045SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 524219b1045SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 5250298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,NULL,&th);CHKERRQ(ierr); 526219b1045SBarry Smith bottom = y - th; 527219b1045SBarry Smith for (i=levels-1; i>=0; i--) { 528b4375e8dSPeter Brune if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) { 529219b1045SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 530219b1045SBarry Smith ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr); 531219b1045SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 532b4375e8dSPeter Brune } else { 533b4375e8dSPeter Brune w = 0.5*PetscMin(1.0-x,x); 534b4375e8dSPeter Brune ierr = PetscDrawPushCurrentPoint(draw,x+w,bottom);CHKERRQ(ierr); 535b4375e8dSPeter Brune ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr); 536b4375e8dSPeter Brune ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 537b4375e8dSPeter Brune ierr = PetscDrawPushCurrentPoint(draw,x-w,bottom);CHKERRQ(ierr); 538b4375e8dSPeter Brune ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr); 539b4375e8dSPeter Brune ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 540b4375e8dSPeter Brune } 5410298fd71SBarry Smith ierr = PetscDrawGetBoundingBox(draw,NULL,&bottom,NULL,NULL);CHKERRQ(ierr); 5421cd381d6SBarry Smith bottom -= th; 543219b1045SBarry Smith } 5445b0b0462SBarry Smith } 545f3fbd535SBarry Smith PetscFunctionReturn(0); 546f3fbd535SBarry Smith } 547f3fbd535SBarry Smith 548af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 549af0996ceSBarry Smith #include <petsc/private/kspimpl.h> 550cab2e9ccSBarry Smith 551f3fbd535SBarry Smith /* 552f3fbd535SBarry Smith Calls setup for the KSP on each level 553f3fbd535SBarry Smith */ 554f3fbd535SBarry Smith #undef __FUNCT__ 555f3fbd535SBarry Smith #define __FUNCT__ "PCSetUp_MG" 556f3fbd535SBarry Smith PetscErrorCode PCSetUp_MG(PC pc) 557f3fbd535SBarry Smith { 558f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 559f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 560f3fbd535SBarry Smith PetscErrorCode ierr; 561f3fbd535SBarry Smith PetscInt i,n = mglevels[0]->levels; 56298e04984SBarry Smith PC cpc; 5633db492dfSBarry Smith PetscBool dump = PETSC_FALSE,opsset,use_amat,missinginterpolate = PETSC_FALSE; 564f3fbd535SBarry Smith Mat dA,dB; 565f3fbd535SBarry Smith Vec tvec; 566218a07d4SBarry Smith DM *dms; 567649052a6SBarry Smith PetscViewer viewer = 0; 568*2134b1e4SBarry Smith PetscBool dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE; 569f3fbd535SBarry Smith 570f3fbd535SBarry Smith PetscFunctionBegin; 57101bc414fSDmitry Karpeev /* FIX: Move this to PCSetFromOptions_MG? */ 5723aeaf226SBarry Smith if (mg->usedmfornumberoflevels) { 5733aeaf226SBarry Smith PetscInt levels; 5743aeaf226SBarry Smith ierr = DMGetRefineLevel(pc->dm,&levels);CHKERRQ(ierr); 5753aeaf226SBarry Smith levels++; 5763aeaf226SBarry Smith if (levels > n) { /* the problem is now being solved on a finer grid */ 5770298fd71SBarry Smith ierr = PCMGSetLevels(pc,levels,NULL);CHKERRQ(ierr); 5783aeaf226SBarry Smith n = levels; 5793aeaf226SBarry Smith ierr = PCSetFromOptions(pc);CHKERRQ(ierr); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */ 5803aeaf226SBarry Smith mglevels = mg->levels; 5813aeaf226SBarry Smith } 5823aeaf226SBarry Smith } 58398e04984SBarry Smith ierr = KSPGetPC(mglevels[0]->smoothd,&cpc);CHKERRQ(ierr); 5843aeaf226SBarry Smith 585f3fbd535SBarry Smith 586f3fbd535SBarry Smith /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */ 587f3fbd535SBarry Smith /* so use those from global PC */ 588f3fbd535SBarry Smith /* Is this what we always want? What if user wants to keep old one? */ 5890298fd71SBarry Smith ierr = KSPGetOperatorsSet(mglevels[n-1]->smoothd,NULL,&opsset);CHKERRQ(ierr); 59098e04984SBarry Smith if (opsset) { 59198e04984SBarry Smith Mat mmat; 59223ee1639SBarry Smith ierr = KSPGetOperators(mglevels[n-1]->smoothd,NULL,&mmat);CHKERRQ(ierr); 59398e04984SBarry Smith if (mmat == pc->pmat) opsset = PETSC_FALSE; 59498e04984SBarry Smith } 5954a5f07a5SMark F. Adams 5964a5f07a5SMark F. Adams if (!opsset) { 59771b23a65SMark F. Adams ierr = PCGetUseAmat(pc,&use_amat);CHKERRQ(ierr); 5984a5f07a5SMark F. Adams if(use_amat){ 599f3fbd535SBarry Smith ierr = PetscInfo(pc,"Using outer operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");CHKERRQ(ierr); 60023ee1639SBarry Smith ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat);CHKERRQ(ierr); 601f3fbd535SBarry Smith } 6024a5f07a5SMark F. Adams else { 6034a5f07a5SMark F. Adams ierr = PetscInfo(pc,"Using matrix (pmat) operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");CHKERRQ(ierr); 60423ee1639SBarry Smith ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->pmat,pc->pmat);CHKERRQ(ierr); 6054a5f07a5SMark F. Adams } 6064a5f07a5SMark F. Adams } 607f3fbd535SBarry Smith 6089c7ffb39SBarry Smith for (i=n-1; i>0; i--) { 6099c7ffb39SBarry Smith if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) { 6109c7ffb39SBarry Smith missinginterpolate = PETSC_TRUE; 6119c7ffb39SBarry Smith continue; 6129c7ffb39SBarry Smith } 6139c7ffb39SBarry Smith } 614*2134b1e4SBarry Smith 615*2134b1e4SBarry Smith ierr = KSPGetOperators(mglevels[n-1]->smoothd,&dA,&dB);CHKERRQ(ierr); 616*2134b1e4SBarry Smith if (dA == dB) dAeqdB = PETSC_TRUE; 617*2134b1e4SBarry Smith if ((mg->galerkin == PC_MG_GALERKIN_NONE) || (((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_MAT)) && !dAeqdB)) { 618*2134b1e4SBarry Smith needRestricts = PETSC_TRUE; /* user must compute either mat, pmat, or both so must restrict x to coarser levels */ 619*2134b1e4SBarry Smith } 620*2134b1e4SBarry Smith 621*2134b1e4SBarry Smith 6229c7ffb39SBarry Smith /* 6239c7ffb39SBarry Smith Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS) 6249c7ffb39SBarry Smith Skipping for galerkin==2 (externally managed hierarchy such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs? 6259c7ffb39SBarry Smith */ 626*2134b1e4SBarry Smith if (missinginterpolate && pc->dm && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) { 6272d2b81a6SBarry Smith /* construct the interpolation from the DMs */ 6282e499ae9SBarry Smith Mat p; 62973250ac0SBarry Smith Vec rscale; 630785e854fSJed Brown ierr = PetscMalloc1(n,&dms);CHKERRQ(ierr); 631218a07d4SBarry Smith dms[n-1] = pc->dm; 632ef1267afSMatthew G. Knepley /* Separately create them so we do not get DMKSP interference between levels */ 633ef1267afSMatthew G. Knepley for (i=n-2; i>-1; i--) {ierr = DMCoarsen(dms[i+1],MPI_COMM_NULL,&dms[i]);CHKERRQ(ierr);} 634218a07d4SBarry Smith for (i=n-2; i>-1; i--) { 635942e3340SBarry Smith DMKSP kdm; 6363ad4599aSBarry Smith PetscBool dmhasrestrict; 6373c0c59f3SBarry Smith ierr = KSPSetDM(mglevels[i]->smoothd,dms[i]);CHKERRQ(ierr); 638*2134b1e4SBarry Smith if (!needRestricts) {ierr = KSPSetDMActive(mglevels[i]->smoothd,PETSC_FALSE);CHKERRQ(ierr);} 639942e3340SBarry Smith ierr = DMGetDMKSPWrite(dms[i],&kdm);CHKERRQ(ierr); 640d1a3e677SJed Brown /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take 641d1a3e677SJed Brown * a bitwise OR of computing the matrix, RHS, and initial iterate. */ 6420298fd71SBarry Smith kdm->ops->computerhs = NULL; 6430298fd71SBarry Smith kdm->rhsctx = NULL; 64424c3aa18SBarry Smith if (!mglevels[i+1]->interpolate) { 645e727c939SJed Brown ierr = DMCreateInterpolation(dms[i],dms[i+1],&p,&rscale);CHKERRQ(ierr); 6462d2b81a6SBarry Smith ierr = PCMGSetInterpolation(pc,i+1,p);CHKERRQ(ierr); 64700ac8be1SBarry Smith if (rscale) {ierr = PCMGSetRScale(pc,i+1,rscale);CHKERRQ(ierr);} 64873250ac0SBarry Smith ierr = VecDestroy(&rscale);CHKERRQ(ierr); 6496bf464f9SBarry Smith ierr = MatDestroy(&p);CHKERRQ(ierr); 650218a07d4SBarry Smith } 6513ad4599aSBarry Smith ierr = DMHasCreateRestriction(dms[i],&dmhasrestrict);CHKERRQ(ierr); 6523ad4599aSBarry Smith if (dmhasrestrict && !mglevels[i+1]->restrct){ 6533ad4599aSBarry Smith ierr = DMCreateRestriction(dms[i],dms[i+1],&p);CHKERRQ(ierr); 6543ad4599aSBarry Smith ierr = PCMGSetRestriction(pc,i+1,p);CHKERRQ(ierr); 6553ad4599aSBarry Smith ierr = MatDestroy(&p);CHKERRQ(ierr); 6563ad4599aSBarry Smith } 65724c3aa18SBarry Smith } 6582d2b81a6SBarry Smith 659ef1267afSMatthew G. Knepley for (i=n-2; i>-1; i--) {ierr = DMDestroy(&dms[i]);CHKERRQ(ierr);} 6602d2b81a6SBarry Smith ierr = PetscFree(dms);CHKERRQ(ierr); 661ce4cda84SJed Brown } 662cab2e9ccSBarry Smith 663ce4cda84SJed Brown if (pc->dm && !pc->setupcalled) { 664*2134b1e4SBarry Smith /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */ 665cab2e9ccSBarry Smith ierr = KSPSetDM(mglevels[n-1]->smoothd,pc->dm);CHKERRQ(ierr); 666cab2e9ccSBarry Smith ierr = KSPSetDMActive(mglevels[n-1]->smoothd,PETSC_FALSE);CHKERRQ(ierr); 667218a07d4SBarry Smith } 668218a07d4SBarry Smith 669*2134b1e4SBarry Smith if (mg->galerkin < PC_MG_GALERKIN_NONE) { 670*2134b1e4SBarry Smith Mat A,B; 671*2134b1e4SBarry Smith PetscBool doA = PETSC_FALSE,doB = PETSC_FALSE; 672*2134b1e4SBarry Smith MatReuse reuse = MAT_INITIAL_MATRIX; 673*2134b1e4SBarry Smith 674*2134b1e4SBarry Smith if ((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_BOTH)) doB = PETSC_TRUE; 675*2134b1e4SBarry Smith if ((mg->galerkin == PC_MG_GALERKIN_MAT) || ((mg->galerkin == PC_MG_GALERKIN_BOTH) && (dA != dB))) doA = PETSC_TRUE; 676*2134b1e4SBarry Smith if (pc->setupcalled) reuse = MAT_REUSE_MATRIX; 677f3fbd535SBarry Smith for (i=n-2; i>-1; i--) { 678b9367914SBarry Smith if (!mglevels[i+1]->restrct && !mglevels[i+1]->interpolate) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must provide interpolation or restriction for each MG level except level 0"); 679b9367914SBarry Smith if (!mglevels[i+1]->interpolate) { 680b9367914SBarry Smith ierr = PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);CHKERRQ(ierr); 681b9367914SBarry Smith } 682b9367914SBarry Smith if (!mglevels[i+1]->restrct) { 683b9367914SBarry Smith ierr = PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);CHKERRQ(ierr); 684b9367914SBarry Smith } 685*2134b1e4SBarry Smith if (reuse == MAT_REUSE_MATRIX) { 686*2134b1e4SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothd,&A,&B);CHKERRQ(ierr); 687*2134b1e4SBarry Smith } 688b9367914SBarry Smith if (mglevels[i+1]->interpolate == mglevels[i+1]->restrct) { 689*2134b1e4SBarry Smith if (doA) { 690*2134b1e4SBarry Smith ierr = MatPtAP(dA,mglevels[i+1]->interpolate,reuse,1.0,&A);CHKERRQ(ierr); 691*2134b1e4SBarry Smith } 692*2134b1e4SBarry Smith if (doB) { 693*2134b1e4SBarry Smith ierr = MatPtAP(dB,mglevels[i+1]->interpolate,reuse,1.0,&B);CHKERRQ(ierr); 694*2134b1e4SBarry Smith } 6957d537d92SJed Brown } else { 696*2134b1e4SBarry Smith if (doA) { 697*2134b1e4SBarry Smith ierr = MatMatMatMult(mglevels[i+1]->restrct,dA,mglevels[i+1]->interpolate,reuse,1.0,&A);CHKERRQ(ierr); 6987d537d92SJed Brown } 699*2134b1e4SBarry Smith if (doB) { 700*2134b1e4SBarry Smith ierr = MatMatMatMult(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,reuse,1.0,&B);CHKERRQ(ierr); 701f3fbd535SBarry Smith } 702b9367914SBarry Smith } 703*2134b1e4SBarry Smith /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */ 704*2134b1e4SBarry Smith if (!doA && dAeqdB) { 705*2134b1e4SBarry Smith if (reuse == MAT_INITIAL_MATRIX) {ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);} 706*2134b1e4SBarry Smith A = B; 707*2134b1e4SBarry Smith } else if (!doA && reuse == MAT_INITIAL_MATRIX ) { 708*2134b1e4SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothd,&A,NULL);CHKERRQ(ierr); 709*2134b1e4SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 710b9367914SBarry Smith } 711*2134b1e4SBarry Smith if (!doB && dAeqdB) { 712*2134b1e4SBarry Smith if (reuse == MAT_INITIAL_MATRIX) {ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);} 713*2134b1e4SBarry Smith B = A; 714*2134b1e4SBarry Smith } else if (!doB && reuse == MAT_INITIAL_MATRIX) { 71523ee1639SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothd,NULL,&B);CHKERRQ(ierr); 716*2134b1e4SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 7177d537d92SJed Brown } 718*2134b1e4SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 719*2134b1e4SBarry Smith ierr = KSPSetOperators(mglevels[i]->smoothd,A,B);CHKERRQ(ierr); 720*2134b1e4SBarry Smith ierr = PetscObjectDereference((PetscObject)A);CHKERRQ(ierr); 721*2134b1e4SBarry Smith ierr = PetscObjectDereference((PetscObject)B);CHKERRQ(ierr); 722*2134b1e4SBarry Smith } 723*2134b1e4SBarry Smith dA = A; 724f3fbd535SBarry Smith dB = B; 725f3fbd535SBarry Smith } 726f3fbd535SBarry Smith } 727*2134b1e4SBarry Smith if (needRestricts && pc->dm && pc->dm->x) { 728cab2e9ccSBarry Smith /* need to restrict Jacobian location to coarser meshes for evaluation */ 729cab2e9ccSBarry Smith for (i=n-2; i>-1; i--) { 730c88c5224SJed Brown Mat R; 731c88c5224SJed Brown Vec rscale; 732cab2e9ccSBarry Smith if (!mglevels[i]->smoothd->dm->x) { 733cab2e9ccSBarry Smith Vec *vecs; 7342a7a6963SBarry Smith ierr = KSPCreateVecs(mglevels[i]->smoothd,1,&vecs,0,NULL);CHKERRQ(ierr); 735cab2e9ccSBarry Smith mglevels[i]->smoothd->dm->x = vecs[0]; 736cab2e9ccSBarry Smith ierr = PetscFree(vecs);CHKERRQ(ierr); 737cab2e9ccSBarry Smith } 738c88c5224SJed Brown ierr = PCMGGetRestriction(pc,i+1,&R);CHKERRQ(ierr); 739c88c5224SJed Brown ierr = PCMGGetRScale(pc,i+1,&rscale);CHKERRQ(ierr); 740c88c5224SJed Brown ierr = MatRestrict(R,mglevels[i+1]->smoothd->dm->x,mglevels[i]->smoothd->dm->x);CHKERRQ(ierr); 741c88c5224SJed Brown ierr = VecPointwiseMult(mglevels[i]->smoothd->dm->x,mglevels[i]->smoothd->dm->x,rscale);CHKERRQ(ierr); 742cab2e9ccSBarry Smith } 743f3fbd535SBarry Smith } 744*2134b1e4SBarry Smith if (needRestricts && pc->dm) { 745caa4e7f2SJed Brown for (i=n-2; i>=0; i--) { 746ccceb50cSJed Brown DM dmfine,dmcoarse; 747caa4e7f2SJed Brown Mat Restrict,Inject; 748caa4e7f2SJed Brown Vec rscale; 749ccceb50cSJed Brown ierr = KSPGetDM(mglevels[i+1]->smoothd,&dmfine);CHKERRQ(ierr); 750ccceb50cSJed Brown ierr = KSPGetDM(mglevels[i]->smoothd,&dmcoarse);CHKERRQ(ierr); 751caa4e7f2SJed Brown ierr = PCMGGetRestriction(pc,i+1,&Restrict);CHKERRQ(ierr); 752caa4e7f2SJed Brown ierr = PCMGGetRScale(pc,i+1,&rscale);CHKERRQ(ierr); 7530298fd71SBarry Smith Inject = NULL; /* Callback should create it if it needs Injection */ 754caa4e7f2SJed Brown ierr = DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);CHKERRQ(ierr); 755caa4e7f2SJed Brown } 756caa4e7f2SJed Brown } 757f3fbd535SBarry Smith 758f3fbd535SBarry Smith if (!pc->setupcalled) { 759f3fbd535SBarry Smith for (i=0; i<n; i++) { 760f3fbd535SBarry Smith ierr = KSPSetFromOptions(mglevels[i]->smoothd);CHKERRQ(ierr); 761f3fbd535SBarry Smith } 762f3fbd535SBarry Smith for (i=1; i<n; i++) { 763f3fbd535SBarry Smith if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) { 764f3fbd535SBarry Smith ierr = KSPSetFromOptions(mglevels[i]->smoothu);CHKERRQ(ierr); 765f3fbd535SBarry Smith } 766f3fbd535SBarry Smith } 7673ad4599aSBarry Smith /* insure that if either interpolation or restriction is set the other other one is set */ 768f3fbd535SBarry Smith for (i=1; i<n; i++) { 7693ad4599aSBarry Smith ierr = PCMGGetInterpolation(pc,i,NULL);CHKERRQ(ierr); 7703ad4599aSBarry Smith ierr = PCMGGetRestriction(pc,i,NULL);CHKERRQ(ierr); 771f3fbd535SBarry Smith } 772f3fbd535SBarry Smith for (i=0; i<n-1; i++) { 773f3fbd535SBarry Smith if (!mglevels[i]->b) { 774f3fbd535SBarry Smith Vec *vec; 7752a7a6963SBarry Smith ierr = KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);CHKERRQ(ierr); 776f3fbd535SBarry Smith ierr = PCMGSetRhs(pc,i,*vec);CHKERRQ(ierr); 7776bf464f9SBarry Smith ierr = VecDestroy(vec);CHKERRQ(ierr); 778f3fbd535SBarry Smith ierr = PetscFree(vec);CHKERRQ(ierr); 779f3fbd535SBarry Smith } 780f3fbd535SBarry Smith if (!mglevels[i]->r && i) { 781f3fbd535SBarry Smith ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr); 782f3fbd535SBarry Smith ierr = PCMGSetR(pc,i,tvec);CHKERRQ(ierr); 7836bf464f9SBarry Smith ierr = VecDestroy(&tvec);CHKERRQ(ierr); 784f3fbd535SBarry Smith } 785f3fbd535SBarry Smith if (!mglevels[i]->x) { 786f3fbd535SBarry Smith ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr); 787f3fbd535SBarry Smith ierr = PCMGSetX(pc,i,tvec);CHKERRQ(ierr); 7886bf464f9SBarry Smith ierr = VecDestroy(&tvec);CHKERRQ(ierr); 789f3fbd535SBarry Smith } 790f3fbd535SBarry Smith } 791f3fbd535SBarry Smith if (n != 1 && !mglevels[n-1]->r) { 792f3fbd535SBarry Smith /* PCMGSetR() on the finest level if user did not supply it */ 793f3fbd535SBarry Smith Vec *vec; 7942a7a6963SBarry Smith ierr = KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);CHKERRQ(ierr); 795f3fbd535SBarry Smith ierr = PCMGSetR(pc,n-1,*vec);CHKERRQ(ierr); 7966bf464f9SBarry Smith ierr = VecDestroy(vec);CHKERRQ(ierr); 797f3fbd535SBarry Smith ierr = PetscFree(vec);CHKERRQ(ierr); 798f3fbd535SBarry Smith } 799f3fbd535SBarry Smith } 800f3fbd535SBarry Smith 80198e04984SBarry Smith if (pc->dm) { 80298e04984SBarry Smith /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */ 80398e04984SBarry Smith for (i=0; i<n-1; i++) { 80498e04984SBarry Smith if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX; 80598e04984SBarry Smith } 80698e04984SBarry Smith } 807f3fbd535SBarry Smith 808f3fbd535SBarry Smith for (i=1; i<n; i++) { 8092a21e185SBarry Smith if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){ 810f3fbd535SBarry Smith /* if doing only down then initial guess is zero */ 811f3fbd535SBarry Smith ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr); 812f3fbd535SBarry Smith } 81363e6d426SJed Brown if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);} 814f3fbd535SBarry Smith ierr = KSPSetUp(mglevels[i]->smoothd);CHKERRQ(ierr); 815899639b0SHong Zhang if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) { 816899639b0SHong Zhang pc->failedreason = PC_SUBPC_ERROR; 817899639b0SHong Zhang } 81863e6d426SJed Brown if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);} 819d42688cbSBarry Smith if (!mglevels[i]->residual) { 820d42688cbSBarry Smith Mat mat; 82123ee1639SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothd,NULL,&mat);CHKERRQ(ierr); 82254b2cd4bSJed Brown ierr = PCMGSetResidual(pc,i,PCMGResidualDefault,mat);CHKERRQ(ierr); 823d42688cbSBarry Smith } 824f3fbd535SBarry Smith } 825f3fbd535SBarry Smith for (i=1; i<n; i++) { 826f3fbd535SBarry Smith if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) { 827f3fbd535SBarry Smith Mat downmat,downpmat; 828f3fbd535SBarry Smith 829f3fbd535SBarry Smith /* check if operators have been set for up, if not use down operators to set them */ 8300298fd71SBarry Smith ierr = KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);CHKERRQ(ierr); 831f3fbd535SBarry Smith if (!opsset) { 83223ee1639SBarry Smith ierr = KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);CHKERRQ(ierr); 83323ee1639SBarry Smith ierr = KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);CHKERRQ(ierr); 834f3fbd535SBarry Smith } 835f3fbd535SBarry Smith 836f3fbd535SBarry Smith ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr); 83763e6d426SJed Brown if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);} 838f3fbd535SBarry Smith ierr = KSPSetUp(mglevels[i]->smoothu);CHKERRQ(ierr); 839899639b0SHong Zhang if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PCSETUP_FAILED) { 840899639b0SHong Zhang pc->failedreason = PC_SUBPC_ERROR; 841899639b0SHong Zhang } 84263e6d426SJed Brown if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);} 843f3fbd535SBarry Smith } 844f3fbd535SBarry Smith } 845f3fbd535SBarry Smith 84663e6d426SJed Brown if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);} 847f3fbd535SBarry Smith ierr = KSPSetUp(mglevels[0]->smoothd);CHKERRQ(ierr); 848899639b0SHong Zhang if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) { 849899639b0SHong Zhang pc->failedreason = PC_SUBPC_ERROR; 850899639b0SHong Zhang } 85163e6d426SJed Brown if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);} 852f3fbd535SBarry Smith 853f3fbd535SBarry Smith /* 854f3fbd535SBarry Smith Dump the interpolation/restriction matrices plus the 855e3c5b3baSBarry Smith Jacobian/stiffness on each level. This allows MATLAB users to 856f3fbd535SBarry Smith easily check if the Galerkin condition A_c = R A_f R^T is satisfied. 857f3fbd535SBarry Smith 858f3fbd535SBarry Smith Only support one or the other at the same time. 859f3fbd535SBarry Smith */ 860f3fbd535SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER) 861c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);CHKERRQ(ierr); 862ce94432eSBarry Smith if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc)); 863f3fbd535SBarry Smith dump = PETSC_FALSE; 864f3fbd535SBarry Smith #endif 865c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);CHKERRQ(ierr); 866ce94432eSBarry Smith if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc)); 867f3fbd535SBarry Smith 868f3fbd535SBarry Smith if (viewer) { 869f3fbd535SBarry Smith for (i=1; i<n; i++) { 870f3fbd535SBarry Smith ierr = MatView(mglevels[i]->restrct,viewer);CHKERRQ(ierr); 871f3fbd535SBarry Smith } 872f3fbd535SBarry Smith for (i=0; i<n; i++) { 873f3fbd535SBarry Smith ierr = KSPGetPC(mglevels[i]->smoothd,&pc);CHKERRQ(ierr); 874f3fbd535SBarry Smith ierr = MatView(pc->mat,viewer);CHKERRQ(ierr); 875f3fbd535SBarry Smith } 876f3fbd535SBarry Smith } 877f3fbd535SBarry Smith PetscFunctionReturn(0); 878f3fbd535SBarry Smith } 879f3fbd535SBarry Smith 880f3fbd535SBarry Smith /* -------------------------------------------------------------------------------------*/ 881f3fbd535SBarry Smith 882f3fbd535SBarry Smith #undef __FUNCT__ 8839dcbbd2bSBarry Smith #define __FUNCT__ "PCMGGetLevels" 8844b9ad928SBarry Smith /*@ 88597177400SBarry Smith PCMGGetLevels - Gets the number of levels to use with MG. 8864b9ad928SBarry Smith 8874b9ad928SBarry Smith Not Collective 8884b9ad928SBarry Smith 8894b9ad928SBarry Smith Input Parameter: 8904b9ad928SBarry Smith . pc - the preconditioner context 8914b9ad928SBarry Smith 8924b9ad928SBarry Smith Output parameter: 8934b9ad928SBarry Smith . levels - the number of levels 8944b9ad928SBarry Smith 8954b9ad928SBarry Smith Level: advanced 8964b9ad928SBarry Smith 8974b9ad928SBarry Smith .keywords: MG, get, levels, multigrid 8984b9ad928SBarry Smith 89997177400SBarry Smith .seealso: PCMGSetLevels() 9004b9ad928SBarry Smith @*/ 9017087cfbeSBarry Smith PetscErrorCode PCMGGetLevels(PC pc,PetscInt *levels) 9024b9ad928SBarry Smith { 903f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 9044b9ad928SBarry Smith 9054b9ad928SBarry Smith PetscFunctionBegin; 9060700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 9074482741eSBarry Smith PetscValidIntPointer(levels,2); 908f3fbd535SBarry Smith *levels = mg->nlevels; 9094b9ad928SBarry Smith PetscFunctionReturn(0); 9104b9ad928SBarry Smith } 9114b9ad928SBarry Smith 9124b9ad928SBarry Smith #undef __FUNCT__ 9139dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetType" 9144b9ad928SBarry Smith /*@ 91597177400SBarry Smith PCMGSetType - Determines the form of multigrid to use: 9164b9ad928SBarry Smith multiplicative, additive, full, or the Kaskade algorithm. 9174b9ad928SBarry Smith 918ad4df100SBarry Smith Logically Collective on PC 9194b9ad928SBarry Smith 9204b9ad928SBarry Smith Input Parameters: 9214b9ad928SBarry Smith + pc - the preconditioner context 9229dcbbd2bSBarry Smith - form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE, 9239dcbbd2bSBarry Smith PC_MG_FULL, PC_MG_KASKADE 9244b9ad928SBarry Smith 9254b9ad928SBarry Smith Options Database Key: 9264b9ad928SBarry Smith . -pc_mg_type <form> - Sets <form>, one of multiplicative, 9274b9ad928SBarry Smith additive, full, kaskade 9284b9ad928SBarry Smith 9294b9ad928SBarry Smith Level: advanced 9304b9ad928SBarry Smith 9314b9ad928SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid 9324b9ad928SBarry Smith 93397177400SBarry Smith .seealso: PCMGSetLevels() 9344b9ad928SBarry Smith @*/ 9357087cfbeSBarry Smith PetscErrorCode PCMGSetType(PC pc,PCMGType form) 9364b9ad928SBarry Smith { 937f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 9384b9ad928SBarry Smith 9394b9ad928SBarry Smith PetscFunctionBegin; 9400700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 941c5eb9154SBarry Smith PetscValidLogicalCollectiveEnum(pc,form,2); 94231567311SBarry Smith mg->am = form; 9439dcbbd2bSBarry Smith if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG; 944c60c7ad4SBarry Smith else pc->ops->applyrichardson = NULL; 945c60c7ad4SBarry Smith PetscFunctionReturn(0); 946c60c7ad4SBarry Smith } 947c60c7ad4SBarry Smith 948f0af28e8SLisandro Dalcin #undef __FUNCT__ 949f0af28e8SLisandro Dalcin #define __FUNCT__ "PCMGGetType" 950c60c7ad4SBarry Smith /*@ 951c60c7ad4SBarry Smith PCMGGetType - Determines the form of multigrid to use: 952c60c7ad4SBarry Smith multiplicative, additive, full, or the Kaskade algorithm. 953c60c7ad4SBarry Smith 954c60c7ad4SBarry Smith Logically Collective on PC 955c60c7ad4SBarry Smith 956c60c7ad4SBarry Smith Input Parameter: 957c60c7ad4SBarry Smith . pc - the preconditioner context 958c60c7ad4SBarry Smith 959c60c7ad4SBarry Smith Output Parameter: 960c60c7ad4SBarry Smith . type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE 961c60c7ad4SBarry Smith 962c60c7ad4SBarry Smith 963c60c7ad4SBarry Smith Level: advanced 964c60c7ad4SBarry Smith 965c60c7ad4SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid 966c60c7ad4SBarry Smith 967c60c7ad4SBarry Smith .seealso: PCMGSetLevels() 968c60c7ad4SBarry Smith @*/ 969c60c7ad4SBarry Smith PetscErrorCode PCMGGetType(PC pc,PCMGType *type) 970c60c7ad4SBarry Smith { 971c60c7ad4SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 972c60c7ad4SBarry Smith 973c60c7ad4SBarry Smith PetscFunctionBegin; 974c60c7ad4SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 975c60c7ad4SBarry Smith *type = mg->am; 9764b9ad928SBarry Smith PetscFunctionReturn(0); 9774b9ad928SBarry Smith } 9784b9ad928SBarry Smith 9794b9ad928SBarry Smith #undef __FUNCT__ 9800d353602SBarry Smith #define __FUNCT__ "PCMGSetCycleType" 9814b9ad928SBarry Smith /*@ 9820d353602SBarry Smith PCMGSetCycleType - Sets the type cycles to use. Use PCMGSetCycleTypeOnLevel() for more 9834b9ad928SBarry Smith complicated cycling. 9844b9ad928SBarry Smith 985ad4df100SBarry Smith Logically Collective on PC 9864b9ad928SBarry Smith 9874b9ad928SBarry Smith Input Parameters: 988c2be2410SBarry Smith + pc - the multigrid context 9890d353602SBarry Smith - PC_MG_CYCLE_V or PC_MG_CYCLE_W 9904b9ad928SBarry Smith 9914b9ad928SBarry Smith Options Database Key: 9924446f3b4SBarry Smith . -pc_mg_cycle_type <v,w> 9934b9ad928SBarry Smith 9944b9ad928SBarry Smith Level: advanced 9954b9ad928SBarry Smith 9964b9ad928SBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid 9974b9ad928SBarry Smith 9980d353602SBarry Smith .seealso: PCMGSetCycleTypeOnLevel() 9994b9ad928SBarry Smith @*/ 10007087cfbeSBarry Smith PetscErrorCode PCMGSetCycleType(PC pc,PCMGCycleType n) 10014b9ad928SBarry Smith { 1002f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 1003f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 100479416396SBarry Smith PetscInt i,levels; 10054b9ad928SBarry Smith 10064b9ad928SBarry Smith PetscFunctionBegin; 10070700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1008ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 100969fbec6eSBarry Smith PetscValidLogicalCollectiveEnum(pc,n,2); 1010f3fbd535SBarry Smith levels = mglevels[0]->levels; 10114b9ad928SBarry Smith 10122fa5cd67SKarl Rupp for (i=0; i<levels; i++) mglevels[i]->cycles = n; 10134b9ad928SBarry Smith PetscFunctionReturn(0); 10144b9ad928SBarry Smith } 10154b9ad928SBarry Smith 10164b9ad928SBarry Smith #undef __FUNCT__ 10178cc2d5dfSBarry Smith #define __FUNCT__ "PCMGMultiplicativeSetCycles" 10188cc2d5dfSBarry Smith /*@ 10198cc2d5dfSBarry Smith PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step 10208cc2d5dfSBarry Smith of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used 10218cc2d5dfSBarry Smith 1022ad4df100SBarry Smith Logically Collective on PC 10238cc2d5dfSBarry Smith 10248cc2d5dfSBarry Smith Input Parameters: 10258cc2d5dfSBarry Smith + pc - the multigrid context 10268cc2d5dfSBarry Smith - n - number of cycles (default is 1) 10278cc2d5dfSBarry Smith 10288cc2d5dfSBarry Smith Options Database Key: 1029e1bc860dSBarry Smith . -pc_mg_multiplicative_cycles n 10308cc2d5dfSBarry Smith 10318cc2d5dfSBarry Smith Level: advanced 10328cc2d5dfSBarry Smith 10338cc2d5dfSBarry Smith Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType() 10348cc2d5dfSBarry Smith 10358cc2d5dfSBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid 10368cc2d5dfSBarry Smith 10378cc2d5dfSBarry Smith .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType() 10388cc2d5dfSBarry Smith @*/ 10397087cfbeSBarry Smith PetscErrorCode PCMGMultiplicativeSetCycles(PC pc,PetscInt n) 10408cc2d5dfSBarry Smith { 1041f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 10428cc2d5dfSBarry Smith 10438cc2d5dfSBarry Smith PetscFunctionBegin; 10440700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1045c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(pc,n,2); 10462a21e185SBarry Smith mg->cyclesperpcapply = n; 10478cc2d5dfSBarry Smith PetscFunctionReturn(0); 10488cc2d5dfSBarry Smith } 10498cc2d5dfSBarry Smith 10508cc2d5dfSBarry Smith #undef __FUNCT__ 1051fb15c04eSBarry Smith #define __FUNCT__ "PCMGSetGalerkin_MG" 1052*2134b1e4SBarry Smith PetscErrorCode PCMGSetGalerkin_MG(PC pc,PCMGGalerkinType use) 1053fb15c04eSBarry Smith { 1054fb15c04eSBarry Smith PC_MG *mg = (PC_MG*)pc->data; 1055fb15c04eSBarry Smith 1056fb15c04eSBarry Smith PetscFunctionBegin; 1057*2134b1e4SBarry Smith mg->galerkin = use; 1058fb15c04eSBarry Smith PetscFunctionReturn(0); 1059fb15c04eSBarry Smith } 1060fb15c04eSBarry Smith 1061fb15c04eSBarry Smith #undef __FUNCT__ 10629dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetGalerkin" 1063c2be2410SBarry Smith /*@ 106497177400SBarry Smith PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the 106582b87d87SMatthew G. Knepley finest grid via the Galerkin process: A_i-1 = r_i * A_i * p_i 1066c2be2410SBarry Smith 1067ad4df100SBarry Smith Logically Collective on PC 1068c2be2410SBarry Smith 1069c2be2410SBarry Smith Input Parameters: 1070c91913d3SJed Brown + pc - the multigrid context 1071*2134b1e4SBarry Smith - use - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, or PC_MG_GALERKIN_NONE 1072c2be2410SBarry Smith 1073c2be2410SBarry Smith Options Database Key: 1074*2134b1e4SBarry Smith . -pc_mg_galerkin <both,pmat,mat,none> 1075c2be2410SBarry Smith 1076c2be2410SBarry Smith Level: intermediate 1077c2be2410SBarry Smith 10781c1aac46SBarry Smith Notes: Some codes that use PCMG such as PCGAMG use Galerkin internally while constructing the hierarchy and thus do not 10791c1aac46SBarry Smith use the PCMG construction of the coarser grids. 10801c1aac46SBarry Smith 1081c2be2410SBarry Smith .keywords: MG, set, Galerkin 1082c2be2410SBarry Smith 1083*2134b1e4SBarry Smith .seealso: PCMGGetGalerkin(), PCMGGalerkinType 10843fc8bf9cSBarry Smith 1085c2be2410SBarry Smith @*/ 1086*2134b1e4SBarry Smith PetscErrorCode PCMGSetGalerkin(PC pc,PCMGGalerkinType use) 1087c2be2410SBarry Smith { 1088fb15c04eSBarry Smith PetscErrorCode ierr; 1089c2be2410SBarry Smith 1090c2be2410SBarry Smith PetscFunctionBegin; 10910700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1092*2134b1e4SBarry Smith ierr = PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PCMGGalerkinType),(pc,use));CHKERRQ(ierr); 1093c2be2410SBarry Smith PetscFunctionReturn(0); 1094c2be2410SBarry Smith } 1095c2be2410SBarry Smith 1096c2be2410SBarry Smith #undef __FUNCT__ 10973fc8bf9cSBarry Smith #define __FUNCT__ "PCMGGetGalerkin" 10983fc8bf9cSBarry Smith /*@ 10993fc8bf9cSBarry Smith PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e. 110082b87d87SMatthew G. Knepley A_i-1 = r_i * A_i * p_i 11013fc8bf9cSBarry Smith 11023fc8bf9cSBarry Smith Not Collective 11033fc8bf9cSBarry Smith 11043fc8bf9cSBarry Smith Input Parameter: 11053fc8bf9cSBarry Smith . pc - the multigrid context 11063fc8bf9cSBarry Smith 11073fc8bf9cSBarry Smith Output Parameter: 1108*2134b1e4SBarry 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 11093fc8bf9cSBarry Smith 11103fc8bf9cSBarry Smith Level: intermediate 11113fc8bf9cSBarry Smith 11123fc8bf9cSBarry Smith .keywords: MG, set, Galerkin 11133fc8bf9cSBarry Smith 1114*2134b1e4SBarry Smith .seealso: PCMGSetGalerkin(), PCMGGalerkinType 11153fc8bf9cSBarry Smith 11163fc8bf9cSBarry Smith @*/ 1117*2134b1e4SBarry Smith PetscErrorCode PCMGGetGalerkin(PC pc,PCMGGalerkinType *galerkin) 11183fc8bf9cSBarry Smith { 1119f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 11203fc8bf9cSBarry Smith 11213fc8bf9cSBarry Smith PetscFunctionBegin; 11220700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1123*2134b1e4SBarry Smith *galerkin = mg->galerkin; 11243fc8bf9cSBarry Smith PetscFunctionReturn(0); 11253fc8bf9cSBarry Smith } 11263fc8bf9cSBarry Smith 11273fc8bf9cSBarry Smith #undef __FUNCT__ 11289dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothDown" 11294b9ad928SBarry Smith /*@ 113097177400SBarry Smith PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to 113197177400SBarry Smith use on all levels. Use PCMGGetSmootherDown() to set different 11324b9ad928SBarry Smith pre-smoothing steps on different levels. 11334b9ad928SBarry Smith 1134ad4df100SBarry Smith Logically Collective on PC 11354b9ad928SBarry Smith 11364b9ad928SBarry Smith Input Parameters: 11374b9ad928SBarry Smith + mg - the multigrid context 11384b9ad928SBarry Smith - n - the number of smoothing steps 11394b9ad928SBarry Smith 11404b9ad928SBarry Smith Options Database Key: 11414b9ad928SBarry Smith . -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps 11424b9ad928SBarry Smith 11434b9ad928SBarry Smith Level: advanced 11444b9ad928SBarry Smith 11454b9ad928SBarry Smith .keywords: MG, smooth, down, pre-smoothing, steps, multigrid 11464b9ad928SBarry Smith 114797177400SBarry Smith .seealso: PCMGSetNumberSmoothUp() 11484b9ad928SBarry Smith @*/ 11497087cfbeSBarry Smith PetscErrorCode PCMGSetNumberSmoothDown(PC pc,PetscInt n) 11504b9ad928SBarry Smith { 1151f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 1152f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 11536849ba73SBarry Smith PetscErrorCode ierr; 115479416396SBarry Smith PetscInt i,levels; 11554b9ad928SBarry Smith 11564b9ad928SBarry Smith PetscFunctionBegin; 11570700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1158ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 1159c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(pc,n,2); 1160f3fbd535SBarry Smith levels = mglevels[0]->levels; 11614b9ad928SBarry Smith 1162b05257ddSBarry Smith for (i=1; i<levels; i++) { 11634b9ad928SBarry Smith /* make sure smoother up and down are different */ 11640298fd71SBarry Smith ierr = PCMGGetSmootherUp(pc,i,NULL);CHKERRQ(ierr); 1165f3fbd535SBarry Smith ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr); 11662fa5cd67SKarl Rupp 116731567311SBarry Smith mg->default_smoothd = n; 11684b9ad928SBarry Smith } 11694b9ad928SBarry Smith PetscFunctionReturn(0); 11704b9ad928SBarry Smith } 11714b9ad928SBarry Smith 11724b9ad928SBarry Smith #undef __FUNCT__ 11739dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothUp" 11744b9ad928SBarry Smith /*@ 117597177400SBarry Smith PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use 117697177400SBarry Smith on all levels. Use PCMGGetSmootherUp() to set different numbers of 11774b9ad928SBarry Smith post-smoothing steps on different levels. 11784b9ad928SBarry Smith 1179ad4df100SBarry Smith Logically Collective on PC 11804b9ad928SBarry Smith 11814b9ad928SBarry Smith Input Parameters: 11824b9ad928SBarry Smith + mg - the multigrid context 11834b9ad928SBarry Smith - n - the number of smoothing steps 11844b9ad928SBarry Smith 11854b9ad928SBarry Smith Options Database Key: 11864b9ad928SBarry Smith . -pc_mg_smoothup <n> - Sets number of post-smoothing steps 11874b9ad928SBarry Smith 11884b9ad928SBarry Smith Level: advanced 11894b9ad928SBarry Smith 11904b9ad928SBarry Smith Note: this does not set a value on the coarsest grid, since we assume that 1191a8c7a070SBarry Smith there is no separate smooth up on the coarsest grid. 11924b9ad928SBarry Smith 11934b9ad928SBarry Smith .keywords: MG, smooth, up, post-smoothing, steps, multigrid 11944b9ad928SBarry Smith 119597177400SBarry Smith .seealso: PCMGSetNumberSmoothDown() 11964b9ad928SBarry Smith @*/ 11977087cfbeSBarry Smith PetscErrorCode PCMGSetNumberSmoothUp(PC pc,PetscInt n) 11984b9ad928SBarry Smith { 1199f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 1200f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 12016849ba73SBarry Smith PetscErrorCode ierr; 120279416396SBarry Smith PetscInt i,levels; 12034b9ad928SBarry Smith 12044b9ad928SBarry Smith PetscFunctionBegin; 12050700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 1206ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 1207c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(pc,n,2); 1208f3fbd535SBarry Smith levels = mglevels[0]->levels; 12094b9ad928SBarry Smith 12104b9ad928SBarry Smith for (i=1; i<levels; i++) { 12114b9ad928SBarry Smith /* make sure smoother up and down are different */ 12120298fd71SBarry Smith ierr = PCMGGetSmootherUp(pc,i,NULL);CHKERRQ(ierr); 1213f3fbd535SBarry Smith ierr = KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr); 12142fa5cd67SKarl Rupp 121531567311SBarry Smith mg->default_smoothu = n; 12164b9ad928SBarry Smith } 12174b9ad928SBarry Smith PetscFunctionReturn(0); 12184b9ad928SBarry Smith } 12194b9ad928SBarry Smith 12204b9ad928SBarry Smith /* ----------------------------------------------------------------------------------------*/ 12214b9ad928SBarry Smith 12223b09bd56SBarry Smith /*MC 1223ccb205f8SBarry Smith PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional 12243b09bd56SBarry Smith information about the coarser grid matrices and restriction/interpolation operators. 12253b09bd56SBarry Smith 12263b09bd56SBarry Smith Options Database Keys: 12273b09bd56SBarry Smith + -pc_mg_levels <nlevels> - number of levels including finest 12284afba7b0SPatrick Sanan . -pc_mg_cycle_type <v,w> - 122979416396SBarry Smith . -pc_mg_smoothup <n> - number of smoothing steps after interpolation 12303b09bd56SBarry Smith . -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator 12318c1c2452SJed Brown . -pc_mg_type <additive,multiplicative,full,kaskade> - multiplicative is the default 12323b09bd56SBarry Smith . -pc_mg_log - log information about time spent on each level of the solver 1233*2134b1e4SBarry Smith . -pc_mg_galerkin <both,pmat,mat,none> - use Galerkin process to compute coarser operators, i.e. Acoarse = R A R' 12348cf6037eSBarry Smith . -pc_mg_multiplicative_cycles - number of cycles to use as the preconditioner (defaults to 1) 12358cf6037eSBarry Smith . -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices 1236e3c5b3baSBarry Smith to the Socket viewer for reading from MATLAB. 12378cf6037eSBarry Smith - -pc_mg_dump_binary - dumps the matrices for each level and the restriction/interpolation matrices 12388cf6037eSBarry Smith to the binary output file called binaryoutput 12393b09bd56SBarry Smith 1240e941fc33SBarry Smith Notes: If one uses a Krylov method such GMRES or CG as the smoother than one must use KSPFGMRES, KSPGCG, or KSPRICHARDSON as the outer Krylov method 12413b09bd56SBarry Smith 12428cf6037eSBarry Smith When run with a single level the smoother options are used on that level NOT the coarse grid solver options 12438cf6037eSBarry Smith 12443b09bd56SBarry Smith Level: intermediate 12453b09bd56SBarry Smith 12468f87f92bSBarry Smith Concepts: multigrid/multilevel 12473b09bd56SBarry Smith 12488cf6037eSBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE 12490d353602SBarry Smith PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(), 125097177400SBarry Smith PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(), 125197177400SBarry Smith PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(), 12520d353602SBarry Smith PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR() 12533b09bd56SBarry Smith M*/ 12543b09bd56SBarry Smith 12554b9ad928SBarry Smith #undef __FUNCT__ 12564b9ad928SBarry Smith #define __FUNCT__ "PCCreate_MG" 12578cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc) 12584b9ad928SBarry Smith { 1259f3fbd535SBarry Smith PC_MG *mg; 1260f3fbd535SBarry Smith PetscErrorCode ierr; 1261f3fbd535SBarry Smith 12624b9ad928SBarry Smith PetscFunctionBegin; 1263b00a9115SJed Brown ierr = PetscNewLog(pc,&mg);CHKERRQ(ierr); 1264f3fbd535SBarry Smith pc->data = (void*)mg; 1265f3fbd535SBarry Smith mg->nlevels = -1; 126610eca3edSLisandro Dalcin mg->am = PC_MG_MULTIPLICATIVE; 1267*2134b1e4SBarry Smith mg->galerkin = PC_MG_GALERKIN_NONE; 1268f3fbd535SBarry Smith 126937a44384SMark Adams pc->useAmat = PETSC_TRUE; 127037a44384SMark Adams 12714b9ad928SBarry Smith pc->ops->apply = PCApply_MG; 12724b9ad928SBarry Smith pc->ops->setup = PCSetUp_MG; 1273a06653b4SBarry Smith pc->ops->reset = PCReset_MG; 12744b9ad928SBarry Smith pc->ops->destroy = PCDestroy_MG; 12754b9ad928SBarry Smith pc->ops->setfromoptions = PCSetFromOptions_MG; 12764b9ad928SBarry Smith pc->ops->view = PCView_MG; 1277fb15c04eSBarry Smith 1278fb15c04eSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);CHKERRQ(ierr); 12794b9ad928SBarry Smith PetscFunctionReturn(0); 12804b9ad928SBarry Smith } 1281