Actual source code: ex20f.F

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

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

 21:        ione         = 1
 22:        n            = 5
 23:        veccookie(1) = 1211211 + 3

 25:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

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

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

 43: !
 44: !      Open the file for reading by PETSc
 45: !
 46:        call PetscViewerBinaryOpen(PETSC_COMM_SELF,'testfile',                &
 47:      &                       FILE_MODE_READ,v,ierr)
 48: !
 49: !      Load the vector
 50: !
 51:        Call VecLoad(v,PETSC_NULL_CHARACTER,x,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

 63: