Actual source code: ex6f.F90
petsc-3.12.4 2020-02-04
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,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
     20:
     21:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     22:    if (ierr /= 0) then
     23:    print*,'PetscInitialize failed'
     24:    stop
     25:   endif
     26:
     27:   call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr)
     28:
     29:   if (mySize /= 1) then
     30:   SETERRA(PETSC_COMM_SELF,1,"This is a uniprocessor example only!")
     31:   endif
     32:
     33:

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

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

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

     62:   ! ----------------------------------------------------------------------
     63:   !          PART 2: Read data from file and form a vector
     64:   ! ----------------------------------------------------------------------

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

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

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

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

     95:   ! Free data structures
     96:   deallocate(t)
     97:   call VecDestroy(vec,ierr);CHKERRA(ierr)
     98:   call PetscViewerDestroy(view_in,ierr);CHKERRA(ierr)
     99:   call PetscFinalize(ierr);CHKERRA(ierr)
    100:
    101:   end program

    103: !/*TEST
    104: !
    105: !     test:
    106: !        output_file: output/ex6_1.out
    107: !
    108: !TEST*/