Actual source code: ex20f.F90

  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:        PetscCallA(PetscInitialize(ierr))

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

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

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

 56:        PetscCallA(VecDestroy(x,ierr))
 57:        PetscCallA(PetscFinalize(ierr))
 58:        end

 60: !/*TEST
 61: !
 62: !     test:
 63: !
 64: !TEST*/