xref: /petsc/src/mat/matfd/fdmatrix.c (revision 0f5bd95cca42d693ada6d048329ab39533680180)
1 
2 #ifdef PETSC_RCS_HEADER
3 static char vcid[] = "$Id: fdmatrix.c,v 1.51 1999/10/01 21:21:44 bsmith Exp bsmith $";
4 #endif
5 
6 /*
7    This is where the abstract matrix operations are defined that are
8   used for finite difference computations of Jacobians using coloring.
9 */
10 
11 #include "petsc.h"
12 #include "src/mat/matimpl.h"        /*I "mat.h" I*/
13 #include "src/vec/vecimpl.h"
14 
15 #undef __FUNC__
16 #define __FUNC__ "MatFDColoringView_Draw"
17 static int MatFDColoringView_Draw(MatFDColoring fd,Viewer viewer)
18 {
19   int         ierr,i,j,pause;
20   PetscTruth  isnull;
21   Draw        draw;
22   double      xr,yr,xl,yl,h,w,x,y,xc,yc,scale = 0.0;
23   DrawButton  button;
24 
25   PetscFunctionBegin;
26   ierr = ViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
27   ierr = DrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
28   ierr = DrawSynchronizedClear(draw);CHKERRQ(ierr);
29 
30   xr  = fd->N; yr = fd->M; h = yr/10.0; w = xr/10.0;
31   xr += w;    yr += h;  xl = -w;     yl = -h;
32   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
33 
34   /* loop over colors  */
35   for (i=0; i<fd->ncolors; i++ ) {
36     for ( j=0; j<fd->nrows[i]; j++ ) {
37       y = fd->M - fd->rows[i][j] - fd->rstart;
38       x = fd->columnsforrow[i][j];
39       ierr = DrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1);CHKERRQ(ierr);
40     }
41   }
42   ierr = DrawSynchronizedFlush(draw);CHKERRQ(ierr);
43   ierr = DrawGetPause(draw,&pause);CHKERRQ(ierr);
44   if (pause >= 0) { PetscSleep(pause); PetscFunctionReturn(0);}
45   ierr = DrawCheckResizedWindow(draw);CHKERRQ(ierr);
46   ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0);CHKERRQ(ierr);
47   while (button != BUTTON_RIGHT) {
48     ierr = DrawSynchronizedClear(draw);CHKERRQ(ierr);
49     if (button == BUTTON_LEFT) scale = .5;
50     else if (button == BUTTON_CENTER) scale = 2.;
51     xl = scale*(xl + w - xc) + xc - w*scale;
52     xr = scale*(xr - w - xc) + xc + w*scale;
53     yl = scale*(yl + h - yc) + yc - h*scale;
54     yr = scale*(yr - h - yc) + yc + h*scale;
55     w *= scale; h *= scale;
56     ierr = DrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
57     /* loop over colors  */
58     for (i=0; i<fd->ncolors; i++ ) {
59       for ( j=0; j<fd->nrows[i]; j++ ) {
60         y = fd->M - fd->rows[i][j] - fd->rstart;
61         x = fd->columnsforrow[i][j];
62         ierr = DrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1);CHKERRQ(ierr);
63       }
64     }
65     ierr = DrawCheckResizedWindow(draw);CHKERRQ(ierr);
66     ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0);CHKERRQ(ierr);
67   }
68 
69   PetscFunctionReturn(0);
70 }
71 
72 #undef __FUNC__
73 #define __FUNC__ "MatFDColoringView"
74 /*@C
75    MatFDColoringView - Views a finite difference coloring context.
76 
77    Collective on MatFDColoring unless Viewer is VIEWER_STDOUT_SELF
78 
79    Input  Parameters:
80 +  c - the coloring context
81 -  viewer - visualization context
82 
83    Level: intermediate
84 
85    Notes:
86    The available visualization contexts include
87 +     VIEWER_STDOUT_SELF - standard output (default)
88 .     VIEWER_STDOUT_WORLD - synchronized standard
89         output where only the first processor opens
90         the file.  All other processors send their
91         data to the first processor to print.
92 -     VIEWER_DRAW_WORLD - graphical display of nonzero structure
93 
94 .seealso: MatFDColoringCreate()
95 
96 .keywords: Mat, finite differences, coloring, view
97 @*/
98 int MatFDColoringView(MatFDColoring c,Viewer viewer)
99 {
100   int        i,j,format,ierr;
101   int        isdraw,isascii;
102 
103   PetscFunctionBegin;
104   PetscValidHeaderSpecific(c,MAT_FDCOLORING_COOKIE);
105   if (!viewer) viewer = VIEWER_STDOUT_SELF;
106   PetscValidHeaderSpecific(viewer,VIEWER_COOKIE);
107 
108   isdraw  = PetscTypeCompare(viewer,DRAW_VIEWER);
109   isascii = PetscTypeCompare(viewer,ASCII_VIEWER);
110   if (isdraw) {
111     ierr = MatFDColoringView_Draw(c,viewer);CHKERRQ(ierr);
112   } else if (isascii) {
113     ierr = ViewerASCIIPrintf(viewer,"MatFDColoring Object:\n");CHKERRQ(ierr);
114     ierr = ViewerASCIIPrintf(viewer,"  Error tolerance=%g\n",c->error_rel);CHKERRQ(ierr);
115     ierr = ViewerASCIIPrintf(viewer,"  Umin=%g\n",c->umin);CHKERRQ(ierr);
116     ierr = ViewerASCIIPrintf(viewer,"  Number of colors=%d\n",c->ncolors);CHKERRQ(ierr);
117 
118     ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
119     if (format != VIEWER_FORMAT_ASCII_INFO) {
120       for ( i=0; i<c->ncolors; i++ ) {
121         ierr = ViewerASCIIPrintf(viewer,"  Information for color %d\n",i);CHKERRQ(ierr);
122         ierr = ViewerASCIIPrintf(viewer,"    Number of columns %d\n",c->ncolumns[i]);CHKERRQ(ierr);
123         for ( j=0; j<c->ncolumns[i]; j++ ) {
124           ierr = ViewerASCIIPrintf(viewer,"      %d\n",c->columns[i][j]);CHKERRQ(ierr);
125         }
126         ierr = ViewerASCIIPrintf(viewer,"    Number of rows %d\n",c->nrows[i]);CHKERRQ(ierr);
127         for ( j=0; j<c->nrows[i]; j++ ) {
128           ierr = ViewerASCIIPrintf(viewer,"      %d %d \n",c->rows[i][j],c->columnsforrow[i][j]);CHKERRQ(ierr);
129         }
130       }
131     }
132   } else {
133     SETERRQ1(1,1,"Viewer type %s not supported for MatFDColoring",((PetscObject)viewer)->type_name);
134   }
135   PetscFunctionReturn(0);
136 }
137 
138 #undef __FUNC__
139 #define __FUNC__ "MatFDColoringSetParameters"
140 /*@
141    MatFDColoringSetParameters - Sets the parameters for the sparse approximation of
142    a Jacobian matrix using finite differences.
143 
144    Collective on MatFDColoring
145 
146    The Jacobian is estimated with the differencing approximation
147 .vb
148        F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where
149        h = error_rel*u[i]                 if  abs(u[i]) > umin
150          = +/- error_rel*umin             otherwise, with +/- determined by the sign of u[i]
151        dx_{i} = (0, ... 1, .... 0)
152 .ve
153 
154    Input Parameters:
155 +  coloring - the coloring context
156 .  error_rel - relative error
157 -  umin - minimum allowable u-value magnitude
158 
159    Level: advanced
160 
161 .keywords: Mat, finite differences, coloring, set, parameters
162 
163 .seealso: MatFDColoringCreate()
164 @*/
165 int MatFDColoringSetParameters(MatFDColoring matfd,double error,double umin)
166 {
167   PetscFunctionBegin;
168   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
169 
170   if (error != PETSC_DEFAULT) matfd->error_rel = error;
171   if (umin != PETSC_DEFAULT)  matfd->umin      = umin;
172   PetscFunctionReturn(0);
173 }
174 
175 #undef __FUNC__
176 #define __FUNC__ "MatFDColoringSetFrequency"
177 /*@
178    MatFDColoringSetFrequency - Sets the frequency for computing new Jacobian
179    matrices.
180 
181    Collective on MatFDColoring
182 
183    Input Parameters:
184 +  coloring - the coloring context
185 -  freq - frequency (default is 1)
186 
187    Options Database Keys:
188 .  -mat_fd_coloring_freq <freq>  - Sets coloring frequency
189 
190    Level: advanced
191 
192    Notes:
193    Using a modified Newton strategy, where the Jacobian remains fixed for several
194    iterations, can be cost effective in terms of overall nonlinear solution
195    efficiency.  This parameter indicates that a new Jacobian will be computed every
196    <freq> nonlinear iterations.
197 
198 .keywords: Mat, finite differences, coloring, set, frequency
199 
200 .seealso: MatFDColoringCreate(), MatFDColoringGetFrequency()
201 @*/
202 int MatFDColoringSetFrequency(MatFDColoring matfd,int freq)
203 {
204   PetscFunctionBegin;
205   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
206 
207   matfd->freq = freq;
208   PetscFunctionReturn(0);
209 }
210 
211 #undef __FUNC__
212 #define __FUNC__ "MatFDColoringGetFrequency"
213 /*@
214    MatFDColoringGetFrequency - Gets the frequency for computing new Jacobian
215    matrices.
216 
217    Not Collective
218 
219    Input Parameters:
220 .  coloring - the coloring context
221 
222    Output Parameters:
223 .  freq - frequency (default is 1)
224 
225    Options Database Keys:
226 .  -mat_fd_coloring_freq <freq> - Sets coloring frequency
227 
228    Level: advanced
229 
230    Notes:
231    Using a modified Newton strategy, where the Jacobian remains fixed for several
232    iterations, can be cost effective in terms of overall nonlinear solution
233    efficiency.  This parameter indicates that a new Jacobian will be computed every
234    <freq> nonlinear iterations.
235 
236 .keywords: Mat, finite differences, coloring, get, frequency
237 
238 .seealso: MatFDColoringSetFrequency()
239 @*/
240 int MatFDColoringGetFrequency(MatFDColoring matfd,int *freq)
241 {
242   PetscFunctionBegin;
243   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
244 
245   *freq = matfd->freq;
246   PetscFunctionReturn(0);
247 }
248 
249 #undef __FUNC__
250 #define __FUNC__ "MatFDColoringSetFunction"
251 /*@C
252    MatFDColoringSetFunction - Sets the function to use for computing the Jacobian.
253 
254    Collective on MatFDColoring
255 
256    Input Parameters:
257 +  coloring - the coloring context
258 .  f - the function
259 -  fctx - the optional user-defined function context
260 
261    Level: intermediate
262 
263    Notes:
264     In Fortran you must call MatFDColoringSetFunctionSNES() for a coloring object to
265   be used with the SNES solvers and MatFDColoringSetFunctionTS() if it is to be used
266   with the TS solvers.
267 
268 .keywords: Mat, Jacobian, finite differences, set, function
269 @*/
270 int MatFDColoringSetFunction(MatFDColoring matfd,int (*f)(void),void *fctx)
271 {
272   PetscFunctionBegin;
273   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
274 
275   matfd->f    = f;
276   matfd->fctx = fctx;
277 
278   PetscFunctionReturn(0);
279 }
280 
281 #undef __FUNC__
282 #define __FUNC__ "MatFDColoringSetFromOptions"
283 /*@
284    MatFDColoringSetFromOptions - Sets coloring finite difference parameters from
285    the options database.
286 
287    Collective on MatFDColoring
288 
289    The Jacobian, F'(u), is estimated with the differencing approximation
290 .vb
291        F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where
292        h = error_rel*u[i]                 if  abs(u[i]) > umin
293          = +/- error_rel*umin             otherwise, with +/- determined by the sign of u[i]
294        dx_{i} = (0, ... 1, .... 0)
295 .ve
296 
297    Input Parameter:
298 .  coloring - the coloring context
299 
300    Options Database Keys:
301 +  -mat_fd_coloring_error <err> - Sets <err> (square root
302            of relative error in the function)
303 .  -mat_fd_coloring_umin <umin> - Sets umin, the minimum allowable u-value magnitude
304 .  -mat_fd_coloring_freq <freq> - Sets frequency of computing a new Jacobian
305 .  -mat_fd_coloring_view - Activates basic viewing
306 .  -mat_fd_coloring_view_info - Activates viewing info
307 -  -mat_fd_coloring_view_draw - Activates drawing
308 
309     Level: intermediate
310 
311 .keywords: Mat, finite differences, parameters
312 @*/
313 int MatFDColoringSetFromOptions(MatFDColoring matfd)
314 {
315   int    ierr,flag,freq = 1;
316   double error = PETSC_DEFAULT,umin = PETSC_DEFAULT;
317 
318   PetscFunctionBegin;
319   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
320 
321   ierr = OptionsGetDouble(matfd->prefix,"-mat_fd_coloring_err",&error,&flag);CHKERRQ(ierr);
322   ierr = OptionsGetDouble(matfd->prefix,"-mat_fd_coloring_umin",&umin,&flag);CHKERRQ(ierr);
323   ierr = MatFDColoringSetParameters(matfd,error,umin);CHKERRQ(ierr);
324   ierr = OptionsGetInt(matfd->prefix,"-mat_fd_coloring_freq",&freq,&flag);CHKERRQ(ierr);
325   ierr = MatFDColoringSetFrequency(matfd,freq);CHKERRQ(ierr);
326   ierr = OptionsHasName(PETSC_NULL,"-help",&flag);CHKERRQ(ierr);
327   if (flag) {
328     ierr = MatFDColoringPrintHelp(matfd);CHKERRQ(ierr);
329   }
330   PetscFunctionReturn(0);
331 }
332 
333 #undef __FUNC__
334 #define __FUNC__ "MatFDColoringPrintHelp"
335 /*@
336     MatFDColoringPrintHelp - Prints help message for matrix finite difference calculations
337     using coloring.
338 
339     Collective on MatFDColoring
340 
341     Input Parameter:
342 .   fdcoloring - the MatFDColoring context
343 
344     Level: intermediate
345 
346 .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringSetFromOptions()
347 @*/
348 int MatFDColoringPrintHelp(MatFDColoring fd)
349 {
350   int ierr;
351 
352   PetscFunctionBegin;
353   PetscValidHeaderSpecific(fd,MAT_FDCOLORING_COOKIE);
354 
355   ierr = (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_err <err>: set sqrt rel tol in function, defaults to %g\n",fd->error_rel);CHKERRQ(ierr);
356   ierr = (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_umin <umin>: see users manual, defaults to %d\n",fd->umin);CHKERRQ(ierr);
357   ierr = (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_freq <freq>: frequency that Jacobian is recomputed, defaults to %d\n",fd->freq);CHKERRQ(ierr);
358   ierr = (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view\n");CHKERRQ(ierr);
359   ierr = (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view_draw\n");CHKERRQ(ierr);
360   ierr = (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view_info\n");CHKERRQ(ierr);
361   PetscFunctionReturn(0);
362 }
363 
364 #undef __FUNC__
365 #define __FUNC__ "MatFDColoringView_Private"
366 int MatFDColoringView_Private(MatFDColoring fd)
367 {
368   int ierr,flg;
369 
370   PetscFunctionBegin;
371   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view",&flg);CHKERRQ(ierr);
372   if (flg) {
373     ierr = MatFDColoringView(fd,VIEWER_STDOUT_(fd->comm));CHKERRQ(ierr);
374   }
375   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view_info",&flg);CHKERRQ(ierr);
376   if (flg) {
377     ierr = ViewerPushFormat(VIEWER_STDOUT_(fd->comm),VIEWER_FORMAT_ASCII_INFO,PETSC_NULL);CHKERRQ(ierr);
378     ierr = MatFDColoringView(fd,VIEWER_STDOUT_(fd->comm));CHKERRQ(ierr);
379     ierr = ViewerPopFormat(VIEWER_STDOUT_(fd->comm));CHKERRQ(ierr);
380   }
381   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view_draw",&flg);CHKERRQ(ierr);
382   if (flg) {
383     ierr = MatFDColoringView(fd,VIEWER_DRAW_(fd->comm));CHKERRQ(ierr);
384     ierr = ViewerFlush(VIEWER_DRAW_(fd->comm));CHKERRQ(ierr);
385   }
386   PetscFunctionReturn(0);
387 }
388 
389 #undef __FUNC__
390 #define __FUNC__ "MatFDColoringCreate"
391 /*@C
392    MatFDColoringCreate - Creates a matrix coloring context for finite difference
393    computation of Jacobians.
394 
395    Collective on Mat
396 
397    Input Parameters:
398 +  mat - the matrix containing the nonzero structure of the Jacobian
399 -  iscoloring - the coloring of the matrix
400 
401     Output Parameter:
402 .   color - the new coloring context
403 
404     Options Database Keys:
405 +    -mat_fd_coloring_view - Activates basic viewing or coloring
406 .    -mat_fd_coloring_view_draw - Activates drawing of coloring
407 -    -mat_fd_coloring_view_info - Activates viewing of coloring info
408 
409     Level: intermediate
410 
411 .seealso: MatFDColoringDestroy()
412 @*/
413 int MatFDColoringCreate(Mat mat,ISColoring iscoloring,MatFDColoring *color)
414 {
415   MatFDColoring c;
416   MPI_Comm      comm;
417   int           ierr,M,N;
418 
419   PetscFunctionBegin;
420   ierr = MatGetSize(mat,&M,&N);CHKERRQ(ierr);
421   if (M != N) SETERRQ(PETSC_ERR_SUP,0,"Only for square matrices");
422 
423   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
424   PetscHeaderCreate(c,_p_MatFDColoring,int,MAT_FDCOLORING_COOKIE,0,"MatFDColoring",comm,
425                     MatFDColoringDestroy,MatFDColoringView);
426   PLogObjectCreate(c);
427 
428   if (mat->ops->fdcoloringcreate) {
429     ierr = (*mat->ops->fdcoloringcreate)(mat,iscoloring,c);CHKERRQ(ierr);
430   } else {
431     SETERRQ(PETSC_ERR_SUP,0,"Code not yet written for this matrix type");
432   }
433 
434   c->error_rel = 1.e-8;
435   c->umin      = 1.e-6;
436   c->freq      = 1;
437 
438   ierr = MatFDColoringView_Private(c);CHKERRQ(ierr);
439 
440   *color = c;
441 
442   PetscFunctionReturn(0);
443 }
444 
445 #undef __FUNC__
446 #define __FUNC__ "MatFDColoringDestroy"
447 /*@C
448     MatFDColoringDestroy - Destroys a matrix coloring context that was created
449     via MatFDColoringCreate().
450 
451     Collective on MatFDColoring
452 
453     Input Parameter:
454 .   c - coloring context
455 
456     Level: intermediate
457 
458 .seealso: MatFDColoringCreate()
459 @*/
460 int MatFDColoringDestroy(MatFDColoring c)
461 {
462   int i,ierr;
463 
464   PetscFunctionBegin;
465   if (--c->refct > 0) PetscFunctionReturn(0);
466 
467 
468   for ( i=0; i<c->ncolors; i++ ) {
469     if (c->columns[i])       {ierr = PetscFree(c->columns[i]);CHKERRQ(ierr);}
470     if (c->rows[i])          {ierr = PetscFree(c->rows[i]);CHKERRQ(ierr);}
471     if (c->columnsforrow[i]) {ierr = PetscFree(c->columnsforrow[i]);CHKERRQ(ierr);}
472   }
473   ierr = PetscFree(c->ncolumns);CHKERRQ(ierr);
474   ierr = PetscFree(c->columns);CHKERRQ(ierr);
475   ierr = PetscFree(c->nrows);CHKERRQ(ierr);
476   ierr = PetscFree(c->rows);CHKERRQ(ierr);
477   ierr = PetscFree(c->columnsforrow);CHKERRQ(ierr);
478   ierr = PetscFree(c->scale);CHKERRQ(ierr);
479   if (c->w1) {
480     ierr = VecDestroy(c->w1);CHKERRQ(ierr);
481     ierr = VecDestroy(c->w2);CHKERRQ(ierr);
482     ierr = VecDestroy(c->w3);CHKERRQ(ierr);
483   }
484   PLogObjectDestroy(c);
485   PetscHeaderDestroy(c);
486   PetscFunctionReturn(0);
487 }
488 
489 
490 #undef __FUNC__
491 #define __FUNC__ "MatFDColoringApply"
492 /*@
493     MatFDColoringApply - Given a matrix for which a MatFDColoring context
494     has been created, computes the Jacobian for a function via finite differences.
495 
496     Collective on MatFDColoring
497 
498     Input Parameters:
499 +   mat - location to store Jacobian
500 .   coloring - coloring context created with MatFDColoringCreate()
501 .   x1 - location at which Jacobian is to be computed
502 -   sctx - optional context required by function (actually a SNES context)
503 
504    Options Database Keys:
505 .  -mat_fd_coloring_freq <freq> - Sets coloring frequency
506 
507    Level: intermediate
508 
509 .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView()
510 
511 .keywords: coloring, Jacobian, finite differences
512 @*/
513 int MatFDColoringApply(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
514 {
515   int           k,fg,ierr,N,start,end,l,row,col,srow;
516   Scalar        dx, mone = -1.0,*y,*scale = coloring->scale,*xx,*wscale = coloring->wscale;
517   double        epsilon = coloring->error_rel, umin = coloring->umin;
518   MPI_Comm      comm = coloring->comm;
519   Vec           w1,w2,w3;
520   int           (*f)(void *,Vec,Vec,void *) = ( int (*)(void *,Vec,Vec,void *))coloring->f;
521   void          *fctx = coloring->fctx;
522 
523   PetscFunctionBegin;
524   PetscValidHeaderSpecific(J,MAT_COOKIE);
525   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_COOKIE);
526   PetscValidHeaderSpecific(x1,VEC_COOKIE);
527 
528 
529   if (!coloring->w1) {
530     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
531     PLogObjectParent(coloring,coloring->w1);
532     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
533     PLogObjectParent(coloring,coloring->w2);
534     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
535     PLogObjectParent(coloring,coloring->w3);
536   }
537   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
538 
539   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&fg);CHKERRQ(ierr);
540   if (fg) {
541     PLogInfo(coloring,"MatFDColoringApply: Not calling MatZeroEntries()\n");
542   } else {
543     ierr = MatZeroEntries(J);CHKERRQ(ierr);
544   }
545 
546   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
547   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
548   ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
549 
550   ierr = PetscMemzero(wscale,N*sizeof(Scalar));CHKERRQ(ierr);
551   /*
552       Loop over each color
553   */
554 
555   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);
556   for (k=0; k<coloring->ncolors; k++) {
557     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
558     /*
559        Loop over each column associated with color adding the
560        perturbation to the vector w3.
561     */
562     for (l=0; l<coloring->ncolumns[k]; l++) {
563       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
564       dx  = xx[col-start];
565       if (dx == 0.0) dx = 1.0;
566 #if !defined(PETSC_USE_COMPLEX)
567       if (dx < umin && dx >= 0.0)      dx = umin;
568       else if (dx < 0.0 && dx > -umin) dx = -umin;
569 #else
570       if (PetscAbsScalar(dx) < umin && PetscReal(dx) >= 0.0)     dx = umin;
571       else if (PetscReal(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
572 #endif
573       dx          *= epsilon;
574       wscale[col] = 1.0/dx;
575       ierr = VecSetValues(w3,1,&col,&dx,ADD_VALUES);CHKERRQ(ierr);
576     }
577 
578     /*
579        Evaluate function at x1 + dx (here dx is a vector of perturbations)
580     */
581     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
582     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
583     /* Communicate scale to all processors */
584 #if !defined(PETSC_USE_COMPLEX)
585     ierr = MPI_Allreduce(wscale,scale,N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
586 #else
587     ierr = MPI_Allreduce(wscale,scale,2*N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
588 #endif
589     /*
590        Loop over rows of vector, putting results into Jacobian matrix
591     */
592     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
593     for (l=0; l<coloring->nrows[k]; l++) {
594       row    = coloring->rows[k][l];
595       col    = coloring->columnsforrow[k][l];
596       y[row] *= scale[col];
597       srow   = row + start;
598       ierr   = MatSetValues(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
599     }
600     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
601   }
602   ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
603   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
604   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
605   PetscFunctionReturn(0);
606 }
607 
608 #undef __FUNC__
609 #define __FUNC__ "MatFDColoringApplyTS"
610 /*@
611     MatFDColoringApplyTS - Given a matrix for which a MatFDColoring context
612     has been created, computes the Jacobian for a function via finite differences.
613 
614    Collective on Mat, MatFDColoring, and Vec
615 
616     Input Parameters:
617 +   mat - location to store Jacobian
618 .   coloring - coloring context created with MatFDColoringCreate()
619 .   x1 - location at which Jacobian is to be computed
620 -   sctx - optional context required by function (actually a SNES context)
621 
622    Options Database Keys:
623 .  -mat_fd_coloring_freq <freq> - Sets coloring frequency
624 
625    Level: intermediate
626 
627 .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView()
628 
629 .keywords: coloring, Jacobian, finite differences
630 @*/
631 int MatFDColoringApplyTS(Mat J,MatFDColoring coloring,double t,Vec x1,MatStructure *flag,void *sctx)
632 {
633   int           k,fg,ierr,N,start,end,l,row,col,srow;
634   Scalar        dx, mone = -1.0,*y,*scale = coloring->scale,*xx,*wscale = coloring->wscale;
635   double        epsilon = coloring->error_rel, umin = coloring->umin;
636   MPI_Comm      comm = coloring->comm;
637   Vec           w1,w2,w3;
638   int           (*f)(void *,double,Vec,Vec,void *) = ( int (*)(void *,double,Vec,Vec,void *))coloring->f;
639   void          *fctx = coloring->fctx;
640 
641   PetscFunctionBegin;
642   PetscValidHeaderSpecific(J,MAT_COOKIE);
643   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_COOKIE);
644   PetscValidHeaderSpecific(x1,VEC_COOKIE);
645 
646   if (!coloring->w1) {
647     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
648     PLogObjectParent(coloring,coloring->w1);
649     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
650     PLogObjectParent(coloring,coloring->w2);
651     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
652     PLogObjectParent(coloring,coloring->w3);
653   }
654   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
655 
656   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&fg);CHKERRQ(ierr);
657   if (fg) {
658     PLogInfo(coloring,"MatFDColoringApplyTS: Not calling MatZeroEntries()\n");
659   } else {
660     ierr = MatZeroEntries(J);CHKERRQ(ierr);
661   }
662 
663   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
664   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
665   ierr = (*f)(sctx,t,x1,w1,fctx);CHKERRQ(ierr);
666 
667   ierr = PetscMemzero(wscale,N*sizeof(Scalar));CHKERRQ(ierr);
668   /*
669       Loop over each color
670   */
671 
672   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);
673   for (k=0; k<coloring->ncolors; k++) {
674     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
675     /*
676        Loop over each column associated with color adding the
677        perturbation to the vector w3.
678     */
679     for (l=0; l<coloring->ncolumns[k]; l++) {
680       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
681       dx  = xx[col-start];
682       if (dx == 0.0) dx = 1.0;
683 #if !defined(PETSC_USE_COMPLEX)
684       if (dx < umin && dx >= 0.0)      dx = umin;
685       else if (dx < 0.0 && dx > -umin) dx = -umin;
686 #else
687       if (PetscAbsScalar(dx) < umin && PetscReal(dx) >= 0.0)     dx = umin;
688       else if (PetscReal(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
689 #endif
690       dx          *= epsilon;
691       wscale[col] = 1.0/dx;
692       ierr = VecSetValues(w3,1,&col,&dx,ADD_VALUES);CHKERRQ(ierr);
693     }
694     /*
695        Evaluate function at x1 + dx (here dx is a vector of perturbations)
696     */
697     ierr = (*f)(sctx,t,w3,w2,fctx);CHKERRQ(ierr);
698     ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr);
699     /* Communicate scale to all processors */
700 #if !defined(PETSC_USE_COMPLEX)
701     ierr = MPI_Allreduce(wscale,scale,N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
702 #else
703     ierr = MPI_Allreduce(wscale,scale,2*N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
704 #endif
705     /*
706        Loop over rows of vector, putting results into Jacobian matrix
707     */
708     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
709     for (l=0; l<coloring->nrows[k]; l++) {
710       row    = coloring->rows[k][l];
711       col    = coloring->columnsforrow[k][l];
712       y[row] *= scale[col];
713       srow   = row + start;
714       ierr   = MatSetValues(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
715     }
716     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
717   }
718   ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
719   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
720   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
721   PetscFunctionReturn(0);
722 }
723 
724 
725 
726