Actual source code: ex11.c
2: static char help[] = "Solves a linear system in parallel with KSP.\n\n";
4: /*T
5: Concepts: KSP^solving a Helmholtz equation
6: Concepts: complex numbers;
7: Concepts: Helmholtz equation
8: Processors: n
9: T*/
11: /*
12: Description: Solves a complex linear system in parallel with KSP.
14: The model problem:
15: Solve Helmholtz equation on the unit square: (0,1) x (0,1)
16: -delta u - sigma1*u + i*sigma2*u = f,
17: where delta = Laplace operator
18: Dirichlet b.c.'s on all sides
19: Use the 2-D, five-point finite difference stencil.
21: Compiling the code:
22: This code uses the complex numbers version of PETSc, so one of the
23: following values of BOPT must be used for compiling the PETSc libraries
24: and this example:
25: BOPT=g_complex - debugging version
26: BOPT=O_complex - optimized version
27: BOPT=Opg_complex - profiling version
28: */
30: /*
31: Include "petscksp.h" so that we can use KSP solvers. Note that this file
32: automatically includes:
33: petsc.h - base PETSc routines petscvec.h - vectors
34: petscsys.h - system routines petscmat.h - matrices
35: petscis.h - index sets petscksp.h - Krylov subspace methods
36: petscviewer.h - viewers petscpc.h - preconditioners
37: */
38: #include petscksp.h
42: int main(int argc,char **args)
43: {
44: Vec x,b,u; /* approx solution, RHS, exact solution */
45: Mat A; /* linear system matrix */
46: KSP ksp; /* linear solver context */
47: PetscReal norm; /* norm of solution error */
48: PetscInt dim,i,j,I,J,Istart,Iend,n = 6,its,use_random;
50: PetscScalar v,none = -1.0,sigma2,pfive = 0.5,*xa;
51: PetscRandom rctx;
52: PetscReal h2,sigma1 = 100.0;
53: PetscTruth flg;
55: PetscInitialize(&argc,&args,(char *)0,help);
56: #if !defined(PETSC_USE_COMPLEX)
57: SETERRQ(1,"This example requires complex numbers");
58: #endif
60: PetscOptionsGetReal(PETSC_NULL,"-sigma1",&sigma1,PETSC_NULL);
61: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
62: dim = n*n;
64: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65: Compute the matrix and right-hand-side vector that define
66: the linear system, Ax = b.
67: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
68: /*
69: Create parallel matrix, specifying only its global dimensions.
70: When using MatCreate(), the matrix format can be specified at
71: runtime. Also, the parallel partitioning of the matrix is
72: determined by PETSc at runtime.
73: */
74: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,dim,dim,&A);
75: MatSetFromOptions(A);
77: /*
78: Currently, all PETSc parallel matrix formats are partitioned by
79: contiguous chunks of rows across the processors. Determine which
80: rows of the matrix are locally owned.
81: */
82: MatGetOwnershipRange(A,&Istart,&Iend);
84: /*
85: Set matrix elements in parallel.
86: - Each processor needs to insert only elements that it owns
87: locally (but any non-local elements will be sent to the
88: appropriate processor during matrix assembly).
89: - Always specify global rows and columns of matrix entries.
90: */
92: PetscOptionsHasName(PETSC_NULL,"-norandom",&flg);
93: if (flg) use_random = 0;
94: else use_random = 1;
95: if (use_random) {
96: PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT_IMAGINARY,&rctx);
97: } else {
98: sigma2 = 10.0*PETSC_i;
99: }
100: h2 = 1.0/((n+1)*(n+1));
101: for (I=Istart; I<Iend; I++) {
102: v = -1.0; i = I/n; j = I - i*n;
103: if (i>0) {
104: J = I-n; MatSetValues(A,1,&I,1,&J,&v,ADD_VALUES);}
105: if (i<n-1) {
106: J = I+n; MatSetValues(A,1,&I,1,&J,&v,ADD_VALUES);}
107: if (j>0) {
108: J = I-1; MatSetValues(A,1,&I,1,&J,&v,ADD_VALUES);}
109: if (j<n-1) {
110: J = I+1; MatSetValues(A,1,&I,1,&J,&v,ADD_VALUES);}
111: if (use_random) {PetscRandomGetValue(rctx,&sigma2);}
112: v = 4.0 - sigma1*h2 + sigma2*h2;
113: MatSetValues(A,1,&I,1,&I,&v,ADD_VALUES);
114: }
115: if (use_random) {PetscRandomDestroy(rctx);}
117: /*
118: Assemble matrix, using the 2-step process:
119: MatAssemblyBegin(), MatAssemblyEnd()
120: Computations can be done while messages are in transition
121: by placing code between these two statements.
122: */
123: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
124: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
126: /*
127: Create parallel vectors.
128: - When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
129: we specify only the vector's global
130: dimension; the parallel partitioning is determined at runtime.
131: - Note: We form 1 vector from scratch and then duplicate as needed.
132: */
133: VecCreate(PETSC_COMM_WORLD,&u);
134: VecSetSizes(u,PETSC_DECIDE,dim);
135: VecSetFromOptions(u);
136: VecDuplicate(u,&b);
137: VecDuplicate(b,&x);
139: /*
140: Set exact solution; then compute right-hand-side vector.
141: */
142:
143: if (use_random) {
144: PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT,&rctx);
145: VecSetRandom(rctx,u);
146: } else {
147: VecSet(&pfive,u);
148: }
149: MatMult(A,u,b);
151: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152: Create the linear solver and set various options
153: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155: /*
156: Create linear solver context
157: */
158: KSPCreate(PETSC_COMM_WORLD,&ksp);
160: /*
161: Set operators. Here the matrix that defines the linear system
162: also serves as the preconditioning matrix.
163: */
164: KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
166: /*
167: Set runtime options, e.g.,
168: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
169: */
170: KSPSetFromOptions(ksp);
172: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173: Solve the linear system
174: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
176: KSPSolve(ksp,b,x);
178: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179: Check solution and clean up
180: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
182: /*
183: Print the first 3 entries of x; this demonstrates extraction of the
184: real and imaginary components of the complex vector, x.
185: */
186: PetscOptionsHasName(PETSC_NULL,"-print_x3",&flg);
187: if (flg) {
188: VecGetArray(x,&xa);
189: PetscPrintf(PETSC_COMM_WORLD,"The first three entries of x are:\n");
190: for (i=0; i<3; i++){
191: PetscPrintf(PETSC_COMM_WORLD,"x[%D] = %g + %g i\n",i,PetscRealPart(xa[i]),PetscImaginaryPart(xa[i]));
192: }
193: VecRestoreArray(x,&xa);
194: }
196: /*
197: Check the error
198: */
199: VecAXPY(&none,u,x);
200: VecNorm(x,NORM_2,&norm);
201: KSPGetIterationNumber(ksp,&its);
202: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %D\n",norm,its);
204: /*
205: Free work space. All PETSc objects should be destroyed when they
206: are no longer needed.
207: */
208: KSPDestroy(ksp);
209: if (use_random) {PetscRandomDestroy(rctx);}
210: VecDestroy(u); VecDestroy(x);
211: VecDestroy(b); MatDestroy(A);
212: PetscFinalize();
213: return 0;
214: }