Actual source code: ex3.c
petsc-3.3-p7 2013-05-11
2: static char help[] = "Solves a linear system in parallel with KSP. The matrix\n\
3: uses simple bilinear elements on the unit square. To test the parallel\n\
4: matrix assembly, the matrix is intentionally laid out across processors\n\
5: differently from the way it is assembled. Input arguments are:\n\
6: -m <size> : problem size\n\n";
8: /*T
9: Concepts: KSP^basic parallel example
10: Concepts: Matrices^inserting elements by blocks
11: Processors: n
12: T*/
14: /*
15: Include "petscksp.h" so that we can use KSP solvers. Note that this file
16: automatically includes:
17: petscsys.h - base PETSc routines petscvec.h - vectors
18: petscmat.h - matrices
19: petscis.h - index sets petscksp.h - Krylov subspace methods
20: petscviewer.h - viewers petscpc.h - preconditioners
21: */
22: #include <petscksp.h>
24: /* Declare user-defined routines */
25: extern PetscErrorCode FormElementStiffness(PetscReal,PetscScalar*);
26: extern PetscErrorCode FormElementRhs(PetscReal,PetscReal,PetscReal,PetscScalar*);
30: int main(int argc,char **args)
31: {
32: Vec u,b,ustar; /* approx solution, RHS, exact solution */
33: Mat A; /* linear system matrix */
34: KSP ksp; /* Krylov subspace method context */
35: PetscInt N; /* dimension of system (global) */
36: PetscInt M; /* number of elements (global) */
37: PetscMPIInt rank; /* processor rank */
38: PetscMPIInt size; /* size of communicator */
39: PetscScalar Ke[16]; /* element matrix */
40: PetscScalar r[4]; /* element vector */
41: PetscReal h; /* mesh width */
42: PetscReal norm; /* norm of solution error */
43: PetscReal x,y;
44: PetscScalar val;
46: PetscInt idx[4],count,*rows,i,m = 5,start,end,its;
48: PetscInitialize(&argc,&args,(char *)0,help);
49: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
50: N = (m+1)*(m+1);
51: M = m*m;
52: h = 1.0/m;
53: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
54: MPI_Comm_size(PETSC_COMM_WORLD,&size);
56: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57: Compute the matrix and right-hand-side vector that define
58: the linear system, Au = b.
59: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
61: /*
62: Create stiffness matrix
63: */
64: MatCreate(PETSC_COMM_WORLD,&A);
65: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
66: MatSetFromOptions(A);
67: MatSeqAIJSetPreallocation(A,9,PETSC_NULL);
68: MatMPIAIJSetPreallocation(A,9,PETSC_NULL,5,PETSC_NULL); /* More than necessary */
69: start = rank*(M/size) + ((M%size) < rank ? (M%size) : rank);
70: end = start + M/size + ((M%size) > rank);
72: /*
73: Assemble matrix
74: */
75: FormElementStiffness(h*h,Ke);
76: for (i=start; i<end; i++) {
77: /* location of lower left corner of element */
78: x = h*(i % m); y = h*(i/m);
79: /* node numbers for the four corners of element */
80: idx[0] = (m+1)*(i/m) + (i % m);
81: idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
82: MatSetValues(A,4,idx,4,idx,Ke,ADD_VALUES);
83: }
84: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
85: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
87: /*
88: Create right-hand-side and solution vectors
89: */
90: VecCreate(PETSC_COMM_WORLD,&u);
91: VecSetSizes(u,PETSC_DECIDE,N);
92: VecSetFromOptions(u);
93: PetscObjectSetName((PetscObject)u,"Approx. Solution");
94: VecDuplicate(u,&b);
95: PetscObjectSetName((PetscObject)b,"Right hand side");
96: VecDuplicate(b,&ustar);
97: VecSet(u,0.0);
98: VecSet(b,0.0);
100: /*
101: Assemble right-hand-side vector
102: */
103: for (i=start; i<end; i++) {
104: /* location of lower left corner of element */
105: x = h*(i % m); y = h*(i/m);
106: /* node numbers for the four corners of element */
107: idx[0] = (m+1)*(i/m) + (i % m);
108: idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
109: FormElementRhs(x,y,h*h,r);
110: VecSetValues(b,4,idx,r,ADD_VALUES);
111: }
112: VecAssemblyBegin(b);
113: VecAssemblyEnd(b);
115: /*
116: Modify matrix and right-hand-side for Dirichlet boundary conditions
117: */
118: PetscMalloc(4*m*sizeof(PetscInt),&rows);
119: for (i=0; i<m+1; i++) {
120: rows[i] = i; /* bottom */
121: rows[3*m - 1 +i] = m*(m+1) + i; /* top */
122: }
123: count = m+1; /* left side */
124: for (i=m+1; i<m*(m+1); i+= m+1) {
125: rows[count++] = i;
126: }
127: count = 2*m; /* left side */
128: for (i=2*m+1; i<m*(m+1); i+= m+1) {
129: rows[count++] = i;
130: }
131: for (i=0; i<4*m; i++) {
132: x = h*(rows[i] % (m+1)); y = h*(rows[i]/(m+1));
133: val = y;
134: VecSetValues(u,1,&rows[i],&val,INSERT_VALUES);
135: VecSetValues(b,1,&rows[i],&val,INSERT_VALUES);
136: }
137: MatZeroRows(A,4*m,rows,1.0,0,0);
138: PetscFree(rows);
140: VecAssemblyBegin(u);
141: VecAssemblyEnd(u);
142: VecAssemblyBegin(b);
143: VecAssemblyEnd(b);
145: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146: Create the linear solver and set various options
147: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
149: KSPCreate(PETSC_COMM_WORLD,&ksp);
150: KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
151: KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
152: KSPSetFromOptions(ksp);
154: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
155: Solve the linear system
156: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
158: KSPSolve(ksp,b,u);
160: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161: Check solution and clean up
162: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
164: /* Check error */
165: VecGetOwnershipRange(ustar,&start,&end);
166: for (i=start; i<end; i++) {
167: x = h*(i % (m+1)); y = h*(i/(m+1));
168: val = y;
169: VecSetValues(ustar,1,&i,&val,INSERT_VALUES);
170: }
171: VecAssemblyBegin(ustar);
172: VecAssemblyEnd(ustar);
173: VecAXPY(u,-1.0,ustar);
174: VecNorm(u,NORM_2,&norm);
175: KSPGetIterationNumber(ksp,&its);
176: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %G Iterations %D\n",norm*h,its);
178: /*
179: Free work space. All PETSc objects should be destroyed when they
180: are no longer needed.
181: */
182: KSPDestroy(&ksp); VecDestroy(&u);
183: VecDestroy(&ustar); VecDestroy(&b);
184: MatDestroy(&A);
186: /*
187: Always call PetscFinalize() before exiting a program. This routine
188: - finalizes the PETSc libraries as well as MPI
189: - provides summary and diagnostic information if certain runtime
190: options are chosen (e.g., -log_summary).
191: */
192: PetscFinalize();
193: return 0;
194: }
196: /* --------------------------------------------------------------------- */
199: /* element stiffness for Laplacian */
200: PetscErrorCode FormElementStiffness(PetscReal H,PetscScalar *Ke)
201: {
203: Ke[0] = H/6.0; Ke[1] = -.125*H; Ke[2] = H/12.0; Ke[3] = -.125*H;
204: Ke[4] = -.125*H; Ke[5] = H/6.0; Ke[6] = -.125*H; Ke[7] = H/12.0;
205: Ke[8] = H/12.0; Ke[9] = -.125*H; Ke[10] = H/6.0; Ke[11] = -.125*H;
206: Ke[12] = -.125*H; Ke[13] = H/12.0; Ke[14] = -.125*H; Ke[15] = H/6.0;
207: return(0);
208: }
209: /* --------------------------------------------------------------------- */
212: PetscErrorCode FormElementRhs(PetscReal x,PetscReal y,PetscReal H,PetscScalar *r)
213: {
215: r[0] = 0.; r[1] = 0.; r[2] = 0.; r[3] = 0.0;
216: return(0);
217: }