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) {
103:     if (i<n-1) {
105:     if (j>0) {
107:     if (j<n-1) {
109:     if (use_random) {PetscRandomGetValueImaginary(rctx,&sigma2);}
110:     v = 4.0 - sigma1*h2 + sigma2*h2;
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:     VecSetRandom(u,rctx);
144:   } else {
145:     VecSet(u,pfive);
146:   }
147:   MatMult(A,u,b);

149:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150:                 Create the linear solver and set various options
151:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

153:   /*
154:      Create linear solver context
155:   */
156:   KSPCreate(PETSC_COMM_WORLD,&ksp);

158:   /*
159:      Set operators. Here the matrix that defines the linear system
160:      also serves as the preconditioning matrix.
161:   */
162:   KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);

164:   /*
165:     Set runtime options, e.g.,
166:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
167:   */
168:   KSPSetFromOptions(ksp);

170:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171:                       Solve the linear system
172:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

174:   KSPSolve(ksp,b,x);

176:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177:                       Check solution and clean up
178:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

180:   /*
181:       Print the first 3 entries of x; this demonstrates extraction of the
182:       real and imaginary components of the complex vector, x.
183:   */
184:   PetscOptionsHasName(PETSC_NULL,"-print_x3",&flg);
185:   if (flg) {
186:     VecGetArray(x,&xa);
187:     PetscPrintf(PETSC_COMM_WORLD,"The first three entries of x are:\n");
188:     for (i=0; i<3; i++){
189:       PetscPrintf(PETSC_COMM_WORLD,"x[%D] = %G + %G i\n",i,PetscRealPart(xa[i]),PetscImaginaryPart(xa[i]));
190:   }
191:     VecRestoreArray(x,&xa);
192:   }

194:   /*
195:      Check the error
196:   */
197:   VecAXPY(x,none,u);
198:   VecNorm(x,NORM_2,&norm);
199:   KSPGetIterationNumber(ksp,&its);
200:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %D\n",norm,its);

202:   /*
203:      Free work space.  All PETSc objects should be destroyed when they
204:      are no longer needed.
205:   */
206:   KSPDestroy(ksp);
207:   if (use_random) {PetscRandomDestroy(rctx);}
208:   VecDestroy(u); VecDestroy(x);
209:   VecDestroy(b); MatDestroy(A);
210:   PetscFinalize();
211:   return 0;
212: }
```