Actual source code: ex4f.F90

  1: !
  2: !
  3: !  Description:  Illustrates the use of VecSetValues() to set
  4: !  multiple values at once; demonstrates VecGetArray().
  5: !
  6: ! -----------------------------------------------------------------------

  8:       program main
  9: #include <petsc/finclude/petscvec.h>
 10:       use petscvec
 11:       implicit none

 13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 14: !                   Macro definitions
 15: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 16: !
 17: !  Macros to make clearer the process of setting values in vectors and
 18: !  getting values from vectors.
 19: !
 20: !   - The element xx_a(ib) is element ib+1 in the vector x
 21: !   - Here we add 1 to the base array index to facilitate the use of
 22: !     conventional Fortran 1-based array indexing.
 23: !
 24: #define xx_a(ib)  xx_v(xx_i + (ib))
 25: #define yy_a(ib)  yy_v(yy_i + (ib))

 27: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 28: !                 Beginning of program
 29: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 31:        PetscScalar xwork(6)
 32:        PetscScalar xx_v(1),yy_v(1)
 33:        PetscInt     i,n,loc(6),isix
 34:        PetscErrorCode ierr
 35:        PetscOffset xx_i,yy_i
 36:        Vec         x,y

 38:        PetscCallA(PetscInitialize(ierr))
 39:        n = 6
 40:        isix = 6

 42: !  Create initial vector and duplicate it

 44:        PetscCallA(VecCreateSeq(PETSC_COMM_SELF,n,x,ierr))
 45:        PetscCallA(VecDuplicate(x,y,ierr))

 47: !  Fill work arrays with vector entries and locations.  Note that
 48: !  the vector indices are 0-based in PETSc (for both Fortran and
 49: !  C vectors)

 51:        do 10 i=1,n
 52:           loc(i) = i-1
 53:           xwork(i) = 10.0*real(i)
 54:   10   continue

 56: !  Set vector values.  Note that we set multiple entries at once.
 57: !  Of course, usually one would create a work array that is the
 58: !  natural size for a particular problem (not one that is as long
 59: !  as the full vector).

 61:        PetscCallA(VecSetValues(x,isix,loc,xwork,INSERT_VALUES,ierr))

 63: !  Assemble vector

 65:        PetscCallA(VecAssemblyBegin(x,ierr))
 66:        PetscCallA(VecAssemblyEnd(x,ierr))

 68: !  View vector
 69:        PetscCallA(PetscObjectSetName(x, 'initial vector:',ierr))
 70:        PetscCallA(VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr))
 71:        PetscCallA(VecCopy(x,y,ierr))

 73: !  Get a pointer to vector data.
 74: !    - For default PETSc vectors, VecGetArray() returns a pointer to
 75: !      the data array.  Otherwise, the routine is implementation dependent.
 76: !    - You MUST call VecRestoreArray() when you no longer need access to
 77: !      the array.
 78: !    - Note that the Fortran interface to VecGetArray() differs from the
 79: !      C version.  See the users manual for details.

 81:        PetscCallA(VecGetArray(x,xx_v,xx_i,ierr))
 82:        PetscCallA(VecGetArray(y,yy_v,yy_i,ierr))

 84: !  Modify vector data

 86:        do 30 i=1,n
 87:           xx_a(i) = 100.0*real(i)
 88:           yy_a(i) = 1000.0*real(i)
 89:   30   continue

 91: !  Restore vectors

 93:        PetscCallA(VecRestoreArray(x,xx_v,xx_i,ierr))
 94:        PetscCallA(VecRestoreArray(y,yy_v,yy_i,ierr))

 96: !  View vectors
 97:        PetscCallA(PetscObjectSetName(x, 'new vector 1:',ierr))
 98:        PetscCallA(VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr))

100:        PetscCallA(PetscObjectSetName(y, 'new vector 2:',ierr))
101:        PetscCallA(VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr))

103: !  Free work space.  All PETSc objects should be destroyed when they
104: !  are no longer needed.

106:        PetscCallA(VecDestroy(x,ierr))
107:        PetscCallA(VecDestroy(y,ierr))
108:        PetscCallA(PetscFinalize(ierr))
109:        end

111: !/*TEST
112: !
113: !     test:
114: !
115: !TEST*/