Actual source code: ex20f.F
petsc-3.12.4 2020-02-04
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,                    &
     37:      &                       PETSC_FALSE,ierr)
     38: !      Write the array length
     39:        call PetscBinaryWrite(fd,n,ione,PETSC_INT,PETSC_FALSE,ierr)
     40: !      Write the array
     41:        call PetscBinaryWrite(fd,array,n,PETSC_SCALAR,                        &
     42:      &                      PETSC_FALSE,ierr)
     43: !      Close the file
     44:        call PetscBinaryClose(fd,ierr)

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

     63:        call VecDestroy(x,ierr)
     64:        call PetscFinalize(ierr)
     65:        end

     68: !/*TEST
     69: !
     70: !     test:
     71: !
     72: !TEST*/