Actual source code: ex20f.F
petsc-3.4.2 2013-07-02

      1: !
      2:       program main
      3:       implicit none
      4: #include <finclude/petscsys.h>
      5: #include <finclude/petscvec.h>
      6: #include <finclude/petscviewer.h>

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

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

     24:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

     26:        do 10, i=1,5
     27:          array(i) = i
     28:  10    continue

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

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

     59:        call VecDestroy(x,ierr)
     60:        call PetscFinalize(ierr)
     61:        end