Actual source code: ex16f.F90

  1: program main
  2: #include <petsc/finclude/petscvec.h>
  3: use petscvec
  4: implicit none

  6:   Vec ::           v,s,r
  7:   Vec,pointer,dimension(:) ::  vecs
  8:   PetscInt :: i,start
  9:   PetscInt :: endd
 10:   PetscInt,parameter :: n = 20, four = 4, two = 2, one = 1
 11:   PetscErrorCode ierr
 12:   PetscScalar  ::  myValue
 13:   PetscBool :: flg

 15:   PetscCallA(PetscInitialize(ierr))

 17:   PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-n",n,flg,ierr))

 19:   !Create multi-component vector with 2 components
 20:   PetscCallA(VecCreate(PETSC_COMM_WORLD,v,ierr))
 21:   PetscCallA(VecSetSizes(v,PETSC_DECIDE,n,ierr))
 22:   PetscCallA(VecSetBlockSize(v,four,ierr))
 23:   PetscCallA(VecSetFromOptions(v,ierr))

 25:   ! Create double-component vectors

 27:   PetscCallA(VecCreate(PETSC_COMM_WORLD,s,ierr))
 28:   PetscCallA(VecSetSizes(s,PETSC_DECIDE,n/two,ierr))
 29:   PetscCallA(VecSetBlockSize(s,two,ierr))
 30:   PetscCallA(VecSetFromOptions(s,ierr))
 31:   PetscCallA(VecDuplicate(s,r,ierr))
 32:   allocate(vecs(0:2))

 34:   vecs(0) = s
 35:   vecs(1) = r

 37:   !Set the vector values

 39:   PetscCallA(VecGetOwnershipRange(v,start,endd,ierr))
 40:   do i=start,endd-1
 41:      myValue = real(i)
 42:      PetscCallA(VecSetValues(v,one,i,myValue,INSERT_VALUES,ierr))
 43:   end do

 45:   ! Get the components from the multi-component vector to the other vectors

 47:   PetscCallA(VecStrideGatherAll(v,vecs,INSERT_VALUES,ierr))

 49:   PetscCallA(VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr))
 50:   PetscCallA(VecView(r,PETSC_VIEWER_STDOUT_WORLD,ierr))

 52:   PetscCallA(VecStrideScatterAll(vecs,v,ADD_VALUES,ierr))

 54:   PetscCallA(VecView(v,PETSC_VIEWER_STDOUT_WORLD,ierr))

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

 58:   deallocate(vecs)
 59:   PetscCallA(VecDestroy(v,ierr))
 60:   PetscCallA(VecDestroy(s,ierr))
 61:   PetscCallA(VecDestroy(r,ierr))
 62:   PetscCallA(PetscFinalize(ierr))

 64: end program

 66: !/*TEST
 67: !
 68: !     test:
 69: !       nsize: 2
 70: !       output_file: output/ex16_1.out
 71: !
 72: !TEST*/