Actual source code: ex4f.F

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1: !
  2: !
  3: !  Description:  Illustrates the use of VecSetValues() to set
  4: !  multiple values at once; demonstrates VecGetArray().
  5: !
  6: !/*T
  7: !   Concepts: vectors^assembling;
  8: !   Concepts: vectors^arrays of vectors;
  9: !   Processors: 1
 10: !T*/
 11: ! -----------------------------------------------------------------------

 13:       program main
 14:  #include <petsc/finclude/petscvec.h>
 15:       use petscvec
 16:       implicit none

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

 32: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 33: !                 Beginning of program
 34: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 36:        PetscScalar xwork(6)
 37:        PetscScalar xx_v(1),yy_v(1)
 38:        PetscInt     i,n,loc(6),isix
 39:        PetscErrorCode ierr
 40:        PetscOffset xx_i,yy_i
 41:        Vec         x,y

 43:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 44:        if (ierr .ne. 0) then
 45:          print*,'PetscInitialize failed'
 46:          stop
 47:        endif
 48:        n = 6
 49:        isix = 6

 51: !  Create initial vector and duplicate it

 53:        call VecCreateSeq(PETSC_COMM_SELF,n,x,ierr)
 54:        call VecDuplicate(x,y,ierr)

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

 60:        do 10 i=1,n
 61:           loc(i) = i-1
 62:           xwork(i) = 10.0*real(i)
 63:   10   continue

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

 70:        call VecSetValues(x,isix,loc,xwork,INSERT_VALUES,ierr)

 72: !  Assemble vector

 74:        call VecAssemblyBegin(x,ierr)
 75:        call VecAssemblyEnd(x,ierr)

 77: !  View vector
 78:        call PetscObjectSetName(x, 'initial vector:',ierr)
 79:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
 80:        call VecCopy(x,y,ierr)

 82: !  Get a pointer to vector data.
 83: !    - For default PETSc vectors, VecGetArray() returns a pointer to
 84: !      the data array.  Otherwise, the routine is implementation dependent.
 85: !    - You MUST call VecRestoreArray() when you no longer need access to
 86: !      the array.
 87: !    - Note that the Fortran interface to VecGetArray() differs from the
 88: !      C version.  See the users manual for details.

 90:        call VecGetArray(x,xx_v,xx_i,ierr)
 91:        call VecGetArray(y,yy_v,yy_i,ierr)

 93: !  Modify vector data

 95:        do 30 i=1,n
 96:           xx_a(i) = 100.0*real(i)
 97:           yy_a(i) = 1000.0*real(i)
 98:   30   continue

100: !  Restore vectors

102:        call VecRestoreArray(x,xx_v,xx_i,ierr)
103:        call VecRestoreArray(y,yy_v,yy_i,ierr)

105: !  View vectors
106:        call PetscObjectSetName(x, 'new vector 1:',ierr)
107:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)

109:        call PetscObjectSetName(y, 'new vector 2:',ierr)
110:        call VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr)

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

115:        call VecDestroy(x,ierr)
116:        call VecDestroy(y,ierr)
117:        call PetscFinalize(ierr)
118:        end


121: !/*TEST
122: !
123: !     test:
124: !
125: !TEST*/