Actual source code: ex4f90.F

  1: !
  2: !
  3: !  Description:  Illustrates the use of VecSetValues() to set
  4: !  multiple values at once; demonstrates VecGetArrayF90().
  5: !
  6: !/*T
  7: !   Concepts: vectors^assembling vectors;
  8: !   Concepts: vectors^arrays;
  9: !   Concepts: Fortran90^assembling vectors;
 10: !   Processors: 1
 11: !T*/
 12: ! -----------------------------------------------------------------------

 14:       program main
 15:       implicit none

 17: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 18: !                    Include files
 19: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 20: !
 21: !  The following include statements are required for Fortran programs
 22: !  that use PETSc vectors:
 23: !     petsc.h       - base PETSc routines
 24: !     petscvec.h    - vectors
 25: !     petscvec.h90  - to allow access to Fortran90 features of vectors

 27:  #include include/finclude/petsc.h
 28:  #include include/finclude/petscvec.h
 29: #include "include/finclude/petscvec.h90"

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

 35:        PetscScalar  xwork(6)
 36:        PetscScalar, pointer ::  xx_v(:),yy_v(:)
 37:        integer i,n,ierr,loc(6)
 38:        Vec     x,y


 41:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 42:        n = 6

 44: !  Create initial vector and duplicate it

 46:        call VecCreateSeq(PETSC_COMM_SELF,n,x,ierr)
 47:        call VecDuplicate(x,y,ierr)

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

 53:        do 10 i=1,n
 54:           loc(i) = i-1
 55:           xwork(i) = 10.0*i
 56:   10   continue

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

 63:        call VecSetValues(x,6,loc,xwork,INSERT_VALUES,ierr)

 65: !  Assemble vector

 67:        call VecAssemblyBegin(x,ierr)
 68:        call VecAssemblyEnd(x,ierr)

 70: !  View vector

 72:        write(6,20)
 73:   20   format('initial vector:')
 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, VecGetArrayF90() 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.

 83:        call VecGetArrayF90(x,xx_v,ierr)
 84:        call VecGetArrayF90(y,yy_v,ierr)

 86: !  Modify vector data

 88:        do 30 i=1,n
 89:           xx_v(i) = 100.0*i
 90:           yy_v(i) = 1000.0*i
 91:   30   continue

 93: !  Restore vectors

 95:        call VecRestoreArrayF90(x,xx_v,ierr)
 96:        call VecRestoreArrayF90(y,yy_v,ierr)

 98: !  View vectors

100:        write(6,40)
101:   40   format('new vector 1:')
102:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)

104:        write(6,50)
105:   50   format('new vector 2:')
106:        call VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr)

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

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