Actual source code: ex11f.F
1: !
2: ! Description: Solves a complex linear system in parallel with KSP (Fortran code).
3: !
4: !/*T
5: ! Concepts: KSP^solving a Helmholtz equation
6: ! Concepts: complex numbers
7: ! Processors: n
8: !T*/
9: !
10: ! The model problem:
11: ! Solve Helmholtz equation on the unit square: (0,1) x (0,1)
12: ! -delta u - sigma1*u + i*sigma2*u = f,
13: ! where delta = Laplace operator
14: ! Dirichlet b.c.'s on all sides
15: ! Use the 2-D, five-point finite difference stencil.
16: !
17: ! Compiling the code:
18: ! This code uses the complex numbers version of PETSc, so configure
19: ! must be run to enable this
20: !
21: !
22: ! -----------------------------------------------------------------------
24: program main
25: implicit none
27: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28: ! Include files
29: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30: !
31: ! The following include statements are required for KSP Fortran programs:
32: ! petsc.h - base PETSc routines
33: ! petscvec.h - vectors
34: ! petscmat.h - matrices
35: ! petscpc.h - preconditioners
36: ! petscksp.h - Krylov subspace methods
37: ! Include the following to use PETSc random numbers:
38: ! petscsys.h - system routines
39: ! Additional include statements may be needed if using other PETSc
40: ! routines in a Fortran program, e.g.,
41: ! petscviewer.h - viewers
42: ! petscis.h - index sets
43: !
44: #include include/finclude/petsc.h
45: #include include/finclude/petscvec.h
46: #include include/finclude/petscmat.h
47: #include include/finclude/petscpc.h
48: #include include/finclude/petscksp.h
49: #include include/finclude/petscsys.h
50: !
51: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: ! Variable declarations
53: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54: !
55: ! Variables:
56: ! ksp - linear solver context
57: ! x, b, u - approx solution, right-hand-side, exact solution vectors
58: ! A - matrix that defines linear system
59: ! its - iterations for convergence
60: ! norm - norm of error in solution
61: ! rctx - random number context
62: !
64: KSP ksp
65: Mat A
66: Vec x,b,u
67: PetscRandom rctx
68: double precision norm,h2,sigma1
69: PetscScalar none,sigma2,v,pfive
70: PetscInt dim,its,n,Istart
71: PetscInt Iend,i,j,II,JJ,one
72: PetscErrorCode ierr
73: PetscMPIInt rank
74: PetscTruth flg
75: logical use_random
77: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78: ! Beginning of program
79: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
82: #if !defined(PETSC_USE_COMPLEX)
83: write(6,*) "This example requires complex numbers."
84: goto 200
85: #endif
87: none = -1.0
88: n = 6
89: sigma1 = 100.0
90: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
91: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-sigma1',sigma1, &
92: & flg,ierr)
93: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
94: dim = n*n
96: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97: ! Compute the matrix and right-hand-side vector that define
98: ! the linear system, Ax = b.
99: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101: ! Create parallel matrix, specifying only its global dimensions.
102: ! When using MatCreate(), the matrix format can be specified at
103: ! runtime. Also, the parallel partitioning of the matrix is
104: ! determined by PETSc at runtime.
106: call MatCreate(PETSC_COMM_WORLD,A,ierr)
107: call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,dim,dim,ierr)
108: call MatSetFromOptions(A,ierr)
110: ! Currently, all PETSc parallel matrix formats are partitioned by
111: ! contiguous chunks of rows across the processors. Determine which
112: ! rows of the matrix are locally owned.
114: call MatGetOwnershipRange(A,Istart,Iend,ierr)
116: ! Set matrix elements in parallel.
117: ! - Each processor needs to insert only elements that it owns
118: ! locally (but any non-local elements will be sent to the
119: ! appropriate processor during matrix assembly).
120: ! - Always specify global rows and columns of matrix entries.
122: call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-norandom', &
123: & flg,ierr)
124: if (flg .eq. 1) then
125: use_random = .false.
126: sigma2 = 10.0*PETSC_i
127: else
128: use_random = .true.
129: call PetscRandomCreate(PETSC_COMM_WORLD, &
130: & RANDOM_DEFAULT_IMAGINARY,rctx,ierr)
131: endif
132: h2 = 1.0/((n+1)*(n+1))
134: one = 1
135: do 10, II=Istart,Iend-1
136: v = -1.0
137: i = II/n
138: j = II - i*n
139: if (i.gt.0) then
140: JJ = II - n
141: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
142: endif
143: if (i.lt.n-1) then
144: JJ = II + n
145: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
146: endif
147: if (j.gt.0) then
148: JJ = II - 1
149: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
150: endif
151: if (j.lt.n-1) then
152: JJ = II + 1
153: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
154: endif
155: if (use_random) call PetscRandomGetValue(rctx,sigma2,ierr)
156: v = 4.0 - sigma1*h2 + sigma2*h2
157: call MatSetValues(A,one,II,one,II,v,ADD_VALUES,ierr)
158: 10 continue
159: if (use_random) call PetscRandomDestroy(rctx,ierr)
161: ! Assemble matrix, using the 2-step process:
162: ! MatAssemblyBegin(), MatAssemblyEnd()
163: ! Computations can be done while messages are in transition
164: ! by placing code between these two statements.
166: call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
167: call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
169: ! Create parallel vectors.
170: ! - Here, the parallel partitioning of the vector is determined by
171: ! PETSc at runtime. We could also specify the local dimensions
172: ! if desired.
173: ! - Note: We form 1 vector from scratch and then duplicate as needed.
175: call VecCreate(PETSC_COMM_WORLD,u,ierr)
176: call VecSetSizes(u,PETSC_DECIDE,dim,ierr)
177: call VecSetFromOptions(u,ierr)
178: call VecDuplicate(u,b,ierr)
179: call VecDuplicate(b,x,ierr)
181: ! Set exact solution; then compute right-hand-side vector.
183: if (use_random) then
184: call PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT, &
185: & rctx,ierr)
186: call VecSetRandom(rctx,u,ierr)
187: else
188: pfive = 0.5
189: call VecSet(u,pfive,ierr)
190: endif
191: call MatMult(A,u,b,ierr)
193: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194: ! Create the linear solver and set various options
195: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
197: ! Create linear solver context
199: call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)
201: ! Set operators. Here the matrix that defines the linear system
202: ! also serves as the preconditioning matrix.
204: call KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN,ierr)
206: ! Set runtime options, e.g.,
207: ! -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
209: call KSPSetFromOptions(ksp,ierr)
211: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
212: ! Solve the linear system
213: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
215: call KSPSolve(ksp,b,x,ierr)
217: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218: ! Check solution and clean up
219: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
221: ! Check the error
223: call VecAXPY(x,none,u,ierr)
224: call VecNorm(x,NORM_2,norm,ierr)
225: call KSPGetIterationNumber(ksp,its,ierr)
226: if (rank .eq. 0) then
227: if (norm .gt. 1.e-12) then
228: write(6,100) norm,its
229: else
230: write(6,110) its
231: endif
232: endif
233: 100 format('Norm of error ',e10.4,',iterations ',i5)
234: 110 format('Norm of error < 1.e-12,iterations ',i5)
236: ! Free work space. All PETSc objects should be destroyed when they
237: ! are no longer needed.
239: if (use_random) call PetscRandomDestroy(rctx,ierr)
240: call KSPDestroy(ksp,ierr)
241: call VecDestroy(u,ierr)
242: call VecDestroy(x,ierr)
243: call VecDestroy(b,ierr)
244: call MatDestroy(A,ierr)
246: 200 continue
247: call PetscFinalize(ierr)
248: end