Actual source code: ex20.c
1: /*$Id: ex20.c,v 1.19 2001/08/07 21:30:50 bsmith Exp $*/
3: static char help[] = "This example solves a linear system in parallel with SLES. The matrix\n\
4: uses simple bilinear elements on the unit square. To test the parallel\n\
5: matrix assembly,the matrix is intentionally laid out across processors\n\
6: differently from the way it is assembled. Input arguments are:\n\
7: -m <size> : problem size\n\n";
9: #include petscsles.h
13: int FormElementStiffness(PetscReal H,PetscScalar *Ke)
14: {
15: Ke[0] = H/6.0; Ke[1] = -.125*H; Ke[2] = H/12.0; Ke[3] = -.125*H;
16: Ke[4] = -.125*H; Ke[5] = H/6.0; Ke[6] = -.125*H; Ke[7] = H/12.0;
17: Ke[8] = H/12.0; Ke[9] = -.125*H; Ke[10] = H/6.0; Ke[11] = -.125*H;
18: Ke[12] = -.125*H; Ke[13] = H/12.0; Ke[14] = -.125*H; Ke[15] = H/6.0;
19: return 0;
20: }
24: int main(int argc,char **args)
25: {
26: Mat C;
27: int i,m = 5,rank,size,N,start,end,M,its;
28: int ierr,idx[4];
29: PetscTruth flg;
30: PetscScalar zero = 0.0,Ke[16], one = 1.0;
31: PetscReal h;
32: Vec u,b;
33: SLES sles;
34: KSP ksp;
35: MatNullSpace nullsp;
36: PC pc;
38: PetscInitialize(&argc,&args,(char *)0,help);
39: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
40: N = (m+1)*(m+1); /* dimension of matrix */
41: M = m*m; /* number of elements */
42: h = 1.0/m; /* mesh width */
43: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
44: MPI_Comm_size(PETSC_COMM_WORLD,&size);
46: /* Create stiffness matrix */
47: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,N,N,&C);
48: MatSetFromOptions(C);
49: start = rank*(M/size) + ((M%size) < rank ? (M%size) : rank);
50: end = start + M/size + ((M%size) > rank);
52: /* Assemble matrix */
53: FormElementStiffness(h*h,Ke); /* element stiffness for Laplacian */
54: for (i=start; i<end; i++) {
55: /* location of lower left corner of element */
56: /* node numbers for the four corners of element */
57: idx[0] = (m+1)*(i/m) + (i % m);
58: idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
59: MatSetValues(C,4,idx,4,idx,Ke,ADD_VALUES);
60: }
61: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
62: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
64: /* Create right-hand-side and solution vectors */
65: VecCreate(PETSC_COMM_WORLD,&u);
66: VecSetSizes(u,PETSC_DECIDE,N);
67: VecSetFromOptions(u);
68: PetscObjectSetName((PetscObject)u,"Approx. Solution");
69: VecDuplicate(u,&b);
70: PetscObjectSetName((PetscObject)b,"Right hand side");
72: VecSet(&one,u);
73: MatMult(C,u,b);
74: VecSet(&zero,u);
76: /* Solve linear system */
77: SLESCreate(PETSC_COMM_WORLD,&sles);
78: SLESSetOperators(sles,C,C,DIFFERENT_NONZERO_PATTERN);
79: SLESSetFromOptions(sles);
80: SLESGetKSP(sles,&ksp);
81: KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
83: PetscOptionsHasName(PETSC_NULL,"-fixnullspace",&flg);
84: if (flg) {
85: SLESGetPC(sles,&pc);
86: MatNullSpaceCreate(PETSC_COMM_WORLD,1,0,PETSC_NULL,&nullsp);
87: PCNullSpaceAttach(pc,nullsp);
88: MatNullSpaceDestroy(nullsp);
89: }
91: SLESSolve(sles,b,u,&its);
94: /* Free work space */
95: SLESDestroy(sles);
96: VecDestroy(u);
97: VecDestroy(b);
98: MatDestroy(C);
99: PetscFinalize();
100: return 0;
101: }