Actual source code: ex20f90.F90
petsc-3.5.4 2015-05-23
1: !
2: !
3: !/*T
4: ! Concepts: vectors^using basic vector routines;
5: ! Concepts: Fortran90^using basic vector routines;
6: ! Processors: n
7: !T*/
8: !
9: ! -----------------------------------------------------------------------
11: program main
13: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15: !
16: !
17: ! This examples uses Fortran 90 MODULES instead of include files
18: ! see the manual page UsingFortran
19: !
20: #define PETSC_USE_FORTRAN_MODULES
21: #include <finclude/petscsysdef.h>
22: #include <finclude/petscvecdef.h>
23: #if defined(PETSC_USE_FORTRAN_MODULES)
24: use petscvec
25: #endif
26: implicit none
27: #if !defined(PETSC_USE_FORTRAN_MODULES)
28: #include <finclude/petscsys.h>
29: #include <finclude/petscvec.h>
30: #include <finclude/petscvec.h90>
32: #endif
33: !
34: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35: ! Variable declarations
36: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37: !
38: ! Variables:
39: ! x, y, w - vectors
40: ! z - array of vectors
41: !
42: #if defined(PETSC_USE_FORTRAN_DATATYPES)
43: type(Vec) x,y,w
44: type(Vec), pointer :: z(:)
45: #else
46: Vec x,y,w
47: Vec, pointer :: z(:)
48: #endif
49: PetscReal norm,v,v1,v2
50: PetscInt n,ithree
51: PetscErrorCode ierr
52: PetscMPIInt rank
53: PetscBool flg
54: PetscScalar one,two,three
55: PetscScalar dots(3),dot
57: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58: ! Beginning of program
59: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
62: one = 1.0
63: two = 2.0
64: three = 3.0
65: n = 20
66: ithree = 3
68: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
69: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
71: ! Create a vector, specifying only its global dimension.
72: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
73: ! the vector format (currently parallel
74: ! or sequential) is determined at runtime. Also, the parallel
75: ! partitioning of the vector is determined by PETSc at runtime.
76: !
77: ! Routines for creating particular vector types directly are:
78: ! VecCreateSeq() - uniprocessor vector
79: ! VecCreateMPI() - distributed vector, where the user can
80: ! determine the parallel partitioning
82: call VecCreate(PETSC_COMM_WORLD,x,ierr)
83: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
84: call VecSetFromOptions(x,ierr)
86: ! Duplicate some work vectors (of the same format and
87: ! partitioning as the initial vector).
89: call VecDuplicate(x,y,ierr)
90: call VecDuplicate(x,w,ierr)
92: ! Duplicate more work vectors (of the same format and
93: ! partitioning as the initial vector). Here we duplicate
94: ! an array of vectors, which is often more convenient than
95: ! duplicating individual ones.
97: call VecDuplicateVecsF90(x,ithree,z,ierr)
99: ! Set the vectors to entries to a constant value.
101: call VecSet(x,one,ierr)
102: call VecSet(y,two,ierr)
103: call VecSet(z(1),one,ierr)
104: call VecSet(z(2),two,ierr)
105: call VecSet(z(3),three,ierr)
107: ! Demonstrate various basic vector routines.
109: call VecDot(x,x,dot,ierr)
110: call VecMDot(x,ithree,z,dots,ierr)
112: ! Note: If using a complex numbers version of PETSc, then
113: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
114: ! (when using real numbers) it is undefined.
116: if (rank .eq. 0) then
117: #if defined(PETSC_USE_COMPLEX)
118: write(6,100) int(PetscRealPart(dot))
119: write(6,110) int(PetscRealPart(dots(1))), &
120: & int(PetscRealPart(dots(2))), &
121: & int(PetscRealPart(dots(3)))
122: #else
123: write(6,100) int(dot)
124: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
125: #endif
126: write(6,120)
127: endif
128: 100 format ("Vector length ",i6)
129: 110 format ("Vector length ",3(i6))
130: 120 format ("All other values should be near zero")
132: call VecScale(x,two,ierr)
133: call VecNorm(x,NORM_2,norm,ierr)
134: v = norm-2.0*sqrt(dble(n))
135: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
136: if (rank .eq. 0) write(6,130) v
137: 130 format ("VecScale ",1pe8.2)
139: call VecCopy(x,w,ierr)
140: call VecNorm(w,NORM_2,norm,ierr)
141: v = norm-2.0*sqrt(dble(n))
142: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
143: if (rank .eq. 0) write(6,140) v
144: 140 format ("VecCopy ",1pe8.2)
146: call VecAXPY(y,three,x,ierr)
147: call VecNorm(y,NORM_2,norm,ierr)
148: v = norm-8.0*sqrt(dble(n))
149: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
150: if (rank .eq. 0) write(6,150) v
151: 150 format ("VecAXPY ",1pe8.2)
153: call VecAYPX(y,two,x,ierr)
154: call VecNorm(y,NORM_2,norm,ierr)
155: v = norm-18.0*sqrt(dble(n))
156: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
157: if (rank .eq. 0) write(6,160) v
158: 160 format ("VecAYXP ",1pe8.2)
160: call VecSwap(x,y,ierr)
161: call VecNorm(y,NORM_2,norm,ierr)
162: v = norm-2.0*sqrt(dble(n))
163: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
164: if (rank .eq. 0) write(6,170) v
165: 170 format ("VecSwap ",1pe8.2)
167: call VecNorm(x,NORM_2,norm,ierr)
168: v = norm-18.0*sqrt(dble(n))
169: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
170: if (rank .eq. 0) write(6,180) v
171: 180 format ("VecSwap ",1pe8.2)
173: call VecWAXPY(w,two,x,y,ierr)
174: call VecNorm(w,NORM_2,norm,ierr)
175: v = norm-38.0*sqrt(dble(n))
176: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
177: if (rank .eq. 0) write(6,190) v
178: 190 format ("VecWAXPY ",1pe8.2)
180: call VecPointwiseMult(w,y,x,ierr)
181: call VecNorm(w,NORM_2,norm,ierr)
182: v = norm-36.0*sqrt(dble(n))
183: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
184: if (rank .eq. 0) write(6,200) v
185: 200 format ("VecPointwiseMult ",1pe8.2)
187: call VecPointwiseDivide(w,x,y,ierr)
188: call VecNorm(w,NORM_2,norm,ierr)
189: v = norm-9.0*sqrt(dble(n))
190: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
191: if (rank .eq. 0) write(6,210) v
192: 210 format ("VecPointwiseDivide ",1pe8.2)
195: dots(1) = one
196: dots(2) = three
197: dots(3) = two
198: call VecSet(x,one,ierr)
199: call VecMAXPY(x,ithree,dots,z,ierr)
200: call VecNorm(z(1),NORM_2,norm,ierr)
201: v = norm-sqrt(dble(n))
202: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
203: call VecNorm(z(2),NORM_2,norm,ierr)
204: v1 = norm-2.0*sqrt(dble(n))
205: if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
206: call VecNorm(z(3),NORM_2,norm,ierr)
207: v2 = norm-3.0*sqrt(dble(n))
208: if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
209: if (rank .eq. 0) write(6,220) v,v1,v2
210: 220 format ("VecMAXPY ",3(1pe8.2))
213: ! Free work space. All PETSc objects should be destroyed when they
214: ! are no longer needed.
216: call VecDestroy(x,ierr)
217: call VecDestroy(y,ierr)
218: call VecDestroy(w,ierr)
219: call VecDestroyVecsF90(ithree,z,ierr)
220: call PetscFinalize(ierr)
222: end