Date: 2013-07-02T14:48:37+00:00

Actual source code: ex5f.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: !
     12: !      Solves a linear system matrix free
     13: !

     15:       Mat         A
     16:       Vec         x,y
     17:       PetscInt  m
     18:       PetscErrorCode ierr
     19:       KSP        ksp
     20:       external    mymatmult
     21:       PetscScalar one

     23:       m = 10

     25:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     26:       one = 1.d0
     27:       call KSPCreate(PETSC_COMM_SELF,ksp,ierr)

     29:       call MatCreateShell(PETSC_COMM_SELF,m,m,m,m,PETSC_NULL_OBJECT,    &
     30:      &     A,ierr)
     31:       call MatShellSetOperation(A,MATOP_MULT,mymatmult,ierr)

     33:       call VecCreateSeq(PETSC_COMM_SELF,m,x,ierr)
     34:       call VecDuplicate(x,y,ierr)
     35:       call VecSet(x,one,ierr)

     37:       call KSPSetOperators(ksp,A,A,SAME_NONZERO_PATTERN,ierr)
     38:       call KSPSetFromOptions(ksp,ierr)

     40:       call KSPSolve(ksp,x,y,ierr)

     42:       call MatDestroy(A,ierr)
     43:       call KSPDestroy(ksp,ierr)
     44:       call VecDestroy(x,ierr)
     45:       call VecDestroy(y,ierr)

     47:       call PetscFinalize(ierr)
     48:       end

     51: !  This is a bogus multiply that copies the vector. This corresponds to
     52: !  an identity matrix A

     54:       subroutine mymatmult(A,x,y,ierr)

     56:       Mat A
     57:       Vec x,y
     58:       PetscErrorCode ierr

     60:       call VecCopy(x,y,ierr)

     62:       return
     63:       end