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,Iend,i,j,II,JJ,one
71: PetscErrorCode ierr
72: PetscMPIInt rank
73: PetscTruth flg
74: logical use_random
76: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77: ! Beginning of program
78: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
81: #if !defined(PETSC_USE_COMPLEX)
82: write(6,*) "This example requires complex numbers."
83: goto 200
84: #endif
86: none = -1.0
87: n = 6
88: sigma1 = 100.0
89: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
90: call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-sigma1',sigma1, &
91: & flg,ierr)
92: call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
93: dim = n*n
95: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96: ! Compute the matrix and right-hand-side vector that define
97: ! the linear system, Ax = b.
98: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100: ! Create parallel matrix, specifying only its global dimensions.
101: ! When using MatCreate(), the matrix format can be specified at
102: ! runtime. Also, the parallel partitioning of the matrix is
103: ! determined by PETSc at runtime.
105: call MatCreate(PETSC_COMM_WORLD,A,ierr)
106: call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,dim,dim,ierr)
107: call MatSetFromOptions(A,ierr)
109: ! Currently, all PETSc parallel matrix formats are partitioned by
110: ! contiguous chunks of rows across the processors. Determine which
111: ! rows of the matrix are locally owned.
113: call MatGetOwnershipRange(A,Istart,Iend,ierr)
115: ! Set matrix elements in parallel.
116: ! - Each processor needs to insert only elements that it owns
117: ! locally (but any non-local elements will be sent to the
118: ! appropriate processor during matrix assembly).
119: ! - Always specify global rows and columns of matrix entries.
121: call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-norandom', &
122: & flg,ierr)
123: if (flg .eq. 1) then
124: use_random = .false.
125: sigma2 = 10.0*PETSC_i
126: else
127: use_random = .true.
128: call PetscRandomCreate(PETSC_COMM_WORLD, &
129: & RANDOM_DEFAULT_IMAGINARY,rctx,ierr)
130: endif
131: h2 = 1.0/((n+1)*(n+1))
133: one = 1
134: do 10, II=Istart,Iend-1
135: v = -1.0
136: i = II/n
137: j = II - i*n
138: if (i.gt.0) then
139: JJ = II - n
140: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
141: endif
142: if (i.lt.n-1) then
143: JJ = II + n
144: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
145: endif
146: if (j.gt.0) then
147: JJ = II - 1
148: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
149: endif
150: if (j.lt.n-1) then
151: JJ = II + 1
152: call MatSetValues(A,one,II,one,JJ,v,ADD_VALUES,ierr)
153: endif
154: if (use_random) call PetscRandomGetValue(rctx,sigma2,ierr)
155: v = 4.0 - sigma1*h2 + sigma2*h2
156: call MatSetValues(A,one,II,one,II,v,ADD_VALUES,ierr)
157: 10 continue
158: if (use_random) call PetscRandomDestroy(rctx,ierr)
160: ! Assemble matrix, using the 2-step process:
161: ! MatAssemblyBegin(), MatAssemblyEnd()
162: ! Computations can be done while messages are in transition
163: ! by placing code between these two statements.
165: call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
166: call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
168: ! Create parallel vectors.
169: ! - Here, the parallel partitioning of the vector is determined by
170: ! PETSc at runtime. We could also specify the local dimensions
171: ! if desired.
172: ! - Note: We form 1 vector from scratch and then duplicate as needed.
174: call VecCreate(PETSC_COMM_WORLD,u,ierr)
175: call VecSetSizes(u,PETSC_DECIDE,dim,ierr)
176: call VecSetFromOptions(u,ierr)
177: call VecDuplicate(u,b,ierr)
178: call VecDuplicate(b,x,ierr)
180: ! Set exact solution; then compute right-hand-side vector.
182: if (use_random) then
183: call PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT, &
184: & rctx,ierr)
185: call VecSetRandom(rctx,u,ierr)
186: else
187: pfive = 0.5
188: call VecSet(u,pfive,ierr)
189: endif
190: call MatMult(A,u,b,ierr)
192: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
193: ! Create the linear solver and set various options
194: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196: ! Create linear solver context
198: call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)
200: ! Set operators. Here the matrix that defines the linear system
201: ! also serves as the preconditioning matrix.
203: call KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN,ierr)
205: ! Set runtime options, e.g.,
206: ! -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
208: call KSPSetFromOptions(ksp,ierr)
210: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
211: ! Solve the linear system
212: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
214: call KSPSolve(ksp,b,x,ierr)
216: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217: ! Check solution and clean up
218: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
220: ! Check the error
222: call VecAXPY(x,none,u,ierr)
223: call VecNorm(x,NORM_2,norm,ierr)
224: call KSPGetIterationNumber(ksp,its,ierr)
225: if (rank .eq. 0) then
226: if (norm .gt. 1.e-12) then
227: write(6,100) norm,its
228: else
229: write(6,110) its
230: endif
231: endif
232: 100 format('Norm of error ',e10.4,',iterations ',i5)
233: 110 format('Norm of error < 1.e-12,iterations ',i5)
235: ! Free work space. All PETSc objects should be destroyed when they
236: ! are no longer needed.
238: if (use_random) call PetscRandomDestroy(rctx,ierr)
239: call KSPDestroy(ksp,ierr)
240: call VecDestroy(u,ierr)
241: call VecDestroy(x,ierr)
242: call VecDestroy(b,ierr)
243: call MatDestroy(A,ierr)
245: 200 continue
246: call PetscFinalize(ierr)
247: end