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
     44:   PetscCallA(VecAssemblyBegin(v,ierr));
     45:   PetscCallA(VecAssemblyEnd(v,ierr));

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

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

     51:   PetscCallA(VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr))
     52:   PetscCallA(VecView(r,PETSC_VIEWER_STDOUT_WORLD,ierr))

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

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

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

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

     66: end program

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