Actual source code: ex6f.F90

petsc-3.11.3 2019-06-26
Report Typos and Errors
  1: program main
  2:  #include <petsc/finclude/petscvec.h>
  3:   use petscvec

  5:   implicit none

  7:   PetscErrorCode ierr
  8:   PetscMPIInt ::   mySize
  9:   integer     ::      fd
 10:   PetscInt    ::   i,sz
 11:   PetscInt,parameter   ::   m = 10
 12:   PetscInt, allocatable,dimension(:) :: t
 13:   PetscScalar, pointer, dimension(:) :: avec
 14:   PetscScalar, pointer, dimension(:) :: array
 15:   Vec            vec
 16:   PetscViewer    view_out,view_in
 17:   character(len=256) :: outstring
 18:   PetscBool :: flg
 19: 
 20:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 21:    if (ierr /= 0) then
 22:    print*,'PetscInitialize failed'
 23:    stop
 24:   endif
 25: 
 26:   call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr)
 27: 
 28:   if (mySize /= 1) then
 29:   SETERRA(PETSC_COMM_SELF,1,"This is a uniprocessor example only!")
 30:   endif
 31: 
 32: 

 34:   call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-m",m,flg,ierr);CHKERRA(ierr)

 36:   ! ----------------------------------------------------------------------
 37:   !          PART 1: Write some data to a file in binary format
 38:   ! ----------------------------------------------------------------------

 40:   ! Allocate array and set values
 41: 
 42:   allocate(array(0:m-1))
 43:   do i=0,m-1
 44:     array(i) =  real(i)*10.0
 45:   end do
 46: 
 47:   allocate(t(1))
 48:   t(1) = m
 49:   ! Open viewer for binary output
 50:   call PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_WRITE,view_out,ierr);CHKERRA(ierr)
 51:   call PetscViewerBinaryGetDescriptor(view_out,fd,ierr);CHKERRA(ierr)
 52: 
 53:   ! Write binary output
 54:   call PetscBinaryWrite(fd,t,1,PETSC_INT,PETSC_FALSE,ierr);CHKERRA(ierr)
 55:   call PetscBinaryWrite(fd,array,m,PETSC_SCALAR,PETSC_FALSE,ierr);CHKERRA(ierr)
 56: 
 57:   ! Destroy the output viewer and work array
 58:   call PetscViewerDestroy(view_out,ierr);CHKERRA(ierr)
 59: 
 60:   ! ----------------------------------------------------------------------
 61:   !          PART 2: Read data from file and form a vector
 62:   ! ----------------------------------------------------------------------

 64:   ! Open input binary viewer
 65:   call PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_READ,view_in,ierr);CHKERRA(ierr)
 66:   call PetscViewerBinaryGetDescriptor(view_in,fd,ierr);CHKERRA(ierr)

 68:   ! Create vector and get pointer to data space
 69:   call VecCreate(PETSC_COMM_SELF,vec,ierr);CHKERRA(ierr)
 70:   call VecSetSizes(vec,PETSC_DECIDE,m,ierr);CHKERRA(ierr)
 71: 
 72:   call VecSetFromOptions(vec,ierr);CHKERRA(ierr)
 73: 
 74:   call VecGetArrayF90(vec,avec,ierr);CHKERRA(ierr)

 76:   ! Read data into vector
 77:   call PetscBinaryRead(fd,t,1,PETSC_INT,ierr);CHKERRA(ierr)
 78:   sz=t(1)
 79: 
 80:   if (sz <= 0) then
 81:    SETERRA(PETSC_COMM_SELF,1,"Error: Must have array length > 0")
 82:   endif
 83: 
 84:   write(outstring,*) "reading data in binary from input.dat, sz = ", sz, "\n"
 85:   call PetscPrintf(PETSC_COMM_SELF,trim(outstring),ierr);CHKERRA(ierr)
 86: 
 87:   call PetscBinaryRead(fd,avec,sz,PETSC_SCALAR,ierr);CHKERRA(ierr)

 89:   ! View vector
 90:   call VecRestoreArrayF90(vec,avec,ierr);CHKERRA(ierr)
 91:   call VecView(vec,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)

 93:   ! Free data structures
 94:   call VecDestroy(vec,ierr);CHKERRA(ierr)
 95:   call PetscViewerDestroy(view_in,ierr);CHKERRA(ierr)
 96:   call PetscFinalize(ierr);CHKERRA(ierr)
 97: 
 98:   end program