Actual source code: ex1f90.F
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
12: implicit none
14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15: ! Include files
16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17: !
18: ! The following include statements are required for Fortran programs
19: ! that use PETSc vectors:
20: ! petsc.h - base PETSc routines
21: ! petscvec.h - vectors
22: ! petscvec.h90 - to allow access to Fortran90 features of vectors
23: !
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
31: #include "include/finclude/petscvec.h90"
32: !
33: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34: ! Variable declarations
35: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36: !
37: ! Variables:
38: ! x, y, w - vectors
39: ! z - array of vectors
40: !
41: Vec x,y,w
42: Vec, pointer :: z(:)
43: double precision norm,v,v1,v2
44: PetscInt n,ithree
45: PetscErrorCode ierr
46: PetscMPIInt rank
47: PetscTruth flg
48: PetscScalar one,two,three
49: PetscScalar dots(3),dot
51: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: ! Beginning of program
53: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
56: one = 1.0
57: two = 2.0
58: three = 3.0
59: n = 20
60: ithree = 3
61: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
62: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
64: ! Create a vector, specifying only its global dimension.
65: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
66: ! the vector format (currently parallel
67: ! or sequential) is determined at runtime. Also, the parallel
68: ! partitioning of the vector is determined by PETSc at runtime.
69: !
70: ! Routines for creating particular vector types directly are:
71: ! VecCreateSeq() - uniprocessor vector
72: ! VecCreateMPI() - distributed vector, where the user can
73: ! determine the parallel partitioning
75: call VecCreate(PETSC_COMM_WORLD,x,ierr)
76: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
77: call VecSetFromOptions(x,ierr)
79: ! Duplicate some work vectors (of the same format and
80: ! partitioning as the initial vector).
82: call VecDuplicate(x,y,ierr)
83: call VecDuplicate(x,w,ierr)
85: ! Duplicate more work vectors (of the same format and
86: ! partitioning as the initial vector). Here we duplicate
87: ! an array of vectors, which is often more convenient than
88: ! duplicating individual ones.
90: call VecDuplicateVecsF90(x,ithree,z,ierr)
92: ! Set the vectors to entries to a constant value.
94: call VecSet(x,one,ierr)
95: call VecSet(y,two,ierr)
96: call VecSet(z(1),one,ierr)
97: call VecSet(z(2),two,ierr)
98: call VecSet(z(3),three,ierr)
100: ! Demonstrate various basic vector routines.
102: call VecDot(x,x,dot,ierr)
103: call VecMDot(x,ithree,z,dots,ierr)
105: ! Note: If using a complex numbers version of PETSc, then
106: ! PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
107: ! (when using real numbers) it is undefined.
109: if (rank .eq. 0) then
110: #if defined(PETSC_USE_COMPLEX)
111: write(6,100) int(PetscRealPart(dot))
112: write(6,110) int(PetscRealPart(dots(1))), &
113: & int(PetscRealPart(dots(2))), &
114: & int(PetscRealPart(dots(3)))
115: #else
116: write(6,100) int(dot)
117: write(6,110) int(dots(1)),int(dots(2)),int(dots(3))
118: #endif
119: write(6,120)
120: endif
121: 100 format ("Vector length ",i6)
122: 110 format ("Vector length ",3(i6))
123: 120 format ("All other values should be near zero")
125: call VecScale(x,two,ierr)
126: call VecNorm(x,NORM_2,norm,ierr)
127: v = norm-2.0*sqrt(dble(n))
128: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
129: if (rank .eq. 0) write(6,130) v
130: 130 format ("VecScale ",1pe8.2)
132: call VecCopy(x,w,ierr)
133: call VecNorm(w,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,140) v
137: 140 format ("VecCopy ",1pe8.2)
139: call VecAXPY(y,three,x,ierr)
140: call VecNorm(y,NORM_2,norm,ierr)
141: v = norm-8.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,150) v
144: 150 format ("VecAXPY ",1pe8.2)
146: call VecAYPX(y,two,x,ierr)
147: call VecNorm(y,NORM_2,norm,ierr)
148: v = norm-18.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,160) v
151: 160 format ("VecAYXP ",1pe8.2)
153: call VecSwap(x,y,ierr)
154: call VecNorm(y,NORM_2,norm,ierr)
155: v = norm-2.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,170) v
158: 170 format ("VecSwap ",1pe8.2)
160: call VecNorm(x,NORM_2,norm,ierr)
161: v = norm-18.0*sqrt(dble(n))
162: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
163: if (rank .eq. 0) write(6,180) v
164: 180 format ("VecSwap ",1pe8.2)
166: call VecWAXPY(w,two,x,y,ierr)
167: call VecNorm(w,NORM_2,norm,ierr)
168: v = norm-38.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,190) v
171: 190 format ("VecWAXPY ",1pe8.2)
173: call VecPointwiseMult(w,y,x,ierr)
174: call VecNorm(w,NORM_2,norm,ierr)
175: v = norm-36.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,200) v
178: 200 format ("VecPointwiseMult ",1pe8.2)
180: call VecPointwiseDivide(w,x,y,ierr)
181: call VecNorm(w,NORM_2,norm,ierr)
182: v = norm-9.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,210) v
185: 210 format ("VecPointwiseDivide ",1pe8.2)
187:
188: dots(1) = one
189: dots(2) = three
190: dots(3) = two
191: call VecSet(x,one,ierr)
192: call VecMAXPY(x,ithree,dots,z,ierr)
193: call VecNorm(z(1),NORM_2,norm,ierr)
194: v = norm-sqrt(dble(n))
195: if (v .gt. -1.d-10 .and. v .lt. 1.d-10) v = 0.0
196: call VecNorm(z(2),NORM_2,norm,ierr)
197: v1 = norm-2.0*sqrt(dble(n))
198: if (v1 .gt. -1.d-10 .and. v1 .lt. 1.d-10) v1 = 0.0
199: call VecNorm(z(3),NORM_2,norm,ierr)
200: v2 = norm-3.0*sqrt(dble(n))
201: if (v2 .gt. -1.d-10 .and. v2 .lt. 1.d-10) v2 = 0.0
202: if (rank .eq. 0) write(6,220) v,v1,v2
203: 220 format ("VecMAXPY ",3(1pe8.2))
206: ! Test whether vector has been corrupted (just to demonstrate this
207: ! routine) not needed in most application codes.
209: call VecValid(x,flg,ierr)
210: if (flg .ne. PETSC_TRUE) then
211: if (rank .eq. 0) then
212: write(6,*) 'Corrupted vector!'
213: endif
214: SETERRQ(1,' ',ierr)
215: endif
217: ! Free work space. All PETSc objects should be destroyed when they
218: ! are no longer needed.
220: call VecDestroy(x,ierr)
221: call VecDestroy(y,ierr)
222: call VecDestroy(w,ierr)
223: call VecDestroyVecsF90(z,ithree,ierr)
224: call PetscFinalize(ierr)
226: end
227: