Actual source code: ex11f.F

petsc-3.7.5 2017-01-01
Report Typos and Errors
  1: !
  2: !  Description: Solves a complex linear system in parallel with KSP (Fortran code).
  3: !
  4: !/*T
  5: !  Concepts: KSP^solving a Helmholtz equation
  6: !  Concepts: complex numbers
  7: !  Processors: n
  8: !T*/
  9: !
 10: !  The model problem:
 11: !     Solve Helmholtz equation on the unit square: (0,1) x (0,1)
 12: !          -delta u - sigma1*u + i*sigma2*u = f,
 13: !           where delta = Laplace operator
 14: !     Dirichlet b.c.'s on all sides
 15: !     Use the 2-D, five-point finite difference stencil.
 16: !
 17: !     Compiling the code:
 18: !      This code uses the complex numbers version of PETSc, so configure
 19: !      must be run to enable this
 20: !
 21: !
 22: ! -----------------------------------------------------------------------

 24:       program main
 25:       implicit none

 27: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 28: !                    Include files
 29: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 30: !
 31: !  The following include statements are required for KSP Fortran programs:
 32: !     petscsys.h       - base PETSc routines
 33: !     petscvec.h    - vectors
 34: !     petscmat.h    - matrices
 35: !     petscpc.h     - preconditioners
 36: !     petscksp.h    - Krylov subspace methods
 37: !  Additional include statements may be needed if using other PETSc
 38: !  routines in a Fortran program, e.g.,
 39: !     petscviewer.h - viewers
 40: !     petscis.h     - index sets
 41: !
 42: #include <petsc/finclude/petscsys.h>
 43: #include <petsc/finclude/petscvec.h>
 44: #include <petsc/finclude/petscmat.h>
 45: #include <petsc/finclude/petscpc.h>
 46: #include <petsc/finclude/petscksp.h>
 47: !
 48: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 49: !                   Variable declarations
 50: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 51: !
 52: !  Variables:
 53: !     ksp     - linear solver context
 54: !     x, b, u  - approx solution, right-hand-side, exact solution vectors
 55: !     A        - matrix that defines linear system
 56: !     its      - iterations for convergence
 57: !     norm     - norm of error in solution
 58: !     rctx     - random number context
 59: !

 61:       KSP             ksp
 62:       Mat              A
 63:       Vec              x,b,u
 64:       PetscRandom      rctx
 65:       PetscReal norm,h2,sigma1
 66:       PetscScalar  none,sigma2,v,pfive,czero
 67:       PetscScalar  cone
 68:       PetscInt dim,its,n,Istart
 69:       PetscInt Iend,i,j,II,JJ,one
 70:       PetscErrorCode ierr
 71:       PetscMPIInt rank
 72:       PetscBool  flg
 73:       logical          use_random

 75: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 76: !                 Beginning of program
 77: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 79:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 80: #if !defined(PETSC_USE_COMPLEX)
 81:       write(6,*) 'This example requires complex numbers.'
 82:       goto 200
 83: #endif

 85:       none   = -1.0
 86:       n      = 6
 87:       sigma1 = 100.0
 88:       czero  = 0.0
 89:       cone   = PETSC_i
 90:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 91:       call PetscOptionsGetReal(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,     &
 92:      &                         '-sigma1',sigma1,flg,ierr)
 93:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,      &
 94:      &                        '-n',n,flg,ierr)
 95:       dim    = n*n

 97: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 98: !      Compute the matrix and right-hand-side vector that define
 99: !      the linear system, Ax = b.
100: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

102: !  Create parallel matrix, specifying only its global dimensions.
103: !  When using MatCreate(), the matrix format can be specified at
104: !  runtime. Also, the parallel partitioning of the matrix is
105: !  determined by PETSc at runtime.

107:       call MatCreate(PETSC_COMM_WORLD,A,ierr)
108:       call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,dim,dim,ierr)
109:       call MatSetFromOptions(A,ierr)
110:       call MatSetUp(A,ierr)

112: !  Currently, all PETSc parallel matrix formats are partitioned by
113: !  contiguous chunks of rows across the processors.  Determine which
114: !  rows of the matrix are locally owned.

116:       call MatGetOwnershipRange(A,Istart,Iend,ierr)

118: !  Set matrix elements in parallel.
119: !   - Each processor needs to insert only elements that it owns
120: !     locally (but any non-local elements will be sent to the
121: !     appropriate processor during matrix assembly).
122: !   - Always specify global rows and columns of matrix entries.

124:       call PetscOptionsHasName(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,   &
125:      &                         '-norandom',flg,ierr)
126:       if (flg) then
127:          use_random = .false.
128:          sigma2 = 10.0*PETSC_i
129:       else
130:          use_random = .true.
131:          call PetscRandomCreate(PETSC_COMM_WORLD,                       &
132:      &        rctx,ierr)
133:          call PetscRandomSetFromOptions(rctx,ierr)
134:          call PetscRandomSetInterval(rctx,czero,cone,ierr)
135:       endif
136:       h2 = 1.0/real((n+1)*(n+1))

138:       one = 1
139:       do 10, II=Istart,Iend-1
140:         v = -1.0
141:         i = II/n
142:         j = II - i*n
143:         if (i.gt.0) then
144:           JJ = II - n
145:           call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
146:         endif
147:         if (i.lt.n-1) then
148:           JJ = II + n
149:           call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
150:         endif
151:         if (j.gt.0) then
152:           JJ = II - 1
153:           call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
154:         endif
155:         if (j.lt.n-1) then
156:           JJ = II + 1
157:           call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
158:         endif
159:         if (use_random) call PetscRandomGetValue(rctx,                          &
160:      &                        sigma2,ierr)
161:         v = 4.0 - sigma1*h2 + sigma2*h2
162:         call  MatSetValues(A,one,II,one,II,v,ADD_VALUES,ierr)
163:  10   continue
164:       if (use_random) call PetscRandomDestroy(rctx,ierr)

166: !  Assemble matrix, using the 2-step process:
167: !       MatAssemblyBegin(), MatAssemblyEnd()
168: !  Computations can be done while messages are in transition
169: !  by placing code between these two statements.

171:       call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
172:       call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)

174: !  Create parallel vectors.
175: !   - Here, the parallel partitioning of the vector is determined by
176: !     PETSc at runtime.  We could also specify the local dimensions
177: !     if desired.
178: !   - Note: We form 1 vector from scratch and then duplicate as needed.

180:       call VecCreate(PETSC_COMM_WORLD,u,ierr)
181:       call VecSetSizes(u,PETSC_DECIDE,dim,ierr)
182:       call VecSetFromOptions(u,ierr)
183:       call VecDuplicate(u,b,ierr)
184:       call VecDuplicate(b,x,ierr)

186: !  Set exact solution; then compute right-hand-side vector.

188:       if (use_random) then
189:          call PetscRandomCreate(PETSC_COMM_WORLD,rctx,ierr)
190:          call PetscRandomSetFromOptions(rctx,ierr)
191:          call VecSetRandom(u,rctx,ierr)
192:       else
193:          pfive = 0.5
194:          call VecSet(u,pfive,ierr)
195:       endif
196:       call MatMult(A,u,b,ierr)

198: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
199: !         Create the linear solver and set various options
200: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

202: !  Create linear solver context

204:       call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)

206: !  Set operators. Here the matrix that defines the linear system
207: !  also serves as the preconditioning matrix.

209:       call KSPSetOperators(ksp,A,A,ierr)

211: !  Set runtime options, e.g.,
212: !      -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>

214:       call KSPSetFromOptions(ksp,ierr)

216: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217: !                      Solve the linear system
218: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

220:       call KSPSolve(ksp,b,x,ierr)

222: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
223: !                     Check solution and clean up
224: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

226: !  Check the error

228:       call VecAXPY(x,none,u,ierr)
229:       call VecNorm(x,NORM_2,norm,ierr)
230:       call KSPGetIterationNumber(ksp,its,ierr)
231:       if (rank .eq. 0) then
232:         if (norm .gt. 1.e-12) then
233:            write(6,100) norm,its
234:         else
235:            write(6,110) its
236:         endif
237:       endif
238:   100 format('Norm of error ',e11.4,',iterations ',i5)
239:   110 format('Norm of error < 1.e-12,iterations ',i5)

241: !  Free work space.  All PETSc objects should be destroyed when they
242: !  are no longer needed.

244:       if (use_random) call PetscRandomDestroy(rctx,ierr)
245:       call KSPDestroy(ksp,ierr)
246:       call VecDestroy(u,ierr)
247:       call VecDestroy(x,ierr)
248:       call VecDestroy(b,ierr)
249:       call MatDestroy(A,ierr)

251: #if !defined(PETSC_USE_COMPLEX)
252:  200  continue
253: #endif
254:       call PetscFinalize(ierr)
255:       end