Actual source code: ex16f.F

petsc-3.7.5 2017-01-01
Report Typos and Errors
  1: !
  2:       program main
  3:        implicit none

  5: #include <petsc/finclude/petscsys.h>
  6: #include <petsc/finclude/petscvec.h>
  7: #include <petsc/finclude/petscmat.h>
  8: #include <petsc/finclude/petscpc.h>
  9: #include <petsc/finclude/petscksp.h>
 10: #include <petsc/finclude/petscviewer.h>
 11: #include <petsc/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_OBJECT,PETSC_NULL_CHARACTER,       &
 45:      &                           '-f',f,flg,ierr)
 46:       call PetscViewerBinaryOpen(PETSC_COMM_WORLD,f,FILE_MODE_READ,            &
 47:      &     fd,ierr)
 48:       if (ierr .ne. 0) then
 49:         print*, 'Unable to open file ',f
 50:         SETERRQ(PETSC_COMM_WORLD,1,' ',ierr)
 51:       endif

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

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

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

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

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


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

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

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

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

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

116:       call PetscFinalize(ierr)
117:       end