Actual source code: snesmfj2.c

  1: /*$Id: snesmfj2.c,v 1.35 2001/08/21 21:03:55 bsmith Exp $*/

 3:  #include src/snes/snesimpl.h

  5: EXTERN int DiffParameterCreate_More(SNES,Vec,void**);
  6: EXTERN int DiffParameterCompute_More(SNES,void*,Vec,Vec,PetscReal*,PetscReal*);
  7: EXTERN int DiffParameterDestroy_More(void*);

  9: typedef struct {  /* default context for matrix-free SNES */
 10:   SNES         snes;             /* SNES context */
 11:   Vec          w;                /* work vector */
 12:   MatNullSpace sp;               /* null space context */
 13:   PetscReal    error_rel;        /* square root of relative error in computing function */
 14:   PetscReal    umin;             /* minimum allowable u'a value relative to |u|_1 */
 15:   int          jorge;            /* flag indicating use of Jorge's method for determining
 16:                                    the differencing parameter */
 17:   PetscReal    h;                /* differencing parameter */
 18:   int          need_h;           /* flag indicating whether we must compute h */
 19:   int          need_err;         /* flag indicating whether we must currently compute error_rel */
 20:   int          compute_err;      /* flag indicating whether we must ever compute error_rel */
 21:   int          compute_err_iter; /* last iter where we've computer error_rel */
 22:   int          compute_err_freq; /* frequency of computing error_rel */
 23:   void         *data;            /* implementation-specific data */
 24: } MFCtx_Private;

 26: #undef __FUNCT__  
 28: int SNESMatrixFreeDestroy2_Private(Mat mat)
 29: {
 30:   int           ierr;
 31:   MFCtx_Private *ctx;

 34:   MatShellGetContext(mat,(void **)&ctx);
 35:   VecDestroy(ctx->w);
 36:   if (ctx->sp) {MatNullSpaceDestroy(ctx->sp);}
 37:   if (ctx->jorge || ctx->compute_err) {DiffParameterDestroy_More(ctx->data);}
 38:   PetscFree(ctx);
 39:   return(0);
 40: }

 42: #undef __FUNCT__  
 44: /*
 45:    SNESMatrixFreeView2_Private - Views matrix-free parameters.
 46:  */
 47: int SNESMatrixFreeView2_Private(Mat J,PetscViewer viewer)
 48: {
 49:   int           ierr;
 50:   MFCtx_Private *ctx;
 51:   PetscTruth    isascii;

 54:   MatShellGetContext(J,(void **)&ctx);
 55:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
 56:   if (isascii) {
 57:      PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:n");
 58:      if (ctx->jorge) {
 59:        PetscViewerASCIIPrintf(viewer,"    using Jorge's method of determining differencing parametern");
 60:      }
 61:      PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)n",ctx->error_rel);
 62:      PetscViewerASCIIPrintf(viewer,"    umin=%g (minimum iterate parameter)n",ctx->umin);
 63:      if (ctx->compute_err) {
 64:        PetscViewerASCIIPrintf(viewer,"    freq_err=%d (frequency for computing err)n",ctx->compute_err_freq);
 65:      }
 66:   } else {
 67:     SETERRQ1(1,"Viewer type %s not supported by SNES matrix free Jorge",((PetscObject)viewer)->type_name);
 68:   }
 69:   return(0);
 70: }

 72: #undef __FUNCT__  
 74: /*
 75:   SNESMatrixFreeMult2_Private - Default matrix-free form for Jacobian-vector
 76:   product, y = F'(u)*a:
 77:         y = (F(u + ha) - F(u)) /h, 
 78:   where F = nonlinear function, as set by SNESSetFunction()
 79:         u = current iterate
 80:         h = difference interval
 81: */
 82: int SNESMatrixFreeMult2_Private(Mat mat,Vec a,Vec y)
 83: {
 84:   MFCtx_Private *ctx;
 85:   SNES          snes;
 86:   PetscReal     h,norm,sum,umin,noise;
 87:   PetscScalar   hs,dot,mone = -1.0;
 88:   Vec           w,U,F;
 89:   int           ierr,iter,(*eval_fct)(SNES,Vec,Vec);
 90:   MPI_Comm      comm;


 94:   /* We log matrix-free matrix-vector products separately, so that we can
 95:      separate the performance monitoring from the cases that use conventional
 96:      storage.  We may eventually modify event logging to associate events
 97:      with particular objects, hence alleviating the more general problem. */
 98:   PetscLogEventBegin(MAT_MultMatrixFree,a,y,0,0);

100:   PetscObjectGetComm((PetscObject)mat,&comm);
101:   MatShellGetContext(mat,(void **)&ctx);
102:   snes = ctx->snes;
103:   w    = ctx->w;
104:   umin = ctx->umin;

106:   SNESGetSolution(snes,&U);
107:   eval_fct = SNESComputeFunction;
108:   SNESGetFunction(snes,&F,PETSC_NULL,PETSC_NULL);

110:   /* Determine a "good" step size, h */
111:   if (ctx->need_h) {

113:     /* Use Jorge's method to compute h */
114:     if (ctx->jorge) {
115:       DiffParameterCompute_More(snes,ctx->data,U,a,&noise,&h);

117:     /* Use the Brown/Saad method to compute h */
118:     } else {
119:       /* Compute error if desired */
120:       SNESGetIterationNumber(snes,&iter);
121:       if ((ctx->need_err) || ((ctx->compute_err_freq) && (ctx->compute_err_iter != iter) && (!((iter-1)%ctx->compute_err_freq)))) {
122:         /* Use Jorge's method to compute noise */
123:         DiffParameterCompute_More(snes,ctx->data,U,a,&noise,&h);
124:         ctx->error_rel = sqrt(noise);
125:         PetscLogInfo(snes,"SNESMatrixFreeMult2_Private: Using Jorge's noise: noise=%g, sqrt(noise)=%g, h_more=%gn",noise,ctx->error_rel,h);
126:         ctx->compute_err_iter = iter;
127:         ctx->need_err = 0;
128:       }

130:       VecDotBegin(U,a,&dot);
131:       VecNormBegin(a,NORM_1,&sum);
132:       VecNormBegin(a,NORM_2,&norm);
133:       VecDotEnd(U,a,&dot);
134:       VecNormEnd(a,NORM_1,&sum);
135:       VecNormEnd(a,NORM_2,&norm);


138:       /* Safeguard for step sizes too small */
139:       if (sum == 0.0) {dot = 1.0; norm = 1.0;}
140: #if defined(PETSC_USE_COMPLEX)
141:       else if (PetscAbsScalar(dot) < umin*sum && PetscRealPart(dot) >= 0.0) dot = umin*sum;
142:       else if (PetscAbsScalar(dot) < 0.0 && PetscRealPart(dot) > -umin*sum) dot = -umin*sum;
143: #else
144:       else if (dot < umin*sum && dot >= 0.0) dot = umin*sum;
145:       else if (dot < 0.0 && dot > -umin*sum) dot = -umin*sum;
146: #endif
147:       h = PetscRealPart(ctx->error_rel*dot/(norm*norm));
148:     }
149:   } else {
150:     h = ctx->h;
151:   }
152:   if (!ctx->jorge || !ctx->need_h) PetscLogInfo(snes,"SNESMatrixFreeMult2_Private: h = %gn",h);

154:   /* Evaluate function at F(u + ha) */
155:   hs = h;
156:   VecWAXPY(&hs,a,U,w);
157:   eval_fct(snes,w,y);
158:   VecAXPY(&mone,F,y);
159:   hs = 1.0/hs;
160:   VecScale(&hs,y);
161:   if (ctx->sp) {MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);}

163:   PetscLogEventEnd(MAT_MultMatrixFree,a,y,0,0);
164:   return(0);
165: }

167: #undef __FUNCT__  
169: /*@C
170:    SNESMatrixFreeMatCreate2 - Creates a matrix-free matrix
171:    context for use with a SNES solver.  This matrix can be used as
172:    the Jacobian argument for the routine SNESSetJacobian().

174:    Input Parameters:
175: .  snes - the SNES context
176: .  x - vector where SNES solution is to be stored.

178:    Output Parameter:
179: .  J - the matrix-free matrix

181:    Level: advanced

183:    Notes:
184:    The matrix-free matrix context merely contains the function pointers
185:    and work space for performing finite difference approximations of
186:    Jacobian-vector products, J(u)*a, via

188: $       J(u)*a = [J(u+h*a) - J(u)]/h,
189: $   where by default
190: $        h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
191: $          = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
192: $   where
193: $        error_rel = square root of relative error in
194: $                    function evaluation
195: $        umin = minimum iterate parameter
196: $   Alternatively, the differencing parameter, h, can be set using
197: $   Jorge's nifty new strategy if one specifies the option 
198: $          -snes_mf_jorge

200:    The user can set these parameters via MatSNESMFSetFunctionError().
201:    See the nonlinear solvers chapter of the users manual for details.

203:    The user should call MatDestroy() when finished with the matrix-free
204:    matrix context.

206:    Options Database Keys:
207: $  -snes_mf_err <error_rel>
208: $  -snes_mf_unim <umin>
209: $  -snes_mf_compute_err
210: $  -snes_mf_freq_err <freq>
211: $  -snes_mf_jorge

213: .keywords: SNES, default, matrix-free, create, matrix

215: .seealso: MatDestroy(), MatSNESMFSetFunctionError()
216: @*/
217: int SNESDefaultMatrixFreeCreate2(SNES snes,Vec x,Mat *J)
218: {
219:   MPI_Comm      comm;
220:   MFCtx_Private *mfctx;
221:   int           n,nloc,ierr;
222:   PetscTruth    flg;
223:   char          p[64];

226:   PetscNew(MFCtx_Private,&mfctx);
227:   ierr  = PetscMemzero(mfctx,sizeof(MFCtx_Private));
228:   PetscLogObjectMemory(snes,sizeof(MFCtx_Private));
229:   mfctx->sp   = 0;
230:   mfctx->snes = snes;
231:   mfctx->error_rel        = PETSC_SQRT_MACHINE_EPSILON;
232:   mfctx->umin             = 1.e-6;
233:   mfctx->h                = 0.0;
234:   mfctx->need_h           = 1;
235:   mfctx->need_err         = 0;
236:   mfctx->compute_err      = 0;
237:   mfctx->compute_err_freq = 0;
238:   mfctx->compute_err_iter = -1;
239:   PetscOptionsGetReal(snes->prefix,"-snes_mf_err",&mfctx->error_rel,PETSC_NULL);
240:   PetscOptionsGetReal(snes->prefix,"-snes_mf_umin",&mfctx->umin,PETSC_NULL);
241:   PetscOptionsHasName(snes->prefix,"-snes_mf_jorge",(PetscTruth*)&mfctx->jorge);
242:   PetscOptionsHasName(snes->prefix,"-snes_mf_compute_err",(PetscTruth*)&mfctx->compute_err);
243:   PetscOptionsGetInt(snes->prefix,"-snes_mf_freq_err",&mfctx->compute_err_freq,&flg);
244:   if (flg) {
245:     if (mfctx->compute_err_freq < 0) mfctx->compute_err_freq = 0;
246:     mfctx->compute_err = 1;
247:   }
248:   if (mfctx->compute_err == 1) mfctx->need_err = 1;
249:   if (mfctx->jorge || mfctx->compute_err) {
250:     DiffParameterCreate_More(snes,x,&mfctx->data);
251:   } else mfctx->data = 0;

253:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
254:   PetscStrcpy(p,"-");
255:   if (snes->prefix) PetscStrcat(p,snes->prefix);
256:   if (flg) {
257:     PetscPrintf(snes->comm," Matrix-free Options (via SNES):n");
258:     PetscPrintf(snes->comm,"   %ssnes_mf_err <err>: set sqrt of relative error in function (default %g)n",p,mfctx->error_rel);
259:     PetscPrintf(snes->comm,"   %ssnes_mf_umin <umin>: see users manual (default %g)n",p,mfctx->umin);
260:     PetscPrintf(snes->comm,"   %ssnes_mf_jorge: use Jorge More's methodn",p);
261:     PetscPrintf(snes->comm,"   %ssnes_mf_compute_err: compute sqrt or relative error in functionn",p);
262:     PetscPrintf(snes->comm,"   %ssnes_mf_freq_err <freq>: frequency to recompute this (default only once)n",p);
263:     PetscPrintf(snes->comm,"   %ssnes_mf_noise_file <file>: set file for printing noise infon",p);
264:   }
265:   VecDuplicate(x,&mfctx->w);
266:   PetscObjectGetComm((PetscObject)x,&comm);
267:   VecGetSize(x,&n);
268:   VecGetLocalSize(x,&nloc);
269:   MatCreateShell(comm,nloc,n,n,n,mfctx,J);
270:   MatShellSetOperation(*J,MATOP_MULT,(void(*)(void))SNESMatrixFreeMult2_Private);
271:   MatShellSetOperation(*J,MATOP_DESTROY,(void(*)(void))SNESMatrixFreeDestroy2_Private);
272:   MatShellSetOperation(*J,MATOP_VIEW,(void(*)(void))SNESMatrixFreeView2_Private);

274:   PetscLogObjectParent(*J,mfctx->w);
275:   PetscLogObjectParent(snes,*J);
276:   return(0);
277: }

279: #undef __FUNCT__  
281: /*@C
282:    SNESDefaultMatrixFreeSetParameters2 - Sets the parameters for the approximation of
283:    matrix-vector products using finite differences.

285: $       J(u)*a = [J(u+h*a) - J(u)]/h where

287:    either the user sets h directly here, or this parameter is computed via

289: $        h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
290: $          = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
291: $

293:    Input Parameters:
294: +  mat - the matrix
295: .  error_rel - relative error (should be set to the square root of
296:      the relative error in the function evaluations)
297: .  umin - minimum allowable u-value
298: -  h - differencing parameter

300:    Level: advanced

302:    Notes:
303:    If the user sets the parameter h directly, then this value will be used
304:    instead of the default computation indicated above.

306: .keywords: SNES, matrix-free, parameters

308: .seealso: MatCreateSNESMF()
309: @*/
310: int SNESDefaultMatrixFreeSetParameters2(Mat mat,PetscReal error,PetscReal umin,PetscReal h)
311: {
312:   MFCtx_Private *ctx;
313:   int           ierr;

316:   MatShellGetContext(mat,(void **)&ctx);
317:   if (ctx) {
318:     if (error != PETSC_DEFAULT) ctx->error_rel = error;
319:     if (umin  != PETSC_DEFAULT) ctx->umin = umin;
320:     if (h     != PETSC_DEFAULT) {
321:       ctx->h = h;
322:       ctx->need_h = 0;
323:     }
324:   }
325:   return(0);
326: }

328: int SNESUnSetMatrixFreeParameter(SNES snes)
329: {
330:   MFCtx_Private *ctx;
331:   int           ierr;
332:   Mat           mat;

335:   SNESGetJacobian(snes,&mat,PETSC_NULL,PETSC_NULL,PETSC_NULL);
336:   MatShellGetContext(mat,(void **)&ctx);
337:   if (ctx) ctx->need_h = 1;
338:   return(0);
339: }
340: