Actual source code: ex4.c

  1: /*$Id: ex4.c,v 1.64 2001/08/07 21:30:50 bsmith Exp $*/

  3: static char help[] = "Solves a linear system with SLES.  The matrix uses simple\n\
  4: bilinear elements on the unit square. Input arguments are:\n\
  5:   -m <size> : problem size\n\n";

 7:  #include petscsles.h

 11: int FormElementStiffness(PetscReal H,PetscScalar *Ke)
 12: {
 13:   Ke[0]  = H/6.0;    Ke[1]  = -.125*H; Ke[2]  = H/12.0;   Ke[3]  = -.125*H;
 14:   Ke[4]  = -.125*H;  Ke[5]  = H/6.0;   Ke[6]  = -.125*H;  Ke[7]  = H/12.0;
 15:   Ke[8]  = H/12.0;   Ke[9]  = -.125*H; Ke[10] = H/6.0;    Ke[11] = -.125*H;
 16:   Ke[12] = -.125*H;  Ke[13] = H/12.0;  Ke[14] = -.125*H;  Ke[15] = H/6.0;
 17:   return 0;
 18: }
 21: int FormElementRhs(PetscReal x,PetscReal y,PetscReal H,PetscScalar *r)
 22: {
 23:   r[0] = 0.; r[1] = 0.; r[2] = 0.; r[3] = 0.0;
 24:   return 0;
 25: }

 29: int main(int argc,char **args)
 30: {
 31:   Mat         C;
 32:   int         i,m = 2,N,M,its,ierr,idx[4],count,*rows;
 33:   PetscScalar val,zero = 0.0,one = 1.0,none = -1.0,Ke[16],r[4];
 34:   PetscReal   x,y,h,norm;
 35:   Vec         u,ustar,b;
 36:   SLES        sles;
 37:   KSP         ksp;
 38:   IS          is;

 40:   PetscInitialize(&argc,&args,(char *)0,help);
 41:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 42:   N = (m+1)*(m+1); /* dimension of matrix */
 43:   M = m*m; /* number of elements */
 44:   h = 1.0/m;       /* mesh width */

 46:   /* create stiffness matrix */
 47:   MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,9,PETSC_NULL,&C);

 49:   /* forms the element stiffness for the Laplacian */
 50:   FormElementStiffness(h*h,Ke);
 51:   for (i=0; i<M; i++) {
 52:      /* location of lower left corner of element */
 53:      x = h*(i % m); y = h*(i/m);
 54:      /* node numbers for the four corners of element */
 55:      idx[0] = (m+1)*(i/m) + (i % m);
 56:      idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
 57:      MatSetValues(C,4,idx,4,idx,Ke,ADD_VALUES);
 58:   }
 59:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 60:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 62:   /* create right hand side and solution */

 64:   VecCreateSeq(PETSC_COMM_SELF,N,&u);
 65:   VecDuplicate(u,&b);
 66:   VecDuplicate(b,&ustar);
 67:   VecSet(&zero,u);
 68:   VecSet(&zero,b);

 70:   for (i=0; i<M; i++) {
 71:      /* location of lower left corner of element */
 72:      x = h*(i % m); y = h*(i/m);
 73:      /* node numbers for the four corners of element */
 74:      idx[0] = (m+1)*(i/m) + (i % m);
 75:      idx[1] = idx[0]+1; idx[2] = idx[1] + m + 1; idx[3] = idx[2] - 1;
 76:      FormElementRhs(x,y,h*h,r);
 77:      VecSetValues(b,4,idx,r,ADD_VALUES);
 78:   }
 79:   VecAssemblyBegin(b);
 80:   VecAssemblyEnd(b);

 82:   /* modify matrix and rhs for Dirichlet boundary conditions */
 83:   PetscMalloc((4*m+1)*sizeof(int),&rows);
 84:   for (i=0; i<m+1; i++) {
 85:     rows[i] = i; /* bottom */
 86:     rows[3*m - 1 +i] = m*(m+1) + i; /* top */
 87:   }
 88:   count = m+1; /* left side */
 89:   for (i=m+1; i<m*(m+1); i+= m+1) {
 90:     rows[count++] = i;
 91:   }
 92:   count = 2*m; /* left side */
 93:   for (i=2*m+1; i<m*(m+1); i+= m+1) {
 94:     rows[count++] = i;
 95:   }
 96:   ISCreateGeneral(PETSC_COMM_SELF,4*m,rows,&is);
 97:   for (i=0; i<4*m; i++) {
 98:      x = h*(rows[i] % (m+1)); y = h*(rows[i]/(m+1));
 99:      val = y;
100:      VecSetValues(u,1,&rows[i],&val,INSERT_VALUES);
101:      VecSetValues(b,1,&rows[i],&val,INSERT_VALUES);
102:   }
103:   PetscFree(rows);
104:   VecAssemblyBegin(u);
105:   VecAssemblyEnd(u);
106:   VecAssemblyBegin(b);
107:   VecAssemblyEnd(b);

109:   MatZeroRows(C,is,&one);
110:   ISDestroy(is);

112:   /* solve linear system */
113:   SLESCreate(PETSC_COMM_WORLD,&sles);
114:   SLESSetOperators(sles,C,C,DIFFERENT_NONZERO_PATTERN);
115: 
116:   SLESSetFromOptions(sles);
117:   SLESGetKSP(sles,&ksp);
118:   KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
119:   SLESSolve(sles,b,u,&its);

121:   /* check error */
122:   for (i=0; i<N; i++) {
123:      x = h*(i % (m+1)); y = h*(i/(m+1));
124:      val = y;
125:      VecSetValues(ustar,1,&i,&val,INSERT_VALUES);
126:   }
127:   VecAssemblyBegin(ustar);
128:   VecAssemblyEnd(ustar);

130:   VecAXPY(&none,ustar,u);
131:   VecNorm(u,NORM_2,&norm);
132:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A Iterations %d\n",norm*h,its);

134:   SLESDestroy(sles);
135:   VecDestroy(ustar);
136:   VecDestroy(u);
137:   VecDestroy(b);
138:   MatDestroy(C);
139:   PetscFinalize();
140:   return 0;
141: }