Actual source code: ex4f.F90
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>
8: program main
9: use petscvec
10: implicit none
12: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13: ! Beginning of program
14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
22: PetscCallA(PetscInitialize(ierr))
23: n = 6
24: isix = 6
26: ! Create initial vector and duplicate it
28: PetscCallA(VecCreateSeq(PETSC_COMM_SELF, n, x, ierr))
29: PetscCallA(VecDuplicate(x, y, ierr))
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)
35: do i = 1, n
36: loc(i) = i - 1
37: xwork(i) = 10.0*real(i)
38: end do
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).
45: PetscCallA(VecSetValues(x, isix, loc, xwork, INSERT_VALUES, ierr))
47: ! Assemble vector
49: PetscCallA(VecAssemblyBegin(x, ierr))
50: PetscCallA(VecAssemblyEnd(x, ierr))
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))
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.
65: PetscCallA(VecGetArray(x, xx_v, ierr))
66: PetscCallA(VecGetArray(y, yy_v, ierr))
68: ! Modify vector data
70: do i = 1, n
71: xx_v(i) = 100.0*real(i)
72: yy_v(i) = 1000.0*real(i)
73: end do
75: ! Restore vectors
77: PetscCallA(VecRestoreArray(x, xx_v, ierr))
78: PetscCallA(VecRestoreArray(y, yy_v, ierr))
80: ! View vectors
81: PetscCallA(PetscObjectSetName(x, 'new vector 1:', ierr))
82: PetscCallA(VecView(x, PETSC_VIEWER_STDOUT_SELF, ierr))
84: PetscCallA(PetscObjectSetName(y, 'new vector 2:', ierr))
85: PetscCallA(VecView(y, PETSC_VIEWER_STDOUT_SELF, ierr))
87: ! Free work space. All PETSc objects should be destroyed when they
88: ! are no longer needed.
90: PetscCallA(VecDestroy(x, ierr))
91: PetscCallA(VecDestroy(y, ierr))
92: PetscCallA(PetscFinalize(ierr))
93: end
95: !/*TEST
96: !
97: ! test:
98: !
99: !TEST*/