Actual source code: ex4f90.F

  1: !
  2: !      "$Id: ex4f90.F,v 1.13 2001/08/07 03:02:34 balay Exp $";
  3: !
  4: !  Description:  Illustrates the use of VecSetValues() to set
  5: !  multiple values at once; demonstrates VecGetArrayF90().
  6: !
  7: !/*T
  8: !   Concepts: vectors^assembling vectors;
  9: !   Concepts: vectors^arrays;
 10: !   Concepts: Fortran90^assembling vectors;
 11: !   Processors: 1
 12: !T*/
 13: ! -----------------------------------------------------------------------

 15:       program main
 16:       implicit none

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

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

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

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


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

 45: !  Create initial vector and duplicate it

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

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

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

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

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

 66: !  Assemble vector

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

 71: !  View vector

 73:        write(6,20)
 74:   20   format('initial vector:')
 75:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
 76:        call VecCopy(x,y,ierr)

 78: !  Get a pointer to vector data.
 79: !    - For default PETSc vectors, VecGetArrayF90() returns a pointer to
 80: !      the data array.  Otherwise, the routine is implementation dependent.
 81: !    - You MUST call VecRestoreArray() when you no longer need access to
 82: !      the array.

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

 87: !  Modify vector data

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

 94: !  Restore vectors

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

 99: !  View vectors

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

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

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

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