1*d8606c27SBarry Smith! 2*d8606c27SBarry Smith! Tests PCMGSetResidual 3*d8606c27SBarry Smith! 4*d8606c27SBarry Smith! ----------------------------------------------------------------------- 5*d8606c27SBarry Smith 6*d8606c27SBarry Smith program main 7*d8606c27SBarry Smith#include <petsc/finclude/petscksp.h> 8*d8606c27SBarry Smith use petscksp 9*d8606c27SBarry Smith implicit none 10*d8606c27SBarry Smith 11*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12*d8606c27SBarry Smith! Variable declarations 13*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 14*d8606c27SBarry Smith! 15*d8606c27SBarry Smith! Variables: 16*d8606c27SBarry Smith! ksp - linear solver context 17*d8606c27SBarry Smith! x, b, u - approx solution, right-hand-side, exact solution vectors 18*d8606c27SBarry Smith! A - matrix that defines linear system 19*d8606c27SBarry Smith! its - iterations for convergence 20*d8606c27SBarry Smith! norm - norm of error in solution 21*d8606c27SBarry Smith! rctx - random number context 22*d8606c27SBarry Smith! 23*d8606c27SBarry Smith 24*d8606c27SBarry Smith Mat A 25*d8606c27SBarry Smith Vec x,b,u 26*d8606c27SBarry Smith PC pc 27*d8606c27SBarry Smith PetscInt n,dim,istart,iend 28*d8606c27SBarry Smith PetscInt i,j,jj,ii,one,zero 29*d8606c27SBarry Smith PetscErrorCode ierr 30*d8606c27SBarry Smith PetscScalar v 31*d8606c27SBarry Smith external MyResidual 32*d8606c27SBarry Smith PetscScalar pfive 33*d8606c27SBarry Smith KSP ksp 34*d8606c27SBarry Smith 35*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 36*d8606c27SBarry Smith! Beginning of program 37*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 38*d8606c27SBarry Smith 39*d8606c27SBarry Smith PetscCallA(PetscInitialize(ierr)) 40*d8606c27SBarry Smith pfive = .5 41*d8606c27SBarry Smith n = 6 42*d8606c27SBarry Smith dim = n*n 43*d8606c27SBarry Smith one = 1 44*d8606c27SBarry Smith zero = 0 45*d8606c27SBarry Smith 46*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 47*d8606c27SBarry Smith! Compute the matrix and right-hand-side vector that define 48*d8606c27SBarry Smith! the linear system, Ax = b. 49*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 50*d8606c27SBarry Smith 51*d8606c27SBarry Smith! Create parallel matrix, specifying only its global dimensions. 52*d8606c27SBarry Smith! When using MatCreate(), the matrix format can be specified at 53*d8606c27SBarry Smith! runtime. Also, the parallel partitioning of the matrix is 54*d8606c27SBarry Smith! determined by PETSc at runtime. 55*d8606c27SBarry Smith 56*d8606c27SBarry Smith PetscCallA(MatCreate(PETSC_COMM_WORLD,A,ierr)) 57*d8606c27SBarry Smith PetscCallA(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,dim,dim,ierr)) 58*d8606c27SBarry Smith PetscCallA(MatSetFromOptions(A,ierr)) 59*d8606c27SBarry Smith PetscCallA(MatSetUp(A,ierr)) 60*d8606c27SBarry Smith 61*d8606c27SBarry Smith! Currently, all PETSc parallel matrix formats are partitioned by 62*d8606c27SBarry Smith! contiguous chunks of rows across the processors. Determine which 63*d8606c27SBarry Smith! rows of the matrix are locally owned. 64*d8606c27SBarry Smith 65*d8606c27SBarry Smith PetscCallA(MatGetOwnershipRange(A,Istart,Iend,ierr)) 66*d8606c27SBarry Smith 67*d8606c27SBarry Smith! Set matrix elements in parallel. 68*d8606c27SBarry Smith! - Each processor needs to insert only elements that it owns 69*d8606c27SBarry Smith! locally (but any non-local elements will be sent to the 70*d8606c27SBarry Smith! appropriate processor during matrix assembly). 71*d8606c27SBarry Smith! - Always specify global rows and columns of matrix entries. 72*d8606c27SBarry Smith 73*d8606c27SBarry Smith do 10, II=Istart,Iend-1 74*d8606c27SBarry Smith v = -1.0 75*d8606c27SBarry Smith i = II/n 76*d8606c27SBarry Smith j = II - i*n 77*d8606c27SBarry Smith if (i.gt.0) then 78*d8606c27SBarry Smith JJ = II - n 79*d8606c27SBarry Smith PetscCallA(MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)) 80*d8606c27SBarry Smith endif 81*d8606c27SBarry Smith if (i.lt.n-1) then 82*d8606c27SBarry Smith JJ = II + n 83*d8606c27SBarry Smith PetscCallA(MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)) 84*d8606c27SBarry Smith endif 85*d8606c27SBarry Smith if (j.gt.0) then 86*d8606c27SBarry Smith JJ = II - 1 87*d8606c27SBarry Smith PetscCallA(MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)) 88*d8606c27SBarry Smith endif 89*d8606c27SBarry Smith if (j.lt.n-1) then 90*d8606c27SBarry Smith JJ = II + 1 91*d8606c27SBarry Smith PetscCallA(MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)) 92*d8606c27SBarry Smith endif 93*d8606c27SBarry Smith v = 4.0 94*d8606c27SBarry Smith PetscCallA( MatSetValues(A,one,II,one,II,v,ADD_VALUES,ierr)) 95*d8606c27SBarry Smith 10 continue 96*d8606c27SBarry Smith 97*d8606c27SBarry Smith! Assemble matrix, using the 2-step process: 98*d8606c27SBarry Smith! MatAssemblyBegin(), MatAssemblyEnd() 99*d8606c27SBarry Smith! Computations can be done while messages are in transition 100*d8606c27SBarry Smith! by placing code between these two statements. 101*d8606c27SBarry Smith 102*d8606c27SBarry Smith PetscCallA(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)) 103*d8606c27SBarry Smith PetscCallA(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)) 104*d8606c27SBarry Smith 105*d8606c27SBarry Smith! Create parallel vectors. 106*d8606c27SBarry Smith! - Here, the parallel partitioning of the vector is determined by 107*d8606c27SBarry Smith! PETSc at runtime. We could also specify the local dimensions 108*d8606c27SBarry Smith! if desired. 109*d8606c27SBarry Smith! - Note: We form 1 vector from scratch and then duplicate as needed. 110*d8606c27SBarry Smith 111*d8606c27SBarry Smith PetscCallA(VecCreate(PETSC_COMM_WORLD,u,ierr)) 112*d8606c27SBarry Smith PetscCallA(VecSetSizes(u,PETSC_DECIDE,dim,ierr)) 113*d8606c27SBarry Smith PetscCallA(VecSetFromOptions(u,ierr)) 114*d8606c27SBarry Smith PetscCallA(VecDuplicate(u,b,ierr)) 115*d8606c27SBarry Smith PetscCallA(VecDuplicate(b,x,ierr)) 116*d8606c27SBarry Smith 117*d8606c27SBarry Smith! Set exact solution; then compute right-hand-side vector. 118*d8606c27SBarry Smith 119*d8606c27SBarry Smith PetscCallA(VecSet(u,pfive,ierr)) 120*d8606c27SBarry Smith PetscCallA(MatMult(A,u,b,ierr)) 121*d8606c27SBarry Smith 122*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 123*d8606c27SBarry Smith! Create the linear solver and set various options 124*d8606c27SBarry Smith! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 125*d8606c27SBarry Smith 126*d8606c27SBarry Smith! Create linear solver context 127*d8606c27SBarry Smith 128*d8606c27SBarry Smith PetscCallA(KSPCreate(PETSC_COMM_WORLD,ksp,ierr)) 129*d8606c27SBarry Smith PetscCallA(KSPGetPC(ksp,pc,ierr)) 130*d8606c27SBarry Smith PetscCallA(PCSetType(pc,PCMG,ierr)) 131*d8606c27SBarry Smith PetscCallA(PCMGSetLevels(pc,one,PETSC_NULL_MPI_COMM,ierr)) 132*d8606c27SBarry Smith PetscCallA(PCMGSetResidual(pc,zero,MyResidual,A,ierr)) 133*d8606c27SBarry Smith 134*d8606c27SBarry Smith! Set operators. Here the matrix that defines the linear system 135*d8606c27SBarry Smith! also serves as the preconditioning matrix. 136*d8606c27SBarry Smith 137*d8606c27SBarry Smith PetscCallA(KSPSetOperators(ksp,A,A,ierr)) 138*d8606c27SBarry Smith 139*d8606c27SBarry Smith PetscCallA(KSPDestroy(ksp,ierr)) 140*d8606c27SBarry Smith PetscCallA(VecDestroy(u,ierr)) 141*d8606c27SBarry Smith PetscCallA(VecDestroy(x,ierr)) 142*d8606c27SBarry Smith PetscCallA(VecDestroy(b,ierr)) 143*d8606c27SBarry Smith PetscCallA(MatDestroy(A,ierr)) 144*d8606c27SBarry Smith 145*d8606c27SBarry Smith PetscCallA(PetscFinalize(ierr)) 146*d8606c27SBarry Smith end 147*d8606c27SBarry Smith 148*d8606c27SBarry Smith subroutine MyResidual(A,b,x,r,ierr) 149*d8606c27SBarry Smith use petscksp 150*d8606c27SBarry Smith implicit none 151*d8606c27SBarry Smith Mat A 152*d8606c27SBarry Smith Vec b,x,r 153*d8606c27SBarry Smith integer ierr 154*d8606c27SBarry Smith return 155*d8606c27SBarry Smith end 156*d8606c27SBarry Smith 157*d8606c27SBarry Smith!/*TEST 158*d8606c27SBarry Smith! 159*d8606c27SBarry Smith! test: 160*d8606c27SBarry Smith! nsize: 1 161*d8606c27SBarry Smith!TEST*/ 162