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 configure
23: must be run to enable this
24: */
26: /*
27: Include "petscksp.h" so that we can use KSP solvers. Note that this file
28: automatically includes:
29: petsc.h - base PETSc routines petscvec.h - vectors
30: petscsys.h - system routines petscmat.h - matrices
31: petscis.h - index sets petscksp.h - Krylov subspace methods
32: petscviewer.h - viewers petscpc.h - preconditioners
33: */
34: #include petscksp.h
38: int main(int argc,char **args)
39: {
40: Vec x,b,u; /* approx solution, RHS, exact solution */
41: Mat A; /* linear system matrix */
42: KSP ksp; /* linear solver context */
43: PetscReal norm; /* norm of solution error */
44: PetscInt dim,i,j,Ii,J,Istart,Iend,n = 6,its,use_random;
46: PetscScalar v,none = -1.0,sigma2,pfive = 0.5,*xa;
47: PetscRandom rctx;
48: PetscReal h2,sigma1 = 100.0;
49: PetscTruth flg;
51: PetscInitialize(&argc,&args,(char *)0,help);
52: #if !defined(PETSC_USE_COMPLEX)
53: SETERRQ(1,"This example requires complex numbers");
54: #endif
56: PetscOptionsGetReal(PETSC_NULL,"-sigma1",&sigma1,PETSC_NULL);
57: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
58: dim = n*n;
60: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61: Compute the matrix and right-hand-side vector that define
62: the linear system, Ax = b.
63: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
64: /*
65: Create parallel matrix, specifying only its global dimensions.
66: When using MatCreate(), the matrix format can be specified at
67: runtime. Also, the parallel partitioning of the matrix is
68: determined by PETSc at runtime.
69: */
70: MatCreate(PETSC_COMM_WORLD,&A);
71: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,dim,dim);
72: MatSetFromOptions(A);
74: /*
75: Currently, all PETSc parallel matrix formats are partitioned by
76: contiguous chunks of rows across the processors. Determine which
77: rows of the matrix are locally owned.
78: */
79: MatGetOwnershipRange(A,&Istart,&Iend);
81: /*
82: Set matrix elements in parallel.
83: - Each processor needs to insert only elements that it owns
84: locally (but any non-local elements will be sent to the
85: appropriate processor during matrix assembly).
86: - Always specify global rows and columns of matrix entries.
87: */
89: PetscOptionsHasName(PETSC_NULL,"-norandom",&flg);
90: if (flg) use_random = 0;
91: else use_random = 1;
92: if (use_random) {
93: PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
94: PetscRandomSetFromOptions(rctx);
95: } else {
96: sigma2 = 10.0*PETSC_i;
97: }
98: h2 = 1.0/((n+1)*(n+1));
99: for (Ii=Istart; Ii<Iend; Ii++) {
100: v = -1.0; i = Ii/n; j = Ii - i*n;
101: if (i>0) {
102: J = Ii-n; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
103: if (i<n-1) {
104: J = Ii+n; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
105: if (j>0) {
106: J = Ii-1; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
107: if (j<n-1) {
108: J = Ii+1; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
109: if (use_random) {PetscRandomGetValueImaginary(rctx,&sigma2);}
110: v = 4.0 - sigma1*h2 + sigma2*h2;
111: MatSetValues(A,1,&Ii,1,&Ii,&v,ADD_VALUES);
112: }
113: if (use_random) {PetscRandomDestroy(rctx);}
115: /*
116: Assemble matrix, using the 2-step process:
117: MatAssemblyBegin(), MatAssemblyEnd()
118: Computations can be done while messages are in transition
119: by placing code between these two statements.
120: */
121: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
122: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
124: /*
125: Create parallel vectors.
126: - When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
127: we specify only the vector's global
128: dimension; the parallel partitioning is determined at runtime.
129: - Note: We form 1 vector from scratch and then duplicate as needed.
130: */
131: VecCreate(PETSC_COMM_WORLD,&u);
132: VecSetSizes(u,PETSC_DECIDE,dim);
133: VecSetFromOptions(u);
134: VecDuplicate(u,&b);
135: VecDuplicate(b,&x);
137: /*
138: Set exact solution; then compute right-hand-side vector.
139: */
140:
141: if (use_random) {
142: PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
143: PetscRandomSetFromOptions(rctx);
144: VecSetRandom(u,rctx);
145: } else {
146: VecSet(u,pfive);
147: }
148: MatMult(A,u,b);
150: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151: Create the linear solver and set various options
152: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
154: /*
155: Create linear solver context
156: */
157: KSPCreate(PETSC_COMM_WORLD,&ksp);
159: /*
160: Set operators. Here the matrix that defines the linear system
161: also serves as the preconditioning matrix.
162: */
163: KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
165: /*
166: Set runtime options, e.g.,
167: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
168: */
169: KSPSetFromOptions(ksp);
171: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172: Solve the linear system
173: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
175: KSPSolve(ksp,b,x);
177: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
178: Check solution and clean up
179: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
181: /*
182: Print the first 3 entries of x; this demonstrates extraction of the
183: real and imaginary components of the complex vector, x.
184: */
185: PetscOptionsHasName(PETSC_NULL,"-print_x3",&flg);
186: if (flg) {
187: VecGetArray(x,&xa);
188: PetscPrintf(PETSC_COMM_WORLD,"The first three entries of x are:\n");
189: for (i=0; i<3; i++){
190: PetscPrintf(PETSC_COMM_WORLD,"x[%D] = %G + %G i\n",i,PetscRealPart(xa[i]),PetscImaginaryPart(xa[i]));
191: }
192: VecRestoreArray(x,&xa);
193: }
195: /*
196: Check the error
197: */
198: VecAXPY(x,none,u);
199: VecNorm(x,NORM_2,&norm);
200: KSPGetIterationNumber(ksp,&its);
201: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %D\n",norm,its);
203: /*
204: Free work space. All PETSc objects should be destroyed when they
205: are no longer needed.
206: */
207: KSPDestroy(ksp);
208: if (use_random) {PetscRandomDestroy(rctx);}
209: VecDestroy(u); VecDestroy(x);
210: VecDestroy(b); MatDestroy(A);
211: PetscFinalize();
212: return 0;
213: }