Actual source code: ex20f.F

petsc-3.13.4 2020-08-01
Report Typos and Errors
  1: !
  2:       program main
  3:  #include <petsc/finclude/petscvec.h>
  4:       use petscvec
  5:       implicit none

  7: !
  8: !      This example demonstrates writing an array to a file in binary
  9: !      format that may be read in by PETSc's VecLoad() routine.
 10: !
 11:        PetscInt n,i,ione
 12:        PetscErrorCode ierr
 13:        integer fd
 14:        PetscInt vecclassid(1)
 15:        PetscScalar      array(5)
 16:        Vec              x
 17:        PetscViewer           v

 19:        ione         = 1
 20:        n            = 5
 21:        vecclassid(1) = 1211211 + 3

 23:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 24:        if (ierr .ne. 0) then
 25:          print*,'Unable to initialize PETSc'
 26:          stop
 27:        endif

 29:        do 10, i=1,5
 30:          array(i) = i
 31:  10    continue

 33: !      Open binary file for writing
 34:        call PetscBinaryOpen('testfile',FILE_MODE_WRITE,fd,ierr)
 35: !      Write the Vec header
 36:        call PetscBinaryWrite(fd,vecclassid,ione,PETSC_INT,ierr)
 37: !      Write the array length
 38:        call PetscBinaryWrite(fd,n,ione,PETSC_INT,ierr)
 39: !      Write the array
 40:        call PetscBinaryWrite(fd,array,n,PETSC_SCALAR,ierr)
 41: !      Close the file
 42:        call PetscBinaryClose(fd,ierr)

 44: !
 45: !      Open the file for reading by PETSc
 46: !
 47:        call PetscViewerBinaryOpen(PETSC_COMM_SELF,'testfile',                &
 48:      &                       FILE_MODE_READ,v,ierr)
 49: !
 50: !      Load the vector
 51: !
 52:        call VecCreate(PETSC_COMM_WORLD,x,ierr)
 53:        call VecLoad(x,v,ierr)
 54:        call PetscViewerDestroy(v,ierr)
 55: !
 56: !      Print the vector
 57: !
 58:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
 59: !

 61:        call VecDestroy(x,ierr)
 62:        call PetscFinalize(ierr)
 63:        end


 66: !/*TEST
 67: !
 68: !     test:
 69: !
 70: !TEST*/