Actual source code: ex5f.F
petsc-3.12.4 2020-02-04
Report Typos and Errors

      1: !
      2:       program main
      3:  #include <petsc/finclude/petscksp.h>
      4:       use petscksp
      5:       implicit none
      6: !
      7: !      Solves a linear system matrix free
      8: !

     10:       Mat         A
     11:       Vec         x,y
     12:       PetscInt  m
     13:       PetscErrorCode ierr
     14:       KSP        ksp
     15:       external    mymatmult
     16:       PetscScalar one

     18:       m = 10

     20:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     21:       if (ierr .ne. 0) then
     22:         print*,'Unable to initialize PETSc'
     23:         stop
     24:       endif
     25:       one = 1.0
     26:       call KSPCreate(PETSC_COMM_SELF,ksp,ierr)

     28:       call MatCreateShell(PETSC_COMM_SELF,m,m,m,m,0,A,ierr)
     29:       call MatShellSetOperation(A,MATOP_MULT,mymatmult,ierr)

     31:       call VecCreateSeq(PETSC_COMM_SELF,m,x,ierr)
     32:       call VecDuplicate(x,y,ierr)
     33:       call VecSet(x,one,ierr)

     35:       call KSPSetOperators(ksp,A,A,ierr)
     36:       call KSPSetFromOptions(ksp,ierr)

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

     40:       call MatDestroy(A,ierr)
     41:       call KSPDestroy(ksp,ierr)
     42:       call VecDestroy(x,ierr)
     43:       call VecDestroy(y,ierr)

     45:       call PetscFinalize(ierr)
     46:       end

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

     52:       subroutine mymatmult(A,x,y,ierr)
     53:       use petscksp
     54:       implicit none

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

     60:       call VecCopy(x,y,ierr)

     62:       return
     63:       end

     65: !/*TEST
     66: !
     67: !    test:
     68: !      args:  -ksp_monitor_short
     69: !
     70: !TEST*/