Actual source code: ex5f.F90

  1: !
  2: #include <petsc/finclude/petscksp.h>
  3: module ex5fmodule
  4:   use petscksp
  5:   implicit none

  7: contains
  8: !  This is a bogus multiply that copies the vector. This corresponds to
  9: !  an identity matrix A
 10:   subroutine mymatmult(A, x, y, ierr)

 12:     Mat A
 13:     Vec x, y
 14:     PetscErrorCode, intent(out) :: ierr

 16:     PetscCallA(VecCopy(x, y, ierr))

 18:   end
 19: end module ex5fmodule

 21: program main
 22:   use petscksp
 23:   use ex5fmodule
 24:   implicit none
 25: !
 26: !  Solves a linear system matrix-free
 27: !
 28:   Mat A
 29:   Vec x, y
 30:   PetscInt, parameter :: m = 10
 31:   PetscErrorCode ierr
 32:   KSP ksp
 33:   PetscScalar, parameter :: one = 1.0

 35:   PetscCallA(PetscInitialize(ierr))
 36:   PetscCallA(KSPCreate(PETSC_COMM_SELF, ksp, ierr))

 38:   PetscCallA(MatCreateShell(PETSC_COMM_SELF, m, m, m, m, 0, A, ierr))
 39:   PetscCallA(MatShellSetOperation(A, MATOP_MULT, mymatmult, ierr))

 41:   PetscCallA(VecCreateSeq(PETSC_COMM_SELF, m, x, ierr))
 42:   PetscCallA(VecDuplicate(x, y, ierr))
 43:   PetscCallA(VecSet(x, one, ierr))

 45:   PetscCallA(KSPSetOperators(ksp, A, A, ierr))
 46:   PetscCallA(KSPSetFromOptions(ksp, ierr))

 48:   PetscCallA(KSPSolve(ksp, x, y, ierr))

 50:   PetscCallA(MatDestroy(A, ierr))
 51:   PetscCallA(KSPDestroy(ksp, ierr))
 52:   PetscCallA(VecDestroy(x, ierr))
 53:   PetscCallA(VecDestroy(y, ierr))

 55:   PetscCallA(PetscFinalize(ierr))
 56: end

 58: !/*TEST
 59: !
 60: !    test:
 61: !      args: -ksp_monitor_short
 62: !
 63: !TEST*/