Actual source code: ex4f.F

      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:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     39:        if (ierr .ne. 0) then
     40:          print*,'PetscInitialize failed'
     41:          stop
     42:        endif
     43:        n = 6
     44:        isix = 6

     46: !  Create initial vector and duplicate it

     48:        call VecCreateSeq(PETSC_COMM_SELF,n,x,ierr)
     49:        call VecDuplicate(x,y,ierr)

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

     55:        do 10 i=1,n
     56:           loc(i) = i-1
     57:           xwork(i) = 10.0*real(i)
     58:   10   continue

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

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

     67: !  Assemble vector

     69:        call VecAssemblyBegin(x,ierr)
     70:        call VecAssemblyEnd(x,ierr)

     72: !  View vector
     73:        call PetscObjectSetName(x, 'initial vector:',ierr)
     74:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
     75:        call VecCopy(x,y,ierr)

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

     85:        call VecGetArray(x,xx_v,xx_i,ierr)
     86:        call VecGetArray(y,yy_v,yy_i,ierr)

     88: !  Modify vector data

     90:        do 30 i=1,n
     91:           xx_a(i) = 100.0*real(i)
     92:           yy_a(i) = 1000.0*real(i)
     93:   30   continue

     95: !  Restore vectors

     97:        call VecRestoreArray(x,xx_v,xx_i,ierr)
     98:        call VecRestoreArray(y,yy_v,yy_i,ierr)

    100: !  View vectors
    101:        call PetscObjectSetName(x, 'new vector 1:',ierr)
    102:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)

    104:        call PetscObjectSetName(y, 'new vector 2:',ierr)
    105:        call VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr)

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

    110:        call VecDestroy(x,ierr)
    111:        call VecDestroy(y,ierr)
    112:        call PetscFinalize(ierr)
    113:        end

    115: !/*TEST
    116: !
    117: !     test:
    118: !
    119: !TEST*/