Date: 2022-03-31T04:46:30+00:00

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:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     22:    if (ierr /= 0) then
     23:    print*,'PetscInitialize failed'
     24:    stop
     25:   endif

     27:   call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr)

     29:   if (mySize /= 1) then
     30:     SETERRA(PETSC_COMM_SELF,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!")
     31:   endif

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

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

     39:   ! Allocate array and set values

     41:   allocate(array(0:m-1))
     42:   do i=0,m-1
     43:     array(i) =  real(i)*10.0
     44:   end do

     46:   allocate(t(1))
     47:   t(1) = m
     48:   ! Open viewer for binary output
     49:   call PetscViewerBinaryOpen(PETSC_COMM_SELF,"input.dat",FILE_MODE_WRITE,view_out,ierr);CHKERRA(ierr)
     50:   call PetscViewerBinaryGetDescriptor(view_out,fd,ierr);CHKERRA(ierr)

     52:   ! Write binary output
     53:   call PetscBinaryWrite(fd,t,one,PETSC_INT,ierr);CHKERRA(ierr)
     54:   call PetscBinaryWrite(fd,array,m,PETSC_SCALAR,ierr);CHKERRA(ierr)

     56:   ! Destroy the output viewer and work array
     57:   call PetscViewerDestroy(view_out,ierr);CHKERRA(ierr)
     58:   deallocate(array)

     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)

     72:   call VecSetFromOptions(vec,ierr);CHKERRA(ierr)

     74:   call VecGetArrayF90(vec,avec,ierr);CHKERRA(ierr)

     76:   ! Read data into vector
     77:   call PetscBinaryRead(fd,t,one,PETSC_NULL_INTEGER,PETSC_INT,ierr);CHKERRA(ierr)
     78:   sz=t(1)

     80:   if (sz <= 0) then
     81:    SETERRA(PETSC_COMM_SELF,PETSC_ERR_USER,"Error: Must have array length > 0")
     82:   endif

     84:   write(outstring,'(a,i2.2,a)') "reading data in binary from input.dat, sz =", sz, " ...\n"
     85:   call PetscPrintf(PETSC_COMM_SELF,trim(outstring),ierr);CHKERRA(ierr)

     87:   call PetscBinaryRead(fd,avec,sz,PETSC_NULL_INTEGER,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:   deallocate(t)
     95:   call VecDestroy(vec,ierr);CHKERRA(ierr)
     96:   call PetscViewerDestroy(view_in,ierr);CHKERRA(ierr)
     97:   call PetscFinalize(ierr);CHKERRA(ierr)

     99:   end program

    101: !/*TEST
    102: !
    103: !     test:
    104: !        output_file: output/ex6_1.out
    105: !
    106: !TEST*/