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: ! -----------------------------------------------------------------------
      7: #include <petsc/finclude/petscvec.h>
      8: program main
      9:   use petscvec
     10:   implicit none

     12: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     13: !                 Beginning of program
     14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

     16:   PetscInt, parameter :: n = 6
     17:   PetscScalar xwork(n)
     18:   PetscScalar, pointer :: xx_v(:), yy_v(:)
     19:   PetscInt i, loc(n)
     20:   PetscErrorCode ierr
     21:   Vec x, y

     23:   PetscCallA(PetscInitialize(ierr))

     25: ! Create initial vector and duplicate it
     26:   PetscCallA(VecCreateSeq(PETSC_COMM_SELF, n, x, ierr))
     27:   PetscCallA(VecDuplicate(x, y, ierr))

     29: !  Fill work arrays with vector entries and locations.  Note that
     30: !  the vector indices are 0-based in PETSc (for both Fortran and
     31: !  C vectors)
     32:   do i = 1, n
     33:     loc(i) = i - 1
     34:     xwork(i) = 10.0*real(i)
     35:   end do

     37: !  Set vector values.  Note that we set multiple entries at once.
     38: !  Of course, usually one would create a work array that is the
     39: !  natural size for a particular problem (not one that is as long
     40: !  as the full vector).
     41:   PetscCallA(VecSetValues(x, n, loc, xwork, INSERT_VALUES, ierr))

     43: ! Assemble vector
     44:   PetscCallA(VecAssemblyBegin(x, ierr))
     45:   PetscCallA(VecAssemblyEnd(x, ierr))

     47: ! View vector
     48:   PetscCallA(PetscObjectSetName(x, 'initial vector:', ierr))
     49:   PetscCallA(VecView(x, PETSC_VIEWER_STDOUT_SELF, ierr))
     50:   PetscCallA(VecCopy(x, y, ierr))

     52: !  Get a pointer to vector data.
     53: !    - For default PETSc vectors, VecGetArray() returns a pointer to
     54: !      the data array.  Otherwise, the routine is implementation dependent.
     55: !    - You MUST call VecRestoreArray() when you no longer need access to
     56: !      the array.
     57: !    - Note that the Fortran interface to VecGetArray() differs from the
     58: !      C version.  See the users manual for details.
     59:   PetscCallA(VecGetArray(x, xx_v, ierr))
     60:   PetscCallA(VecGetArray(y, yy_v, ierr))

     62: ! Modify vector data
     63:   do i = 1, n
     64:     xx_v(i) = 100.0*real(i)
     65:     yy_v(i) = 1000.0*real(i)
     66:   end do

     68: ! Restore vectors
     69:   PetscCallA(VecRestoreArray(x, xx_v, ierr))
     70:   PetscCallA(VecRestoreArray(y, yy_v, ierr))

     72: ! View vectors
     73:   PetscCallA(PetscObjectSetName(x, 'new vector 1:', ierr))
     74:   PetscCallA(VecView(x, PETSC_VIEWER_STDOUT_SELF, ierr))

     76:   PetscCallA(PetscObjectSetName(y, 'new vector 2:', ierr))
     77:   PetscCallA(VecView(y, PETSC_VIEWER_STDOUT_SELF, ierr))

     79: ! Free work space.  All PETSc objects should be destroyed when they
     80: ! are no longer needed.
     81:   PetscCallA(VecDestroy(x, ierr))
     82:   PetscCallA(VecDestroy(y, ierr))
     83:   PetscCallA(PetscFinalize(ierr))
     84: end

     86: !/*TEST
     87: !
     88: !     test:
     89: !
     90: !TEST*/