# petsc / src / ksp / ksp / examples / tests / ex19.c

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226``` ``` static char help[] ="Solvers Laplacian with multigrid, bad way.\n\ -mx , where = number of grid points in the x-direction\n\ -my , where = number of grid points in the y-direction\n\ -Nx , where = number of processors in the x-direction\n\ -Ny , where = number of processors in the y-direction\n\n"; /* This problem is modeled by the partial differential equation -Laplacian u = g, 0 < x,y < 1, with boundary conditions u = 0 for x = 0, x = 1, y = 0, y = 1. A finite difference approximation with the usual 5-point stencil is used to discretize the boundary value problem to obtain a nonlinear system of equations. */ #include #include #include /* User-defined application contexts */ typedef struct { PetscInt mx,my; /* number grid points in x and y direction */ Vec localX,localF; /* local vectors with ghost region */ DM da; Vec x,b,r; /* global vectors */ Mat J; /* Jacobian on grid */ } GridCtx; typedef struct { GridCtx fine; GridCtx coarse; KSP ksp_coarse; PetscInt ratio; Mat Ii; /* interpolation from coarse to fine */ } AppCtx; #define COARSE_LEVEL 0 #define FINE_LEVEL 1 extern int FormJacobian_Grid(AppCtx*,GridCtx*,Mat*); /* Mm_ratio - ration of grid lines between fine and coarse grids. */ #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **argv) { AppCtx user; PetscErrorCode ierr; PetscInt its,N,n,Nx = PETSC_DECIDE,Ny = PETSC_DECIDE,nlocal,Nlocal; PetscMPIInt size; KSP ksp,ksp_fine; PC pc; PetscScalar one = 1.0; PetscInitialize(&argc,&argv,NULL,help); user.ratio = 2; user.coarse.mx = 5; user.coarse.my = 5; ierr = PetscOptionsGetInt(NULL,"-Mx",&user.coarse.mx,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,"-My",&user.coarse.my,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,"-ratio",&user.ratio,NULL);CHKERRQ(ierr); user.fine.mx = user.ratio*(user.coarse.mx-1)+1; user.fine.my = user.ratio*(user.coarse.my-1)+1; PetscPrintf(PETSC_COMM_WORLD,"Coarse grid size %D by %D\n",user.coarse.mx,user.coarse.my); PetscPrintf(PETSC_COMM_WORLD,"Fine grid size %D by %D\n",user.fine.mx,user.fine.my); n = user.fine.mx*user.fine.my; N = user.coarse.mx*user.coarse.my; MPI_Comm_size(PETSC_COMM_WORLD,&size); ierr = PetscOptionsGetInt(NULL,"-Nx",&Nx,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,"-Ny",&Ny,NULL);CHKERRQ(ierr); /* Set up distributed array for fine grid */ ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,user.fine.mx, user.fine.my,Nx,Ny,1,1,NULL,NULL,&user.fine.da);CHKERRQ(ierr); ierr = DMCreateGlobalVector(user.fine.da,&user.fine.x);CHKERRQ(ierr); ierr = VecDuplicate(user.fine.x,&user.fine.r);CHKERRQ(ierr); ierr = VecDuplicate(user.fine.x,&user.fine.b);CHKERRQ(ierr); ierr = VecGetLocalSize(user.fine.x,&nlocal);CHKERRQ(ierr); ierr = DMCreateLocalVector(user.fine.da,&user.fine.localX);CHKERRQ(ierr); ierr = VecDuplicate(user.fine.localX,&user.fine.localF);CHKERRQ(ierr); ierr = MatCreateAIJ(PETSC_COMM_WORLD,nlocal,nlocal,n,n,5,NULL,3,NULL,&user.fine.J);CHKERRQ(ierr); /* Set up distributed array for coarse grid */ ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,user.coarse.mx, user.coarse.my,Nx,Ny,1,1,NULL,NULL,&user.coarse.da);CHKERRQ(ierr); ierr = DMCreateGlobalVector(user.coarse.da,&user.coarse.x);CHKERRQ(ierr); ierr = VecDuplicate(user.coarse.x,&user.coarse.b);CHKERRQ(ierr); ierr = VecGetLocalSize(user.coarse.x,&Nlocal);CHKERRQ(ierr); ierr = DMCreateLocalVector(user.coarse.da,&user.coarse.localX);CHKERRQ(ierr); ierr = VecDuplicate(user.coarse.localX,&user.coarse.localF);CHKERRQ(ierr); ierr = MatCreateAIJ(PETSC_COMM_WORLD,Nlocal,Nlocal,N,N,5,NULL,3,NULL,&user.coarse.J);CHKERRQ(ierr); /* Create linear solver */ ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); /* set two level additive Schwarz preconditioner */ ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCMG);CHKERRQ(ierr); ierr = PCMGSetLevels(pc,2,NULL);CHKERRQ(ierr); ierr = PCMGSetType(pc,PC_MG_ADDITIVE);CHKERRQ(ierr); ierr = FormJacobian_Grid(&user,&user.coarse,&user.coarse.J);CHKERRQ(ierr); ierr = FormJacobian_Grid(&user,&user.fine,&user.fine.J);CHKERRQ(ierr); /* Create coarse level */ ierr = PCMGGetCoarseSolve(pc,&user.ksp_coarse);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(user.ksp_coarse,"coarse_");CHKERRQ(ierr); ierr = KSPSetFromOptions(user.ksp_coarse);CHKERRQ(ierr); ierr = KSPSetOperators(user.ksp_coarse,user.coarse.J,user.coarse.J,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); ierr = PCMGSetX(pc,COARSE_LEVEL,user.coarse.x);CHKERRQ(ierr); ierr = PCMGSetRhs(pc,COARSE_LEVEL,user.coarse.b);CHKERRQ(ierr); /* Create fine level */ ierr = PCMGGetSmoother(pc,FINE_LEVEL,&ksp_fine);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(ksp_fine,"fine_");CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp_fine);CHKERRQ(ierr); ierr = KSPSetOperators(ksp_fine,user.fine.J,user.fine.J,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); ierr = PCMGSetR(pc,FINE_LEVEL,user.fine.r);CHKERRQ(ierr); /* Create interpolation between the levels */ ierr = DMCreateInterpolation(user.coarse.da,user.fine.da,&user.Ii,NULL);CHKERRQ(ierr); ierr = PCMGSetInterpolation(pc,FINE_LEVEL,user.Ii);CHKERRQ(ierr); ierr = PCMGSetRestriction(pc,FINE_LEVEL,user.Ii);CHKERRQ(ierr); ierr = KSPSetOperators(ksp,user.fine.J,user.fine.J,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); ierr = VecSet(user.fine.b,one);CHKERRQ(ierr); { PetscRandom rdm; ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rdm);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rdm);CHKERRQ(ierr); ierr = VecSetRandom(user.fine.b,rdm);CHKERRQ(ierr); ierr = PetscRandomDestroy(&rdm);CHKERRQ(ierr); } /* Set options, then solve nonlinear system */ ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSolve(ksp,user.fine.b,user.fine.x);CHKERRQ(ierr); ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %D\n",its);CHKERRQ(ierr); /* Free data structures */ ierr = MatDestroy(&user.fine.J);CHKERRQ(ierr); ierr = VecDestroy(&user.fine.x);CHKERRQ(ierr); ierr = VecDestroy(&user.fine.r);CHKERRQ(ierr); ierr = VecDestroy(&user.fine.b);CHKERRQ(ierr); ierr = DMDestroy(&user.fine.da);CHKERRQ(ierr); ierr = VecDestroy(&user.fine.localX);CHKERRQ(ierr); ierr = VecDestroy(&user.fine.localF);CHKERRQ(ierr); ierr = MatDestroy(&user.coarse.J);CHKERRQ(ierr); ierr = VecDestroy(&user.coarse.x);CHKERRQ(ierr); ierr = VecDestroy(&user.coarse.b);CHKERRQ(ierr); ierr = DMDestroy(&user.coarse.da);CHKERRQ(ierr); ierr = VecDestroy(&user.coarse.localX);CHKERRQ(ierr); ierr = VecDestroy(&user.coarse.localF);CHKERRQ(ierr); ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = MatDestroy(&user.Ii);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; } #undef __FUNCT__ #define __FUNCT__ "FormJacobian_Grid" int FormJacobian_Grid(AppCtx *user,GridCtx *grid,Mat *J) { Mat jac = *J; PetscErrorCode ierr; PetscInt i,j,row,mx,my,xs,ys,xm,ym,Xs,Ys,Xm,Ym,col[5]; PetscInt nloc,grow; const PetscInt *ltog; PetscScalar two = 2.0,one = 1.0,v[5],hx,hy,hxdhy,hydhx,value; mx = grid->mx; my = grid->my; hx = one/(PetscReal)(mx-1); hy = one/(PetscReal)(my-1); hxdhy = hx/hy; hydhx = hy/hx; /* Get ghost points */ ierr = DMDAGetCorners(grid->da,&xs,&ys,0,&xm,&ym,0);CHKERRQ(ierr); ierr = DMDAGetGhostCorners(grid->da,&Xs,&Ys,0,&Xm,&Ym,0);CHKERRQ(ierr); ierr = DMDAGetGlobalIndices(grid->da,&nloc,<og);CHKERRQ(ierr); /* Evaluate Jacobian of function */ for (j=ys; j 0 && i < mx-1 && j > 0 && j < my-1) { v[0] = -hxdhy; col[0] = ltog[row - Xm]; v[1] = -hydhx; col[1] = ltog[row - 1]; v[2] = two*(hydhx + hxdhy); col[2] = grow; v[3] = -hydhx; col[3] = ltog[row + 1]; v[4] = -hxdhy; col[4] = ltog[row + Xm]; ierr = MatSetValues(jac,1,&grow,5,col,v,INSERT_VALUES);CHKERRQ(ierr); } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)) { value = .5*two*(hydhx + hxdhy); ierr = MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);CHKERRQ(ierr); } else { value = .25*two*(hydhx + hxdhy); ierr = MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);CHKERRQ(ierr); } } } ierr = DMDARestoreGlobalIndices(grid->da,&nloc,<og);CHKERRQ(ierr); ierr = MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); return 0; } ```