Actual source code: ex100f.F90

petsc-master 2019-05-24
Report Typos and Errors
  1:       program main
  2: #include "petsc/finclude/petscksp.h"
  3:       use petscksp

  5:       PetscInt       N
  6:       PetscBool      draw, flg
  7:       PetscReal      rnorm,rtwo
  8:       PetscScalar    one,mone
  9:       Mat            A
 10:       Vec            b,x,r
 11:       KSP            ksp
 12:       PC             pc
 13:       PetscErrorCode ierr

 15:       N    = 100
 16:       draw = .FALSE.
 17:       one  =  1.0
 18:       mone = -1.0
 19:       rtwo = 2.0

 21:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 22:       if (ierr .ne. 0) then
 23:         print*,'PetscInitialize failed'
 24:         stop
 25:       endif
 26:       call PetscPythonInitialize(PETSC_NULL_CHARACTER,PETSC_NULL_CHARACTER,ierr)

 28:       call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-N', N,flg,ierr)
 29:       call PetscOptionsGetBool(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-draw',draw,flg,ierr)

 31:       call MatCreate(PETSC_COMM_WORLD,A,ierr)
 32:       call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N,ierr)
 33:       call MatSetType(A,'python',ierr)
 34:       call MatPythonSetType(A,'example100.py:Laplace1D',ierr)
 35:       call MatSetUp(A,ierr)

 37:       call MatCreateVecs(A,x,b,ierr)
 38:       call VecSet(b,one,ierr)

 40:       call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)
 41:       call KSPSetType(ksp,'python',ierr)
 42:       call KSPPythonSetType(ksp,'example100.py:ConjGrad',ierr)

 44:       call KSPGetPC(ksp,pc,ierr)
 45:       call PCSetType(pc,'python',ierr)
 46:       call PCPythonSetType(pc,'example100.py:Jacobi',ierr)

 48:       call KSPSetOperators(ksp,A,A,ierr)
 49:       call KSPSetFromOptions(ksp,ierr)
 50:       call KSPSolve(ksp,b,x,ierr)

 52:       call VecDuplicate(b,r,ierr)
 53:       call MatMult(A,x,r,ierr)
 54:       call VecAYPX(r,mone,b,ierr)
 55:       call VecNorm(r,NORM_2,rnorm,ierr)
 56:       print*,'error norm = ',rnorm

 58:       if (draw) then
 59:          call VecView(x,PETSC_VIEWER_DRAW_WORLD,ierr)
 60:          call PetscSleep(rtwo,ierr)
 61:       endif

 63:       call VecDestroy(x,ierr)
 64:       call VecDestroy(b,ierr)
 65:       call VecDestroy(r,ierr)
 66:       call MatDestroy(A,ierr)
 67:       call KSPDestroy(ksp,ierr)

 69:       call PetscFinalize(ierr)
 70:       end

 72: !/*TEST
 73: !
 74: !    test:
 75: !      requires: Section 1.1 Suggested Reading
 76: !      localrunfiles: example100.py
 77: !
 78: !TEST*/