xref: /petsc/src/mat/matfd/fdmatrix.c (revision 65f2ba5b4a48bc2ffe646adabb0c3d6c70b2f427)
1840b8ebdSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*65f2ba5bSLois Curfman McInnes static char vcid[] = "$Id: fdmatrix.c,v 1.43 1999/03/17 23:23:40 bsmith Exp curfman $";
4bbf0e169SBarry Smith #endif
5bbf0e169SBarry Smith 
6bbf0e169SBarry Smith /*
7639f9d9dSBarry Smith    This is where the abstract matrix operations are defined that are
8639f9d9dSBarry Smith   used for finite difference computations of Jacobians using coloring.
9bbf0e169SBarry Smith */
10bbf0e169SBarry Smith 
11bbf0e169SBarry Smith #include "petsc.h"
12bbf0e169SBarry Smith #include "src/mat/matimpl.h"        /*I "mat.h" I*/
13bbf0e169SBarry Smith #include "src/vec/vecimpl.h"
14bbf0e169SBarry Smith 
155615d1e5SSatish Balay #undef __FUNC__
16d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringView_Draw"
17005c665bSBarry Smith static int MatFDColoringView_Draw(MatFDColoring fd,Viewer viewer)
18005c665bSBarry Smith {
19005c665bSBarry Smith   int         ierr,i,j,pause;
20005c665bSBarry Smith   PetscTruth  isnull;
21005c665bSBarry Smith   Draw        draw;
22d4bb536fSBarry Smith   double      xr,yr,xl,yl,h,w,x,y,xc,yc,scale = 0.0;
23005c665bSBarry Smith   DrawButton  button;
24005c665bSBarry Smith 
253a40ed3dSBarry Smith   PetscFunctionBegin;
2677ed5343SBarry Smith   ierr = ViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr);
273a40ed3dSBarry Smith   ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
282bdab257SBarry Smith   ierr = DrawSynchronizedClear(draw); CHKERRQ(ierr);
29005c665bSBarry Smith 
30005c665bSBarry Smith   xr  = fd->N; yr = fd->M; h = yr/10.0; w = xr/10.0;
31005c665bSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
32005c665bSBarry Smith   ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr);
33005c665bSBarry Smith 
34005c665bSBarry Smith   /* loop over colors  */
35005c665bSBarry Smith   for (i=0; i<fd->ncolors; i++ ) {
36005c665bSBarry Smith     for ( j=0; j<fd->nrows[i]; j++ ) {
37005c665bSBarry Smith       y = fd->M - fd->rows[i][j] - fd->rstart;
38005c665bSBarry Smith       x = fd->columnsforrow[i][j];
395cd90555SBarry Smith       ierr = DrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1); CHKERRQ(ierr);
40005c665bSBarry Smith     }
41005c665bSBarry Smith   }
422bdab257SBarry Smith   ierr = DrawSynchronizedFlush(draw); CHKERRQ(ierr);
43005c665bSBarry Smith   ierr = DrawGetPause(draw,&pause); CHKERRQ(ierr);
443a40ed3dSBarry Smith   if (pause >= 0) { PetscSleep(pause); PetscFunctionReturn(0);}
455cd90555SBarry Smith   ierr = DrawCheckResizedWindow(draw); CHKERRQ(ierr);
465cd90555SBarry Smith   ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0);  CHKERRQ(ierr);
47005c665bSBarry Smith   while (button != BUTTON_RIGHT) {
482bdab257SBarry Smith     ierr = DrawSynchronizedClear(draw); CHKERRQ(ierr);
49005c665bSBarry Smith     if (button == BUTTON_LEFT) scale = .5;
50005c665bSBarry Smith     else if (button == BUTTON_CENTER) scale = 2.;
51005c665bSBarry Smith     xl = scale*(xl + w - xc) + xc - w*scale;
52005c665bSBarry Smith     xr = scale*(xr - w - xc) + xc + w*scale;
53005c665bSBarry Smith     yl = scale*(yl + h - yc) + yc - h*scale;
54005c665bSBarry Smith     yr = scale*(yr - h - yc) + yc + h*scale;
55005c665bSBarry Smith     w *= scale; h *= scale;
56005c665bSBarry Smith     ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr);
57005c665bSBarry Smith     /* loop over colors  */
58005c665bSBarry Smith     for (i=0; i<fd->ncolors; i++ ) {
59005c665bSBarry Smith       for ( j=0; j<fd->nrows[i]; j++ ) {
60005c665bSBarry Smith         y = fd->M - fd->rows[i][j] - fd->rstart;
61005c665bSBarry Smith         x = fd->columnsforrow[i][j];
625cd90555SBarry Smith         ierr = DrawRectangle(draw,x,y,x+1,y+1,i+1,i+1,i+1,i+1); CHKERRQ(ierr);
63005c665bSBarry Smith       }
64005c665bSBarry Smith     }
655cd90555SBarry Smith     ierr = DrawCheckResizedWindow(draw); CHKERRQ(ierr);
665cd90555SBarry Smith     ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0);  CHKERRQ(ierr);
67005c665bSBarry Smith   }
68005c665bSBarry Smith 
693a40ed3dSBarry Smith   PetscFunctionReturn(0);
70005c665bSBarry Smith }
71005c665bSBarry Smith 
72005c665bSBarry Smith #undef __FUNC__
73d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringView"
74bbf0e169SBarry Smith /*@C
75639f9d9dSBarry Smith    MatFDColoringView - Views a finite difference coloring context.
76bbf0e169SBarry Smith 
77fee21e36SBarry Smith    Collective on MatFDColoring unless Viewer is VIEWER_STDOUT_SELF
78fee21e36SBarry Smith 
79ef5ee4d1SLois Curfman McInnes    Input  Parameters:
80ef5ee4d1SLois Curfman McInnes +  c - the coloring context
81ef5ee4d1SLois Curfman McInnes -  viewer - visualization context
82ef5ee4d1SLois Curfman McInnes 
8315091d37SBarry Smith    Level: intermediate
8415091d37SBarry Smith 
85b4fc646aSLois Curfman McInnes    Notes:
86b4fc646aSLois Curfman McInnes    The available visualization contexts include
87ef5ee4d1SLois Curfman McInnes +     VIEWER_STDOUT_SELF - standard output (default)
88ef5ee4d1SLois Curfman McInnes .     VIEWER_STDOUT_WORLD - synchronized standard
89ef5ee4d1SLois Curfman McInnes         output where only the first processor opens
90ef5ee4d1SLois Curfman McInnes         the file.  All other processors send their
91ef5ee4d1SLois Curfman McInnes         data to the first processor to print.
92c655490fSBarry Smith -     VIEWER_DRAW_WORLD - graphical display of nonzero structure
93bbf0e169SBarry Smith 
94639f9d9dSBarry Smith .seealso: MatFDColoringCreate()
95005c665bSBarry Smith 
96b4fc646aSLois Curfman McInnes .keywords: Mat, finite differences, coloring, view
97bbf0e169SBarry Smith @*/
98b4fc646aSLois Curfman McInnes int MatFDColoringView(MatFDColoring c,Viewer viewer)
99bbf0e169SBarry Smith {
100005c665bSBarry Smith   ViewerType vtype;
101639f9d9dSBarry Smith   int        i,j,format,ierr;
102bbf0e169SBarry Smith 
1033a40ed3dSBarry Smith   PetscFunctionBegin;
104b4fc646aSLois Curfman McInnes   PetscValidHeaderSpecific(c,MAT_FDCOLORING_COOKIE);
105b4fc646aSLois Curfman McInnes   if (viewer) {PetscValidHeader(viewer);}
106b4fc646aSLois Curfman McInnes   else {viewer = VIEWER_STDOUT_SELF;}
107bbf0e169SBarry Smith 
108005c665bSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
1093f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,DRAW_VIEWER)) {
110b4fc646aSLois Curfman McInnes     ierr = MatFDColoringView_Draw(c,viewer); CHKERRQ(ierr);
1113a40ed3dSBarry Smith     PetscFunctionReturn(0);
1123f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
1130ef38995SBarry Smith     ViewerASCIIPrintf(viewer,"MatFDColoring Object:\n");
1140ef38995SBarry Smith     ViewerASCIIPrintf(viewer,"  Error tolerance=%g\n",c->error_rel);
1150ef38995SBarry Smith     ViewerASCIIPrintf(viewer,"  Umin=%g\n",c->umin);
1160ef38995SBarry Smith     ViewerASCIIPrintf(viewer,"  Number of colors=%d\n",c->ncolors);
117ae09f205SBarry Smith 
118ae09f205SBarry Smith     ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr);
119ae09f205SBarry Smith     if (format != VIEWER_FORMAT_ASCII_INFO) {
120b4fc646aSLois Curfman McInnes       for ( i=0; i<c->ncolors; i++ ) {
1210ef38995SBarry Smith         ViewerASCIIPrintf(viewer,"  Information for color %d\n",i);
1220ef38995SBarry Smith         ViewerASCIIPrintf(viewer,"    Number of columns %d\n",c->ncolumns[i]);
123b4fc646aSLois Curfman McInnes         for ( j=0; j<c->ncolumns[i]; j++ ) {
1240ef38995SBarry Smith           ViewerASCIIPrintf(viewer,"      %d\n",c->columns[i][j]);
125639f9d9dSBarry Smith         }
1260ef38995SBarry Smith         ViewerASCIIPrintf(viewer,"    Number of rows %d\n",c->nrows[i]);
127b4fc646aSLois Curfman McInnes         for ( j=0; j<c->nrows[i]; j++ ) {
1280ef38995SBarry Smith           ViewerASCIIPrintf(viewer,"      %d %d \n",c->rows[i][j],c->columnsforrow[i][j]);
129b4fc646aSLois Curfman McInnes         }
130bbf0e169SBarry Smith       }
131bbf0e169SBarry Smith     }
1325cd90555SBarry Smith   } else {
1335cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported for this object");
134bbf0e169SBarry Smith   }
1353a40ed3dSBarry Smith   PetscFunctionReturn(0);
136639f9d9dSBarry Smith }
137639f9d9dSBarry Smith 
1385615d1e5SSatish Balay #undef __FUNC__
1395615d1e5SSatish Balay #define __FUNC__ "MatFDColoringSetParameters"
140639f9d9dSBarry Smith /*@
141b4fc646aSLois Curfman McInnes    MatFDColoringSetParameters - Sets the parameters for the sparse approximation of
142b4fc646aSLois Curfman McInnes    a Jacobian matrix using finite differences.
143639f9d9dSBarry Smith 
144ef5ee4d1SLois Curfman McInnes    Collective on MatFDColoring
145ef5ee4d1SLois Curfman McInnes 
146ef5ee4d1SLois Curfman McInnes    The Jacobian is estimated with the differencing approximation
147ef5ee4d1SLois Curfman McInnes .vb
148*65f2ba5bSLois Curfman McInnes        F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where
149f23b5b22SLois Curfman McInnes        h = error_rel*u[i]                 if  abs(u[i]) > umin
150f23b5b22SLois Curfman McInnes          = +/- error_rel*umin             otherwise, with +/- determined by the sign of u[i]
151ef5ee4d1SLois Curfman McInnes        dx_{i} = (0, ... 1, .... 0)
152ef5ee4d1SLois Curfman McInnes .ve
153639f9d9dSBarry Smith 
154639f9d9dSBarry Smith    Input Parameters:
155ef5ee4d1SLois Curfman McInnes +  coloring - the coloring context
156639f9d9dSBarry Smith .  error_rel - relative error
157f23b5b22SLois Curfman McInnes -  umin - minimum allowable u-value magnitude
158fee21e36SBarry Smith 
15915091d37SBarry Smith    Level: advanced
16015091d37SBarry Smith 
161b4fc646aSLois Curfman McInnes .keywords: Mat, finite differences, coloring, set, parameters
162b4fc646aSLois Curfman McInnes 
163b4fc646aSLois Curfman McInnes .seealso: MatFDColoringCreate()
164639f9d9dSBarry Smith @*/
165639f9d9dSBarry Smith int MatFDColoringSetParameters(MatFDColoring matfd,double error,double umin)
166639f9d9dSBarry Smith {
1673a40ed3dSBarry Smith   PetscFunctionBegin;
168639f9d9dSBarry Smith   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
169639f9d9dSBarry Smith 
170639f9d9dSBarry Smith   if (error != PETSC_DEFAULT) matfd->error_rel = error;
171639f9d9dSBarry Smith   if (umin != PETSC_DEFAULT)  matfd->umin      = umin;
1723a40ed3dSBarry Smith   PetscFunctionReturn(0);
173639f9d9dSBarry Smith }
174639f9d9dSBarry Smith 
1755615d1e5SSatish Balay #undef __FUNC__
176005c665bSBarry Smith #define __FUNC__ "MatFDColoringSetFrequency"
177005c665bSBarry Smith /*@
178e0907662SLois Curfman McInnes    MatFDColoringSetFrequency - Sets the frequency for computing new Jacobian
179e0907662SLois Curfman McInnes    matrices.
180005c665bSBarry Smith 
181fee21e36SBarry Smith    Collective on MatFDColoring
182fee21e36SBarry Smith 
183ef5ee4d1SLois Curfman McInnes    Input Parameters:
184ef5ee4d1SLois Curfman McInnes +  coloring - the coloring context
185ef5ee4d1SLois Curfman McInnes -  freq - frequency (default is 1)
186ef5ee4d1SLois Curfman McInnes 
18715091d37SBarry Smith    Options Database Keys:
18815091d37SBarry Smith .  -mat_fd_coloring_freq <freq>  - Sets coloring frequency
18915091d37SBarry Smith 
19015091d37SBarry Smith    Level: advanced
19115091d37SBarry Smith 
192e0907662SLois Curfman McInnes    Notes:
193e0907662SLois Curfman McInnes    Using a modified Newton strategy, where the Jacobian remains fixed for several
194e0907662SLois Curfman McInnes    iterations, can be cost effective in terms of overall nonlinear solution
195e0907662SLois Curfman McInnes    efficiency.  This parameter indicates that a new Jacobian will be computed every
196e0907662SLois Curfman McInnes    <freq> nonlinear iterations.
197e0907662SLois Curfman McInnes 
198b4fc646aSLois Curfman McInnes .keywords: Mat, finite differences, coloring, set, frequency
199ef5ee4d1SLois Curfman McInnes 
200ef5ee4d1SLois Curfman McInnes .seealso: MatFDColoringCreate(), MatFDColoringGetFrequency()
201005c665bSBarry Smith @*/
202005c665bSBarry Smith int MatFDColoringSetFrequency(MatFDColoring matfd,int freq)
203005c665bSBarry Smith {
2043a40ed3dSBarry Smith   PetscFunctionBegin;
205005c665bSBarry Smith   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
206005c665bSBarry Smith 
207005c665bSBarry Smith   matfd->freq = freq;
2083a40ed3dSBarry Smith   PetscFunctionReturn(0);
209005c665bSBarry Smith }
210005c665bSBarry Smith 
211005c665bSBarry Smith #undef __FUNC__
212ff0cfa39SBarry Smith #define __FUNC__ "MatFDColoringGetFrequency"
213ff0cfa39SBarry Smith /*@
214ff0cfa39SBarry Smith    MatFDColoringGetFrequency - Gets the frequency for computing new Jacobian
215ff0cfa39SBarry Smith    matrices.
216ff0cfa39SBarry Smith 
217ef5ee4d1SLois Curfman McInnes    Not Collective
218ef5ee4d1SLois Curfman McInnes 
219ff0cfa39SBarry Smith    Input Parameters:
220ff0cfa39SBarry Smith .  coloring - the coloring context
221ff0cfa39SBarry Smith 
222ff0cfa39SBarry Smith    Output Parameters:
223ff0cfa39SBarry Smith .  freq - frequency (default is 1)
224ff0cfa39SBarry Smith 
22515091d37SBarry Smith    Options Database Keys:
22615091d37SBarry Smith .  -mat_fd_coloring_freq <freq> - Sets coloring frequency
22715091d37SBarry Smith 
22815091d37SBarry Smith    Level: advanced
22915091d37SBarry Smith 
230ff0cfa39SBarry Smith    Notes:
231ff0cfa39SBarry Smith    Using a modified Newton strategy, where the Jacobian remains fixed for several
232ff0cfa39SBarry Smith    iterations, can be cost effective in terms of overall nonlinear solution
233ff0cfa39SBarry Smith    efficiency.  This parameter indicates that a new Jacobian will be computed every
234ff0cfa39SBarry Smith    <freq> nonlinear iterations.
235ff0cfa39SBarry Smith 
236ff0cfa39SBarry Smith .keywords: Mat, finite differences, coloring, get, frequency
237ef5ee4d1SLois Curfman McInnes 
238ef5ee4d1SLois Curfman McInnes .seealso: MatFDColoringSetFrequency()
239ff0cfa39SBarry Smith @*/
240ff0cfa39SBarry Smith int MatFDColoringGetFrequency(MatFDColoring matfd,int *freq)
241ff0cfa39SBarry Smith {
2423a40ed3dSBarry Smith   PetscFunctionBegin;
243ff0cfa39SBarry Smith   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
244ff0cfa39SBarry Smith 
245ff0cfa39SBarry Smith   *freq = matfd->freq;
2463a40ed3dSBarry Smith   PetscFunctionReturn(0);
247ff0cfa39SBarry Smith }
248ff0cfa39SBarry Smith 
249ff0cfa39SBarry Smith #undef __FUNC__
250005c665bSBarry Smith #define __FUNC__ "MatFDColoringSetFunction"
251d64ed03dSBarry Smith /*@C
252005c665bSBarry Smith    MatFDColoringSetFunction - Sets the function to use for computing the Jacobian.
253005c665bSBarry Smith 
254fee21e36SBarry Smith    Collective on MatFDColoring
255fee21e36SBarry Smith 
256ef5ee4d1SLois Curfman McInnes    Input Parameters:
257ef5ee4d1SLois Curfman McInnes +  coloring - the coloring context
258ef5ee4d1SLois Curfman McInnes .  f - the function
259ef5ee4d1SLois Curfman McInnes -  fctx - the optional user-defined function context
260ef5ee4d1SLois Curfman McInnes 
26115091d37SBarry Smith    Level: intermediate
26215091d37SBarry Smith 
263f881d145SBarry Smith    Notes:
264f881d145SBarry Smith     In Fortran you must call MatFDColoringSetFunctionSNES() for a coloring object to
265f881d145SBarry Smith   be used with the SNES solvers and MatFDColoringSetFunctionTS() if it is to be used
266f881d145SBarry Smith   with the TS solvers.
267f881d145SBarry Smith 
268b4fc646aSLois Curfman McInnes .keywords: Mat, Jacobian, finite differences, set, function
269005c665bSBarry Smith @*/
270840b8ebdSBarry Smith int MatFDColoringSetFunction(MatFDColoring matfd,int (*f)(void),void *fctx)
271005c665bSBarry Smith {
2723a40ed3dSBarry Smith   PetscFunctionBegin;
273005c665bSBarry Smith   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
274005c665bSBarry Smith 
275005c665bSBarry Smith   matfd->f    = f;
276005c665bSBarry Smith   matfd->fctx = fctx;
277005c665bSBarry Smith 
2783a40ed3dSBarry Smith   PetscFunctionReturn(0);
279005c665bSBarry Smith }
280005c665bSBarry Smith 
281005c665bSBarry Smith #undef __FUNC__
282d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringSetFromOptions"
283639f9d9dSBarry Smith /*@
284b4fc646aSLois Curfman McInnes    MatFDColoringSetFromOptions - Sets coloring finite difference parameters from
285639f9d9dSBarry Smith    the options database.
286639f9d9dSBarry Smith 
287fee21e36SBarry Smith    Collective on MatFDColoring
288fee21e36SBarry Smith 
289*65f2ba5bSLois Curfman McInnes    The Jacobian, F'(u), is estimated with the differencing approximation
290ef5ee4d1SLois Curfman McInnes .vb
291*65f2ba5bSLois Curfman McInnes        F'(u)_{:,i} = [F(u+h*dx_{i}) - F(u)]/h where
292f23b5b22SLois Curfman McInnes        h = error_rel*u[i]                 if  abs(u[i]) > umin
293f23b5b22SLois Curfman McInnes          = +/- error_rel*umin             otherwise, with +/- determined by the sign of u[i]
294ef5ee4d1SLois Curfman McInnes        dx_{i} = (0, ... 1, .... 0)
295ef5ee4d1SLois Curfman McInnes .ve
296ef5ee4d1SLois Curfman McInnes 
297ef5ee4d1SLois Curfman McInnes    Input Parameter:
298ef5ee4d1SLois Curfman McInnes .  coloring - the coloring context
299ef5ee4d1SLois Curfman McInnes 
300b4fc646aSLois Curfman McInnes    Options Database Keys:
301ef5ee4d1SLois Curfman McInnes +  -mat_fd_coloring_error <err> - Sets <err> (square root
302ef5ee4d1SLois Curfman McInnes            of relative error in the function)
303f23b5b22SLois Curfman McInnes .  -mat_fd_coloring_umin <umin> - Sets umin, the minimum allowable u-value magnitude
304ef5ee4d1SLois Curfman McInnes .  -mat_fd_coloring_freq <freq> - Sets frequency of computing a new Jacobian
305ef5ee4d1SLois Curfman McInnes .  -mat_fd_coloring_view - Activates basic viewing
306ef5ee4d1SLois Curfman McInnes .  -mat_fd_coloring_view_info - Activates viewing info
307ef5ee4d1SLois Curfman McInnes -  -mat_fd_coloring_view_draw - Activates drawing
308639f9d9dSBarry Smith 
30915091d37SBarry Smith     Level: intermediate
31015091d37SBarry Smith 
311b4fc646aSLois Curfman McInnes .keywords: Mat, finite differences, parameters
312639f9d9dSBarry Smith @*/
313639f9d9dSBarry Smith int MatFDColoringSetFromOptions(MatFDColoring matfd)
314639f9d9dSBarry Smith {
315005c665bSBarry Smith   int    ierr,flag,freq = 1;
316639f9d9dSBarry Smith   double error = PETSC_DEFAULT,umin = PETSC_DEFAULT;
3173a40ed3dSBarry Smith 
3183a40ed3dSBarry Smith   PetscFunctionBegin;
319639f9d9dSBarry Smith   PetscValidHeaderSpecific(matfd,MAT_FDCOLORING_COOKIE);
320639f9d9dSBarry Smith 
321639f9d9dSBarry Smith   ierr = OptionsGetDouble(matfd->prefix,"-mat_fd_coloring_err",&error,&flag);CHKERRQ(ierr);
322639f9d9dSBarry Smith   ierr = OptionsGetDouble(matfd->prefix,"-mat_fd_coloring_umin",&umin,&flag);CHKERRQ(ierr);
323639f9d9dSBarry Smith   ierr = MatFDColoringSetParameters(matfd,error,umin); CHKERRQ(ierr);
324005c665bSBarry Smith   ierr = OptionsGetInt(matfd->prefix,"-mat_fd_coloring_freq",&freq,&flag);CHKERRQ(ierr);
325005c665bSBarry Smith   ierr = MatFDColoringSetFrequency(matfd,freq);CHKERRQ(ierr);
326005c665bSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flag); CHKERRQ(ierr);
327639f9d9dSBarry Smith   if (flag) {
328639f9d9dSBarry Smith     ierr = MatFDColoringPrintHelp(matfd); CHKERRQ(ierr);
329639f9d9dSBarry Smith   }
3303a40ed3dSBarry Smith   PetscFunctionReturn(0);
331639f9d9dSBarry Smith }
332639f9d9dSBarry Smith 
3335615d1e5SSatish Balay #undef __FUNC__
334d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringPrintHelp"
335639f9d9dSBarry Smith /*@
336639f9d9dSBarry Smith     MatFDColoringPrintHelp - Prints help message for matrix finite difference calculations
337639f9d9dSBarry Smith     using coloring.
338639f9d9dSBarry Smith 
339ef5ee4d1SLois Curfman McInnes     Collective on MatFDColoring
340ef5ee4d1SLois Curfman McInnes 
341639f9d9dSBarry Smith     Input Parameter:
342639f9d9dSBarry Smith .   fdcoloring - the MatFDColoring context
343639f9d9dSBarry Smith 
34415091d37SBarry Smith     Level: intermediate
34515091d37SBarry Smith 
346639f9d9dSBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringSetFromOptions()
347639f9d9dSBarry Smith @*/
348639f9d9dSBarry Smith int MatFDColoringPrintHelp(MatFDColoring fd)
349639f9d9dSBarry Smith {
3503a40ed3dSBarry Smith   PetscFunctionBegin;
351639f9d9dSBarry Smith   PetscValidHeaderSpecific(fd,MAT_FDCOLORING_COOKIE);
352639f9d9dSBarry Smith 
35376be9ce4SBarry Smith   (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_err <err>: set sqrt rel tol in function, defaults to %g\n",fd->error_rel);
35476be9ce4SBarry Smith   (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_umin <umin>: see users manual, defaults to %d\n",fd->umin);
35576be9ce4SBarry Smith   (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_freq <freq>: frequency that Jacobian is recomputed, defaults to %d\n",fd->freq);
35676be9ce4SBarry Smith   (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view\n");
35776be9ce4SBarry Smith   (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view_draw\n");
35876be9ce4SBarry Smith   (*PetscHelpPrintf)(fd->comm,"-mat_fd_coloring_view_info\n");
3593a40ed3dSBarry Smith   PetscFunctionReturn(0);
360005c665bSBarry Smith }
361005c665bSBarry Smith 
362005c665bSBarry Smith int MatFDColoringView_Private(MatFDColoring fd)
363005c665bSBarry Smith {
364005c665bSBarry Smith   int ierr,flg;
365005c665bSBarry Smith 
3663a40ed3dSBarry Smith   PetscFunctionBegin;
367005c665bSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view",&flg); CHKERRQ(ierr);
368005c665bSBarry Smith   if (flg) {
369f8590f6eSBarry Smith     ierr = MatFDColoringView(fd,VIEWER_STDOUT_(fd->comm)); CHKERRQ(ierr);
370005c665bSBarry Smith   }
371ae09f205SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view_info",&flg); CHKERRQ(ierr);
372ae09f205SBarry Smith   if (flg) {
373f8590f6eSBarry Smith     ierr = ViewerPushFormat(VIEWER_STDOUT_(fd->comm),VIEWER_FORMAT_ASCII_INFO,PETSC_NULL);CHKERRQ(ierr);
374f8590f6eSBarry Smith     ierr = MatFDColoringView(fd,VIEWER_STDOUT_(fd->comm)); CHKERRQ(ierr);
375f8590f6eSBarry Smith     ierr = ViewerPopFormat(VIEWER_STDOUT_(fd->comm));CHKERRQ(ierr);
376ae09f205SBarry Smith   }
377005c665bSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_view_draw",&flg); CHKERRQ(ierr);
378005c665bSBarry Smith   if (flg) {
379c655490fSBarry Smith     ierr = MatFDColoringView(fd,VIEWER_DRAW_(fd->comm)); CHKERRQ(ierr);
380c655490fSBarry Smith     ierr = ViewerFlush(VIEWER_DRAW_(fd->comm)); CHKERRQ(ierr);
381005c665bSBarry Smith   }
3823a40ed3dSBarry Smith   PetscFunctionReturn(0);
383bbf0e169SBarry Smith }
384bbf0e169SBarry Smith 
3855615d1e5SSatish Balay #undef __FUNC__
3865615d1e5SSatish Balay #define __FUNC__ "MatFDColoringCreate"
387bbf0e169SBarry Smith /*@C
388639f9d9dSBarry Smith    MatFDColoringCreate - Creates a matrix coloring context for finite difference
389639f9d9dSBarry Smith    computation of Jacobians.
390bbf0e169SBarry Smith 
391ef5ee4d1SLois Curfman McInnes    Collective on Mat
392ef5ee4d1SLois Curfman McInnes 
393639f9d9dSBarry Smith    Input Parameters:
394ef5ee4d1SLois Curfman McInnes +  mat - the matrix containing the nonzero structure of the Jacobian
395ef5ee4d1SLois Curfman McInnes -  iscoloring - the coloring of the matrix
396bbf0e169SBarry Smith 
397bbf0e169SBarry Smith     Output Parameter:
398639f9d9dSBarry Smith .   color - the new coloring context
399bbf0e169SBarry Smith 
400b4fc646aSLois Curfman McInnes     Options Database Keys:
401ef5ee4d1SLois Curfman McInnes +    -mat_fd_coloring_view - Activates basic viewing or coloring
402ef5ee4d1SLois Curfman McInnes .    -mat_fd_coloring_view_draw - Activates drawing of coloring
403ef5ee4d1SLois Curfman McInnes -    -mat_fd_coloring_view_info - Activates viewing of coloring info
404639f9d9dSBarry Smith 
40515091d37SBarry Smith     Level: intermediate
40615091d37SBarry Smith 
407639f9d9dSBarry Smith .seealso: MatFDColoringDestroy()
408bbf0e169SBarry Smith @*/
409639f9d9dSBarry Smith int MatFDColoringCreate(Mat mat,ISColoring iscoloring,MatFDColoring *color)
410bbf0e169SBarry Smith {
411639f9d9dSBarry Smith   MatFDColoring c;
412639f9d9dSBarry Smith   MPI_Comm      comm;
413639f9d9dSBarry Smith   int           ierr,M,N;
414639f9d9dSBarry Smith 
4153a40ed3dSBarry Smith   PetscFunctionBegin;
416639f9d9dSBarry Smith   ierr = MatGetSize(mat,&M,&N); CHKERRQ(ierr);
417e3372554SBarry Smith   if (M != N) SETERRQ(PETSC_ERR_SUP,0,"Only for square matrices");
418639f9d9dSBarry Smith 
419f881d145SBarry Smith   ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr);
4203f1db9ecSBarry Smith   PetscHeaderCreate(c,_p_MatFDColoring,int,MAT_FDCOLORING_COOKIE,0,"MatFDColoring",comm,
4213f1db9ecSBarry Smith                     MatFDColoringDestroy,MatFDColoringView);
422639f9d9dSBarry Smith   PLogObjectCreate(c);
423639f9d9dSBarry Smith 
424f830108cSBarry Smith   if (mat->ops->fdcoloringcreate) {
425f830108cSBarry Smith     ierr = (*mat->ops->fdcoloringcreate)(mat,iscoloring,c); CHKERRQ(ierr);
426639f9d9dSBarry Smith   } else {
427e3372554SBarry Smith     SETERRQ(PETSC_ERR_SUP,0,"Code not yet written for this matrix type");
428639f9d9dSBarry Smith   }
429639f9d9dSBarry Smith 
430639f9d9dSBarry Smith   c->error_rel = 1.e-8;
431ae09f205SBarry Smith   c->umin      = 1.e-6;
432005c665bSBarry Smith   c->freq      = 1;
433005c665bSBarry Smith 
434005c665bSBarry Smith   ierr = MatFDColoringView_Private(c); CHKERRQ(ierr);
435639f9d9dSBarry Smith 
436639f9d9dSBarry Smith   *color = c;
437639f9d9dSBarry Smith 
4383a40ed3dSBarry Smith   PetscFunctionReturn(0);
439bbf0e169SBarry Smith }
440bbf0e169SBarry Smith 
4415615d1e5SSatish Balay #undef __FUNC__
442d4bb536fSBarry Smith #define __FUNC__ "MatFDColoringDestroy"
443bbf0e169SBarry Smith /*@C
444639f9d9dSBarry Smith     MatFDColoringDestroy - Destroys a matrix coloring context that was created
445639f9d9dSBarry Smith     via MatFDColoringCreate().
446bbf0e169SBarry Smith 
447ef5ee4d1SLois Curfman McInnes     Collective on MatFDColoring
448ef5ee4d1SLois Curfman McInnes 
449b4fc646aSLois Curfman McInnes     Input Parameter:
450639f9d9dSBarry Smith .   c - coloring context
451bbf0e169SBarry Smith 
45215091d37SBarry Smith     Level: intermediate
45315091d37SBarry Smith 
454639f9d9dSBarry Smith .seealso: MatFDColoringCreate()
455bbf0e169SBarry Smith @*/
456639f9d9dSBarry Smith int MatFDColoringDestroy(MatFDColoring c)
457bbf0e169SBarry Smith {
458263760aaSBarry Smith   int i,ierr;
459bbf0e169SBarry Smith 
4603a40ed3dSBarry Smith   PetscFunctionBegin;
4613a40ed3dSBarry Smith   if (--c->refct > 0) PetscFunctionReturn(0);
462d4bb536fSBarry Smith 
463639f9d9dSBarry Smith 
464639f9d9dSBarry Smith   for ( i=0; i<c->ncolors; i++ ) {
465639f9d9dSBarry Smith     if (c->columns[i])       PetscFree(c->columns[i]);
466639f9d9dSBarry Smith     if (c->rows[i])          PetscFree(c->rows[i]);
467639f9d9dSBarry Smith     if (c->columnsforrow[i]) PetscFree(c->columnsforrow[i]);
468bbf0e169SBarry Smith   }
469639f9d9dSBarry Smith   PetscFree(c->ncolumns);
470639f9d9dSBarry Smith   PetscFree(c->columns);
471639f9d9dSBarry Smith   PetscFree(c->nrows);
472639f9d9dSBarry Smith   PetscFree(c->rows);
473639f9d9dSBarry Smith   PetscFree(c->columnsforrow);
474639f9d9dSBarry Smith   PetscFree(c->scale);
475005c665bSBarry Smith   if (c->w1) {
476005c665bSBarry Smith     ierr = VecDestroy(c->w1); CHKERRQ(ierr);
477005c665bSBarry Smith     ierr = VecDestroy(c->w2); CHKERRQ(ierr);
478005c665bSBarry Smith     ierr = VecDestroy(c->w3); CHKERRQ(ierr);
479005c665bSBarry Smith   }
480639f9d9dSBarry Smith   PLogObjectDestroy(c);
481639f9d9dSBarry Smith   PetscHeaderDestroy(c);
4823a40ed3dSBarry Smith   PetscFunctionReturn(0);
483bbf0e169SBarry Smith }
48443a90d84SBarry Smith 
485005c665bSBarry Smith #include "snes.h"
486005c665bSBarry Smith 
4875615d1e5SSatish Balay #undef __FUNC__
4885615d1e5SSatish Balay #define __FUNC__ "MatFDColoringApply"
48943a90d84SBarry Smith /*@
490e0907662SLois Curfman McInnes     MatFDColoringApply - Given a matrix for which a MatFDColoring context
491e0907662SLois Curfman McInnes     has been created, computes the Jacobian for a function via finite differences.
49243a90d84SBarry Smith 
493fee21e36SBarry Smith     Collective on MatFDColoring
494fee21e36SBarry Smith 
495ef5ee4d1SLois Curfman McInnes     Input Parameters:
496ef5ee4d1SLois Curfman McInnes +   mat - location to store Jacobian
497ef5ee4d1SLois Curfman McInnes .   coloring - coloring context created with MatFDColoringCreate()
498ef5ee4d1SLois Curfman McInnes .   x1 - location at which Jacobian is to be computed
499ef5ee4d1SLois Curfman McInnes -   sctx - optional context required by function (actually a SNES context)
500ef5ee4d1SLois Curfman McInnes 
5018bba8e72SBarry Smith    Options Database Keys:
502ef5ee4d1SLois Curfman McInnes .  -mat_fd_coloring_freq <freq> - Sets coloring frequency
5038bba8e72SBarry Smith 
50415091d37SBarry Smith    Level: intermediate
50515091d37SBarry Smith 
50643a90d84SBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView()
50743a90d84SBarry Smith 
50843a90d84SBarry Smith .keywords: coloring, Jacobian, finite differences
50943a90d84SBarry Smith @*/
510005c665bSBarry Smith int MatFDColoringApply(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
51143a90d84SBarry Smith {
512e0907662SLois Curfman McInnes   int           k,fg,ierr,N,start,end,l,row,col,srow;
51343a90d84SBarry Smith   Scalar        dx, mone = -1.0,*y,*scale = coloring->scale,*xx,*wscale = coloring->wscale;
51443a90d84SBarry Smith   double        epsilon = coloring->error_rel, umin = coloring->umin;
51543a90d84SBarry Smith   MPI_Comm      comm = coloring->comm;
516005c665bSBarry Smith   Vec           w1,w2,w3;
517840b8ebdSBarry Smith   int           (*f)(void *,Vec,Vec,void *) = ( int (*)(void *,Vec,Vec,void *))coloring->f;
518005c665bSBarry Smith   void          *fctx = coloring->fctx;
519005c665bSBarry Smith 
5203a40ed3dSBarry Smith   PetscFunctionBegin;
521e0907662SLois Curfman McInnes   PetscValidHeaderSpecific(J,MAT_COOKIE);
522e0907662SLois Curfman McInnes   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_COOKIE);
523e0907662SLois Curfman McInnes   PetscValidHeaderSpecific(x1,VEC_COOKIE);
524e0907662SLois Curfman McInnes 
525005c665bSBarry Smith 
526005c665bSBarry Smith   if (!coloring->w1) {
527005c665bSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1); CHKERRQ(ierr);
528005c665bSBarry Smith     PLogObjectParent(coloring,coloring->w1);
529005c665bSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2); CHKERRQ(ierr);
530005c665bSBarry Smith     PLogObjectParent(coloring,coloring->w2);
531005c665bSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3); CHKERRQ(ierr);
532005c665bSBarry Smith     PLogObjectParent(coloring,coloring->w3);
533005c665bSBarry Smith   }
534005c665bSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
53543a90d84SBarry Smith 
536e0907662SLois Curfman McInnes   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&fg); CHKERRQ(ierr);
537e0907662SLois Curfman McInnes   if (fg) {
538e0907662SLois Curfman McInnes     PLogInfo(coloring,"MatFDColoringApply: Not calling MatZeroEntries()\n");
539e0907662SLois Curfman McInnes   } else {
54043a90d84SBarry Smith     ierr = MatZeroEntries(J); CHKERRQ(ierr);
541e0907662SLois Curfman McInnes   }
54243a90d84SBarry Smith 
54343a90d84SBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr);
54443a90d84SBarry Smith   ierr = VecGetSize(x1,&N); CHKERRQ(ierr);
54543a90d84SBarry Smith   ierr = (*f)(sctx,x1,w1,fctx); CHKERRQ(ierr);
54643a90d84SBarry Smith 
54743a90d84SBarry Smith   PetscMemzero(wscale,N*sizeof(Scalar));
54843a90d84SBarry Smith   /*
54943a90d84SBarry Smith       Loop over each color
55043a90d84SBarry Smith   */
55143a90d84SBarry Smith 
5523b28642cSBarry Smith   ierr = VecGetArray(x1,&xx); CHKERRQ(ierr);
55343a90d84SBarry Smith   for (k=0; k<coloring->ncolors; k++) {
55443a90d84SBarry Smith     ierr = VecCopy(x1,w3); CHKERRQ(ierr);
55543a90d84SBarry Smith     /*
55643a90d84SBarry Smith        Loop over each column associated with color adding the
55743a90d84SBarry Smith        perturbation to the vector w3.
55843a90d84SBarry Smith     */
55943a90d84SBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
56043a90d84SBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
56143a90d84SBarry Smith       dx  = xx[col-start];
562ae09f205SBarry Smith       if (dx == 0.0) dx = 1.0;
5633a40ed3dSBarry Smith #if !defined(USE_PETSC_COMPLEX)
564ae09f205SBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
565ae09f205SBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
56643a90d84SBarry Smith #else
567e20fef11SSatish Balay       if (PetscAbsScalar(dx) < umin && PetscReal(dx) >= 0.0)     dx = umin;
568e20fef11SSatish Balay       else if (PetscReal(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
56943a90d84SBarry Smith #endif
57043a90d84SBarry Smith       dx          *= epsilon;
57143a90d84SBarry Smith       wscale[col] = 1.0/dx;
5723b28642cSBarry Smith       ierr = VecSetValues(w3,1,&col,&dx,ADD_VALUES);CHKERRQ(ierr);
57343a90d84SBarry Smith     }
5743b28642cSBarry Smith 
57543a90d84SBarry Smith     /*
576e0907662SLois Curfman McInnes        Evaluate function at x1 + dx (here dx is a vector of perturbations)
57743a90d84SBarry Smith     */
57843a90d84SBarry Smith     ierr = (*f)(sctx,w3,w2,fctx); CHKERRQ(ierr);
57943a90d84SBarry Smith     ierr = VecAXPY(&mone,w1,w2); CHKERRQ(ierr);
58043a90d84SBarry Smith     /* Communicate scale to all processors */
5813a40ed3dSBarry Smith #if !defined(USE_PETSC_COMPLEX)
582ca161407SBarry Smith     ierr = MPI_Allreduce(wscale,scale,N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
58343a90d84SBarry Smith #else
584ca161407SBarry Smith     ierr = MPI_Allreduce(wscale,scale,2*N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
58543a90d84SBarry Smith #endif
58643a90d84SBarry Smith     /*
587e0907662SLois Curfman McInnes        Loop over rows of vector, putting results into Jacobian matrix
58843a90d84SBarry Smith     */
5893b28642cSBarry Smith     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
59043a90d84SBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
59143a90d84SBarry Smith       row    = coloring->rows[k][l];
59243a90d84SBarry Smith       col    = coloring->columnsforrow[k][l];
59343a90d84SBarry Smith       y[row] *= scale[col];
59443a90d84SBarry Smith       srow   = row + start;
59543a90d84SBarry Smith       ierr   = MatSetValues(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
59643a90d84SBarry Smith     }
5973b28642cSBarry Smith     ierr = VecRestoreArray(w2,&y); CHKERRQ(ierr);
59843a90d84SBarry Smith   }
5993b28642cSBarry Smith   ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
60043a90d84SBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
60143a90d84SBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
6023a40ed3dSBarry Smith   PetscFunctionReturn(0);
60343a90d84SBarry Smith }
604840b8ebdSBarry Smith 
605840b8ebdSBarry Smith #include "ts.h"
606840b8ebdSBarry Smith 
607840b8ebdSBarry Smith #undef __FUNC__
608840b8ebdSBarry Smith #define __FUNC__ "MatFDColoringApplyTS"
609840b8ebdSBarry Smith /*@
610840b8ebdSBarry Smith     MatFDColoringApplyTS - Given a matrix for which a MatFDColoring context
611840b8ebdSBarry Smith     has been created, computes the Jacobian for a function via finite differences.
612840b8ebdSBarry Smith 
613fee21e36SBarry Smith    Collective on Mat, MatFDColoring, and Vec
614fee21e36SBarry Smith 
615ef5ee4d1SLois Curfman McInnes     Input Parameters:
6163b28642cSBarry Smith +   mat - location to store Jacobian
617ef5ee4d1SLois Curfman McInnes .   coloring - coloring context created with MatFDColoringCreate()
618ef5ee4d1SLois Curfman McInnes .   x1 - location at which Jacobian is to be computed
619ef5ee4d1SLois Curfman McInnes -   sctx - optional context required by function (actually a SNES context)
620ef5ee4d1SLois Curfman McInnes 
621840b8ebdSBarry Smith    Options Database Keys:
622ef5ee4d1SLois Curfman McInnes .  -mat_fd_coloring_freq <freq> - Sets coloring frequency
623840b8ebdSBarry Smith 
62415091d37SBarry Smith    Level: intermediate
62515091d37SBarry Smith 
626840b8ebdSBarry Smith .seealso: MatFDColoringCreate(), MatFDColoringDestroy(), MatFDColoringView()
627840b8ebdSBarry Smith 
628840b8ebdSBarry Smith .keywords: coloring, Jacobian, finite differences
629840b8ebdSBarry Smith @*/
630840b8ebdSBarry Smith int MatFDColoringApplyTS(Mat J,MatFDColoring coloring,double t,Vec x1,MatStructure *flag,void *sctx)
631840b8ebdSBarry Smith {
632840b8ebdSBarry Smith   int           k,fg,ierr,N,start,end,l,row,col,srow;
633840b8ebdSBarry Smith   Scalar        dx, mone = -1.0,*y,*scale = coloring->scale,*xx,*wscale = coloring->wscale;
634840b8ebdSBarry Smith   double        epsilon = coloring->error_rel, umin = coloring->umin;
635840b8ebdSBarry Smith   MPI_Comm      comm = coloring->comm;
636840b8ebdSBarry Smith   Vec           w1,w2,w3;
637840b8ebdSBarry Smith   int           (*f)(void *,double,Vec,Vec,void *) = ( int (*)(void *,double,Vec,Vec,void *))coloring->f;
638840b8ebdSBarry Smith   void          *fctx = coloring->fctx;
639840b8ebdSBarry Smith 
6403a40ed3dSBarry Smith   PetscFunctionBegin;
641840b8ebdSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE);
642840b8ebdSBarry Smith   PetscValidHeaderSpecific(coloring,MAT_FDCOLORING_COOKIE);
643840b8ebdSBarry Smith   PetscValidHeaderSpecific(x1,VEC_COOKIE);
644840b8ebdSBarry Smith 
645840b8ebdSBarry Smith   if (!coloring->w1) {
646840b8ebdSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1); CHKERRQ(ierr);
647840b8ebdSBarry Smith     PLogObjectParent(coloring,coloring->w1);
648840b8ebdSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2); CHKERRQ(ierr);
649840b8ebdSBarry Smith     PLogObjectParent(coloring,coloring->w2);
650840b8ebdSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3); CHKERRQ(ierr);
651840b8ebdSBarry Smith     PLogObjectParent(coloring,coloring->w3);
652840b8ebdSBarry Smith   }
653840b8ebdSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
654840b8ebdSBarry Smith 
655840b8ebdSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&fg); CHKERRQ(ierr);
656840b8ebdSBarry Smith   if (fg) {
657840b8ebdSBarry Smith     PLogInfo(coloring,"MatFDColoringApplyTS: Not calling MatZeroEntries()\n");
658840b8ebdSBarry Smith   } else {
659840b8ebdSBarry Smith     ierr = MatZeroEntries(J); CHKERRQ(ierr);
660840b8ebdSBarry Smith   }
661840b8ebdSBarry Smith 
662840b8ebdSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end); CHKERRQ(ierr);
663840b8ebdSBarry Smith   ierr = VecGetSize(x1,&N); CHKERRQ(ierr);
664840b8ebdSBarry Smith   ierr = (*f)(sctx,t,x1,w1,fctx); CHKERRQ(ierr);
665840b8ebdSBarry Smith 
666840b8ebdSBarry Smith   PetscMemzero(wscale,N*sizeof(Scalar));
667840b8ebdSBarry Smith   /*
668840b8ebdSBarry Smith       Loop over each color
669840b8ebdSBarry Smith   */
670840b8ebdSBarry Smith 
6713b28642cSBarry Smith   ierr = VecGetArray(x1,&xx); CHKERRQ(ierr);
672840b8ebdSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
673840b8ebdSBarry Smith     ierr = VecCopy(x1,w3); CHKERRQ(ierr);
674840b8ebdSBarry Smith     /*
675840b8ebdSBarry Smith        Loop over each column associated with color adding the
676840b8ebdSBarry Smith        perturbation to the vector w3.
677840b8ebdSBarry Smith     */
678840b8ebdSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
679840b8ebdSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
680840b8ebdSBarry Smith       dx  = xx[col-start];
681840b8ebdSBarry Smith       if (dx == 0.0) dx = 1.0;
6823a40ed3dSBarry Smith #if !defined(USE_PETSC_COMPLEX)
683840b8ebdSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
684840b8ebdSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
685840b8ebdSBarry Smith #else
686e20fef11SSatish Balay       if (PetscAbsScalar(dx) < umin && PetscReal(dx) >= 0.0)     dx = umin;
687e20fef11SSatish Balay       else if (PetscReal(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
688840b8ebdSBarry Smith #endif
689840b8ebdSBarry Smith       dx          *= epsilon;
690840b8ebdSBarry Smith       wscale[col] = 1.0/dx;
6913b28642cSBarry Smith       ierr = VecSetValues(w3,1,&col,&dx,ADD_VALUES); CHKERRQ(ierr);
692840b8ebdSBarry Smith     }
693840b8ebdSBarry Smith     /*
694840b8ebdSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
695840b8ebdSBarry Smith     */
696840b8ebdSBarry Smith     ierr = (*f)(sctx,t,w3,w2,fctx); CHKERRQ(ierr);
697840b8ebdSBarry Smith     ierr = VecAXPY(&mone,w1,w2); CHKERRQ(ierr);
698840b8ebdSBarry Smith     /* Communicate scale to all processors */
6993a40ed3dSBarry Smith #if !defined(USE_PETSC_COMPLEX)
700ca161407SBarry Smith     ierr = MPI_Allreduce(wscale,scale,N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
701840b8ebdSBarry Smith #else
702ca161407SBarry Smith     ierr = MPI_Allreduce(wscale,scale,2*N,MPI_DOUBLE,MPI_SUM,comm);CHKERRQ(ierr);
703840b8ebdSBarry Smith #endif
704840b8ebdSBarry Smith     /*
705840b8ebdSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
706840b8ebdSBarry Smith     */
7073b28642cSBarry Smith     ierr = VecGetArray(w2,&y); CHKERRQ(ierr);
708840b8ebdSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
709840b8ebdSBarry Smith       row    = coloring->rows[k][l];
710840b8ebdSBarry Smith       col    = coloring->columnsforrow[k][l];
711840b8ebdSBarry Smith       y[row] *= scale[col];
712840b8ebdSBarry Smith       srow   = row + start;
713840b8ebdSBarry Smith       ierr   = MatSetValues(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
714840b8ebdSBarry Smith     }
7153b28642cSBarry Smith     ierr = VecRestoreArray(w2,&y); CHKERRQ(ierr);
716840b8ebdSBarry Smith   }
7173b28642cSBarry Smith   ierr  = VecRestoreArray(x1,&xx); CHKERRQ(ierr);
718840b8ebdSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
719840b8ebdSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
7203a40ed3dSBarry Smith   PetscFunctionReturn(0);
721840b8ebdSBarry Smith }
7223b28642cSBarry Smith 
7233b28642cSBarry Smith 
7243b28642cSBarry Smith 
725