Actual source code: ex6f.F90

  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,parameter   ::   one = 1
 13:   PetscInt, allocatable,dimension(:) :: t
 14:   PetscScalar, pointer, dimension(:) :: avec
 15:   PetscScalar, pointer, dimension(:) :: array
 16:   Vec            vec
 17:   PetscViewer    view_out,view_in
 18:   character(len=256) :: outstring
 19:   PetscBool :: flg

 21:   PetscCallA(PetscInitialize(ierr))

 23:   PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr))

 25:   if (mySize /= 1) then
 26:     SETERRA(PETSC_COMM_SELF,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!")
 27:   endif

 29:   PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-m",m,flg,ierr))

 31:   ! ----------------------------------------------------------------------
 32:   !          PART 1: Write some data to a file in binary format
 33:   ! ----------------------------------------------------------------------

 35:   ! Allocate array and set values

 37:   allocate(array(0:m-1))
 38:   do i=0,m-1
 39:     array(i) =  real(i)*10.0
 40:   end do

 42:   allocate(t(1))
 43:   t(1) = m
 44:   ! Open viewer for binary output
 45:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_WRITE,view_out,ierr))
 46:   PetscCallA(PetscViewerBinaryGetDescriptor(view_out,fd,ierr))

 48:   ! Write binary output
 49:   PetscCallA(PetscBinaryWrite(fd,t,one,PETSC_INT,ierr))
 50:   PetscCallA(PetscBinaryWrite(fd,array,m,PETSC_SCALAR,ierr))

 52:   ! Destroy the output viewer and work array
 53:   PetscCallA(PetscViewerDestroy(view_out,ierr))
 54:   deallocate(array)

 56:   ! ----------------------------------------------------------------------
 57:   !          PART 2: Read data from file and form a vector
 58:   ! ----------------------------------------------------------------------

 60:   ! Open input binary viewer
 61:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_READ,view_in,ierr))
 62:   PetscCallA(PetscViewerBinaryGetDescriptor(view_in,fd,ierr))

 64:   ! Create vector and get pointer to data space
 65:   PetscCallA(VecCreate(PETSC_COMM_SELF,vec,ierr))
 66:   PetscCallA(VecSetSizes(vec,PETSC_DECIDE,m,ierr))

 68:   PetscCallA(VecSetFromOptions(vec,ierr))

 70:   PetscCallA(VecGetArrayF90(vec,avec,ierr))

 72:   ! Read data into vector
 73:   PetscCallA(PetscBinaryRead(fd,t,one,PETSC_NULL_INTEGER,PETSC_INT,ierr))
 74:   sz=t(1)

 76:   if (sz <= 0) then
 77:    SETERRA(PETSC_COMM_SELF,PETSC_ERR_USER,"Error: Must have array length > 0")
 78:   endif

 80:   write(outstring,'(a,i2.2,a)') "reading data in binary from input.dat, sz =", sz, " ...\n"
 81:   PetscCallA(PetscPrintf(PETSC_COMM_SELF,trim(outstring),ierr))

 83:   PetscCallA(PetscBinaryRead(fd,avec,sz,PETSC_NULL_INTEGER,PETSC_SCALAR,ierr))

 85:   ! View vector
 86:   PetscCallA(VecRestoreArrayF90(vec,avec,ierr))
 87:   PetscCallA(VecView(vec,PETSC_VIEWER_STDOUT_SELF,ierr))

 89:   ! Free data structures
 90:   deallocate(t)
 91:   PetscCallA(VecDestroy(vec,ierr))
 92:   PetscCallA(PetscViewerDestroy(view_in,ierr))
 93:   PetscCallA(PetscFinalize(ierr))

 95:   end program

 97: !/*TEST
 98: !
 99: !     test:
100: !        output_file: output/ex6_1.out
101: !
102: !TEST*/