Actual source code: ex20f.F90

  1: !
  2: #include <petsc/finclude/petscvec.h>
  3: program main
  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 i, n ! MarDiehl: fails for "PetscInt, parameter :: n = 5" in the second write, but should work
 12:   PetscErrorCode ierr
 13:   integer4 fd
 14:   PetscInt vecclassid(1)
 15:   PetscScalar array(5)
 16:   Vec x
 17:   PetscViewer v

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

 22:   PetscCallA(PetscInitialize(ierr))

 24:   array = [(real(i), i=1, n)]

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

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

 53:   PetscCallA(VecDestroy(x, ierr))
 54:   PetscCallA(PetscFinalize(ierr))
 55: end

 57: !/*TEST
 58: !
 59: !     test:
 60: !
 61: !TEST*/