Actual source code: ex16.c
1: /*$Id: ex16.c,v 1.23 2001/04/10 19:36:40 bsmith Exp $*/
3: /* Usage: mpirun ex16 [-help] [all PETSc options] */
5: static char help[] = "Solves a sequence of linear systems with different right-hand-side vectors.n
6: Input parameters include:n
7: -ntimes <ntimes> : number of linear systems to solven
8: -view_exact_sol : write exact solution vector to stdoutn
9: -m <mesh_x> : number of mesh points in x-directionn
10: -n <mesh_n> : number of mesh points in y-directionnn";
12: /*T
13: Concepts: SLES^repeatedly solving linear systems;
14: Concepts: SLES^Laplacian, 2d
15: Concepts: Laplacian, 2d
16: Processors: n
17: T*/
19: /*
20: Include "petscsles.h" so that we can use SLES solvers. Note that this file
21: automatically includes:
22: petsc.h - base PETSc routines petscvec.h - vectors
23: petscsys.h - system routines petscmat.h - matrices
24: petscis.h - index sets petscksp.h - Krylov subspace methods
25: petscviewer.h - viewers petscpc.h - preconditioners
26: */
27: #include "petscsles.h"
29: int main(int argc,char **args)
30: {
31: Vec x,b,u; /* approx solution, RHS, exact solution */
32: Mat A; /* linear system matrix */
33: SLES sles; /* linear solver context */
34: double norm; /* norm of solution error */
35: int ntimes,i,j,k,I,J,Istart,Iend,ierr;
36: int m = 8,n = 7,its;
37: PetscTruth flg;
38: Scalar v,one = 1.0,neg_one = -1.0,rhs;
40: PetscInitialize(&argc,&args,(char *)0,help);
41: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
42: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
44: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45: Compute the matrix for use in solving a series of
46: linear systems of the form, A x_i = b_i, for i=1,2,...
47: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48: /*
49: Create parallel matrix, specifying only its global dimensions.
50: When using MatCreate(), the matrix format can be specified at
51: runtime. Also, the parallel partitioning of the matrix is
52: determined by PETSc at runtime.
53: */
54: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&A);
55: MatSetFromOptions(A);
57: /*
58: Currently, all PETSc parallel matrix formats are partitioned by
59: contiguous chunks of rows across the processors. Determine which
60: rows of the matrix are locally owned.
61: */
62: MatGetOwnershipRange(A,&Istart,&Iend);
64: /*
65: Set matrix elements for the 2-D, five-point stencil in parallel.
66: - Each processor needs to insert only elements that it owns
67: locally (but any non-local elements will be sent to the
68: appropriate processor during matrix assembly).
69: - Always specify global rows and columns of matrix entries.
70: */
71: for (I=Istart; I<Iend; I++) {
72: v = -1.0; i = I/n; j = I - i*n;
73: if (i>0) {J = I - n; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
74: if (i<m-1) {J = I + n; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
75: if (j>0) {J = I - 1; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
76: if (j<n-1) {J = I + 1; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
77: v = 4.0; MatSetValues(A,1,&I,1,&I,&v,INSERT_VALUES);
78: }
80: /*
81: Assemble matrix, using the 2-step process:
82: MatAssemblyBegin(), MatAssemblyEnd()
83: Computations can be done while messages are in transition
84: by placing code between these two statements.
85: */
86: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
87: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
89: /*
90: Create parallel vectors.
91: - When using VecCreate() and VecSetFromOptions(), we specify only the vector's global
92: dimension; the parallel partitioning is determined at runtime.
93: - When solving a linear system, the vectors and matrices MUST
94: be partitioned accordingly. PETSc automatically generates
95: appropriately partitioned matrices and vectors when MatCreate()
96: and VecCreate() are used with the same communicator.
97: - Note: We form 1 vector from scratch and then duplicate as needed.
98: */
99: VecCreate(PETSC_COMM_WORLD,PETSC_DECIDE,m*n,&u);
100: VecSetFromOptions(u);
101: VecDuplicate(u,&b);
102: VecDuplicate(b,&x);
104: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105: Create the linear solver and set various options
106: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
108: /*
109: Create linear solver context
110: */
111: SLESCreate(PETSC_COMM_WORLD,&sles);
113: /*
114: Set operators. Here the matrix that defines the linear system
115: also serves as the preconditioning matrix.
116: */
117: SLESSetOperators(sles,A,A,SAME_PRECONDITIONER);
119: /*
120: Set runtime options, e.g.,
121: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
122: These options will override those specified above as long as
123: SLESSetFromOptions() is called _after_ any other customization
124: routines.
125: */
126: SLESSetFromOptions(sles);
128: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129: Solve several linear systems of the form A x_i = b_i
130: I.e., we retain the same matrix (A) for all systems, but
131: change the right-hand-side vector (b_i) at each step.
133: In this case, we simply call SLESSolve() multiple times. The
134: preconditioner setup operations (e.g., factorization for ILU)
135: be done during the first call to SLESSolve() only; such operations
136: will NOT be repeated for successive solves.
137: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
139: ntimes = 2;
140: PetscOptionsGetInt(PETSC_NULL,"-ntimes",&ntimes,PETSC_NULL);
141: for (k=1; k<ntimes+1; k++) {
143: /*
144: Set exact solution; then compute right-hand-side vector. We use
145: an exact solution of a vector with all elements equal to 1.0*k.
146: */
147: rhs = one * (double)k;
148: VecSet(&rhs,u);
149: MatMult(A,u,b);
151: /*
152: View the exact solution vector if desired
153: */
154: PetscOptionsHasName(PETSC_NULL,"-view_exact_sol",&flg);
155: if (flg) {VecView(u,PETSC_VIEWER_STDOUT_WORLD);}
157: SLESSolve(sles,b,x,&its);
159: /*
160: Check the error
161: */
162: VecAXPY(&neg_one,u,x);
163: VecNorm(x,NORM_2,&norm);
165: /*
166: Print convergence information. PetscPrintf() produces a single
167: print statement from all processes that share a communicator.
168: */
169: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A System %d: iterations %dn",norm,k,its);
170: }
172: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173: Clean up
174: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
175: /*
176: Free work space. All PETSc objects should be destroyed when they
177: are no longer needed.
178: */
179: SLESDestroy(sles);
180: VecDestroy(u); VecDestroy(x);
181: VecDestroy(b); MatDestroy(A);
183: /*
184: Always call PetscFinalize() before exiting a program. This routine
185: - finalizes the PETSc libraries as well as MPI
186: - provides summary and diagnostic information if certain runtime
187: options are chosen (e.g., -log_summary).
188: */
189: PetscFinalize();
190: return 0;
191: }