Actual source code: ex4f90.F
petsc-3.7.5 2017-01-01
1: !
2: !
3: ! Description: Illustrates the use of VecSetValues() to set
4: ! multiple values at once; demonstrates VecGetArrayF90().
5: !
6: !/*T
7: ! Concepts: vectors^assembling vectors;
8: ! Concepts: vectors^arrays;
9: ! Concepts: Fortran90^assembling vectors;
10: ! Processors: 1
11: !T*/
12: ! -----------------------------------------------------------------------
14: program main
15: implicit none
17: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18: ! Include files
19: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: !
21: ! The following include statements are required for Fortran programs
22: ! that use PETSc vectors:
23: ! petscsys.h - base PETSc routines
24: ! petscvec.h - vectors
25: ! petscvec.h90 - to allow access to Fortran90 features of vectors
27: #include <petsc/finclude/petscsys.h>
28: #include <petsc/finclude/petscvec.h>
29: #include <petsc/finclude/petscvec.h90>
31: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32: ! Beginning of program
33: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35: PetscScalar xwork(6)
36: PetscScalar, pointer :: xx_v(:),yy_v(:)
37: PetscInt i,n,loc(6)
38: PetscErrorCode ierr
39: Vec x,y
42: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
43: n = 6
45: ! Create initial vector and duplicate it
47: call VecCreateSeq(PETSC_COMM_SELF,n,x,ierr)
48: call VecDuplicate(x,y,ierr)
50: ! Fill work arrays with vector entries and locations. Note that
51: ! the vector indices are 0-based in PETSc (for both Fortran and
52: ! C vectors)
54: do 10 i=1,n
55: loc(i) = i-1
56: xwork(i) = 10.0*real(i)
57: 10 continue
59: ! Set vector values. Note that we set multiple entries at once.
60: ! Of course, usually one would create a work array that is the
61: ! natural size for a particular problem (not one that is as long
62: ! as the full vector).
64: call VecSetValues(x,n,loc,xwork,INSERT_VALUES,ierr)
66: ! Assemble vector
68: call VecAssemblyBegin(x,ierr)
69: call VecAssemblyEnd(x,ierr)
71: ! View vector
72: call PetscObjectSetName(x, 'initial vector:',ierr)
73: call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
74: call VecCopy(x,y,ierr)
76: ! Get a pointer to vector data.
77: ! - For default PETSc vectors, VecGetArrayF90() returns a pointer to
78: ! the data array. Otherwise, the routine is implementation dependent.
79: ! - You MUST call VecRestoreArray() when you no longer need access to
80: ! the array.
82: call VecGetArrayF90(x,xx_v,ierr)
83: call VecGetArrayF90(y,yy_v,ierr)
85: ! Modify vector data
87: do 30 i=1,n
88: xx_v(i) = 100.0*real(i)
89: yy_v(i) = 1000.0*real(i)
90: 30 continue
92: ! Restore vectors
94: call VecRestoreArrayF90(x,xx_v,ierr)
95: call VecRestoreArrayF90(y,yy_v,ierr)
97: ! View vectors
98: call PetscObjectSetName(x, 'new vector 1:',ierr)
99: call VecView(x,PETSC_VIEWER_STDOUT_SELF,ierr)
101: call PetscObjectSetName(y, 'new vector 2:',ierr)
102: call VecView(y,PETSC_VIEWER_STDOUT_SELF,ierr)
104: ! Free work space. All PETSc objects should be destroyed when they
105: ! are no longer needed.
107: call VecDestroy(x,ierr)
108: call VecDestroy(y,ierr)
109: call PetscFinalize(ierr)
110: end