xref: /petsc/src/vec/vec/tutorials/ex12f.F90 (revision f971d49813e8f917b9adad75568de3e4defeb2d5)
1#include <petsc/finclude/petscvec.h>
2program main
3  use petscvec
4  implicit none
5
6  PetscErrorCode ierr
7  Vec v, s
8  PetscInt, parameter ::      n = 20
9  PetscScalar, parameter ::   sone = 1.0
10  PetscBool :: flg
11  PetscInt, parameter :: zero = 0, one = 1, two = 2
12
13  PetscCallA(PetscInitialize(ierr))
14
15  PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-n', n, flg, ierr))
16
17  ! Create multi-component vector with 2 components
18  PetscCallA(VecCreate(PETSC_COMM_WORLD, v, ierr))
19  PetscCallA(VecSetSizes(v, PETSC_DECIDE, n, ierr))
20  PetscCallA(VecSetBlockSize(v, two, ierr))
21  PetscCallA(VecSetFromOptions(v, ierr))
22
23  ! Create single-component vector
24  PetscCallA(VecCreate(PETSC_COMM_WORLD, s, ierr))
25  PetscCallA(VecSetSizes(s, PETSC_DECIDE, n/2, ierr))
26  PetscCallA(VecSetFromOptions(s, ierr))
27
28  !Set the vectors to entries to a constant value.
29  PetscCallA(VecSet(v, sone, ierr))
30
31  !Get the first component from the multi-component vector to the single vector
32  PetscCallA(VecStrideGather(v, zero, s, INSERT_VALUES, ierr))
33
34  PetscCallA(VecView(s, PETSC_VIEWER_STDOUT_WORLD, ierr))
35
36  !Put the values back into the second component
37  PetscCallA(VecStrideScatter(s, one, v, ADD_VALUES, ierr))
38
39  PetscCallA(VecView(v, PETSC_VIEWER_STDOUT_WORLD, ierr))
40
41  ! Free work space.All PETSc objects should be destroyed when they are no longer needed.
42  PetscCallA(VecDestroy(v, ierr))
43  PetscCallA(VecDestroy(s, ierr))
44  PetscCallA(PetscFinalize(ierr))
45
46end program
47
48!/*TEST
49!
50!     test:
51!       nsize: 2
52!       output_file: output/ex12_1.out
53!
54!TEST*/
55