Actual source code: ex3.c

  1: /*$Id: ex3.c,v 1.73 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: {
 16:   Ke[0]  = H/6.0;    Ke[1]  = -.125*H; Ke[2]  = H/12.0;   Ke[3]  = -.125*H;
 17:   Ke[4]  = -.125*H;  Ke[5]  = H/6.0;   Ke[6]  = -.125*H;  Ke[7]  = H/12.0;
 18:   Ke[8]  = H/12.0;   Ke[9]  = -.125*H; Ke[10] = H/6.0;    Ke[11] = -.125*H;
 19:   Ke[12] = -.125*H;  Ke[13] = H/12.0;  Ke[14] = -.125*H;  Ke[15] = H/6.0;
 20:   return(0);
 21: }
 24: int FormElementRhs(PetscReal x,PetscReal y,PetscReal H,PetscScalar *r)
 25: {
 27:   r[0] = 0.; r[1] = 0.; r[2] = 0.; r[3] = 0.0;
 28:   return(0);
 29: }

 33: int main(int argc,char **args)
 34: {
 35:   Mat         C;
 36:   int         i,m = 5,rank,size,N,start,end,M,its;
 37:   PetscScalar val,zero = 0.0,one = 1.0,none = -1.0,Ke[16],r[4];
 38:   PetscReal   x,y,h,norm;
 39:   int         ierr,idx[4],count,*rows;
 40:   Vec         u,ustar,b;
 41:   SLES        sles;
 42:   KSP         ksp;
 43:   IS          is;

 45:   PetscInitialize(&argc,&args,(char *)0,help);
 46:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 47:   N = (m+1)*(m+1); /* dimension of matrix */
 48:   M = m*m; /* number of elements */
 49:   h = 1.0/m;       /* mesh width */
 50:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 51:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 53:   /* Create stiffness matrix */
 54:   MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,N,N,&C);
 55:   MatSetFromOptions(C);
 56:   start = rank*(M/size) + ((M%size) < rank ? (M%size) : rank);
 57:   end   = start + M/size + ((M%size) > rank);

 59:   /* Assemble matrix */
 60:   FormElementStiffness(h*h,Ke);   /* element stiffness for Laplacian */
 61:   for (i=start; i<end; i++) {
 62:      /* location of lower left corner of element */
 63:      x = h*(i % m); y = h*(i/m);
 64:      /* node numbers for the four corners of element */
 65:      idx[0] = (m+1)*(i/m) + (i % m);
 66:      idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
 67:      MatSetValues(C,4,idx,4,idx,Ke,ADD_VALUES);
 68:   }
 69:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 70:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 72:   /* Create right-hand-side and solution vectors */
 73:   VecCreate(PETSC_COMM_WORLD,&u);
 74:   VecSetSizes(u,PETSC_DECIDE,N);
 75:   VecSetFromOptions(u);
 76:   PetscObjectSetName((PetscObject)u,"Approx. Solution");
 77:   VecDuplicate(u,&b);
 78:   PetscObjectSetName((PetscObject)b,"Right hand side");
 79:   VecDuplicate(b,&ustar);
 80:   VecSet(&zero,u);
 81:   VecSet(&zero,b);

 83:   /* Assemble right-hand-side vector */
 84:   for (i=start; i<end; i++) {
 85:      /* location of lower left corner of element */
 86:      x = h*(i % m); y = h*(i/m);
 87:      /* node numbers for the four corners of element */
 88:      idx[0] = (m+1)*(i/m) + (i % m);
 89:      idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
 90:      FormElementRhs(x,y,h*h,r);
 91:      VecSetValues(b,4,idx,r,ADD_VALUES);
 92:   }
 93:   VecAssemblyBegin(b);
 94:   VecAssemblyEnd(b);

 96:   /* Modify matrix and right-hand-side for Dirichlet boundary conditions */
 97:   PetscMalloc(4*m*sizeof(int),&rows);
 98:   for (i=0; i<m+1; i++) {
 99:     rows[i] = i; /* bottom */
100:     rows[3*m - 1 +i] = m*(m+1) + i; /* top */
101:   }
102:   count = m+1; /* left side */
103:   for (i=m+1; i<m*(m+1); i+= m+1) {
104:     rows[count++] = i;
105:   }
106:   count = 2*m; /* left side */
107:   for (i=2*m+1; i<m*(m+1); i+= m+1) {
108:     rows[count++] = i;
109:   }
110:   ISCreateGeneral(PETSC_COMM_SELF,4*m,rows,&is);
111:   for (i=0; i<4*m; i++) {
112:      x = h*(rows[i] % (m+1)); y = h*(rows[i]/(m+1));
113:      val = y;
114:      VecSetValues(u,1,&rows[i],&val,INSERT_VALUES);
115:      VecSetValues(b,1,&rows[i],&val,INSERT_VALUES);
116:   }
117:   PetscFree(rows);
118:   VecAssemblyBegin(u);
119:   VecAssemblyEnd(u);
120:   VecAssemblyBegin(b);
121:   VecAssemblyEnd(b);

123:   MatZeroRows(C,is,&one);
124:   ISDestroy(is);


127:   { Mat A;
128:   MatConvert(C,MATSAME,&A);
129:   MatDestroy(C);
130:   MatConvert(A,MATSAME,&C);
131:   MatDestroy(A);
132:   }

134:   /* Solve linear system */
135:   SLESCreate(PETSC_COMM_WORLD,&sles);
136:   SLESSetOperators(sles,C,C,DIFFERENT_NONZERO_PATTERN);
137:   SLESSetFromOptions(sles);
138:   SLESGetKSP(sles,&ksp);
139:   KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
140:   SLESSolve(sles,b,u,&its);

142:   /* Check error */
143:   VecGetOwnershipRange(ustar,&start,&end);
144:   for (i=start; i<end; i++) {
145:      x = h*(i % (m+1)); y = h*(i/(m+1));
146:      val = y;
147:      VecSetValues(ustar,1,&i,&val,INSERT_VALUES);
148:   }
149:   VecAssemblyBegin(ustar);
150:   VecAssemblyEnd(ustar);
151:   VecAXPY(&none,ustar,u);
152:   VecNorm(u,NORM_2,&norm);
153:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A Iterations %d\n",norm*h,its);

155:   /* Free work space */
156:   SLESDestroy(sles);
157:   VecDestroy(ustar);
158:   VecDestroy(u);
159:   VecDestroy(b);
160:   MatDestroy(C);
161:   PetscFinalize();
162:   return 0;
163: }