14b9ad928SBarry Smith 2c6db04a5SJed Brown #include <../src/ksp/pc/impls/mg/mgimpl.h> /*I "petscksp.h" I*/ 34b9ad928SBarry Smith 4*f9426fe0SMark Adams /* ---------------------------------------------------------------------------*/ 54b9ad928SBarry Smith #undef __FUNCT__ 654b2cd4bSJed Brown #define __FUNCT__ "PCMGResidualDefault" 71f6cc5b2SSatish Balay /*@C 854b2cd4bSJed Brown PCMGResidualDefault - Default routine to calculate the residual. 94b9ad928SBarry Smith 104b9ad928SBarry Smith Collective on Mat and Vec 114b9ad928SBarry Smith 124b9ad928SBarry Smith Input Parameters: 134b9ad928SBarry Smith + mat - the matrix 144b9ad928SBarry Smith . b - the right-hand-side 154b9ad928SBarry Smith - x - the approximate solution 164b9ad928SBarry Smith 174b9ad928SBarry Smith Output Parameter: 184b9ad928SBarry Smith . r - location to store the residual 194b9ad928SBarry Smith 20d0e4de75SBarry Smith Level: developer 214b9ad928SBarry Smith 224b9ad928SBarry Smith .keywords: MG, default, multigrid, residual 234b9ad928SBarry Smith 2497177400SBarry Smith .seealso: PCMGSetResidual() 254b9ad928SBarry Smith @*/ 2654b2cd4bSJed Brown PetscErrorCode PCMGResidualDefault(Mat mat,Vec b,Vec x,Vec r) 274b9ad928SBarry Smith { 28dfbe8321SBarry Smith PetscErrorCode ierr; 294b9ad928SBarry Smith 304b9ad928SBarry Smith PetscFunctionBegin; 31*f9426fe0SMark Adams ierr = MatResidual(mat,b,x,r);CHKERRQ(ierr); 324b9ad928SBarry Smith PetscFunctionReturn(0); 334b9ad928SBarry Smith } 344b9ad928SBarry Smith 354b9ad928SBarry Smith #undef __FUNCT__ 36d6337fedSHong Zhang #define __FUNCT__ "PCMGGetCoarseSolve" 37f39d8e23SSatish Balay /*@ 3897177400SBarry Smith PCMGGetCoarseSolve - Gets the solver context to be used on the coarse grid. 394b9ad928SBarry Smith 404b9ad928SBarry Smith Not Collective 414b9ad928SBarry Smith 424b9ad928SBarry Smith Input Parameter: 434b9ad928SBarry Smith . pc - the multigrid context 444b9ad928SBarry Smith 454b9ad928SBarry Smith Output Parameter: 464b9ad928SBarry Smith . ksp - the coarse grid solver context 474b9ad928SBarry Smith 484b9ad928SBarry Smith Level: advanced 494b9ad928SBarry Smith 504b9ad928SBarry Smith .keywords: MG, multigrid, get, coarse grid 514b9ad928SBarry Smith @*/ 527087cfbeSBarry Smith PetscErrorCode PCMGGetCoarseSolve(PC pc,KSP *ksp) 534b9ad928SBarry Smith { 54f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 55f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 564b9ad928SBarry Smith 574b9ad928SBarry Smith PetscFunctionBegin; 58c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 59f3fbd535SBarry Smith *ksp = mglevels[0]->smoothd; 604b9ad928SBarry Smith PetscFunctionReturn(0); 614b9ad928SBarry Smith } 624b9ad928SBarry Smith 634b9ad928SBarry Smith #undef __FUNCT__ 64d6337fedSHong Zhang #define __FUNCT__ "PCMGSetResidual" 654b9ad928SBarry Smith /*@C 6697177400SBarry Smith PCMGSetResidual - Sets the function to be used to calculate the residual 674b9ad928SBarry Smith on the lth level. 684b9ad928SBarry Smith 69ad4df100SBarry Smith Logically Collective on PC and Mat 704b9ad928SBarry Smith 714b9ad928SBarry Smith Input Parameters: 724b9ad928SBarry Smith + pc - the multigrid context 734b9ad928SBarry Smith . l - the level (0 is coarsest) to supply 74157726a2SBarry Smith . residual - function used to form residual, if none is provided the previously provide one is used, if no 75d0e4de75SBarry Smith previous one were provided then a default is used 764b9ad928SBarry Smith - mat - matrix associated with residual 774b9ad928SBarry Smith 784b9ad928SBarry Smith Level: advanced 794b9ad928SBarry Smith 804b9ad928SBarry Smith .keywords: MG, set, multigrid, residual, level 814b9ad928SBarry Smith 8254b2cd4bSJed Brown .seealso: PCMGResidualDefault() 834b9ad928SBarry Smith @*/ 847087cfbeSBarry Smith PetscErrorCode PCMGSetResidual(PC pc,PetscInt l,PetscErrorCode (*residual)(Mat,Vec,Vec,Vec),Mat mat) 854b9ad928SBarry Smith { 86f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 87f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 88298cc208SBarry Smith PetscErrorCode ierr; 894b9ad928SBarry Smith 904b9ad928SBarry Smith PetscFunctionBegin; 91c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 92ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 932fa5cd67SKarl Rupp if (residual) mglevels[l]->residual = residual; 9454b2cd4bSJed Brown if (!mglevels[l]->residual) mglevels[l]->residual = PCMGResidualDefault; 95f3ae41bdSBarry Smith if (mat) {ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);} 96298cc208SBarry Smith ierr = MatDestroy(&mglevels[l]->A);CHKERRQ(ierr); 97f3fbd535SBarry Smith mglevels[l]->A = mat; 984b9ad928SBarry Smith PetscFunctionReturn(0); 994b9ad928SBarry Smith } 1004b9ad928SBarry Smith 1014b9ad928SBarry Smith #undef __FUNCT__ 102d6337fedSHong Zhang #define __FUNCT__ "PCMGSetInterpolation" 1034b9ad928SBarry Smith /*@ 104aea2a34eSBarry Smith PCMGSetInterpolation - Sets the function to be used to calculate the 105bf5b2e24SBarry Smith interpolation from l-1 to the lth level 1064b9ad928SBarry Smith 107ad4df100SBarry Smith Logically Collective on PC and Mat 1084b9ad928SBarry Smith 1094b9ad928SBarry Smith Input Parameters: 1104b9ad928SBarry Smith + pc - the multigrid context 1114b9ad928SBarry Smith . mat - the interpolation operator 112bf5b2e24SBarry Smith - l - the level (0 is coarsest) to supply [do not supply 0] 1134b9ad928SBarry Smith 1144b9ad928SBarry Smith Level: advanced 1154b9ad928SBarry Smith 1164b9ad928SBarry Smith Notes: 1174b9ad928SBarry Smith Usually this is the same matrix used also to set the restriction 1184b9ad928SBarry Smith for the same level. 1194b9ad928SBarry Smith 1204b9ad928SBarry Smith One can pass in the interpolation matrix or its transpose; PETSc figures 1214b9ad928SBarry Smith out from the matrix size which one it is. 1224b9ad928SBarry Smith 1234b9ad928SBarry Smith .keywords: multigrid, set, interpolate, level 1244b9ad928SBarry Smith 12597177400SBarry Smith .seealso: PCMGSetRestriction() 1264b9ad928SBarry Smith @*/ 1277087cfbeSBarry Smith PetscErrorCode PCMGSetInterpolation(PC pc,PetscInt l,Mat mat) 1284b9ad928SBarry Smith { 129f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 130f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 131fccaa45eSBarry Smith PetscErrorCode ierr; 1324b9ad928SBarry Smith 1334b9ad928SBarry Smith PetscFunctionBegin; 134c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 135ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 136ce94432eSBarry Smith if (!l) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Do not set interpolation routine for coarsest level"); 137c3122656SLisandro Dalcin ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 1386bf464f9SBarry Smith ierr = MatDestroy(&mglevels[l]->interpolate);CHKERRQ(ierr); 1392fa5cd67SKarl Rupp 140f3fbd535SBarry Smith mglevels[l]->interpolate = mat; 1414b9ad928SBarry Smith PetscFunctionReturn(0); 1424b9ad928SBarry Smith } 1434b9ad928SBarry Smith 1444b9ad928SBarry Smith #undef __FUNCT__ 145c88c5224SJed Brown #define __FUNCT__ "PCMGGetInterpolation" 146c88c5224SJed Brown /*@ 147c88c5224SJed Brown PCMGGetInterpolation - Gets the function to be used to calculate the 148c88c5224SJed Brown interpolation from l-1 to the lth level 149c88c5224SJed Brown 150c88c5224SJed Brown Logically Collective on PC 151c88c5224SJed Brown 152c88c5224SJed Brown Input Parameters: 153c88c5224SJed Brown + pc - the multigrid context 154c88c5224SJed Brown - l - the level (0 is coarsest) to supply [Do not supply 0] 155c88c5224SJed Brown 156c88c5224SJed Brown Output Parameter: 157c88c5224SJed Brown . mat - the interpolation matrix 158c88c5224SJed Brown 159c88c5224SJed Brown Level: advanced 160c88c5224SJed Brown 161c88c5224SJed Brown .keywords: MG, get, multigrid, interpolation, level 162c88c5224SJed Brown 163c88c5224SJed Brown .seealso: PCMGGetRestriction(), PCMGSetInterpolation(), PCMGGetRScale() 164c88c5224SJed Brown @*/ 165c88c5224SJed Brown PetscErrorCode PCMGGetInterpolation(PC pc,PetscInt l,Mat *mat) 166c88c5224SJed Brown { 167c88c5224SJed Brown PC_MG *mg = (PC_MG*)pc->data; 168c88c5224SJed Brown PC_MG_Levels **mglevels = mg->levels; 169c88c5224SJed Brown PetscErrorCode ierr; 170c88c5224SJed Brown 171c88c5224SJed Brown PetscFunctionBegin; 172c88c5224SJed Brown PetscValidHeaderSpecific(pc,PC_CLASSID,1); 173c88c5224SJed Brown PetscValidPointer(mat,3); 174ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 175ce94432eSBarry Smith if (l <= 0 || mg->nlevels <= l) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Level %D must be in range {1,...,%D}",l,mg->nlevels-1); 176c88c5224SJed Brown if (!mglevels[l]->interpolate) { 177ce94432eSBarry Smith if (!mglevels[l]->restrct) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call PCMGSetInterpolation() or PCMGSetInterpolation()"); 178c88c5224SJed Brown ierr = PCMGSetInterpolation(pc,l,mglevels[l]->restrct);CHKERRQ(ierr); 179c88c5224SJed Brown } 180c88c5224SJed Brown *mat = mglevels[l]->interpolate; 181c88c5224SJed Brown PetscFunctionReturn(0); 182c88c5224SJed Brown } 183c88c5224SJed Brown 184c88c5224SJed Brown #undef __FUNCT__ 185d6337fedSHong Zhang #define __FUNCT__ "PCMGSetRestriction" 1864b9ad928SBarry Smith /*@ 18797177400SBarry Smith PCMGSetRestriction - Sets the function to be used to restrict vector 1884b9ad928SBarry Smith from level l to l-1. 1894b9ad928SBarry Smith 190ad4df100SBarry Smith Logically Collective on PC and Mat 1914b9ad928SBarry Smith 1924b9ad928SBarry Smith Input Parameters: 1934b9ad928SBarry Smith + pc - the multigrid context 194c88c5224SJed Brown . l - the level (0 is coarsest) to supply [Do not supply 0] 195c88c5224SJed Brown - mat - the restriction matrix 1964b9ad928SBarry Smith 1974b9ad928SBarry Smith Level: advanced 1984b9ad928SBarry Smith 1994b9ad928SBarry Smith Notes: 2004b9ad928SBarry Smith Usually this is the same matrix used also to set the interpolation 2014b9ad928SBarry Smith for the same level. 2024b9ad928SBarry Smith 2034b9ad928SBarry Smith One can pass in the interpolation matrix or its transpose; PETSc figures 2044b9ad928SBarry Smith out from the matrix size which one it is. 2054b9ad928SBarry Smith 206aea2a34eSBarry Smith If you do not set this, the transpose of the Mat set with PCMGSetInterpolation() 207fccaa45eSBarry Smith is used. 208fccaa45eSBarry Smith 2094b9ad928SBarry Smith .keywords: MG, set, multigrid, restriction, level 2104b9ad928SBarry Smith 211aea2a34eSBarry Smith .seealso: PCMGSetInterpolation() 2124b9ad928SBarry Smith @*/ 2137087cfbeSBarry Smith PetscErrorCode PCMGSetRestriction(PC pc,PetscInt l,Mat mat) 2144b9ad928SBarry Smith { 215fccaa45eSBarry Smith PetscErrorCode ierr; 216f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 217f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 2184b9ad928SBarry Smith 2194b9ad928SBarry Smith PetscFunctionBegin; 220c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 221c88c5224SJed Brown PetscValidHeaderSpecific(mat,MAT_CLASSID,3); 222ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 223ce94432eSBarry Smith if (!l) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Do not set restriction routine for coarsest level"); 224c3122656SLisandro Dalcin ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr); 2256bf464f9SBarry Smith ierr = MatDestroy(&mglevels[l]->restrct);CHKERRQ(ierr); 2262fa5cd67SKarl Rupp 227f3fbd535SBarry Smith mglevels[l]->restrct = mat; 2284b9ad928SBarry Smith PetscFunctionReturn(0); 2294b9ad928SBarry Smith } 2304b9ad928SBarry Smith 2314b9ad928SBarry Smith #undef __FUNCT__ 232c88c5224SJed Brown #define __FUNCT__ "PCMGGetRestriction" 233c88c5224SJed Brown /*@ 234c88c5224SJed Brown PCMGGetRestriction - Gets the function to be used to restrict vector 235c88c5224SJed Brown from level l to l-1. 236c88c5224SJed Brown 237c88c5224SJed Brown Logically Collective on PC and Mat 238c88c5224SJed Brown 239c88c5224SJed Brown Input Parameters: 240c88c5224SJed Brown + pc - the multigrid context 241c88c5224SJed Brown - l - the level (0 is coarsest) to supply [Do not supply 0] 242c88c5224SJed Brown 243c88c5224SJed Brown Output Parameter: 244c88c5224SJed Brown . mat - the restriction matrix 245c88c5224SJed Brown 246c88c5224SJed Brown Level: advanced 247c88c5224SJed Brown 248c88c5224SJed Brown .keywords: MG, get, multigrid, restriction, level 249c88c5224SJed Brown 250c88c5224SJed Brown .seealso: PCMGGetInterpolation(), PCMGSetRestriction(), PCMGGetRScale() 251c88c5224SJed Brown @*/ 252c88c5224SJed Brown PetscErrorCode PCMGGetRestriction(PC pc,PetscInt l,Mat *mat) 253c88c5224SJed Brown { 254c88c5224SJed Brown PC_MG *mg = (PC_MG*)pc->data; 255c88c5224SJed Brown PC_MG_Levels **mglevels = mg->levels; 256c88c5224SJed Brown PetscErrorCode ierr; 257c88c5224SJed Brown 258c88c5224SJed Brown PetscFunctionBegin; 259c88c5224SJed Brown PetscValidHeaderSpecific(pc,PC_CLASSID,1); 260c88c5224SJed Brown PetscValidPointer(mat,3); 261ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 262ce94432eSBarry Smith if (l <= 0 || mg->nlevels <= l) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Level %D must be in range {1,...,%D}",l,mg->nlevels-1); 263c88c5224SJed Brown if (!mglevels[l]->restrct) { 264ce94432eSBarry Smith if (!mglevels[l]->interpolate) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call PCMGSetRestriction() or PCMGSetInterpolation()"); 265c88c5224SJed Brown ierr = PCMGSetRestriction(pc,l,mglevels[l]->interpolate);CHKERRQ(ierr); 266c88c5224SJed Brown } 267c88c5224SJed Brown *mat = mglevels[l]->restrct; 268c88c5224SJed Brown PetscFunctionReturn(0); 269c88c5224SJed Brown } 270c88c5224SJed Brown 271c88c5224SJed Brown #undef __FUNCT__ 27273250ac0SBarry Smith #define __FUNCT__ "PCMGSetRScale" 27373250ac0SBarry Smith /*@ 27473250ac0SBarry Smith PCMGSetRScale - Sets the pointwise scaling for the restriction operator from level l to l-1. 27573250ac0SBarry Smith 276c88c5224SJed Brown Logically Collective on PC and Vec 277c88c5224SJed Brown 278c88c5224SJed Brown Input Parameters: 279c88c5224SJed Brown + pc - the multigrid context 280c88c5224SJed Brown - l - the level (0 is coarsest) to supply [Do not supply 0] 281c88c5224SJed Brown . rscale - the scaling 282c88c5224SJed Brown 283c88c5224SJed Brown Level: advanced 284c88c5224SJed Brown 285c88c5224SJed Brown Notes: 286c88c5224SJed Brown When evaluating a function on a coarse level one does not want to do F(R * x) one does F(rscale * R * x) where rscale is 1 over the row sums of R. 287c88c5224SJed Brown 288c88c5224SJed Brown .keywords: MG, set, multigrid, restriction, level 289c88c5224SJed Brown 290c88c5224SJed Brown .seealso: PCMGSetInterpolation(), PCMGSetRestriction(), PCMGGetRScale() 291c88c5224SJed Brown @*/ 292c88c5224SJed Brown PetscErrorCode PCMGSetRScale(PC pc,PetscInt l,Vec rscale) 293c88c5224SJed Brown { 294c88c5224SJed Brown PetscErrorCode ierr; 295c88c5224SJed Brown PC_MG *mg = (PC_MG*)pc->data; 296c88c5224SJed Brown PC_MG_Levels **mglevels = mg->levels; 297c88c5224SJed Brown 298c88c5224SJed Brown PetscFunctionBegin; 299c88c5224SJed Brown PetscValidHeaderSpecific(pc,PC_CLASSID,1); 300ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 301ce94432eSBarry Smith if (l <= 0 || mg->nlevels <= l) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Level %D must be in range {1,...,%D}",l,mg->nlevels-1); 302c88c5224SJed Brown ierr = PetscObjectReference((PetscObject)rscale);CHKERRQ(ierr); 303c88c5224SJed Brown ierr = VecDestroy(&mglevels[l]->rscale);CHKERRQ(ierr); 3042fa5cd67SKarl Rupp 305c88c5224SJed Brown mglevels[l]->rscale = rscale; 306c88c5224SJed Brown PetscFunctionReturn(0); 307c88c5224SJed Brown } 308c88c5224SJed Brown 309c88c5224SJed Brown #undef __FUNCT__ 310c88c5224SJed Brown #define __FUNCT__ "PCMGGetRScale" 311c88c5224SJed Brown /*@ 312c88c5224SJed Brown PCMGGetRScale - Gets the pointwise scaling for the restriction operator from level l to l-1. 313c88c5224SJed Brown 314c88c5224SJed Brown Collective on PC 31573250ac0SBarry Smith 31673250ac0SBarry Smith Input Parameters: 31773250ac0SBarry Smith + pc - the multigrid context 31873250ac0SBarry Smith . rscale - the scaling 31973250ac0SBarry Smith - l - the level (0 is coarsest) to supply [Do not supply 0] 32073250ac0SBarry Smith 32173250ac0SBarry Smith Level: advanced 32273250ac0SBarry Smith 32373250ac0SBarry Smith Notes: 32473250ac0SBarry Smith When evaluating a function on a coarse level one does not want to do F(R * x) one does F(rscale * R * x) where rscale is 1 over the row sums of R. 32573250ac0SBarry Smith 32673250ac0SBarry Smith .keywords: MG, set, multigrid, restriction, level 32773250ac0SBarry Smith 328c88c5224SJed Brown .seealso: PCMGSetInterpolation(), PCMGGetRestriction() 32973250ac0SBarry Smith @*/ 330c88c5224SJed Brown PetscErrorCode PCMGGetRScale(PC pc,PetscInt l,Vec *rscale) 33173250ac0SBarry Smith { 33273250ac0SBarry Smith PetscErrorCode ierr; 33373250ac0SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 33473250ac0SBarry Smith PC_MG_Levels **mglevels = mg->levels; 33573250ac0SBarry Smith 33673250ac0SBarry Smith PetscFunctionBegin; 33773250ac0SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 338ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 339ce94432eSBarry Smith if (l <= 0 || mg->nlevels <= l) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Level %D must be in range {1,...,%D}",l,mg->nlevels-1); 340c88c5224SJed Brown if (!mglevels[l]->rscale) { 341c88c5224SJed Brown Mat R; 342c88c5224SJed Brown Vec X,Y,coarse,fine; 343c88c5224SJed Brown PetscInt M,N; 344c88c5224SJed Brown ierr = PCMGGetRestriction(pc,l,&R);CHKERRQ(ierr); 345c88c5224SJed Brown ierr = MatGetVecs(R,&X,&Y);CHKERRQ(ierr); 346c88c5224SJed Brown ierr = MatGetSize(R,&M,&N);CHKERRQ(ierr); 3472fa5cd67SKarl Rupp if (M < N) { 3482fa5cd67SKarl Rupp fine = X; 3492fa5cd67SKarl Rupp coarse = Y; 3502fa5cd67SKarl Rupp } else if (N < M) { 3512fa5cd67SKarl Rupp fine = Y; coarse = X; 352ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)R),PETSC_ERR_SUP,"Restriction matrix is square, cannot determine which Vec is coarser"); 353c88c5224SJed Brown ierr = VecSet(fine,1.);CHKERRQ(ierr); 354c88c5224SJed Brown ierr = MatRestrict(R,fine,coarse);CHKERRQ(ierr); 355c88c5224SJed Brown ierr = VecDestroy(&fine);CHKERRQ(ierr); 356c88c5224SJed Brown ierr = VecReciprocal(coarse);CHKERRQ(ierr); 357c88c5224SJed Brown mglevels[l]->rscale = coarse; 358c88c5224SJed Brown } 359c88c5224SJed Brown *rscale = mglevels[l]->rscale; 36073250ac0SBarry Smith PetscFunctionReturn(0); 36173250ac0SBarry Smith } 36273250ac0SBarry Smith 36373250ac0SBarry Smith #undef __FUNCT__ 364d6337fedSHong Zhang #define __FUNCT__ "PCMGGetSmoother" 365f39d8e23SSatish Balay /*@ 36697177400SBarry Smith PCMGGetSmoother - Gets the KSP context to be used as smoother for 36797177400SBarry Smith both pre- and post-smoothing. Call both PCMGGetSmootherUp() and 36897177400SBarry Smith PCMGGetSmootherDown() to use different functions for pre- and 3694b9ad928SBarry Smith post-smoothing. 3704b9ad928SBarry Smith 3714b9ad928SBarry Smith Not Collective, KSP returned is parallel if PC is 3724b9ad928SBarry Smith 3734b9ad928SBarry Smith Input Parameters: 3744b9ad928SBarry Smith + pc - the multigrid context 3754b9ad928SBarry Smith - l - the level (0 is coarsest) to supply 3764b9ad928SBarry Smith 3774b9ad928SBarry Smith Ouput Parameters: 3784b9ad928SBarry Smith . ksp - the smoother 3794b9ad928SBarry Smith 3804b9ad928SBarry Smith Level: advanced 3814b9ad928SBarry Smith 3824b9ad928SBarry Smith .keywords: MG, get, multigrid, level, smoother, pre-smoother, post-smoother 3834b9ad928SBarry Smith 38497177400SBarry Smith .seealso: PCMGGetSmootherUp(), PCMGGetSmootherDown() 3854b9ad928SBarry Smith @*/ 3867087cfbeSBarry Smith PetscErrorCode PCMGGetSmoother(PC pc,PetscInt l,KSP *ksp) 3874b9ad928SBarry Smith { 388f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 389f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 3904b9ad928SBarry Smith 3914b9ad928SBarry Smith PetscFunctionBegin; 392c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 393f3fbd535SBarry Smith *ksp = mglevels[l]->smoothd; 3944b9ad928SBarry Smith PetscFunctionReturn(0); 3954b9ad928SBarry Smith } 3964b9ad928SBarry Smith 3974b9ad928SBarry Smith #undef __FUNCT__ 398d6337fedSHong Zhang #define __FUNCT__ "PCMGGetSmootherUp" 399f39d8e23SSatish Balay /*@ 40097177400SBarry Smith PCMGGetSmootherUp - Gets the KSP context to be used as smoother after 4014b9ad928SBarry Smith coarse grid correction (post-smoother). 4024b9ad928SBarry Smith 4034b9ad928SBarry Smith Not Collective, KSP returned is parallel if PC is 4044b9ad928SBarry Smith 4054b9ad928SBarry Smith Input Parameters: 4064b9ad928SBarry Smith + pc - the multigrid context 4074b9ad928SBarry Smith - l - the level (0 is coarsest) to supply 4084b9ad928SBarry Smith 4094b9ad928SBarry Smith Ouput Parameters: 4104b9ad928SBarry Smith . ksp - the smoother 4114b9ad928SBarry Smith 4124b9ad928SBarry Smith Level: advanced 4134b9ad928SBarry Smith 41489cce641SBarry Smith Notes: calling this will result in a different pre and post smoother so you may need to 41589cce641SBarry Smith set options on the pre smoother also 41689cce641SBarry Smith 4174b9ad928SBarry Smith .keywords: MG, multigrid, get, smoother, up, post-smoother, level 4184b9ad928SBarry Smith 41997177400SBarry Smith .seealso: PCMGGetSmootherUp(), PCMGGetSmootherDown() 4204b9ad928SBarry Smith @*/ 4217087cfbeSBarry Smith PetscErrorCode PCMGGetSmootherUp(PC pc,PetscInt l,KSP *ksp) 4224b9ad928SBarry Smith { 423f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 424f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 425dfbe8321SBarry Smith PetscErrorCode ierr; 426f69a0ea3SMatthew Knepley const char *prefix; 4274b9ad928SBarry Smith MPI_Comm comm; 4284b9ad928SBarry Smith 4294b9ad928SBarry Smith PetscFunctionBegin; 430c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4314b9ad928SBarry Smith /* 4324b9ad928SBarry Smith This is called only if user wants a different pre-smoother from post. 4334b9ad928SBarry Smith Thus we check if a different one has already been allocated, 4344b9ad928SBarry Smith if not we allocate it. 4354b9ad928SBarry Smith */ 436ce94432eSBarry Smith if (!l) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"There is no such thing as a up smoother on the coarse grid"); 437f3fbd535SBarry Smith if (mglevels[l]->smoothu == mglevels[l]->smoothd) { 43819fd82e9SBarry Smith KSPType ksptype; 43919fd82e9SBarry Smith PCType pctype; 440336babb1SJed Brown PC ipc; 441336babb1SJed Brown PetscReal rtol,abstol,dtol; 442336babb1SJed Brown PetscInt maxits; 443336babb1SJed Brown KSPNormType normtype; 444f3fbd535SBarry Smith ierr = PetscObjectGetComm((PetscObject)mglevels[l]->smoothd,&comm);CHKERRQ(ierr); 445f3fbd535SBarry Smith ierr = KSPGetOptionsPrefix(mglevels[l]->smoothd,&prefix);CHKERRQ(ierr); 446336babb1SJed Brown ierr = KSPGetTolerances(mglevels[l]->smoothd,&rtol,&abstol,&dtol,&maxits);CHKERRQ(ierr); 4473bf036e2SBarry Smith ierr = KSPGetType(mglevels[l]->smoothd,&ksptype);CHKERRQ(ierr); 448336babb1SJed Brown ierr = KSPGetNormType(mglevels[l]->smoothd,&normtype);CHKERRQ(ierr); 449336babb1SJed Brown ierr = KSPGetPC(mglevels[l]->smoothd,&ipc);CHKERRQ(ierr); 450336babb1SJed Brown ierr = PCGetType(ipc,&pctype);CHKERRQ(ierr); 451336babb1SJed Brown 452f3fbd535SBarry Smith ierr = KSPCreate(comm,&mglevels[l]->smoothu);CHKERRQ(ierr); 453f3fbd535SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[l]->smoothu,(PetscObject)pc,mglevels[0]->levels-l);CHKERRQ(ierr); 454f3fbd535SBarry Smith ierr = KSPSetOptionsPrefix(mglevels[l]->smoothu,prefix);CHKERRQ(ierr); 455336babb1SJed Brown ierr = KSPSetTolerances(mglevels[l]->smoothu,rtol,abstol,dtol,maxits);CHKERRQ(ierr); 456336babb1SJed Brown ierr = KSPSetType(mglevels[l]->smoothu,ksptype);CHKERRQ(ierr); 457336babb1SJed Brown ierr = KSPSetNormType(mglevels[l]->smoothu,normtype);CHKERRQ(ierr); 4580298fd71SBarry Smith ierr = KSPSetConvergenceTest(mglevels[l]->smoothu,KSPSkipConverged,NULL,NULL);CHKERRQ(ierr); 459336babb1SJed Brown ierr = KSPGetPC(mglevels[l]->smoothu,&ipc);CHKERRQ(ierr); 460336babb1SJed Brown ierr = PCSetType(ipc,pctype);CHKERRQ(ierr); 4613bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[l]->smoothu);CHKERRQ(ierr); 4624b9ad928SBarry Smith } 463f3fbd535SBarry Smith if (ksp) *ksp = mglevels[l]->smoothu; 4644b9ad928SBarry Smith PetscFunctionReturn(0); 4654b9ad928SBarry Smith } 4664b9ad928SBarry Smith 4674b9ad928SBarry Smith #undef __FUNCT__ 4689dcbbd2bSBarry Smith #define __FUNCT__ "PCMGGetSmootherDown" 469f39d8e23SSatish Balay /*@ 47097177400SBarry Smith PCMGGetSmootherDown - Gets the KSP context to be used as smoother before 4714b9ad928SBarry Smith coarse grid correction (pre-smoother). 4724b9ad928SBarry Smith 4734b9ad928SBarry Smith Not Collective, KSP returned is parallel if PC is 4744b9ad928SBarry Smith 4754b9ad928SBarry Smith Input Parameters: 4764b9ad928SBarry Smith + pc - the multigrid context 4774b9ad928SBarry Smith - l - the level (0 is coarsest) to supply 4784b9ad928SBarry Smith 4794b9ad928SBarry Smith Ouput Parameters: 4804b9ad928SBarry Smith . ksp - the smoother 4814b9ad928SBarry Smith 4824b9ad928SBarry Smith Level: advanced 4834b9ad928SBarry Smith 48489cce641SBarry Smith Notes: calling this will result in a different pre and post smoother so you may need to 48589cce641SBarry Smith set options on the post smoother also 48689cce641SBarry Smith 4874b9ad928SBarry Smith .keywords: MG, multigrid, get, smoother, down, pre-smoother, level 4884b9ad928SBarry Smith 48997177400SBarry Smith .seealso: PCMGGetSmootherUp(), PCMGGetSmoother() 4904b9ad928SBarry Smith @*/ 4917087cfbeSBarry Smith PetscErrorCode PCMGGetSmootherDown(PC pc,PetscInt l,KSP *ksp) 4924b9ad928SBarry Smith { 493dfbe8321SBarry Smith PetscErrorCode ierr; 494f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 495f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 4964b9ad928SBarry Smith 4974b9ad928SBarry Smith PetscFunctionBegin; 498c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4994b9ad928SBarry Smith /* make sure smoother up and down are different */ 500c5eb9154SBarry Smith if (l) { 5010298fd71SBarry Smith ierr = PCMGGetSmootherUp(pc,l,NULL);CHKERRQ(ierr); 502d8148a5aSMatthew Knepley } 503f3fbd535SBarry Smith *ksp = mglevels[l]->smoothd; 5044b9ad928SBarry Smith PetscFunctionReturn(0); 5054b9ad928SBarry Smith } 5064b9ad928SBarry Smith 5074b9ad928SBarry Smith #undef __FUNCT__ 5089dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetCyclesOnLevel" 5094b9ad928SBarry Smith /*@ 51097177400SBarry Smith PCMGSetCyclesOnLevel - Sets the number of cycles to run on this level. 5114b9ad928SBarry Smith 512ad4df100SBarry Smith Logically Collective on PC 5134b9ad928SBarry Smith 5144b9ad928SBarry Smith Input Parameters: 5154b9ad928SBarry Smith + pc - the multigrid context 5164b9ad928SBarry Smith . l - the level (0 is coarsest) this is to be used for 5174b9ad928SBarry Smith - n - the number of cycles 5184b9ad928SBarry Smith 5194b9ad928SBarry Smith Level: advanced 5204b9ad928SBarry Smith 5214b9ad928SBarry Smith .keywords: MG, multigrid, set, cycles, V-cycle, W-cycle, level 5224b9ad928SBarry Smith 52397177400SBarry Smith .seealso: PCMGSetCycles() 5244b9ad928SBarry Smith @*/ 5257087cfbeSBarry Smith PetscErrorCode PCMGSetCyclesOnLevel(PC pc,PetscInt l,PetscInt c) 5264b9ad928SBarry Smith { 527f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 528f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 5294b9ad928SBarry Smith 5304b9ad928SBarry Smith PetscFunctionBegin; 531c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 532ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 533c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(pc,l,2); 534c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(pc,c,3); 535f3fbd535SBarry Smith mglevels[l]->cycles = c; 5364b9ad928SBarry Smith PetscFunctionReturn(0); 5374b9ad928SBarry Smith } 5384b9ad928SBarry Smith 5394b9ad928SBarry Smith #undef __FUNCT__ 540d6337fedSHong Zhang #define __FUNCT__ "PCMGSetRhs" 5414b9ad928SBarry Smith /*@ 54297177400SBarry Smith PCMGSetRhs - Sets the vector space to be used to store the right-hand side 543fccaa45eSBarry Smith on a particular level. 5444b9ad928SBarry Smith 545ad4df100SBarry Smith Logically Collective on PC and Vec 5464b9ad928SBarry Smith 5474b9ad928SBarry Smith Input Parameters: 5484b9ad928SBarry Smith + pc - the multigrid context 5494b9ad928SBarry Smith . l - the level (0 is coarsest) this is to be used for 5504b9ad928SBarry Smith - c - the space 5514b9ad928SBarry Smith 5524b9ad928SBarry Smith Level: advanced 5534b9ad928SBarry Smith 554fccaa45eSBarry Smith Notes: If this is not provided PETSc will automatically generate one. 555fccaa45eSBarry Smith 556fccaa45eSBarry Smith You do not need to keep a reference to this vector if you do 557fccaa45eSBarry Smith not need it PCDestroy() will properly free it. 558fccaa45eSBarry Smith 5594b9ad928SBarry Smith .keywords: MG, multigrid, set, right-hand-side, rhs, level 5604b9ad928SBarry Smith 56197177400SBarry Smith .seealso: PCMGSetX(), PCMGSetR() 5624b9ad928SBarry Smith @*/ 5637087cfbeSBarry Smith PetscErrorCode PCMGSetRhs(PC pc,PetscInt l,Vec c) 5644b9ad928SBarry Smith { 565fccaa45eSBarry Smith PetscErrorCode ierr; 566f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 567f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 5684b9ad928SBarry Smith 5694b9ad928SBarry Smith PetscFunctionBegin; 570c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 571ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 572ce94432eSBarry Smith if (l == mglevels[0]->levels-1) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"Do not set rhs for finest level"); 573c3122656SLisandro Dalcin ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr); 5746bf464f9SBarry Smith ierr = VecDestroy(&mglevels[l]->b);CHKERRQ(ierr); 5752fa5cd67SKarl Rupp 576f3fbd535SBarry Smith mglevels[l]->b = c; 5774b9ad928SBarry Smith PetscFunctionReturn(0); 5784b9ad928SBarry Smith } 5794b9ad928SBarry Smith 5804b9ad928SBarry Smith #undef __FUNCT__ 581d6337fedSHong Zhang #define __FUNCT__ "PCMGSetX" 5824b9ad928SBarry Smith /*@ 58397177400SBarry Smith PCMGSetX - Sets the vector space to be used to store the solution on a 584fccaa45eSBarry Smith particular level. 5854b9ad928SBarry Smith 586ad4df100SBarry Smith Logically Collective on PC and Vec 5874b9ad928SBarry Smith 5884b9ad928SBarry Smith Input Parameters: 5894b9ad928SBarry Smith + pc - the multigrid context 590251f4c67SDmitry Karpeev . l - the level (0 is coarsest) this is to be used for (do not supply the finest level) 5914b9ad928SBarry Smith - c - the space 5924b9ad928SBarry Smith 5934b9ad928SBarry Smith Level: advanced 5944b9ad928SBarry Smith 595fccaa45eSBarry Smith Notes: If this is not provided PETSc will automatically generate one. 596fccaa45eSBarry Smith 597fccaa45eSBarry Smith You do not need to keep a reference to this vector if you do 598fccaa45eSBarry Smith not need it PCDestroy() will properly free it. 599fccaa45eSBarry Smith 6004b9ad928SBarry Smith .keywords: MG, multigrid, set, solution, level 6014b9ad928SBarry Smith 60297177400SBarry Smith .seealso: PCMGSetRhs(), PCMGSetR() 6034b9ad928SBarry Smith @*/ 6047087cfbeSBarry Smith PetscErrorCode PCMGSetX(PC pc,PetscInt l,Vec c) 6054b9ad928SBarry Smith { 606fccaa45eSBarry Smith PetscErrorCode ierr; 607f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 608f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 6094b9ad928SBarry Smith 6104b9ad928SBarry Smith PetscFunctionBegin; 611c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 612ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 613ce94432eSBarry Smith if (l == mglevels[0]->levels-1) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"Do not set x for finest level"); 614c3122656SLisandro Dalcin ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr); 6156bf464f9SBarry Smith ierr = VecDestroy(&mglevels[l]->x);CHKERRQ(ierr); 6162fa5cd67SKarl Rupp 617f3fbd535SBarry Smith mglevels[l]->x = c; 6184b9ad928SBarry Smith PetscFunctionReturn(0); 6194b9ad928SBarry Smith } 6204b9ad928SBarry Smith 6214b9ad928SBarry Smith #undef __FUNCT__ 622d6337fedSHong Zhang #define __FUNCT__ "PCMGSetR" 6234b9ad928SBarry Smith /*@ 62497177400SBarry Smith PCMGSetR - Sets the vector space to be used to store the residual on a 625fccaa45eSBarry Smith particular level. 6264b9ad928SBarry Smith 627ad4df100SBarry Smith Logically Collective on PC and Vec 6284b9ad928SBarry Smith 6294b9ad928SBarry Smith Input Parameters: 6304b9ad928SBarry Smith + pc - the multigrid context 6314b9ad928SBarry Smith . l - the level (0 is coarsest) this is to be used for 6324b9ad928SBarry Smith - c - the space 6334b9ad928SBarry Smith 6344b9ad928SBarry Smith Level: advanced 6354b9ad928SBarry Smith 636fccaa45eSBarry Smith Notes: If this is not provided PETSc will automatically generate one. 637fccaa45eSBarry Smith 638fccaa45eSBarry Smith You do not need to keep a reference to this vector if you do 639fccaa45eSBarry Smith not need it PCDestroy() will properly free it. 640fccaa45eSBarry Smith 6414b9ad928SBarry Smith .keywords: MG, multigrid, set, residual, level 6424b9ad928SBarry Smith @*/ 6437087cfbeSBarry Smith PetscErrorCode PCMGSetR(PC pc,PetscInt l,Vec c) 6444b9ad928SBarry Smith { 645fccaa45eSBarry Smith PetscErrorCode ierr; 646f3fbd535SBarry Smith PC_MG *mg = (PC_MG*)pc->data; 647f3fbd535SBarry Smith PC_MG_Levels **mglevels = mg->levels; 6484b9ad928SBarry Smith 6494b9ad928SBarry Smith PetscFunctionBegin; 650c5eb9154SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 651ce94432eSBarry Smith if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 652ce94432eSBarry Smith if (!l) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Need not set residual vector for coarse grid"); 653c3122656SLisandro Dalcin ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr); 6546bf464f9SBarry Smith ierr = VecDestroy(&mglevels[l]->r);CHKERRQ(ierr); 6552fa5cd67SKarl Rupp 656f3fbd535SBarry Smith mglevels[l]->r = c; 6574b9ad928SBarry Smith PetscFunctionReturn(0); 6584b9ad928SBarry Smith } 659