Actual source code: ex20f90.F90
petsc-3.7.5 2017-01-01
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 <petsc/finclude/petscsysdef.h>
22: #include <petsc/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 <petsc/finclude/petscsys.h>
29: #include <petsc/finclude/petscvec.h>
30: #include <petsc/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
56: PetscReal nfloat
58: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59: ! Beginning of program
60: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
63: one = 1.0
64: two = 2.0
65: three = 3.0
66: n = 20
67: ithree = 3
69: call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER, &
70: & '-n',n,flg,ierr)
71: nfloat = n
72: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
74: ! Create a vector, specifying only its global dimension.
75: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
76: ! the vector format (currently parallel
77: ! or sequential) is determined at runtime. Also, the parallel
78: ! partitioning of the vector is determined by PETSc at runtime.
79: !
80: ! Routines for creating particular vector types directly are:
81: ! VecCreateSeq() - uniprocessor vector
82: ! VecCreateMPI() - distributed vector, where the user can
83: ! determine the parallel partitioning
85: call VecCreate(PETSC_COMM_WORLD,x,ierr)
86: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
87: call VecSetFromOptions(x,ierr)
89: ! Duplicate some work vectors (of the same format and
90: ! partitioning as the initial vector).
92: call VecDuplicate(x,y,ierr)
93: call VecDuplicate(x,w,ierr)
95: ! Duplicate more work vectors (of the same format and
96: ! partitioning as the initial vector). Here we duplicate
97: ! an array of vectors, which is often more convenient than
98: ! duplicating individual ones.
100: call VecDuplicateVecsF90(x,ithree,z,ierr)
102: ! Set the vectors to entries to a constant value.
104: call VecSet(x,one,ierr)
105: call VecSet(y,two,ierr)
106: call VecSet(z(1),one,ierr)
107: call VecSet(z(2),two,ierr)
108: call VecSet(z(3),three,ierr)
110: ! Demonstrate various basic vector routines.
112: call VecDot(x,x,dot,ierr)
113: call VecMDot(x,ithree,z,dots,ierr)
115: ! Note: If using a complex numbers version of PETSc, then
116: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
117: ! (when using real numbers) it is undefined.
119: if (rank .eq. 0) then
120: #if defined(PETSC_USE_COMPLEX)
121: write(6,100) int(PetscRealPart(dot))
122: write(6,110) int(PetscRealPart(dots(1))), &
123: & int(PetscRealPart(dots(2))), &
124: & int(PetscRealPart(dots(3)))
125: #else
126: write(6,100) int(dot)
127: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
128: #endif
129: write(6,120)
130: endif
131: 100 format ('Vector length ',i6)
132: 110 format ('Vector length ',3(i6))
133: 120 format ('All other values should be near zero')
135: call VecScale(x,two,ierr)
136: call VecNorm(x,NORM_2,norm,ierr)
137: v = abs(norm-2.0*sqrt(nfloat))
138: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
139: if (rank .eq. 0) write(6,130) v
140: 130 format ('VecScale ',1pe8.2)
142: call VecCopy(x,w,ierr)
143: call VecNorm(w,NORM_2,norm,ierr)
144: v = abs(norm-2.0*sqrt(nfloat))
145: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
146: if (rank .eq. 0) write(6,140) v
147: 140 format ('VecCopy ',1pe8.2)
149: call VecAXPY(y,three,x,ierr)
150: call VecNorm(y,NORM_2,norm,ierr)
151: v = abs(norm-8.0*sqrt(nfloat))
152: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
153: if (rank .eq. 0) write(6,150) v
154: 150 format ('VecAXPY ',1pe8.2)
156: call VecAYPX(y,two,x,ierr)
157: call VecNorm(y,NORM_2,norm,ierr)
158: v = abs(norm-18.0*sqrt(nfloat))
159: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
160: if (rank .eq. 0) write(6,160) v
161: 160 format ('VecAYXP ',1pe8.2)
163: call VecSwap(x,y,ierr)
164: call VecNorm(y,NORM_2,norm,ierr)
165: v = abs(norm-2.0*sqrt(nfloat))
166: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
167: if (rank .eq. 0) write(6,170) v
168: 170 format ('VecSwap ',1pe8.2)
170: call VecNorm(x,NORM_2,norm,ierr)
171: v = abs(norm-18.0*sqrt(nfloat))
172: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
173: if (rank .eq. 0) write(6,180) v
174: 180 format ('VecSwap ',1pe8.2)
176: call VecWAXPY(w,two,x,y,ierr)
177: call VecNorm(w,NORM_2,norm,ierr)
178: v = abs(norm-38.0*sqrt(nfloat))
179: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
180: if (rank .eq. 0) write(6,190) v
181: 190 format ('VecWAXPY ',1pe8.2)
183: call VecPointwiseMult(w,y,x,ierr)
184: call VecNorm(w,NORM_2,norm,ierr)
185: v = abs(norm-36.0*sqrt(nfloat))
186: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
187: if (rank .eq. 0) write(6,200) v
188: 200 format ('VecPointwiseMult ',1pe8.2)
190: call VecPointwiseDivide(w,x,y,ierr)
191: call VecNorm(w,NORM_2,norm,ierr)
192: v = abs(norm-9.0*sqrt(nfloat))
193: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
194: if (rank .eq. 0) write(6,210) v
195: 210 format ('VecPointwiseDivide ',1pe8.2)
198: dots(1) = one
199: dots(2) = three
200: dots(3) = two
201: call VecSet(x,one,ierr)
202: call VecMAXPY(x,ithree,dots,z,ierr)
203: call VecNorm(z(1),NORM_2,norm,ierr)
204: v = abs(norm-sqrt(nfloat))
205: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
206: call VecNorm(z(2),NORM_2,norm,ierr)
207: v1 = abs(norm-2.0*sqrt(nfloat))
208: if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
209: call VecNorm(z(3),NORM_2,norm,ierr)
210: v2 = abs(norm-3.0*sqrt(nfloat))
211: if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
212: if (rank .eq. 0) write(6,220) v,v1,v2
213: 220 format ('VecMAXPY ',3(1pe8.2))
216: ! Free work space. All PETSc objects should be destroyed when they
217: ! are no longer needed.
219: call VecDestroy(x,ierr)
220: call VecDestroy(y,ierr)
221: call VecDestroy(w,ierr)
222: call VecDestroyVecsF90(ithree,z,ierr)
223: call PetscFinalize(ierr)
225: end