Actual source code: ex20f90.F90
1: !
2: !
3: !
4: ! -----------------------------------------------------------------------
6: program main
8: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
10: !
11: !
12: ! This examples uses Fortran 90 MODULES instead of include files
13: !
14: #include <petsc/finclude/petscvec.h>
15: use petscvec
16: implicit none
18: !
19: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: ! Variable declarations
21: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: !
23: ! Variables:
24: ! x, y, w - vectors
25: ! z - array of vectors
26: !
27: type(tVec) x,y,w
28: type(tVec), pointer :: z(:)
30: PetscReal norm,v,v1,v2,tol
31: PetscInt n,ithree
32: PetscErrorCode ierr
33: PetscMPIInt rank
34: PetscBool flg
35: PetscScalar one,two,three
36: PetscScalar dots(3),dot
37: PetscReal nfloat
39: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40: ! Beginning of program
41: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
44: if (ierr .ne. 0) then
45: print*,'Unable to initialize PETSc'
46: stop
47: endif
49: tol = 1.e-10_PETSC_REAL_KIND
50: one = 1.0
51: two = 2.0
52: three = 3.0
53: n = 20
54: ithree = 3
56: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr);CHKERRA(ierr)
57: nfloat = n
58: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr);CHKERRA(ierr)
60: ! Create a vector, specifying only its global dimension.
61: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
62: ! the vector format (currently parallel
63: ! or sequential) is determined at runtime. Also, the parallel
64: ! partitioning of the vector is determined by PETSc at runtime.
65: !
66: ! Routines for creating particular vector types directly are:
67: ! VecCreateSeq() - uniprocessor vector
68: ! VecCreateMPI() - distributed vector, where the user can
69: ! determine the parallel partitioning
71: call VecCreate(PETSC_COMM_WORLD,x,ierr);CHKERRA(ierr)
72: call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
73: call VecSetFromOptions(x,ierr);CHKERRA(ierr)
75: ! Duplicate some work vectors (of the same format and
76: ! partitioning as the initial vector).
78: call VecDuplicate(x,y,ierr);CHKERRA(ierr)
79: call VecDuplicate(x,w,ierr);CHKERRA(ierr)
81: ! Duplicate more work vectors (of the same format and
82: ! partitioning as the initial vector). Here we duplicate
83: ! an array of vectors, which is often more convenient than
84: ! duplicating individual ones.
86: call VecDuplicateVecsF90(x,ithree,z,ierr);CHKERRA(ierr)
88: ! Set the vectors to entries to a constant value.
90: call VecSet(x,one,ierr);CHKERRA(ierr)
91: call VecSet(y,two,ierr);CHKERRA(ierr)
92: call VecSet(z(1),one,ierr);CHKERRA(ierr)
93: call VecSet(z(2),two,ierr);CHKERRA(ierr)
94: call VecSet(z(3),three,ierr);CHKERRA(ierr)
96: ! Demonstrate various basic vector routines.
98: call VecDot(x,x,dot,ierr);CHKERRA(ierr)
99: call VecMDot(x,ithree,z,dots,ierr);CHKERRA(ierr)
101: ! Note: If using a complex numbers version of PETSc, then
102: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
103: ! (when using real numbers) it is undefined.
105: if (rank .eq. 0) then
106: #if defined(PETSC_USE_COMPLEX)
107: write(6,100) int(PetscRealPart(dot))
108: write(6,110) int(PetscRealPart(dots(1))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
109: #else
110: write(6,100) int(dot)
111: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
112: #endif
113: write(6,120)
114: endif
115: 100 format ('Vector length ',i6)
116: 110 format ('Vector length ',3(i6))
117: 120 format ('All other values should be near zero')
119: call VecScale(x,two,ierr);CHKERRA(ierr)
120: call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
121: v = abs(norm-2.0*sqrt(nfloat))
122: if (v .gt. -tol .and. v .lt. tol) v = 0.0
123: if (rank .eq. 0) write(6,130) v
124: 130 format ('VecScale ',1pe9.2)
126: call VecCopy(x,w,ierr);CHKERRA(ierr)
127: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
128: v = abs(norm-2.0*sqrt(nfloat))
129: if (v .gt. -tol .and. v .lt. tol) v = 0.0
130: if (rank .eq. 0) write(6,140) v
131: 140 format ('VecCopy ',1pe9.2)
133: call VecAXPY(y,three,x,ierr);CHKERRA(ierr)
134: call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
135: v = abs(norm-8.0*sqrt(nfloat))
136: if (v .gt. -tol .and. v .lt. tol) v = 0.0
137: if (rank .eq. 0) write(6,150) v
138: 150 format ('VecAXPY ',1pe9.2)
140: call VecAYPX(y,two,x,ierr);CHKERRA(ierr)
141: call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
142: v = abs(norm-18.0*sqrt(nfloat))
143: if (v .gt. -tol .and. v .lt. tol) v = 0.0
144: if (rank .eq. 0) write(6,160) v
145: 160 format ('VecAYXP ',1pe9.2)
147: call VecSwap(x,y,ierr);CHKERRA(ierr)
148: call VecNorm(y,NORM_2,norm,ierr);CHKERRA(ierr)
149: v = abs(norm-2.0*sqrt(nfloat))
150: if (v .gt. -tol .and. v .lt. tol) v = 0.0
151: if (rank .eq. 0) write(6,170) v
152: 170 format ('VecSwap ',1pe9.2)
154: call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
155: v = abs(norm-18.0*sqrt(nfloat))
156: if (v .gt. -tol .and. v .lt. tol) v = 0.0
157: if (rank .eq. 0) write(6,180) v
158: 180 format ('VecSwap ',1pe9.2)
160: call VecWAXPY(w,two,x,y,ierr);CHKERRA(ierr)
161: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
162: v = abs(norm-38.0*sqrt(nfloat))
163: if (v .gt. -tol .and. v .lt. tol) v = 0.0
164: if (rank .eq. 0) write(6,190) v
165: 190 format ('VecWAXPY ',1pe9.2)
167: call VecPointwiseMult(w,y,x,ierr);CHKERRA(ierr)
168: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
169: v = abs(norm-36.0*sqrt(nfloat))
170: if (v .gt. -tol .and. v .lt. tol) v = 0.0
171: if (rank .eq. 0) write(6,200) v
172: 200 format ('VecPointwiseMult ',1pe9.2)
174: call VecPointwiseDivide(w,x,y,ierr);CHKERRA(ierr)
175: call VecNorm(w,NORM_2,norm,ierr);CHKERRA(ierr)
176: v = abs(norm-9.0*sqrt(nfloat))
177: if (v .gt. -tol .and. v .lt. tol) v = 0.0
178: if (rank .eq. 0) write(6,210) v
179: 210 format ('VecPointwiseDivide ',1pe9.2)
181: dots(1) = one
182: dots(2) = three
183: dots(3) = two
184: call VecSet(x,one,ierr);CHKERRA(ierr)
185: call VecMAXPY(x,ithree,dots,z,ierr);CHKERRA(ierr)
186: call VecNorm(z(1),NORM_2,norm,ierr);CHKERRA(ierr)
187: v = abs(norm-sqrt(nfloat))
188: if (v .gt. -tol .and. v .lt. tol) v = 0.0
189: call VecNorm(z(2),NORM_2,norm,ierr);CHKERRA(ierr)
190: v1 = abs(norm-2.0*sqrt(nfloat))
191: if (v1 .gt. -tol .and. v1 .lt. tol) v1 = 0.0
192: call VecNorm(z(3),NORM_2,norm,ierr);CHKERRA(ierr)
193: v2 = abs(norm-3.0*sqrt(nfloat))
194: if (v2 .gt. -tol .and. v2 .lt. tol) v2 = 0.0
195: if (rank .eq. 0) write(6,220) v,v1,v2
196: 220 format ('VecMAXPY ',3(1pe9.2))
198: ! Free work space. All PETSc objects should be destroyed when they
199: ! are no longer needed.
201: call VecDestroy(x,ierr);CHKERRA(ierr)
202: call VecDestroy(y,ierr);CHKERRA(ierr)
203: call VecDestroy(w,ierr);CHKERRA(ierr)
204: call VecDestroyVecsF90(ithree,z,ierr);CHKERRA(ierr)
205: call PetscFinalize(ierr)
207: end
209: !/*TEST
210: !
211: ! test:
212: !
213: !TEST*/