Date: 2025-03-29T05:58:55+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 ::   size
      9:   integer4     ::      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,size,ierr))
     24:   PetscCheckA(size .eq. 1,PETSC_COMM_SELF,PETSC_ERR_WRONG_MPI_SIZE,'This is a uniprocessor example only!')

     26:   PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-m',m,flg,ierr))

     28:   ! ----------------------------------------------------------------------
     29:   !          PART 1: Write some data to a file in binary format
     30:   ! ----------------------------------------------------------------------

     32:   ! Allocate array and set values

     34:   allocate(array(0:m-1))
     35:   do i=0,m-1
     36:     array(i) =  real(i)*10.0
     37:   end do

     39:   allocate(t(1))
     40:   t(1) = m
     41:   ! Open viewer for binary output
     42:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_SELF,'input.dat',FILE_MODE_WRITE,view_out,ierr))
     43:   PetscCallA(PetscViewerBinaryGetDescriptor(view_out,fd,ierr))

     45:   ! Write binary output
     46:   PetscCallA(PetscBinaryWrite(fd,t,one,PETSC_INT,ierr))
     47:   PetscCallA(PetscBinaryWrite(fd,array,m,PETSC_SCALAR,ierr))

     49:   ! Destroy the output viewer and work array
     50:   PetscCallA(PetscViewerDestroy(view_out,ierr))
     51:   deallocate(array)

     53:   ! ----------------------------------------------------------------------
     54:   !          PART 2: Read data from file and form a vector
     55:   ! ----------------------------------------------------------------------

     57:   ! Open input binary viewer
     58:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_SELF,'input.dat',FILE_MODE_READ,view_in,ierr))
     59:   PetscCallA(PetscViewerBinaryGetDescriptor(view_in,fd,ierr))

     61:   ! Create vector and get pointer to data space
     62:   PetscCallA(VecCreate(PETSC_COMM_SELF,vec,ierr))
     63:   PetscCallA(VecSetSizes(vec,PETSC_DECIDE,m,ierr))

     65:   PetscCallA(VecSetFromOptions(vec,ierr))

     67:   PetscCallA(VecGetArray(vec,avec,ierr))

     69:   ! Read data into vector
     70:   PetscCallA(PetscBinaryRead(fd,t,one,PETSC_NULL_INTEGER,PETSC_INT,ierr))
     71:   sz=t(1)

     73:   PetscCheckA(sz >= 0,PETSC_COMM_SELF,PETSC_ERR_USER,'Error: Must have array length > 0')

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

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

     80:   ! View vector
     81:   PetscCallA(VecRestoreArray(vec,avec,ierr))
     82:   PetscCallA(VecView(vec,PETSC_VIEWER_STDOUT_SELF,ierr))

     84:   ! Free data structures
     85:   deallocate(t)
     86:   PetscCallA(VecDestroy(vec,ierr))
     87:   PetscCallA(PetscViewerDestroy(view_in,ierr))
     88:   PetscCallA(PetscFinalize(ierr))

     90:   end program

     92: !/*TEST
     93: !
     94: !     test:
     95: !        output_file: output/ex6_1.out
     96: !
     97: !TEST*/