Actual source code: ex16f.F

petsc-3.4.2 2013-07-02
  1: !
  2:       program main
  3:        implicit none

  5: #include <finclude/petscsys.h>
  6: #include <finclude/petscvec.h>
  7: #include <finclude/petscmat.h>
  8: #include <finclude/petscpc.h>
  9: #include <finclude/petscksp.h>
 10: #include <finclude/petscviewer.h>
 11: #include <finclude/petscis.h>
 12: !
 13: !  This example is a modified Fortran version of ex6.c.  It tests the use of
 14: !  options prefixes in PETSc. Two linear problems are solved in this program.
 15: !  The first problem is read from a file. The second problem is constructed
 16: !  from the first, by eliminating some of the entries of the linear matrix 'A'.

 18: !  Each solve is distinguished by a unique prefix - 'a' for the first, 'b'
 19: !  for the second.  With the prefix the user can distinguish between the various
 20: !  options (command line, from .petscrc file, etc.) for each of the solvers.
 21: !  Input arguments are:
 22: !     -f <input_file> : file to load.  For a 5X5 example of the 5-pt. stencil
 23: !                       use the file petsc/src/mat/examples/mat.ex.binary

 25:       PetscErrorCode  ierr
 26:       PetscInt its,ione,ifive,izero
 27:       PetscErrorCode flg
 28:       PetscScalar      norm,none,five
 29:       Vec              x,b,u
 30:       Mat              A
 31:       KSP             ksp1,ksp2
 32:       character*(128)  f
 33:       PetscViewer      fd
 34:       IS               isrow
 35:       none  = -1.0
 36:       five  = 5.0
 37:       ifive = 5
 38:       ione  = 1
 39:       izero = 0

 41:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

 43: ! Read in matrix and RHS
 44:       call PetscOptionsGetString(PETSC_NULL_CHARACTER,'-f',f,flg,ierr)
 45:       call PetscViewerBinaryOpen(PETSC_COMM_WORLD,f,FILE_MODE_READ,            &
 46:      &     fd,ierr)
 47:       if (ierr .ne. 0) then
 48:         print*, 'Unable to open file ',f
 49:         SETERRQ(PETSC_COMM_WORLD,1,' ',ierr)
 50:       endif

 52:       call MatCreate(PETSC_COMM_WORLD,A,ierr)
 53:       call MatSetType(A, MATSEQAIJ,ierr)
 54:       call MatLoad(A,fd,ierr)
 55:       if (ierr .ne. 0) then
 56:         print*, 'Unable to load matrix '
 57:         SETERRQ(PETSC_COMM_WORLD,1,' ',ierr)
 58:       endif

 60:       call VecCreate(PETSC_COMM_WORLD,b,ierr)
 61:       call VecLoad(b,fd,ierr)
 62:       call PetscViewerDestroy(fd,ierr)

 64: ! Set up solution
 65:       call VecDuplicate(b,x,ierr)
 66:       call VecDuplicate(b,u,ierr)

 68: ! Solve system-1
 69:       call KSPCreate(PETSC_COMM_WORLD,ksp1,ierr)
 70:       call KSPSetOptionsPrefix(ksp1,'a',ierr)
 71:       call KSPAppendOptionsPrefix(ksp1,'_',ierr)
 72:       call KSPSetOperators(ksp1,A,A,DIFFERENT_NONZERO_PATTERN,ierr)
 73:       call KSPSetFromOptions(ksp1,ierr)
 74:       call KSPSolve(ksp1,b,x,ierr)

 76: ! Show result
 77:       call MatMult(A,x,u,ierr)
 78:       call VecAXPY(u,none,b,ierr)
 79:       call VecNorm(u,NORM_2,norm,ierr)
 80:       call KSPGetIterationNumber(ksp1,its,ierr)


 83:       write(6,100) norm,its
 84:   100 format('Residual norm ',e10.4,' iterations ',i5)

 86: ! Create system 2 by striping off some rows of the matrix
 87:       call ISCreateStride(PETSC_COMM_SELF,ifive,izero,ione,isrow,ierr)
 88:       call MatZeroRowsIS(A,isrow,five,PETSC_NULL_OBJECT,                   &
 89:      &                   PETSC_NULL_OBJECT,ierr)

 91: ! Solve system-2
 92:       call KSPCreate(PETSC_COMM_WORLD,ksp2,ierr)
 93:       call KSPSetOptionsPrefix(ksp2,'b',ierr)
 94:       call KSPAppendOptionsPrefix(ksp2,'_',ierr)
 95:       call KSPSetOperators(ksp2,A,A,DIFFERENT_NONZERO_PATTERN,ierr)
 96:       call KSPSetFromOptions(ksp2,ierr)
 97:       call KSPSolve(ksp2,b,x,ierr)

 99: ! Show result
100:       call MatMult(A,x,u,ierr)
101:       call VecAXPY(u,none,b,ierr)
102:       call VecNorm(u,NORM_2,norm,ierr)
103:       call KSPGetIterationNumber(ksp2,its,ierr)
104:       write(6,100) norm,its

106: ! Cleanup
107:       call KSPDestroy(ksp1,ierr)
108:       call KSPDestroy(ksp2,ierr)
109:       call VecDestroy(b,ierr)
110:       call VecDestroy(x,ierr)
111:       call VecDestroy(u,ierr)
112:       call MatDestroy(A,ierr)
113:       call ISDestroy(isrow,ierr)

115:       call PetscFinalize(ierr)
116:       end