xref: /petsc/src/vec/vec/tutorials/ex12f.F90 (revision 3703a6a7d9ceda88ee07755360990e1609c9169a)
1program main
2
3#include <petsc/finclude/petscvec.h>
4
5use petscvec
6implicit 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 call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
16  if (ierr /= 0) then
17   print*,'PetscInitialize failed'
18   stop
19  endif
20
21  call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-n",n,flg,ierr);CHKERRA(ierr)
22
23
24  !Create multi-component vector with 2 components
25  call VecCreate(PETSC_COMM_WORLD,v,ierr);CHKERRA(ierr)
26  call VecSetSizes(v,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
27  call VecSetBlockSize(v,two,ierr);CHKERRA(ierr)
28  call VecSetFromOptions(v,ierr);CHKERRA(ierr)
29
30
31  !Create single-component vector
32  call VecCreate(PETSC_COMM_WORLD,s,ierr);CHKERRA(ierr)
33  call VecSetSizes(s,PETSC_DECIDE,n/2,ierr);CHKERRA(ierr)
34  call VecSetFromOptions(s,ierr);CHKERRA(ierr)
35
36  !Set the vectors to entries to a constant value.
37  call VecSet(v,sone,ierr);CHKERRA(ierr)
38
39  !Get the first component from the multi-component vector to the single vector
40  call VecStrideGather(v,zero,s,INSERT_VALUES,ierr);CHKERRA(ierr)
41
42  call VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
43
44
45  !Put the values back into the second component
46  call VecStrideScatter(s,one,v,ADD_VALUES,ierr);CHKERRA(ierr)
47
48  call VecView(v,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
49
50
51  !Free work space.All PETSc objects should be destroyed when they are no longer needed.
52
53  call VecDestroy(v,ierr);CHKERRA(ierr)
54  call VecDestroy(s,ierr);CHKERRA(ierr)
55  call PetscFinalize(ierr);CHKERRA(ierr)
56
57  end program
58
59!/*TEST
60!
61!     test:
62!       nsize: 2
63!       output_file: output/ex12_1.out
64!
65!TEST*/
66