1program main 2 3#include <petsc/finclude/petscvec.h> 4 5 use petscvec 6 implicit none 7 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 14 15 PetscCallA(PetscInitialize(ierr)) 16 17 PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-n', n, flg, ierr)) 18 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)) 24 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)) 29 30 !Set the vectors to entries to a constant value. 31 PetscCallA(VecSet(v, sone, ierr)) 32 33 !Get the first component from the multi-component vector to the single vector 34 PetscCallA(VecStrideGather(v, zero, s, INSERT_VALUES, ierr)) 35 36 PetscCallA(VecView(s, PETSC_VIEWER_STDOUT_WORLD, ierr)) 37 38 !Put the values back into the second component 39 PetscCallA(VecStrideScatter(s, one, v, ADD_VALUES, ierr)) 40 41 PetscCallA(VecView(v, PETSC_VIEWER_STDOUT_WORLD, ierr)) 42 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)) 47 48end program 49 50!/*TEST 51! 52! test: 53! nsize: 2 54! output_file: output/ex12_1.out 55! 56!TEST*/ 57