Actual source code: ex16.F

  1: !
  2: !      "$Id: ex16.F,v 1.29 2001/08/07 03:03:57 balay Exp $";
  3: !
  4:       program main
  5:        implicit none

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

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

 27:       integer          ierr,its,flg
 28:       PetscScalar      norm,none,five
 29:       Vec              x,b,u
 30:       Mat              A
 31:       SLES             sles1,sles2
 32:       character*(128)  f
 33:       PetscViewer           fd
 34:       IS               isrow
 35:       PetscLogDouble       time1,time2

 37:       none = -1.0
 38:       five = 5.0
 39:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

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

 50:       call MatLoad(fd,MATSEQAIJ,A,ierr)
 51:       if (ierr .ne. 0) then
 52:         print*, 'Unable to load matrix '
 53:         SETERRQ(1,' ',ierr)
 54:       endif

 56:       call VecLoad(fd,b,ierr)
 57:       call PetscViewerDestroy(fd,ierr)

 59: ! Set up solution
 60:       call VecDuplicate(b,x,ierr)
 61:       call VecDuplicate(b,u,ierr)

 63: ! Solve system-1
 64:       call SLESCreate(PETSC_COMM_WORLD,sles1,ierr)
 65:       call SLESSetOptionsPrefix(sles1,'a',ierr)
 66:       call SLESAppendOptionsPrefix(sles1,'_',ierr)
 67:       call SLESSetOperators(sles1,A,A,DIFFERENT_NONZERO_PATTERN,        &
 68:      &                      ierr)
 69:       call SLESSetFromOptions(sles1,ierr)
 70:       call PetscGetTime(time1,ierr)
 71:       call SLESSolve(sles1,b,x,its,ierr)
 72:       call PetscGetTime(time2,ierr)

 74: ! Show result
 75:       call MatMult(A,x,u,ierr)
 76:       call VecAXPY(none,b,u,ierr)
 77:       call VecNorm(u,NORM_2,norm,ierr)

 79: !      print*, 'Time for solve = ',time2-time1

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

 84: ! Create system 2 by striping off some rows of the matrix
 85:       call ISCreateStride(PETSC_COMM_SELF,5,0,1,isrow,ierr)
 86:       call MatZeroRows(A,isrow,five,ierr)

 88: ! Solve system-2
 89:       call SLESCreate(PETSC_COMM_WORLD,sles2,ierr)
 90:       call SLESSetOptionsPrefix(sles2,'b',ierr)
 91:       call SLESAppendOptionsPrefix(sles2,'_',ierr)
 92:       call SLESSetOperators(sles2,A,A,DIFFERENT_NONZERO_PATTERN,        &
 93:      &                      ierr)
 94:       call SLESSetFromOptions(sles2,ierr)
 95:       call PetscGetTime(time1,ierr)
 96:       call SLESSolve(sles2,b,x,its,ierr)
 97:       call PetscGetTime(time2,ierr)

 99: ! Show result
100:       call MatMult(A,x,u,ierr)
101:       call VecAXPY(none,b,u,ierr)
102:       call VecNorm(u,NORM_2,norm,ierr)
103: !      print*, 'Time for solve = ',time2-time1
104:       write(6,100) norm,its

106: ! Cleanup
107:       call SLESDestroy(sles1,ierr)
108:       call SLESDestroy(sles2,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