Actual source code: ex7.c

  1: /*$Id: ex7.c,v 1.59 2001/08/07 21:31:12 bsmith Exp $*/

  3: static char help[] = "Solves u`` + u^{2} = f with Newton-like methods. Using\n\
  4:  matrix-free techniques with user-provided explicit preconditioner matrix.\n\n";

 6:  #include petscsnes.h

  8: extern int  FormJacobian(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
  9: extern int  FormFunction(SNES,Vec,Vec,void*);
 10: extern int  FormInitialGuess(SNES,Vec);
 11: extern int  Monitor(SNES,int,PetscReal,void *);

 13: typedef struct {
 14:    PetscViewer viewer;
 15: } MonitorCtx;

 17: typedef struct {
 18:   Mat        precond;
 19:   PetscTruth variant;
 20: } AppCtx;

 24: int main(int argc,char **argv)
 25: {
 26:   SNES         snes;                 /* SNES context */
 27:   SNESType     type = SNESLS;        /* default nonlinear solution method */
 28:   Vec          x,r,F,U,work;         /* vectors */
 29:   Mat          J,B;                  /* Jacobian matrix-free, explicit preconditioner */
 30:   MonitorCtx   monP;                 /* monitoring context */
 31:   AppCtx       user;                 /* user-defined work context */
 32:   PetscScalar  h,xp = 0.0,v;
 33:   int          ierr,its,n = 5,i;

 35:   PetscInitialize(&argc,&argv,(char *)0,help);
 36:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 37:   PetscOptionsHasName(PETSC_NULL,"-variant",&user.variant);
 38:   h = 1.0/(n-1);

 40:   /* Set up data structures */
 41:   PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,400,400,&monP.viewer);
 42:   VecCreateSeq(PETSC_COMM_SELF,n,&x);
 43:   PetscObjectSetName((PetscObject)x,"Approximate Solution");
 44:   VecDuplicate(x,&r);
 45:   VecDuplicate(x,&F);
 46:   VecDuplicate(x,&U);
 47:   PetscObjectSetName((PetscObject)U,"Exact Solution");

 49:   /* create explict matrix preconditioner */
 50:   MatCreateSeqAIJ(PETSC_COMM_SELF,n,n,3,PETSC_NULL,&B);
 51:   user.precond = B;

 53:   /* Store right-hand-side of PDE and exact solution */
 54:   for (i=0; i<n; i++) {
 55:     v = 6.0*xp + PetscPowScalar(xp+1.e-12,6.0); /* +1.e-12 is to prevent 0^6 */
 56:     VecSetValues(F,1,&i,&v,INSERT_VALUES);
 57:     v= xp*xp*xp;
 58:     VecSetValues(U,1,&i,&v,INSERT_VALUES);
 59:     xp += h;
 60:   }

 62:   /* Create nonlinear solver */
 63:   SNESCreate(PETSC_COMM_WORLD,&snes);
 64:   SNESSetType(snes,type);

 66:   if (user.variant) {
 67:     MatCreateMF(x,&J);
 68:     VecDuplicate(x,&work);
 69:     MatSNESMFSetFunction(J,work,FormFunction,F);
 70:   } else {
 71:     /* create matrix free matrix for Jacobian */
 72:     MatCreateSNESMF(snes,x,&J);
 73:   }

 75:   /* Set various routines and options */
 76:   SNESSetFunction(snes,r,FormFunction,F);
 77:   SNESSetJacobian(snes,J,B,FormJacobian,&user);
 78:   SNESSetMonitor(snes,Monitor,&monP,0);
 79:   SNESSetFromOptions(snes);

 81:   /* Solve nonlinear system */
 82:   FormInitialGuess(snes,x);
 83:   SNESSolve(snes,x,&its);
 84:   PetscPrintf(PETSC_COMM_SELF,"number of Newton iterations = %d\n\n",its);

 86:   /* Free data structures */
 87:   if (user.variant) {
 88:     VecDestroy(work);
 89:   }
 90:   VecDestroy(x);  VecDestroy(r);
 91:   VecDestroy(U);  VecDestroy(F);
 92:   MatDestroy(J);  MatDestroy(B);
 93:   SNESDestroy(snes);
 94:   PetscViewerDestroy(monP.viewer);
 95:   PetscFinalize();

 97:   return 0;
 98: }
 99: /* --------------------  Evaluate Function F(x) --------------------- */

101: int FormFunction(SNES snes,Vec x,Vec f,void *dummy)
102: {
103:   PetscScalar *xx,*ff,*FF,d;
104:   int    i,ierr,n;

106:   VecGetArray(x,&xx);
107:   VecGetArray(f,&ff);
108:   VecGetArray((Vec) dummy,&FF);
109:   VecGetSize(x,&n);
110:   d = (PetscReal)(n - 1); d = d*d;
111:   ff[0]   = xx[0];
112:   for (i=1; i<n-1; i++) {
113:     ff[i] = d*(xx[i-1] - 2.0*xx[i] + xx[i+1]) + xx[i]*xx[i] - FF[i];
114:   }
115:   ff[n-1] = xx[n-1] - 1.0;
116:   VecRestoreArray(x,&xx);
117:   VecRestoreArray(f,&ff);
118:   VecRestoreArray((Vec)dummy,&FF);
119:   return 0;
120: }
121: /* --------------------  Form initial approximation ----------------- */

125: int FormInitialGuess(SNES snes,Vec x)
126: {
127:   int    ierr;
128:   PetscScalar pfive = .50;
129:   VecSet(&pfive,x);
130:   return 0;
131: }
134: /* --------------------  Evaluate Jacobian F'(x) -------------------- */
135: /*  Evaluates a matrix that is used to precondition the matrix-free
136:     jacobian. In this case, the explict preconditioner matrix is 
137:     also EXACTLY the Jacobian. In general, it would be some lower
138:     order, simplified apprioximation */

140: int FormJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure*flag,void *dummy)
141: {
142:   PetscScalar *xx,A[3],d;
143:   int    i,n,j[3],ierr,iter;
144:   AppCtx *user = (AppCtx*) dummy;

146:   SNESGetIterationNumber(snes,&iter);

148:   if (iter%2 ==0) { /* Compute new preconditioner matrix */
149:     printf("iter=%d, computing new preconditioning matrix\n",iter+1);
150:     *B = user->precond;
151:     VecGetArray(x,&xx);
152:     VecGetSize(x,&n);
153:     d = (PetscReal)(n - 1); d = d*d;

155:     i = 0; A[0] = 1.0;
156:     MatSetValues(*B,1,&i,1,&i,&A[0],INSERT_VALUES);
157:     for (i=1; i<n-1; i++) {
158:       j[0] = i - 1; j[1] = i;                   j[2] = i + 1;
159:       A[0] = d;     A[1] = -2.0*d + 2.0*xx[i];  A[2] = d;
160:       MatSetValues(*B,1,&i,3,j,A,INSERT_VALUES);
161:     }
162:     i = n-1; A[0] = 1.0;
163:     MatSetValues(*B,1,&i,1,&i,&A[0],INSERT_VALUES);
164:     MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
165:     MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
166:     VecRestoreArray(x,&xx);
167:     *flag = SAME_NONZERO_PATTERN;
168:   }  else { /* reuse preconditioner from last iteration */
169:     printf("iter=%d, using old preconditioning matrix\n",iter+1);
170:     *flag = SAME_PRECONDITIONER;
171:   }
172:   if (user->variant) {
173:     MatSNESMFSetBase(*jac,x);
174:   }
175:   MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);
176:   MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);
177:   return 0;
178: }
179: /* --------------------  User-defined monitor ----------------------- */

183: int Monitor(SNES snes,int its,PetscReal fnorm,void *dummy)
184: {
185:   int        ierr;
186:   MonitorCtx *monP = (MonitorCtx*) dummy;
187:   Vec        x;
188:   MPI_Comm   comm;

190:   PetscObjectGetComm((PetscObject)snes,&comm);
191:   PetscFPrintf(comm,stdout,"iter = %d, SNES Function norm %g \n",its,fnorm);
192:   SNESGetSolution(snes,&x);
193:   VecView(x,monP->viewer);
194:   return 0;
195: }