xref: /petsc/src/ksp/pc/impls/mg/fmg.c (revision 72ce74bd30d5d09d1371a3eb387704f4af395d1a)
1 /*
2      Full multigrid using either additive or multiplicative V or W cycle
3 */
4 #include "src/ksp/pc/impls/mg/mgimpl.h"
5 
6 EXTERN PetscErrorCode MGMCycle_Private(MG *,PetscTruth*);
7 
8 /*
9        MGFCycle_Private - Given an MG structure created with MGCreate() runs
10                full multigrid.
11 
12     Iput Parameters:
13 .   mg - structure created with MGCreate().
14 
15     Note: This may not be what others call full multigrid. What we
16           do is restrict the rhs to all levels, then starting
17           on the coarsest level work our way up generating
18           initial guess for the next level. This provides an
19           improved preconditioner but not a great improvement.
20 */
21 #undef __FUNCT__
22 #define __FUNCT__ "MGFCycle_Private"
23 PetscErrorCode MGFCycle_Private(MG *mg)
24 {
25   int    i,l = mg[0]->levels,ierr;
26   PetscScalar zero = 0.0;
27 
28   PetscFunctionBegin;
29   /* restrict the RHS through all levels to coarsest. */
30   for (i=l-1; i>0; i--){
31     ierr = MatRestrict(mg[i]->restrct,mg[i]->b,mg[i-1]->b);CHKERRQ(ierr);
32   }
33 
34   /* work our way up through the levels */
35   ierr = VecSet(&zero,mg[0]->x);CHKERRQ(ierr);
36   for (i=0; i<l-1; i++) {
37     ierr = MGMCycle_Private(&mg[i],PETSC_NULL);CHKERRQ(ierr);
38     ierr = MatInterpolate(mg[i+1]->interpolate,mg[i]->x,mg[i+1]->x);CHKERRQ(ierr);
39   }
40   ierr = MGMCycle_Private(&mg[l-1],PETSC_NULL);CHKERRQ(ierr);
41   PetscFunctionReturn(0);
42 }
43 
44 /*
45        MGKCycle_Private - Given an MG structure created with MGCreate() runs
46                full Kascade MG solve.
47 
48     Iput Parameters:
49 .   mg - structure created with MGCreate().
50 
51     Note: This may not be what others call Kascadic MG.
52 */
53 #undef __FUNCT__
54 #define __FUNCT__ "MGKCycle_Private"
55 PetscErrorCode MGKCycle_Private(MG *mg)
56 {
57   int    i,l = mg[0]->levels,ierr;
58   PetscScalar zero = 0.0;
59 
60   PetscFunctionBegin;
61   /* restrict the RHS through all levels to coarsest. */
62   for (i=l-1; i>0; i--){
63     ierr = MatRestrict(mg[i]->restrct,mg[i]->b,mg[i-1]->b);CHKERRQ(ierr);
64   }
65 
66   /* work our way up through the levels */
67   ierr = VecSet(&zero,mg[0]->x);CHKERRQ(ierr);
68   for (i=0; i<l-1; i++) {
69     if (mg[i]->eventsolve) {ierr = PetscLogEventBegin(mg[i]->eventsolve,0,0,0,0);CHKERRQ(ierr);}
70     ierr = KSPSolve(mg[i]->smoothd,mg[i]->b,mg[i]->x);CHKERRQ(ierr);
71     if (mg[i]->eventsolve) {ierr = PetscLogEventEnd(mg[i]->eventsolve,0,0,0,0);CHKERRQ(ierr);}
72     ierr = MatInterpolate(mg[i+1]->interpolate,mg[i]->x,mg[i+1]->x);CHKERRQ(ierr);
73   }
74   if (mg[l-1]->eventsolve) {ierr = PetscLogEventBegin(mg[l-1]->eventsolve,0,0,0,0);CHKERRQ(ierr);}
75   ierr = KSPSolve(mg[l-1]->smoothd,mg[l-1]->b,mg[l-1]->x);CHKERRQ(ierr);
76   if (mg[l-1]->eventsolve) {ierr = PetscLogEventEnd(mg[l-1]->eventsolve,0,0,0,0);CHKERRQ(ierr);}
77 
78   PetscFunctionReturn(0);
79 }
80 
81 
82