Actual source code: ex1f.F90
petsc-3.13.4 2020-08-01
1: !
2: !
3: !/*T
4: ! Concepts: vectors^basic routines
5: ! Processors: n
6: !T*/
7: !
8: ! -----------------------------------------------------------------------
10: program main
11: #include <petsc/finclude/petscvec.h>
12: use petscvec
13: implicit none
15: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16: ! Variable declarations
17: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18: !
19: ! Variables:
20: ! x, y, w - vectors
21: ! z - array of vectors
23: Vec :: x,y,w
24: Vec, dimension(5) :: z
25: PetscReal :: norm,v,v1,v2
26: PetscInt, parameter :: &
27: ithree = 3, &
28: n = 20
29: PetscBool :: flg
30: PetscErrorCode :: ierr
31: PetscMPIInt :: rank
32: PetscScalar, parameter :: &
33: one = 1.0, &
34: two = 2.0, &
35: three = 3.0
36: PetscScalar :: dot
37: PetscScalar, dimension(3) :: dots
38: character(len=40) :: name
39: PetscReal :: nfloat
41: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42: ! Beginning of program
43: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
46: if (ierr /= 0) then
47: print*,'Unable to initialize PETSc'
48: stop
49: endif
50: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
51: nfloat = n
52: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
54: ! Create a vector, specifying only its global dimension.
55: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
56: ! the vector format (currently parallel
57: ! or sequential) is determined at runtime. Also, the parallel
58: ! partitioning of the vector is determined by PETSc at runtime.
59: !
60: ! Routines for creating particular vector types directly are:
61: ! VecCreateSeq() - uniprocessor vector
62: ! VecCreateMPI() - distributed vector, where the user can
63: ! determine the parallel partitioning
64: ! VecCreateShared() - parallel vector that uses shared memory
65: ! (available only on the SGI); otherwise,
66: ! is the same as VecCreateMPI()
67: !
68: ! VecCreate(), VecSetSizes() and VecSetFromOptions() allows one
69: ! to determine at runtime which version to use
70: ! with the options -vec_type mpi or -vec_type shared
71: !
72: call VecCreate(PETSC_COMM_WORLD,x,ierr)
73: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
74: call VecSetFromOptions(x,ierr)
75: call VecGetType(x,name,ierr)
77: ! Duplicate some work vectors (of the same format and
78: ! partitioning as the initial vector).
80: call VecDuplicate(x,y,ierr)
81: call VecDuplicate(x,w,ierr)
83: ! Duplicate more work vectors (of the same format and
84: ! partitioning as the initial vector). Here we duplicate
85: ! an array of vectors, which is often more convenient than
86: ! duplicating individual ones.
88: call VecDuplicateVecs(x,ithree,z,ierr)
90: ! Set the vectors to entries to a constant value.
92: call VecSet(x,one,ierr)
94: call VecSet(y,two,ierr)
95: call VecSet(z(1),one,ierr)
96: call VecSet(z(2),two,ierr)
97: call VecSet(z(3),three,ierr)
99: ! Demonstrate various basic vector routines.
101: call VecDot(x,x,dot,ierr)
102: call VecMDot(x,ithree,z,dots,ierr)
104: ! Note: If using a complex numbers version of PETSc, then
105: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
106: ! (when using real numbers) it is undefined.
108: if (rank .eq. 0) then
109: #if defined(PETSC_USE_COMPLEX)
110: write(6,100) int(PetscRealPart(dot))
111: write(6,110) int(PetscRealPart(dots(1))),int(PetscRealPart(dots(2))),int(PetscRealPart(dots(3)))
112: #else
113: write(6,100) int(dot)
114: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
115: #endif
116: write(6,120)
117: endif
118: 100 format ('Vector length ',i6)
119: 110 format ('Vector length ',3(i6))
120: 120 format ('All other values should be near zero')
122: call VecScale(x,two,ierr)
123: call VecNorm(x,NORM_2,norm,ierr)
124: v = abs(norm-2.0*sqrt(nfloat))
125: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
126: if (rank == 0) write(6,130) v
127: 130 format ('VecScale ',1pe9.2)
129: call VecCopy(x,w,ierr)
130: call VecNorm(w,NORM_2,norm,ierr)
131: v = abs(norm-2.0*sqrt(nfloat))
132: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
133: if (rank == 0) write(6,140) v
134: 140 format ('VecCopy ',1pe9.2)
136: call VecAXPY(y,three,x,ierr)
137: call VecNorm(y,NORM_2,norm,ierr)
138: v = abs(norm-8.0*sqrt(nfloat))
139: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
140: if (rank == 0) write(6,150) v
141: 150 format ('VecAXPY ',1pe9.2)
143: call VecAYPX(y,two,x,ierr)
144: call VecNorm(y,NORM_2,norm,ierr)
145: v = abs(norm-18.0*sqrt(nfloat))
146: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
147: if (rank == 0) write(6,160) v
148: 160 format ('VecAYXP ',1pe9.2)
150: call VecSwap(x,y,ierr)
151: call VecNorm(y,NORM_2,norm,ierr)
152: v = abs(norm-2.0*sqrt(nfloat))
153: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
154: if (rank == 0) write(6,170) v
155: 170 format ('VecSwap ',1pe9.2)
157: call VecNorm(x,NORM_2,norm,ierr)
158: v = abs(norm-18.0*sqrt(nfloat))
159: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
160: if (rank == 0) write(6,180) v
161: 180 format ('VecSwap ',1pe9.2)
163: call VecWAXPY(w,two,x,y,ierr)
164: call VecNorm(w,NORM_2,norm,ierr)
165: v = abs(norm-38.0*sqrt(nfloat))
166: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
167: if (rank == 0) write(6,190) v
168: 190 format ('VecWAXPY ',1pe9.2)
170: call VecPointwiseMult(w,y,x,ierr)
171: call VecNorm(w,NORM_2,norm,ierr)
172: v = abs(norm-36.0*sqrt(nfloat))
173: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
174: if (rank == 0) write(6,200) v
175: 200 format ('VecPointwiseMult ',1pe9.2)
177: call VecPointwiseDivide(w,x,y,ierr)
178: call VecNorm(w,NORM_2,norm,ierr)
179: v = abs(norm-9.0*sqrt(nfloat))
180: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
181: if (rank == 0) write(6,210) v
182: 210 format ('VecPointwiseDivide ',1pe9.2)
185: dots(1) = one
186: dots(2) = three
187: dots(3) = two
188: call VecSet(x,one,ierr)
189: call VecMAXPY(x,ithree,dots,z,ierr)
190: call VecNorm(z(1),NORM_2,norm,ierr)
191: v = abs(norm-sqrt(nfloat))
192: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
193: call VecNorm(z(2),NORM_2,norm,ierr)
194: v1 = abs(norm-2.0*sqrt(nfloat))
195: if (v1 .gt. -PETSC_SMALL .and. v1 .lt. PETSC_SMALL) v1 = 0.0
196: call VecNorm(z(3),NORM_2,norm,ierr)
197: v2 = abs(norm-3.0*sqrt(nfloat))
198: if (v2 .gt. -PETSC_SMALL .and. v2 .lt. PETSC_SMALL) v2 = 0.0
199: if (rank .eq. 0) write(6,220) v,v1,v2
200: 220 format ('VecMAXPY ',3(2x,1pe9.2))
202: ! Free work space. All PETSc objects should be destroyed when they
203: ! are no longer needed.
205: call VecDestroy(x,ierr)
206: call VecDestroy(y,ierr)
207: call VecDestroy(w,ierr)
208: call VecDestroyVecs(ithree,z,ierr)
209: call PetscFinalize(ierr)
211: end
213: !
214: !/*TEST
215: !
216: ! test:
217: ! suffix: 1
218: !
219: ! test:
220: ! suffix: 2
221: ! nsize: 2
222: !
223: !TEST*/