xref: /petsc/src/vec/vec/tutorials/ex4f.F90 (revision 9b88ac225e01f016352a5f4cd90e158abe5f5675)
1!
2!
3!  Description:  Illustrates the use of VecSetValues() to set
4!  multiple values at once; demonstrates VecGetArray().
5!
6! -----------------------------------------------------------------------
7#include <petsc/finclude/petscvec.h>
8program main
9  use petscvec
10  implicit none
11
12! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13!                 Beginning of program
14! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15
16  PetscScalar xwork(6)
17  PetscScalar, pointer :: xx_v(:), yy_v(:)
18  PetscInt i, n, loc(6), isix
19  PetscErrorCode ierr
20  Vec x, y
21
22  PetscCallA(PetscInitialize(ierr))
23  n = 6
24  isix = 6
25
26!  Create initial vector and duplicate it
27
28  PetscCallA(VecCreateSeq(PETSC_COMM_SELF, n, x, ierr))
29  PetscCallA(VecDuplicate(x, y, ierr))
30
31!  Fill work arrays with vector entries and locations.  Note that
32!  the vector indices are 0-based in PETSc (for both Fortran and
33!  C vectors)
34
35  do i = 1, n
36    loc(i) = i - 1
37    xwork(i) = 10.0*real(i)
38  end do
39
40!  Set vector values.  Note that we set multiple entries at once.
41!  Of course, usually one would create a work array that is the
42!  natural size for a particular problem (not one that is as long
43!  as the full vector).
44
45  PetscCallA(VecSetValues(x, isix, loc, xwork, INSERT_VALUES, ierr))
46
47!  Assemble vector
48
49  PetscCallA(VecAssemblyBegin(x, ierr))
50  PetscCallA(VecAssemblyEnd(x, ierr))
51
52!  View vector
53  PetscCallA(PetscObjectSetName(x, 'initial vector:', ierr))
54  PetscCallA(VecView(x, PETSC_VIEWER_STDOUT_SELF, ierr))
55  PetscCallA(VecCopy(x, y, ierr))
56
57!  Get a pointer to vector data.
58!    - For default PETSc vectors, VecGetArray() returns a pointer to
59!      the data array.  Otherwise, the routine is implementation dependent.
60!    - You MUST call VecRestoreArray() when you no longer need access to
61!      the array.
62!    - Note that the Fortran interface to VecGetArray() differs from the
63!      C version.  See the users manual for details.
64
65  PetscCallA(VecGetArray(x, xx_v, ierr))
66  PetscCallA(VecGetArray(y, yy_v, ierr))
67
68!  Modify vector data
69
70  do i = 1, n
71    xx_v(i) = 100.0*real(i)
72    yy_v(i) = 1000.0*real(i)
73  end do
74
75!  Restore vectors
76
77  PetscCallA(VecRestoreArray(x, xx_v, ierr))
78  PetscCallA(VecRestoreArray(y, yy_v, ierr))
79
80!  View vectors
81  PetscCallA(PetscObjectSetName(x, 'new vector 1:', ierr))
82  PetscCallA(VecView(x, PETSC_VIEWER_STDOUT_SELF, ierr))
83
84  PetscCallA(PetscObjectSetName(y, 'new vector 2:', ierr))
85  PetscCallA(VecView(y, PETSC_VIEWER_STDOUT_SELF, ierr))
86
87!  Free work space.  All PETSc objects should be destroyed when they
88!  are no longer needed.
89
90  PetscCallA(VecDestroy(x, ierr))
91  PetscCallA(VecDestroy(y, ierr))
92  PetscCallA(PetscFinalize(ierr))
93end
94
95!/*TEST
96!
97!     test:
98!
99!TEST*/
100