Actual source code: ex62f.F90
1: !
2: ! Solves a linear system in parallel with KSP. Also indicates
3: ! use of a user-provided preconditioner. Input parameters include:
4: !
5: !
7: !
8: ! -------------------------------------------------------------------------
9: #include <petsc/finclude/petscksp.h>
10: module ex62fmodule
11: use petscksp
12: implicit none
13: PC jacobi, sor
14: Vec work
16: contains
17: !/***********************************************************************/
18: !/* Routines for a user-defined shell preconditioner */
19: !/***********************************************************************/
21: !
22: ! SampleShellPCSetUp - This routine sets up a user-defined
23: ! preconditioner context.
24: !
25: ! Input Parameters:
26: ! pc - preconditioner object
27: ! x - vector
28: !
29: ! Output Parameter:
30: ! ierr - error code (nonzero if error has been detected)
31: !
32: ! Notes:
33: ! In this example, we define the shell preconditioner to be Jacobi
34: ! method. Thus, here we create a work vector for storing the reciprocal
35: ! of the diagonal of the matrix used to compute the preconditioner; this vector is then
36: ! used within the routine SampleShellPCApply().
37: !
38: subroutine SampleShellPCSetUp(pc, x, ierr)
40: PC pc
41: Vec x
42: Mat pmat
43: PetscErrorCode ierr
45: PetscCallA(PCGetOperators(pc, PETSC_NULL_MAT, pmat, ierr))
46: PetscCallA(PCCreate(PETSC_COMM_WORLD, jacobi, ierr))
47: PetscCallA(PCSetType(jacobi, PCJACOBI, ierr))
48: PetscCallA(PCSetOperators(jacobi, pmat, pmat, ierr))
49: PetscCallA(PCSetUp(jacobi, ierr))
51: PetscCallA(PCCreate(PETSC_COMM_WORLD, sor, ierr))
52: PetscCallA(PCSetType(sor, PCSOR, ierr))
53: PetscCallA(PCSetOperators(sor, pmat, pmat, ierr))
54: ! PetscCallA(PCSORSetSymmetric(sor,SOR_LOCAL_SYMMETRIC_SWEEP,ierr))
55: PetscCallA(PCSetUp(sor, ierr))
57: PetscCallA(VecDuplicate(x, work, ierr))
58: end
60: ! -------------------------------------------------------------------
61: !
62: ! SampleShellPCApply - This routine demonstrates the use of a
63: ! user-provided preconditioner.
64: !
65: ! Input Parameters:
66: ! pc - preconditioner object
67: ! x - input vector
68: !
69: ! Output Parameters:
70: ! y - preconditioned vector
71: ! ierr - error code (nonzero if error has been detected)
72: !
73: ! Notes:
74: ! This code implements the Jacobi preconditioner plus the
75: ! SOR preconditioner
76: !
77: ! YOU CAN GET THE EXACT SAME EFFECT WITH THE PCCOMPOSITE preconditioner using
78: ! mpiexec -n 1 ex21f -ksp_monitor -pc_type composite -pc_composite_pcs jacobi,sor -pc_composite_type additive
79: !
80: subroutine SampleShellPCApply(pc, x, y, ierr)
82: PC pc
83: Vec x, y
84: PetscErrorCode ierr
85: PetscScalar one
87: one = 1.0
88: PetscCallA(PCApply(jacobi, x, y, ierr))
89: PetscCallA(PCApply(sor, x, work, ierr))
90: PetscCallA(VecAXPY(y, one, work, ierr))
91: end
93: end module
95: program main
96: use ex62fmodule
97: implicit none
99: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
100: ! Variable declarations
101: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102: !
103: ! Variables:
104: ! ksp - linear solver context
105: ! ksp - Krylov subspace method context
106: ! pc - preconditioner context
107: ! x, b, u - approx solution, right-hand side, exact solution vectors
108: ! A - matrix that defines linear system
109: ! its - iterations for convergence
110: ! norm - norm of solution error
112: Vec x, b, u
113: Mat A
114: PC pc
115: KSP ksp
116: PetscScalar v, one, neg_one
117: PetscReal norm, tol
118: PetscInt i, j, II, JJ, Istart
119: PetscInt Iend, m, n, its, ione
120: PetscMPIInt rank
121: PetscBool flg
122: PetscErrorCode ierr
124: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125: ! Beginning of program
126: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128: PetscCallA(PetscInitialize(ierr))
129: one = 1.0
130: neg_one = -1.0
131: m = 8
132: n = 7
133: ione = 1
134: PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-m', m, flg, ierr))
135: PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-n', n, flg, ierr))
136: PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr))
138: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139: ! Compute the matrix and right-hand-side vector that define
140: ! the linear system, Ax = b.
141: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143: ! Create parallel matrix, specifying only its global dimensions.
144: ! When using MatCreate(), the matrix format can be specified at
145: ! runtime. Also, the parallel partitioning of the matrix is
146: ! determined by PETSc at runtime.
148: PetscCallA(MatCreate(PETSC_COMM_WORLD, A, ierr))
149: PetscCallA(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m*n, m*n, ierr))
150: PetscCallA(MatSetFromOptions(A, ierr))
151: PetscCallA(MatSetUp(A, ierr))
153: ! Currently, all PETSc parallel matrix formats are partitioned by
154: ! contiguous chunks of rows across the processors. Determine which
155: ! rows of the matrix are locally owned.
157: PetscCallA(MatGetOwnershipRange(A, Istart, Iend, ierr))
159: ! Set matrix elements for the 2-D, five-point stencil in parallel.
160: ! - Each processor needs to insert only elements that it owns
161: ! locally (but any non-local elements will be sent to the
162: ! appropriate processor during matrix assembly).
163: ! - Always specify global row and columns of matrix entries.
164: ! - Note that MatSetValues() uses 0-based row and column numbers
165: ! in Fortran as well as in C.
167: do II = Istart, Iend - 1
168: v = -1.0
169: i = II/n
170: j = II - i*n
171: if (i > 0) then
172: JJ = II - n
173: PetscCallA(MatSetValues(A, ione, [II], ione, [JJ], [v], ADD_VALUES, ierr))
174: end if
175: if (i < m - 1) then
176: JJ = II + n
177: PetscCallA(MatSetValues(A, ione, [II], ione, [JJ], [v], ADD_VALUES, ierr))
178: end if
179: if (j > 0) then
180: JJ = II - 1
181: PetscCallA(MatSetValues(A, ione, [II], ione, [JJ], [v], ADD_VALUES, ierr))
182: end if
183: if (j < n - 1) then
184: JJ = II + 1
185: PetscCallA(MatSetValues(A, ione, [II], ione, [JJ], [v], ADD_VALUES, ierr))
186: end if
187: v = 4.0
188: PetscCallA(MatSetValues(A, ione, [II], ione, [II], [v], ADD_VALUES, ierr))
189: end do
191: ! Assemble matrix, using the 2-step process:
192: ! MatAssemblyBegin(), MatAssemblyEnd()
193: ! Computations can be done while messages are in transition,
194: ! by placing code between these two statements.
196: PetscCallA(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY, ierr))
197: PetscCallA(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY, ierr))
199: ! Create parallel vectors.
200: ! - Here, the parallel partitioning of the vector is determined by
201: ! PETSc at runtime. We could also specify the local dimensions
202: ! if desired -- or use the more general routine VecCreate().
203: ! - When solving a linear system, the vectors and matrices MUST
204: ! be partitioned accordingly. PETSc automatically generates
205: ! appropriately partitioned matrices and vectors when MatCreate()
206: ! and VecCreate() are used with the same communicator.
207: ! - Note: We form 1 vector from scratch and then duplicate as needed.
209: PetscCallA(VecCreateFromOptions(PETSC_COMM_WORLD, PETSC_NULL_CHARACTER, ione, PETSC_DECIDE, m*n, u, ierr))
210: PetscCallA(VecDuplicate(u, b, ierr))
211: PetscCallA(VecDuplicate(b, x, ierr))
213: ! Set exact solution; then compute right-hand-side vector.
215: PetscCallA(VecSet(u, one, ierr))
216: PetscCallA(MatMult(A, u, b, ierr))
218: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219: ! Create the linear solver and set various options
220: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222: ! Create linear solver context
224: PetscCallA(KSPCreate(PETSC_COMM_WORLD, ksp, ierr))
226: ! Set operators. Here the matrix that defines the linear system
227: ! also serves as the matrix from which the preconditioner is constructed.
229: PetscCallA(KSPSetOperators(ksp, A, A, ierr))
231: ! Set linear solver defaults for this problem (optional).
232: ! - By extracting the KSP and PC contexts from the KSP context,
233: ! we can then directly call any KSP and PC routines
234: ! to set various options.
236: PetscCallA(KSPGetPC(ksp, pc, ierr))
237: tol = 1.e-7
238: PetscCallA(KSPSetTolerances(ksp, tol, PETSC_CURRENT_REAL, PETSC_CURRENT_REAL, PETSC_CURRENT_INTEGER, ierr))
240: !
241: ! Set a user-defined shell preconditioner
242: !
244: ! (Required) Indicate to PETSc that we are using a shell preconditioner
245: PetscCallA(PCSetType(pc, PCSHELL, ierr))
247: ! (Required) Set the user-defined routine for applying the preconditioner
248: PetscCallA(PCShellSetApply(pc, SampleShellPCApply, ierr))
250: ! (Optional) Do any setup required for the preconditioner
251: ! Note: if you use PCShellSetSetUp, this will be done for your
252: PetscCallA(SampleShellPCSetUp(pc, x, ierr))
254: ! Set runtime options, e.g.,
255: ! -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
256: ! These options will override those specified above as long as
257: ! KSPSetFromOptions() is called _after_ any other customization
258: ! routines.
260: PetscCallA(KSPSetFromOptions(ksp, ierr))
262: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
263: ! Solve the linear system
264: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
266: PetscCallA(KSPSolve(ksp, b, x, ierr))
268: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
269: ! Check solution and clean up
270: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
272: ! Check the error
274: PetscCallA(VecAXPY(x, neg_one, u, ierr))
275: PetscCallA(VecNorm(x, NORM_2, norm, ierr))
276: PetscCallA(KSPGetIterationNumber(ksp, its, ierr))
278: if (rank == 0) then
279: if (norm > 1.e-12) then
280: write (6, 100) norm, its
281: else
282: write (6, 110) its
283: end if
284: end if
285: 100 format('Norm of error ', 1pe11.4, ' iterations ', i5)
286: 110 format('Norm of error < 1.e-12,iterations ', i5)
288: ! Free work space. All PETSc objects should be destroyed when they
289: ! are no longer needed.
291: PetscCallA(KSPDestroy(ksp, ierr))
292: PetscCallA(VecDestroy(u, ierr))
293: PetscCallA(VecDestroy(x, ierr))
294: PetscCallA(VecDestroy(b, ierr))
295: PetscCallA(MatDestroy(A, ierr))
297: ! Free up PCShell data
298: PetscCallA(PCDestroy(sor, ierr))
299: PetscCallA(PCDestroy(jacobi, ierr))
300: PetscCallA(VecDestroy(work, ierr))
302: ! Always call PetscFinalize() before exiting a program.
304: PetscCallA(PetscFinalize(ierr))
305: end
307: !/*TEST
308: !
309: ! test:
310: ! requires: !single
311: !
312: !TEST*/