Date: 2022-08-02T03:13:37+00:00

Actual source code: ex4f90.F90

      1: !
      2: !
      3: !  Description:  Illustrates the use of VecSetValues() to set
      4: !  multiple values at once; demonstrates VecGetArrayF90().
      5: !
      6: ! -----------------------------------------------------------------------

      8:       program main
      9: #include <petsc/finclude/petscvec.h>
     10:       use petscvec
     11:       implicit none

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

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

     25:        call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     26:        if (ierr /= 0) then
     27:          print*,'PetscInitialize failed'
     28:          stop
     29:        endif

     31: !  Create initial vector and duplicate it

     33:        call VecCreateSeq(PETSC_COMM_SELF,n,x,ierr);CHKERRA(ierr)
     34:        call VecDuplicate(x,y,ierr);CHKERRA(ierr)

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

     40:        do 10 i=1,n
     41:           loc(i) = i-1
     42:           xwork(i) = 10.0*real(i)
     43:   10   continue

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

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

     52: !  Assemble vector

     54:        call VecAssemblyBegin(x,ierr);CHKERRA(ierr)
     55:        call VecAssemblyEnd(x,ierr);CHKERRA(ierr)

     57: !  View vector
     58:        call PetscObjectSetName(x, 'initial vector:',ierr);CHKERRA(ierr)
     59:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)
     60:        call VecCopy(x,y,ierr);CHKERRA(ierr)

     62: !  Get a pointer to vector data.
     63: !    - For default PETSc vectors, VecGetArrayF90() returns a pointer to
     64: !      the data array.  Otherwise, the routine is implementation dependent.
     65: !    - You MUST call VecRestoreArray() when you no longer need access to
     66: !      the array.

     68:        call VecGetArrayF90(x,xx_v,ierr);CHKERRA(ierr)
     69:        call VecGetArrayF90(y,yy_v,ierr);CHKERRA(ierr)

     71: !  Modify vector data

     73:        do 30 i=1,n
     74:           xx_v(i) = 100.0*real(i)
     75:           yy_v(i) = 1000.0*real(i)
     76:   30   continue

     78: !  Restore vectors

     80:        call VecRestoreArrayF90(x,xx_v,ierr);CHKERRA(ierr)
     81:        call VecRestoreArrayF90(y,yy_v,ierr);CHKERRA(ierr)

     83: !  View vectors
     84:        call PetscObjectSetName(x, 'new vector 1:',ierr);CHKERRA(ierr)
     85:        call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr);CHKERRA(ierr)

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

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

     93:        call VecDestroy(x,ierr);CHKERRA(ierr)
     94:        call VecDestroy(y,ierr);CHKERRA(ierr)
     95:        call PetscFinalize(ierr)
     96:        end

     98: !
     99: !/*TEST
    100: !
    101: !     test:
    102: !
    103: !TEST*/