Actual source code: ex12f.F90

      1: program main

      3: #include <petsc/finclude/petscvec.h>

      5: use petscvec
      6: implicit none

      8:   PetscErrorCode ierr
      9:   Vec   v,s
     10:   PetscInt,parameter ::      n   = 20
     11:   PetscScalar,parameter ::   sone = 1.0
     12:   PetscBool :: flg
     13:   PetscInt,parameter :: zero = 0, one = 1, two = 2

     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,two,ierr))
     23:   PetscCallA(VecSetFromOptions(v,ierr))

     25:   ! Create single-component vector
     26:   PetscCallA(VecCreate(PETSC_COMM_WORLD,s,ierr))
     27:   PetscCallA(VecSetSizes(s,PETSC_DECIDE,n/2,ierr))
     28:   PetscCallA(VecSetFromOptions(s,ierr))

     30:   !Set the vectors to entries to a constant value.
     31:   PetscCallA(VecSet(v,sone,ierr))

     33:   !Get the first component from the multi-component vector to the single vector
     34:   PetscCallA(VecStrideGather(v,zero,s,INSERT_VALUES,ierr))

     36:   PetscCallA(VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr))

     38:   !Put the values back into the second component
     39:   PetscCallA(VecStrideScatter(s,one,v,ADD_VALUES,ierr))

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

     43:   ! Free work space.All PETSc objects should be destroyed when they are no longer needed.
     44:   PetscCallA(VecDestroy(v,ierr))
     45:   PetscCallA(VecDestroy(s,ierr))
     46:   PetscCallA(PetscFinalize(ierr))

     48:   end program

     50: !/*TEST
     51: !
     52: !     test:
     53: !       nsize: 2
     54: !       output_file: output/ex12_1.out
     55: !
     56: !TEST*/