Actual source code: ex1f.F
1: !
2: ! "$Id: ex1f.F,v 1.33 2001/08/10 18:41:04 balay Exp $"
3: !
4: ! Program usage: mpirun ex1f [-help] [all PETSc options]
5: !
6: !/*T
7: ! Concepts: vectors^basic routines
8: ! Processors: n
9: !T*/
10: !
11: ! -----------------------------------------------------------------------
13: program main
14: implicit none
16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17: ! Include files
18: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
19: !
20: ! The following include statements are required for Fortran programs
21: ! that use PETSc vectors:
22: ! petsc.h - base PETSc routines
23: ! petscvec.h - vectors
24: ! Additional include statements may be needed if using additional
25: ! PETSc routines in a Fortran program, e.g.,
26: ! petscviewer.h - viewers
27: ! petscis.h - index sets
28: !
29: #include include/finclude/petsc.h
30: #include include/finclude/petscvec.h
32: !
33: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: ! Variable declarations
35: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36: !
37: ! Variables:
38: ! x, y, w - vectors
39: ! z - array of vectors
41: Vec x,y,w,z(5)
42: PetscReal norm,v,v1,v2
43: integer n,ierr,flg,rank
44: PetscScalar one,two,three,dots(3),dot
46: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47: ! Beginning of program
48: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
51: one = 1.0
52: two = 2.0
53: three = 3.0
54: n = 20
55: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
56: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
58: ! Create a vector, specifying only its global dimension.
59: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
60: ! the vector format (currently parallel
61: ! or sequential) is determined at runtime. Also, the parallel
62: ! partitioning of the vector is determined by PETSc at runtime.
63: !
64: ! Routines for creating particular vector types directly are:
65: ! VecCreateSeq() - uniprocessor vector
66: ! VecCreateMPI() - distributed vector, where the user can
67: ! determine the parallel partitioning
68: ! VecCreateShared() - parallel vector that uses shared memory
69: ! (available only on the SGI); otherwise,
70: ! is the same as VecCreateMPI()
71: !
72: ! VecCreate(), VecSetSizes() and VecSetFromOptions() allows one
73: ! to determine at runtime which version to use
74: ! with the options -vec_type mpi or -vec_type shared
75: !
76: call VecCreate(PETSC_COMM_WORLD,x,ierr)
77: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
78: call VecSetFromOptions(x,ierr)
80: ! Duplicate some work vectors (of the same format and
81: ! partitioning as the initial vector).
83: call VecDuplicate(x,y,ierr)
84: call VecDuplicate(x,w,ierr)
86: ! Duplicate more work vectors (of the same format and
87: ! partitioning as the initial vector). Here we duplicate
88: ! an array of vectors, which is often more convenient than
89: ! duplicating individual ones.
91: call VecDuplicateVecs(x,3,z,ierr)
93: ! Set the vectors to entries to a constant value.
95: call VecSet(one,x,ierr)
96: call VecSet(two,y,ierr)
97: call VecSet(one,z(1),ierr)
98: call VecSet(two,z(2),ierr)
99: call VecSet(three,z(3),ierr)
101: ! Demonstrate various basic vector routines.
103: call VecDot(x,x,dot,ierr)
104: call VecMDot(3,x,z,dots,ierr)
106: ! Note: If using a complex numbers version of PETSc, then
107: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
108: ! (when using real numbers) it is undefined.
110: if (rank .eq. 0) then
111: #if defined(PETSC_USE_COMPLEX)
112: write(6,100) int(PetscRealPart(dot))
113: write(6,110) int(PetscRealPart(dots(1))), &
114: & int(PetscRealPart(dots(2))), &
115: & int(PetscRealPart(dots(3)))
116: #else
117: write(6,100) int(dot)
118: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
119: #endif
120: write(6,120)
121: endif
122: 100 format ("Vector length ",i6)
123: 110 format ("Vector length ",3(i6))
124: 120 format ("All other values should be near zero")
126: call VecScale(two,x,ierr)
127: call VecNorm(x,NORM_2,norm,ierr)
128: v = norm-2.0*sqrt(dble(n))
129: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
130: if (rank .eq. 0) write(6,130) v
131: 130 format ("VecScale ",1pe8.2)
133: call VecCopy(x,w,ierr)
134: call VecNorm(w,NORM_2,norm,ierr)
135: v = norm-2.0*sqrt(dble(n))
136: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
137: if (rank .eq. 0) write(6,140) v
138: 140 format ("VecCopy ",1pe8.2)
140: call VecAXPY(three,x,y,ierr)
141: call VecNorm(y,NORM_2,norm,ierr)
142: v = norm-8.0*sqrt(dble(n))
143: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
144: if (rank .eq. 0) write(6,150) v
145: 150 format ("VecAXPY ",1pe8.2)
147: call VecAYPX(two,x,y,ierr)
148: call VecNorm(y,NORM_2,norm,ierr)
149: v = norm-18.0*sqrt(dble(n))
150: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
151: if (rank .eq. 0) write(6,160) v
152: 160 format ("VecAYXP ",1pe8.2)
154: call VecSwap(x,y,ierr)
155: call VecNorm(y,NORM_2,norm,ierr)
156: v = norm-2.0*sqrt(dble(n))
157: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
158: if (rank .eq. 0) write(6,170) v
159: 170 format ("VecSwap ",1pe8.2)
161: call VecNorm(x,NORM_2,norm,ierr)
162: v = norm-18.0*sqrt(dble(n))
163: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
164: if (rank .eq. 0) write(6,180) v
165: 180 format ("VecSwap ",1pe8.2)
167: call VecWAXPY(two,x,y,w,ierr)
168: call VecNorm(w,NORM_2,norm,ierr)
169: v = norm-38.0*sqrt(dble(n))
170: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
171: if (rank .eq. 0) write(6,190) v
172: 190 format ("VecWAXPY ",1pe8.2)
174: call VecPointwiseMult(y,x,w,ierr)
175: call VecNorm(w,NORM_2,norm,ierr)
176: v = norm-36.0*sqrt(dble(n))
177: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
178: if (rank .eq. 0) write(6,200) v
179: 200 format ("VecPointwiseMult ",1pe8.2)
181: call VecPointwiseDivide(x,y,w,ierr)
182: call VecNorm(w,NORM_2,norm,ierr)
183: v = norm-9.0*sqrt(dble(n))
184: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
185: if (rank .eq. 0) write(6,210) v
186: 210 format ("VecPointwiseDivide ",1pe8.2)
188:
189: dots(1) = one
190: dots(2) = three
191: dots(3) = two
192: call VecSet(one,x,ierr)
193: call VecMAXPY(3,dots,x,z,ierr)
194: call VecNorm(z(1),NORM_2,norm,ierr)
195: v = norm-sqrt(dble(n))
196: if (v .gt. -PETSC_SMALL .and. v .lt. PETSC_SMALL) v = 0.0
197: call VecNorm(z(2),NORM_2,norm,ierr)
198: v1 = norm-2.0*sqrt(dble(n))
199: if (v1 .gt. -PETSC_SMALL .and. v1 .lt. PETSC_SMALL) v1 = 0.0
200: call VecNorm(z(3),NORM_2,norm,ierr)
201: v2 = norm-3.0*sqrt(dble(n))
202: if (v2 .gt. -PETSC_SMALL .and. v2 .lt. PETSC_SMALL) v2 = 0.0
203: if (rank .eq. 0) write(6,220) v,v1,v2
204: 220 format ("VecMAXPY ",3(1pe8.2))
207: ! Test whether vector has been corrupted (just to demonstrate this
208: ! routine) not needed in most application codes.
210: call VecValid(x,flg,ierr)
211: if (flg .ne. PETSC_TRUE) then
212: if (rank .eq. 0) then
213: write(6,*) 'Corrupted vector!'
214: endif
215: SETERRQ(1,' ',ierr)
216: endif
218: ! Free work space. All PETSc objects should be destroyed when they
219: ! are no longer needed.
221: call VecDestroy(x,ierr)
222: call VecDestroy(y,ierr)
223: call VecDestroy(w,ierr)
224: call VecDestroyVecs(z,3,ierr)
225: call PetscFinalize(ierr)
227: end
228: