Actual source code: ex11f.F

  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 one of the
 19: !     following values of BOPT must be used for compiling the PETSc libraries
 20: !     and this example:
 21: !        BOPT=g_complex   - debugging version
 22: !        BOPT=O_complex   - optimized version
 23: !        BOPT=Opg_complex - profiling version
 24: !
 25: ! -----------------------------------------------------------------------

 27:       program main
 28:       implicit none

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

 67:       KSP             ksp
 68:       Mat              A
 69:       Vec              x,b,u
 70:       PetscRandom      rctx
 71:       double precision norm,h2,sigma1
 72:       PetscScalar      none,sigma2,v,pfive
 73:       PetscInt          dim,its,n,Istart,Iend,i,j,II,JJ,one
 74:       PetscErrorCode ierr
 75:       PetscMPIInt rank
 76:       PetscTruth flg
 77:       logical          use_random

 79: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 80: !                 Beginning of program
 81: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 83:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 84: #if !defined(PETSC_USE_COMPLEX)
 85:       write(6,*) "This example requires complex numbers."
 86:       goto 200
 87: #endif

 89:       none   = -1.0
 90:       n      = 6
 91:       sigma1 = 100.0
 92:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 93:       call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-sigma1',sigma1,      &
 94:      &                       flg,ierr)
 95:       call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
 96:       dim    = n*n

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

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

108:       call MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,dim,    &
109:      &               dim,A,ierr)
110:       call MatSetFromOptions(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_CHARACTER,'-norandom',        &
125:      &     flg,ierr)
126:       if (flg .eq. 1) then
127:          use_random = .false.
128:          sigma2 = 10.0*PETSC_i
129:       else
130:          use_random = .true.
131:          call PetscRandomCreate(PETSC_COMM_WORLD,                       &
132:      &        RANDOM_DEFAULT_IMAGINARY,rctx,ierr)
133:       endif
134:       h2 = 1.0/((n+1)*(n+1))

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

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

168:       call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
169:       call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)

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

177:       call VecCreate(PETSC_COMM_WORLD,u,ierr)
178:       call VecSetSizes(u,PETSC_DECIDE,dim,ierr)
179:       call VecSetFromOptions(u,ierr)
180:       call VecDuplicate(u,b,ierr)
181:       call VecDuplicate(b,x,ierr)

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

185:       if (use_random) then
186:          call PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT,        &
187:      &                          rctx,ierr)
188:          call VecSetRandom(rctx,u,ierr)
189:       else
190:          pfive = 0.5
191:          call VecSet(pfive,u,ierr)
192:       endif
193:       call MatMult(A,u,b,ierr)

195: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196: !         Create the linear solver and set various options
197: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

199: !  Create linear solver context

201:       call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)

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

206:       call KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN,ierr)

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

211:       call KSPSetFromOptions(ksp,ierr)

213: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
214: !                      Solve the linear system
215: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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

219: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
220: !                     Check solution and clean up
221: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

223: !  Check the error

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

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

241:       if (use_random) call PetscRandomDestroy(rctx,ierr)
242:       call KSPDestroy(ksp,ierr)
243:       call VecDestroy(u,ierr)
244:       call VecDestroy(x,ierr)
245:       call VecDestroy(b,ierr)
246:       call MatDestroy(A,ierr)

248:  200  continue
249:       call PetscFinalize(ierr)
250:       end