xref: /petsc/src/ksp/pc/impls/mg/mgfunc.c (revision 73250ac08b49c1abb7691050589cabd5069d0005)
14b9ad928SBarry Smith 
2c6db04a5SJed Brown #include <../src/ksp/pc/impls/mg/mgimpl.h>       /*I "petscksp.h" I*/
3f3f935beSJed Brown                           /*I "petscpcmg.h"   I*/
44b9ad928SBarry Smith 
54b9ad928SBarry Smith #undef __FUNCT__
697177400SBarry Smith #define __FUNCT__ "PCMGDefaultResidual"
71f6cc5b2SSatish Balay /*@C
897177400SBarry Smith    PCMGDefaultResidual - 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 
204b9ad928SBarry Smith    Level: advanced
214b9ad928SBarry Smith 
224b9ad928SBarry Smith .keywords: MG, default, multigrid, residual
234b9ad928SBarry Smith 
2497177400SBarry Smith .seealso: PCMGSetResidual()
254b9ad928SBarry Smith @*/
267087cfbeSBarry Smith PetscErrorCode  PCMGDefaultResidual(Mat mat,Vec b,Vec x,Vec r)
274b9ad928SBarry Smith {
28dfbe8321SBarry Smith   PetscErrorCode ierr;
294b9ad928SBarry Smith 
304b9ad928SBarry Smith   PetscFunctionBegin;
314b9ad928SBarry Smith   ierr = MatMult(mat,x,r);CHKERRQ(ierr);
32efb30889SBarry Smith   ierr = VecAYPX(r,-1.0,b);CHKERRQ(ierr);
334b9ad928SBarry Smith   PetscFunctionReturn(0);
344b9ad928SBarry Smith }
354b9ad928SBarry Smith 
364b9ad928SBarry Smith /* ---------------------------------------------------------------------------*/
374b9ad928SBarry Smith 
384b9ad928SBarry Smith #undef __FUNCT__
39d6337fedSHong Zhang #define __FUNCT__ "PCMGGetCoarseSolve"
40f39d8e23SSatish Balay /*@
4197177400SBarry Smith    PCMGGetCoarseSolve - Gets the solver context to be used on the coarse grid.
424b9ad928SBarry Smith 
434b9ad928SBarry Smith    Not Collective
444b9ad928SBarry Smith 
454b9ad928SBarry Smith    Input Parameter:
464b9ad928SBarry Smith .  pc - the multigrid context
474b9ad928SBarry Smith 
484b9ad928SBarry Smith    Output Parameter:
494b9ad928SBarry Smith .  ksp - the coarse grid solver context
504b9ad928SBarry Smith 
514b9ad928SBarry Smith    Level: advanced
524b9ad928SBarry Smith 
534b9ad928SBarry Smith .keywords: MG, multigrid, get, coarse grid
544b9ad928SBarry Smith @*/
557087cfbeSBarry Smith PetscErrorCode  PCMGGetCoarseSolve(PC pc,KSP *ksp)
564b9ad928SBarry Smith {
57f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
58f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
594b9ad928SBarry Smith 
604b9ad928SBarry Smith   PetscFunctionBegin;
61c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
62f3fbd535SBarry Smith   *ksp =  mglevels[0]->smoothd;
634b9ad928SBarry Smith   PetscFunctionReturn(0);
644b9ad928SBarry Smith }
654b9ad928SBarry Smith 
664b9ad928SBarry Smith #undef __FUNCT__
67d6337fedSHong Zhang #define __FUNCT__ "PCMGSetResidual"
684b9ad928SBarry Smith /*@C
6997177400SBarry Smith    PCMGSetResidual - Sets the function to be used to calculate the residual
704b9ad928SBarry Smith    on the lth level.
714b9ad928SBarry Smith 
72ad4df100SBarry Smith    Logically Collective on PC and Mat
734b9ad928SBarry Smith 
744b9ad928SBarry Smith    Input Parameters:
754b9ad928SBarry Smith +  pc       - the multigrid context
764b9ad928SBarry Smith .  l        - the level (0 is coarsest) to supply
7797177400SBarry Smith .  residual - function used to form residual (usually PCMGDefaultResidual)
784b9ad928SBarry Smith -  mat      - matrix associated with residual
794b9ad928SBarry Smith 
804b9ad928SBarry Smith    Level: advanced
814b9ad928SBarry Smith 
824b9ad928SBarry Smith .keywords:  MG, set, multigrid, residual, level
834b9ad928SBarry Smith 
8497177400SBarry Smith .seealso: PCMGDefaultResidual()
854b9ad928SBarry Smith @*/
867087cfbeSBarry Smith PetscErrorCode  PCMGSetResidual(PC pc,PetscInt l,PetscErrorCode (*residual)(Mat,Vec,Vec,Vec),Mat mat)
874b9ad928SBarry Smith {
88f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
89f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
90298cc208SBarry Smith   PetscErrorCode ierr;
914b9ad928SBarry Smith 
924b9ad928SBarry Smith   PetscFunctionBegin;
93c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
94e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
95f3fbd535SBarry Smith   mglevels[l]->residual = residual;
96f3ae41bdSBarry Smith   if (mat) {ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);}
97298cc208SBarry Smith   ierr = MatDestroy(&mglevels[l]->A);CHKERRQ(ierr);
98f3fbd535SBarry Smith   mglevels[l]->A        = mat;
994b9ad928SBarry Smith   PetscFunctionReturn(0);
1004b9ad928SBarry Smith }
1014b9ad928SBarry Smith 
1024b9ad928SBarry Smith #undef __FUNCT__
103d6337fedSHong Zhang #define __FUNCT__ "PCMGSetInterpolation"
1044b9ad928SBarry Smith /*@
105aea2a34eSBarry Smith    PCMGSetInterpolation - Sets the function to be used to calculate the
106bf5b2e24SBarry Smith    interpolation from l-1 to the lth level
1074b9ad928SBarry Smith 
108ad4df100SBarry Smith    Logically Collective on PC and Mat
1094b9ad928SBarry Smith 
1104b9ad928SBarry Smith    Input Parameters:
1114b9ad928SBarry Smith +  pc  - the multigrid context
1124b9ad928SBarry Smith .  mat - the interpolation operator
113bf5b2e24SBarry Smith -  l   - the level (0 is coarsest) to supply [do not supply 0]
1144b9ad928SBarry Smith 
1154b9ad928SBarry Smith    Level: advanced
1164b9ad928SBarry Smith 
1174b9ad928SBarry Smith    Notes:
1184b9ad928SBarry Smith           Usually this is the same matrix used also to set the restriction
1194b9ad928SBarry Smith     for the same level.
1204b9ad928SBarry Smith 
1214b9ad928SBarry Smith           One can pass in the interpolation matrix or its transpose; PETSc figures
1224b9ad928SBarry Smith     out from the matrix size which one it is.
1234b9ad928SBarry Smith 
1244b9ad928SBarry Smith .keywords:  multigrid, set, interpolate, level
1254b9ad928SBarry Smith 
12697177400SBarry Smith .seealso: PCMGSetRestriction()
1274b9ad928SBarry Smith @*/
1287087cfbeSBarry Smith PetscErrorCode  PCMGSetInterpolation(PC pc,PetscInt l,Mat mat)
1294b9ad928SBarry Smith {
130f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
131f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
132fccaa45eSBarry Smith   PetscErrorCode ierr;
1334b9ad928SBarry Smith 
1344b9ad928SBarry Smith   PetscFunctionBegin;
135c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
136e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
137e7e72b3dSBarry Smith   if (!l) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Do not set interpolation routine for coarsest level");
138c3122656SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
1396bf464f9SBarry Smith   ierr = MatDestroy(&mglevels[l]->interpolate);CHKERRQ(ierr);
140f3fbd535SBarry Smith   mglevels[l]->interpolate = mat;
1414b9ad928SBarry Smith   PetscFunctionReturn(0);
1424b9ad928SBarry Smith }
1434b9ad928SBarry Smith 
1444b9ad928SBarry Smith #undef __FUNCT__
145d6337fedSHong Zhang #define __FUNCT__ "PCMGSetRestriction"
1464b9ad928SBarry Smith /*@
14797177400SBarry Smith    PCMGSetRestriction - Sets the function to be used to restrict vector
1484b9ad928SBarry Smith    from level l to l-1.
1494b9ad928SBarry Smith 
150ad4df100SBarry Smith    Logically Collective on PC and Mat
1514b9ad928SBarry Smith 
1524b9ad928SBarry Smith    Input Parameters:
1534b9ad928SBarry Smith +  pc - the multigrid context
1544b9ad928SBarry Smith .  mat - the restriction matrix
155bf5b2e24SBarry Smith -  l - the level (0 is coarsest) to supply [Do not supply 0]
1564b9ad928SBarry Smith 
1574b9ad928SBarry Smith    Level: advanced
1584b9ad928SBarry Smith 
1594b9ad928SBarry Smith    Notes:
1604b9ad928SBarry Smith           Usually this is the same matrix used also to set the interpolation
1614b9ad928SBarry Smith     for the same level.
1624b9ad928SBarry Smith 
1634b9ad928SBarry Smith           One can pass in the interpolation matrix or its transpose; PETSc figures
1644b9ad928SBarry Smith     out from the matrix size which one it is.
1654b9ad928SBarry Smith 
166aea2a34eSBarry Smith          If you do not set this, the transpose of the Mat set with PCMGSetInterpolation()
167fccaa45eSBarry Smith     is used.
168fccaa45eSBarry Smith 
1694b9ad928SBarry Smith .keywords: MG, set, multigrid, restriction, level
1704b9ad928SBarry Smith 
171aea2a34eSBarry Smith .seealso: PCMGSetInterpolation()
1724b9ad928SBarry Smith @*/
1737087cfbeSBarry Smith PetscErrorCode  PCMGSetRestriction(PC pc,PetscInt l,Mat mat)
1744b9ad928SBarry Smith {
175fccaa45eSBarry Smith   PetscErrorCode ierr;
176f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
177f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
1784b9ad928SBarry Smith 
1794b9ad928SBarry Smith   PetscFunctionBegin;
180c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
181e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
182e7e72b3dSBarry Smith   if (!l) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Do not set restriction routine for coarsest level");
183c3122656SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
1846bf464f9SBarry Smith   ierr = MatDestroy(&mglevels[l]->restrct);CHKERRQ(ierr);
185f3fbd535SBarry Smith   mglevels[l]->restrct  = mat;
1864b9ad928SBarry Smith   PetscFunctionReturn(0);
1874b9ad928SBarry Smith }
1884b9ad928SBarry Smith 
1894b9ad928SBarry Smith #undef __FUNCT__
190*73250ac0SBarry Smith #define __FUNCT__ "PCMGSetRScale"
191*73250ac0SBarry Smith /*@
192*73250ac0SBarry Smith    PCMGSetRScale - Sets the pointwise scaling for the restriction operator from level l to l-1.
193*73250ac0SBarry Smith 
194*73250ac0SBarry Smith    Logically Collective on PC and Mat
195*73250ac0SBarry Smith 
196*73250ac0SBarry Smith    Input Parameters:
197*73250ac0SBarry Smith +  pc - the multigrid context
198*73250ac0SBarry Smith .  rscale - the scaling
199*73250ac0SBarry Smith -  l - the level (0 is coarsest) to supply [Do not supply 0]
200*73250ac0SBarry Smith 
201*73250ac0SBarry Smith    Level: advanced
202*73250ac0SBarry Smith 
203*73250ac0SBarry Smith    Notes:
204*73250ac0SBarry 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.
205*73250ac0SBarry Smith 
206*73250ac0SBarry Smith .keywords: MG, set, multigrid, restriction, level
207*73250ac0SBarry Smith 
208*73250ac0SBarry Smith .seealso: PCMGSetInterpolation(), PCMGSetRestriction()
209*73250ac0SBarry Smith @*/
210*73250ac0SBarry Smith PetscErrorCode  PCMGSetRScale(PC pc,PetscInt l,Vec rscale)
211*73250ac0SBarry Smith {
212*73250ac0SBarry Smith   PetscErrorCode ierr;
213*73250ac0SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
214*73250ac0SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
215*73250ac0SBarry Smith 
216*73250ac0SBarry Smith   PetscFunctionBegin;
217*73250ac0SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
218*73250ac0SBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
219*73250ac0SBarry Smith   if (!l) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Do not set restriction routine for coarsest level");
220*73250ac0SBarry Smith   ierr = PetscObjectReference((PetscObject)rscale);CHKERRQ(ierr);
221*73250ac0SBarry Smith   ierr = VecDestroy(&mglevels[l]->rscale);CHKERRQ(ierr);
222*73250ac0SBarry Smith   mglevels[l]->rscale  = rscale;
223*73250ac0SBarry Smith   PetscFunctionReturn(0);
224*73250ac0SBarry Smith }
225*73250ac0SBarry Smith 
226*73250ac0SBarry Smith #undef __FUNCT__
227d6337fedSHong Zhang #define __FUNCT__ "PCMGGetSmoother"
228f39d8e23SSatish Balay /*@
22997177400SBarry Smith    PCMGGetSmoother - Gets the KSP context to be used as smoother for
23097177400SBarry Smith    both pre- and post-smoothing.  Call both PCMGGetSmootherUp() and
23197177400SBarry Smith    PCMGGetSmootherDown() to use different functions for pre- and
2324b9ad928SBarry Smith    post-smoothing.
2334b9ad928SBarry Smith 
2344b9ad928SBarry Smith    Not Collective, KSP returned is parallel if PC is
2354b9ad928SBarry Smith 
2364b9ad928SBarry Smith    Input Parameters:
2374b9ad928SBarry Smith +  pc - the multigrid context
2384b9ad928SBarry Smith -  l - the level (0 is coarsest) to supply
2394b9ad928SBarry Smith 
2404b9ad928SBarry Smith    Ouput Parameters:
2414b9ad928SBarry Smith .  ksp - the smoother
2424b9ad928SBarry Smith 
2434b9ad928SBarry Smith    Level: advanced
2444b9ad928SBarry Smith 
2454b9ad928SBarry Smith .keywords: MG, get, multigrid, level, smoother, pre-smoother, post-smoother
2464b9ad928SBarry Smith 
24797177400SBarry Smith .seealso: PCMGGetSmootherUp(), PCMGGetSmootherDown()
2484b9ad928SBarry Smith @*/
2497087cfbeSBarry Smith PetscErrorCode  PCMGGetSmoother(PC pc,PetscInt l,KSP *ksp)
2504b9ad928SBarry Smith {
251f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
252f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
2534b9ad928SBarry Smith 
2544b9ad928SBarry Smith   PetscFunctionBegin;
255c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
256f3fbd535SBarry Smith   *ksp = mglevels[l]->smoothd;
2574b9ad928SBarry Smith   PetscFunctionReturn(0);
2584b9ad928SBarry Smith }
2594b9ad928SBarry Smith 
2604b9ad928SBarry Smith #undef __FUNCT__
261d6337fedSHong Zhang #define __FUNCT__ "PCMGGetSmootherUp"
262f39d8e23SSatish Balay /*@
26397177400SBarry Smith    PCMGGetSmootherUp - Gets the KSP context to be used as smoother after
2644b9ad928SBarry Smith    coarse grid correction (post-smoother).
2654b9ad928SBarry Smith 
2664b9ad928SBarry Smith    Not Collective, KSP returned is parallel if PC is
2674b9ad928SBarry Smith 
2684b9ad928SBarry Smith    Input Parameters:
2694b9ad928SBarry Smith +  pc - the multigrid context
2704b9ad928SBarry Smith -  l  - the level (0 is coarsest) to supply
2714b9ad928SBarry Smith 
2724b9ad928SBarry Smith    Ouput Parameters:
2734b9ad928SBarry Smith .  ksp - the smoother
2744b9ad928SBarry Smith 
2754b9ad928SBarry Smith    Level: advanced
2764b9ad928SBarry Smith 
2774b9ad928SBarry Smith .keywords: MG, multigrid, get, smoother, up, post-smoother, level
2784b9ad928SBarry Smith 
27997177400SBarry Smith .seealso: PCMGGetSmootherUp(), PCMGGetSmootherDown()
2804b9ad928SBarry Smith @*/
2817087cfbeSBarry Smith PetscErrorCode  PCMGGetSmootherUp(PC pc,PetscInt l,KSP *ksp)
2824b9ad928SBarry Smith {
283f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
284f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
285dfbe8321SBarry Smith   PetscErrorCode ierr;
286f69a0ea3SMatthew Knepley   const char     *prefix;
2874b9ad928SBarry Smith   MPI_Comm       comm;
2884b9ad928SBarry Smith 
2894b9ad928SBarry Smith   PetscFunctionBegin;
290c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
2914b9ad928SBarry Smith   /*
2924b9ad928SBarry Smith      This is called only if user wants a different pre-smoother from post.
2934b9ad928SBarry Smith      Thus we check if a different one has already been allocated,
2944b9ad928SBarry Smith      if not we allocate it.
2954b9ad928SBarry Smith   */
296e7e72b3dSBarry Smith   if (!l) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_OUTOFRANGE,"There is no such thing as a up smoother on the coarse grid");
297f3fbd535SBarry Smith   if (mglevels[l]->smoothu == mglevels[l]->smoothd) {
298f3fbd535SBarry Smith     ierr = PetscObjectGetComm((PetscObject)mglevels[l]->smoothd,&comm);CHKERRQ(ierr);
299f3fbd535SBarry Smith     ierr = KSPGetOptionsPrefix(mglevels[l]->smoothd,&prefix);CHKERRQ(ierr);
300f3fbd535SBarry Smith     ierr = KSPCreate(comm,&mglevels[l]->smoothu);CHKERRQ(ierr);
301f3fbd535SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[l]->smoothu,(PetscObject)pc,mglevels[0]->levels-l);CHKERRQ(ierr);
302f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[l]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,1);CHKERRQ(ierr);
303f3fbd535SBarry Smith     ierr = KSPSetOptionsPrefix(mglevels[l]->smoothu,prefix);CHKERRQ(ierr);
304f3fbd535SBarry Smith     ierr = PetscLogObjectParent(pc,mglevels[l]->smoothu);CHKERRQ(ierr);
3054b9ad928SBarry Smith   }
306f3fbd535SBarry Smith   if (ksp) *ksp = mglevels[l]->smoothu;
3074b9ad928SBarry Smith   PetscFunctionReturn(0);
3084b9ad928SBarry Smith }
3094b9ad928SBarry Smith 
3104b9ad928SBarry Smith #undef __FUNCT__
3119dcbbd2bSBarry Smith #define __FUNCT__ "PCMGGetSmootherDown"
312f39d8e23SSatish Balay /*@
31397177400SBarry Smith    PCMGGetSmootherDown - Gets the KSP context to be used as smoother before
3144b9ad928SBarry Smith    coarse grid correction (pre-smoother).
3154b9ad928SBarry Smith 
3164b9ad928SBarry Smith    Not Collective, KSP returned is parallel if PC is
3174b9ad928SBarry Smith 
3184b9ad928SBarry Smith    Input Parameters:
3194b9ad928SBarry Smith +  pc - the multigrid context
3204b9ad928SBarry Smith -  l  - the level (0 is coarsest) to supply
3214b9ad928SBarry Smith 
3224b9ad928SBarry Smith    Ouput Parameters:
3234b9ad928SBarry Smith .  ksp - the smoother
3244b9ad928SBarry Smith 
3254b9ad928SBarry Smith    Level: advanced
3264b9ad928SBarry Smith 
3274b9ad928SBarry Smith .keywords: MG, multigrid, get, smoother, down, pre-smoother, level
3284b9ad928SBarry Smith 
32997177400SBarry Smith .seealso: PCMGGetSmootherUp(), PCMGGetSmoother()
3304b9ad928SBarry Smith @*/
3317087cfbeSBarry Smith PetscErrorCode  PCMGGetSmootherDown(PC pc,PetscInt l,KSP *ksp)
3324b9ad928SBarry Smith {
333dfbe8321SBarry Smith   PetscErrorCode ierr;
334f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
335f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
3364b9ad928SBarry Smith 
3374b9ad928SBarry Smith   PetscFunctionBegin;
338c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
3394b9ad928SBarry Smith   /* make sure smoother up and down are different */
340c5eb9154SBarry Smith   if (l) {
34197177400SBarry Smith     ierr = PCMGGetSmootherUp(pc,l,PETSC_NULL);CHKERRQ(ierr);
342d8148a5aSMatthew Knepley   }
343f3fbd535SBarry Smith   *ksp = mglevels[l]->smoothd;
3444b9ad928SBarry Smith   PetscFunctionReturn(0);
3454b9ad928SBarry Smith }
3464b9ad928SBarry Smith 
3474b9ad928SBarry Smith #undef __FUNCT__
3489dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetCyclesOnLevel"
3494b9ad928SBarry Smith /*@
35097177400SBarry Smith    PCMGSetCyclesOnLevel - Sets the number of cycles to run on this level.
3514b9ad928SBarry Smith 
352ad4df100SBarry Smith    Logically Collective on PC
3534b9ad928SBarry Smith 
3544b9ad928SBarry Smith    Input Parameters:
3554b9ad928SBarry Smith +  pc - the multigrid context
3564b9ad928SBarry Smith .  l  - the level (0 is coarsest) this is to be used for
3574b9ad928SBarry Smith -  n  - the number of cycles
3584b9ad928SBarry Smith 
3594b9ad928SBarry Smith    Level: advanced
3604b9ad928SBarry Smith 
3614b9ad928SBarry Smith .keywords: MG, multigrid, set, cycles, V-cycle, W-cycle, level
3624b9ad928SBarry Smith 
36397177400SBarry Smith .seealso: PCMGSetCycles()
3644b9ad928SBarry Smith @*/
3657087cfbeSBarry Smith PetscErrorCode  PCMGSetCyclesOnLevel(PC pc,PetscInt l,PetscInt c)
3664b9ad928SBarry Smith {
367f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
368f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
3694b9ad928SBarry Smith 
3704b9ad928SBarry Smith   PetscFunctionBegin;
371c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
372e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
373c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,l,2);
374c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,c,3);
375f3fbd535SBarry Smith   mglevels[l]->cycles  = c;
3764b9ad928SBarry Smith   PetscFunctionReturn(0);
3774b9ad928SBarry Smith }
3784b9ad928SBarry Smith 
3794b9ad928SBarry Smith #undef __FUNCT__
380d6337fedSHong Zhang #define __FUNCT__ "PCMGSetRhs"
3814b9ad928SBarry Smith /*@
38297177400SBarry Smith    PCMGSetRhs - Sets the vector space to be used to store the right-hand side
383fccaa45eSBarry Smith    on a particular level.
3844b9ad928SBarry Smith 
385ad4df100SBarry Smith    Logically Collective on PC and Vec
3864b9ad928SBarry Smith 
3874b9ad928SBarry Smith    Input Parameters:
3884b9ad928SBarry Smith +  pc - the multigrid context
3894b9ad928SBarry Smith .  l  - the level (0 is coarsest) this is to be used for
3904b9ad928SBarry Smith -  c  - the space
3914b9ad928SBarry Smith 
3924b9ad928SBarry Smith    Level: advanced
3934b9ad928SBarry Smith 
394fccaa45eSBarry Smith    Notes: If this is not provided PETSc will automatically generate one.
395fccaa45eSBarry Smith 
396fccaa45eSBarry Smith           You do not need to keep a reference to this vector if you do
397fccaa45eSBarry Smith           not need it PCDestroy() will properly free it.
398fccaa45eSBarry Smith 
3994b9ad928SBarry Smith .keywords: MG, multigrid, set, right-hand-side, rhs, level
4004b9ad928SBarry Smith 
40197177400SBarry Smith .seealso: PCMGSetX(), PCMGSetR()
4024b9ad928SBarry Smith @*/
4037087cfbeSBarry Smith PetscErrorCode  PCMGSetRhs(PC pc,PetscInt l,Vec c)
4044b9ad928SBarry Smith {
405fccaa45eSBarry Smith   PetscErrorCode ierr;
406f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
407f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
4084b9ad928SBarry Smith 
4094b9ad928SBarry Smith   PetscFunctionBegin;
410c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
411e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
412e7e72b3dSBarry Smith   if (l == mglevels[0]->levels-1) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_INCOMP,"Do not set rhs for finest level");
413c3122656SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr);
4146bf464f9SBarry Smith   ierr = VecDestroy(&mglevels[l]->b);CHKERRQ(ierr);
415f3fbd535SBarry Smith   mglevels[l]->b  = c;
4164b9ad928SBarry Smith   PetscFunctionReturn(0);
4174b9ad928SBarry Smith }
4184b9ad928SBarry Smith 
4194b9ad928SBarry Smith #undef __FUNCT__
420d6337fedSHong Zhang #define __FUNCT__ "PCMGSetX"
4214b9ad928SBarry Smith /*@
42297177400SBarry Smith    PCMGSetX - Sets the vector space to be used to store the solution on a
423fccaa45eSBarry Smith    particular level.
4244b9ad928SBarry Smith 
425ad4df100SBarry Smith    Logically Collective on PC and Vec
4264b9ad928SBarry Smith 
4274b9ad928SBarry Smith    Input Parameters:
4284b9ad928SBarry Smith +  pc - the multigrid context
4294b9ad928SBarry Smith .  l - the level (0 is coarsest) this is to be used for
4304b9ad928SBarry Smith -  c - the space
4314b9ad928SBarry Smith 
4324b9ad928SBarry Smith    Level: advanced
4334b9ad928SBarry Smith 
434fccaa45eSBarry Smith    Notes: If this is not provided PETSc will automatically generate one.
435fccaa45eSBarry Smith 
436fccaa45eSBarry Smith           You do not need to keep a reference to this vector if you do
437fccaa45eSBarry Smith           not need it PCDestroy() will properly free it.
438fccaa45eSBarry Smith 
4394b9ad928SBarry Smith .keywords: MG, multigrid, set, solution, level
4404b9ad928SBarry Smith 
44197177400SBarry Smith .seealso: PCMGSetRhs(), PCMGSetR()
4424b9ad928SBarry Smith @*/
4437087cfbeSBarry Smith PetscErrorCode  PCMGSetX(PC pc,PetscInt l,Vec c)
4444b9ad928SBarry Smith {
445fccaa45eSBarry Smith   PetscErrorCode ierr;
446f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
447f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
4484b9ad928SBarry Smith 
4494b9ad928SBarry Smith   PetscFunctionBegin;
450c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
451e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
452e7e72b3dSBarry Smith   if (l == mglevels[0]->levels-1) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_INCOMP,"Do not set x for finest level");
453c3122656SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr);
4546bf464f9SBarry Smith   ierr = VecDestroy(&mglevels[l]->x);CHKERRQ(ierr);
455f3fbd535SBarry Smith   mglevels[l]->x  = c;
4564b9ad928SBarry Smith   PetscFunctionReturn(0);
4574b9ad928SBarry Smith }
4584b9ad928SBarry Smith 
4594b9ad928SBarry Smith #undef __FUNCT__
460d6337fedSHong Zhang #define __FUNCT__ "PCMGSetR"
4614b9ad928SBarry Smith /*@
46297177400SBarry Smith    PCMGSetR - Sets the vector space to be used to store the residual on a
463fccaa45eSBarry Smith    particular level.
4644b9ad928SBarry Smith 
465ad4df100SBarry Smith    Logically Collective on PC and Vec
4664b9ad928SBarry Smith 
4674b9ad928SBarry Smith    Input Parameters:
4684b9ad928SBarry Smith +  pc - the multigrid context
4694b9ad928SBarry Smith .  l - the level (0 is coarsest) this is to be used for
4704b9ad928SBarry Smith -  c - the space
4714b9ad928SBarry Smith 
4724b9ad928SBarry Smith    Level: advanced
4734b9ad928SBarry Smith 
474fccaa45eSBarry Smith    Notes: If this is not provided PETSc will automatically generate one.
475fccaa45eSBarry Smith 
476fccaa45eSBarry Smith           You do not need to keep a reference to this vector if you do
477fccaa45eSBarry Smith           not need it PCDestroy() will properly free it.
478fccaa45eSBarry Smith 
4794b9ad928SBarry Smith .keywords: MG, multigrid, set, residual, level
4804b9ad928SBarry Smith @*/
4817087cfbeSBarry Smith PetscErrorCode  PCMGSetR(PC pc,PetscInt l,Vec c)
4824b9ad928SBarry Smith {
483fccaa45eSBarry Smith   PetscErrorCode ierr;
484f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
485f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
4864b9ad928SBarry Smith 
4874b9ad928SBarry Smith   PetscFunctionBegin;
488c5eb9154SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
489e7e72b3dSBarry Smith   if (!mglevels) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
490e7e72b3dSBarry Smith   if (!l) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Need not set residual vector for coarse grid");
491c3122656SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr);
4926bf464f9SBarry Smith   ierr = VecDestroy(&mglevels[l]->r);CHKERRQ(ierr);
493f3fbd535SBarry Smith   mglevels[l]->r  = c;
4944b9ad928SBarry Smith   PetscFunctionReturn(0);
4954b9ad928SBarry Smith }
496