Actual source code: ex6f.F90

      1: #include <petsc/finclude/petscvec.h>
      2: program main
      3:   use petscvec

      5:   implicit none

      7:   PetscErrorCode ierr
      8:   PetscMPIInt :: size
      9:   integer4    :: fd
     10:   PetscInt    :: i, sz
     11:   PetscInt :: m
     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

     20:   PetscCallA(PetscInitialize(ierr))

     22:   PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD, size, ierr))
     23:   PetscCheckA(size == 1, PETSC_COMM_SELF, PETSC_ERR_WRONG_MPI_SIZE, 'This is a uniprocessor example only!')

     25:   m = 10
     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:   array = [(real(i)*10.0, i=1, m)]
     36:   allocate (t(1), source=m)
     37:   ! Open viewer for binary output
     38:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_SELF, 'input.dat', FILE_MODE_WRITE, view_out, ierr))
     39:   PetscCallA(PetscViewerBinaryGetDescriptor(view_out, fd, ierr))

     41:   ! Write binary output
     42:   PetscCallA(PetscBinaryWrite(fd, t, 1_PETSC_INT_KIND, PETSC_INT, ierr))
     43:   PetscCallA(PetscBinaryWrite(fd, array, m, PETSC_SCALAR, ierr))

     45:   ! Destroy the output viewer and work array
     46:   PetscCallA(PetscViewerDestroy(view_out, ierr))
     47:   deallocate (array)

     49:   ! ----------------------------------------------------------------------
     50:   !          PART 2: Read data from file and form a vector
     51:   ! ----------------------------------------------------------------------

     53:   ! Open input binary viewer
     54:   PetscCallA(PetscViewerBinaryOpen(PETSC_COMM_SELF, 'input.dat', FILE_MODE_READ, view_in, ierr))
     55:   PetscCallA(PetscViewerBinaryGetDescriptor(view_in, fd, ierr))

     57:   ! Create vector and get pointer to data space
     58:   PetscCallA(VecCreate(PETSC_COMM_SELF, vec, ierr))
     59:   PetscCallA(VecSetSizes(vec, PETSC_DECIDE, m, ierr))

     61:   PetscCallA(VecSetFromOptions(vec, ierr))

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

     65:   ! Read data into vector
     66:   PetscCallA(PetscBinaryRead(fd, t, 1_PETSC_INT_KIND, PETSC_NULL_INTEGER, PETSC_INT, ierr))
     67:   sz = t(1)

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

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

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

     76:   ! View vector
     77:   PetscCallA(VecRestoreArray(vec, avec, ierr))
     78:   PetscCallA(VecView(vec, PETSC_VIEWER_STDOUT_SELF, ierr))

     80:   ! Free data structures
     81:   deallocate (t)
     82:   PetscCallA(VecDestroy(vec, ierr))
     83:   PetscCallA(PetscViewerDestroy(view_in, ierr))
     84:   PetscCallA(PetscFinalize(ierr))

     86: end program

     88: !/*TEST
     89: !
     90: !     test:
     91: !        output_file: output/ex6_1.out
     92: !
     93: !TEST*/