Actual source code: ex4f90.F90

petsc-3.10.5 2019-03-28
Report Typos and Errors
  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:  #include <petsc/finclude/petscvec.h>
 16:       use petscvec
 17:       implicit none

 19: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 20: !                 Beginning of program
 21: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 23:        PetscScalar  xwork(6)
 24:        PetscScalar, pointer ::  xx_v(:),yy_v(:)
 25:        PetscInt i,n,loc(6)
 26:        PetscErrorCode ierr
 27:        Vec     x,y


 30:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 31:        if (ierr .ne. 0) then
 32:          print*,'PetscInitialize failed'
 33:          stop
 34:        endif
 35:        n = 6

 37: !  Create initial vector and duplicate it

 39:        call VecCreateSeq(PETSC_COMM_SELF,n,x,ierr);CHKERRA(ierr)
 40:        call VecDuplicate(x,y,ierr);CHKERRA(ierr)

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

 46:        do 10 i=1,n
 47:           loc(i) = i-1
 48:           xwork(i) = 10.0*real(i)
 49:   10   continue

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

 56:        call VecSetValues(x,n,loc,xwork,INSERT_VALUES,ierr);CHKERRA(ierr)

 58: !  Assemble vector

 60:        call VecAssemblyBegin(x,ierr);CHKERRA(ierr)
 61:        call VecAssemblyEnd(x,ierr);CHKERRA(ierr)

 63: !  View vector
 64:        call PetscObjectSetName(x, 'initial vector:',ierr);CHKERRA(ierr)
 65:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)
 66:        call VecCopy(x,y,ierr);CHKERRA(ierr)

 68: !  Get a pointer to vector data.
 69: !    - For default PETSc vectors, VecGetArrayF90() returns a pointer to
 70: !      the data array.  Otherwise, the routine is implementation dependent.
 71: !    - You MUST call VecRestoreArray() when you no longer need access to
 72: !      the array.

 74:        call VecGetArrayF90(x,xx_v,ierr);CHKERRA(ierr)
 75:        call VecGetArrayF90(y,yy_v,ierr);CHKERRA(ierr)

 77: !  Modify vector data

 79:        do 30 i=1,n
 80:           xx_v(i) = 100.0*real(i)
 81:           yy_v(i) = 1000.0*real(i)
 82:   30   continue

 84: !  Restore vectors

 86:        call VecRestoreArrayF90(x,xx_v,ierr);CHKERRA(ierr)
 87:        call VecRestoreArrayF90(y,yy_v,ierr);CHKERRA(ierr)

 89: !  View vectors
 90:        call PetscObjectSetName(x, 'new vector 1:',ierr);CHKERRA(ierr)
 91:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)

 93:        call PetscObjectSetName(y, 'new vector 2:',ierr);CHKERRA(ierr)
 94:        call VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)

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

 99:        call VecDestroy(x,ierr);CHKERRA(ierr)
100:        call VecDestroy(y,ierr);CHKERRA(ierr)
101:        call PetscFinalize(ierr)
102:        end

104: !
105: !/*TEST
106: !
107: !     test:
108: !
109: !TEST*/