Date: 2025-03-29T05:58:55+00:00

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: ! -----------------------------------------------------------------------

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

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

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

     23:        PetscCallA(PetscInitialize(ierr))
     24:        n = 6
     25:        isix = 6

     27: !  Create initial vector and duplicate it

     29:        PetscCallA(VecCreateSeq(PETSC_COMM_SELF,n,x,ierr))
     30:        PetscCallA(VecDuplicate(x,y,ierr))

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

     36:        do 10 i=1,n
     37:           loc(i) = i-1
     38:           xwork(i) = 10.0*real(i)
     39:   10   continue

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

     46:        PetscCallA(VecSetValues(x,isix,loc,xwork,INSERT_VALUES,ierr))

     48: !  Assemble vector

     50:        PetscCallA(VecAssemblyBegin(x,ierr))
     51:        PetscCallA(VecAssemblyEnd(x,ierr))

     53: !  View vector
     54:        PetscCallA(PetscObjectSetName(x, 'initial vector:',ierr))
     55:        PetscCallA(VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr))
     56:        PetscCallA(VecCopy(x,y,ierr))

     58: !  Get a pointer to vector data.
     59: !    - For default PETSc vectors, VecGetArray() returns a pointer to
     60: !      the data array.  Otherwise, the routine is implementation dependent.
     61: !    - You MUST call VecRestoreArray() when you no longer need access to
     62: !      the array.
     63: !    - Note that the Fortran interface to VecGetArray() differs from the
     64: !      C version.  See the users manual for details.

     66:        PetscCallA(VecGetArray(x,xx_v,ierr))
     67:        PetscCallA(VecGetArray(y,yy_v,ierr))

     69: !  Modify vector data

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

     76: !  Restore vectors

     78:        PetscCallA(VecRestoreArray(x,xx_v,ierr))
     79:        PetscCallA(VecRestoreArray(y,yy_v,ierr))

     81: !  View vectors
     82:        PetscCallA(PetscObjectSetName(x, 'new vector 1:',ierr))
     83:        PetscCallA(VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr))

     85:        PetscCallA(PetscObjectSetName(y, 'new vector 2:',ierr))
     86:        PetscCallA(VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr))

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

     91:        PetscCallA(VecDestroy(x,ierr))
     92:        PetscCallA(VecDestroy(y,ierr))
     93:        PetscCallA(PetscFinalize(ierr))
     94:        end

     96: !/*TEST
     97: !
     98: !     test:
     99: !
    100: !TEST*/