xref: /petsc/src/snes/interface/snesj2.c (revision 454a90a3eb7bca6958262e5eca1eb393ad97e108)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*454a90a3SBarry Smith static char vcid[] = "$Id: snesj2.c,v 1.19 1999/05/04 20:35:43 balay Exp bsmith $";
391627157SBarry Smith #endif
491627157SBarry Smith 
5639f9d9dSBarry Smith #include "src/mat/matimpl.h"      /*I  "mat.h"  I*/
6639f9d9dSBarry Smith #include "src/snes/snesimpl.h"    /*I  "snes.h"  I*/
791627157SBarry Smith 
85615d1e5SSatish Balay #undef __FUNC__
92d0c0e3bSBarry Smith #define __FUNC__ "SNESDefaultComputeJacobianColor"
1091627157SBarry Smith /*@C
112d0c0e3bSBarry Smith     SNESDefaultComputeJacobianColor - Computes the Jacobian using
12b4fc646aSLois Curfman McInnes     finite differences and coloring to exploit matrix sparsity.
1391627157SBarry Smith 
14fee21e36SBarry Smith     Collective on SNES
15fee21e36SBarry Smith 
16c7afd0dbSLois Curfman McInnes     Input Parameters:
17c7afd0dbSLois Curfman McInnes +   snes - nonlinear solver object
18c7afd0dbSLois Curfman McInnes .   x1 - location at which to evaluate Jacobian
19c7afd0dbSLois Curfman McInnes -   ctx - coloring context, where ctx must have type MatFDColoring,
20c7afd0dbSLois Curfman McInnes           as created via MatFDColoringCreate()
21c7afd0dbSLois Curfman McInnes 
22c7afd0dbSLois Curfman McInnes     Output Parameters:
23c7afd0dbSLois Curfman McInnes +   J - Jacobian matrix (not altered in this routine)
24c7afd0dbSLois Curfman McInnes .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
25c7afd0dbSLois Curfman McInnes -   flag - flag indicating whether the matrix sparsity structure has changed
26c7afd0dbSLois Curfman McInnes 
27dff777c9SBarry Smith     Options Database Keys:
282d0c0e3bSBarry Smith .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()
29dff777c9SBarry Smith 
3036851e7fSLois Curfman McInnes     Level: intermediate
3136851e7fSLois Curfman McInnes 
32b4fc646aSLois Curfman McInnes .keywords: SNES, finite differences, Jacobian, coloring, sparse
3391627157SBarry Smith 
34b4fc646aSLois Curfman McInnes .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
352d0c0e3bSBarry Smith           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
36cb5b572fSBarry Smith           MatFDColoringSetFunction()
37cb5b572fSBarry Smith 
3891627157SBarry Smith @*/
39*454a90a3SBarry Smith int SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
4091627157SBarry Smith {
4191627157SBarry Smith   MatFDColoring color = (MatFDColoring) ctx;
42dff777c9SBarry Smith   int           ierr,freq,it;
43dff777c9SBarry Smith 
443a40ed3dSBarry Smith   PetscFunctionBegin;
45dff777c9SBarry Smith   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
46dff777c9SBarry Smith   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
47dff777c9SBarry Smith 
48dff777c9SBarry Smith   if ((freq > 1) && ((it % freq) != 1)) {
492d0c0e3bSBarry Smith     PLogInfo(color,"SNESDefaultComputeJacobianColor:Skipping Jacobian, it %d, freq %d\n",it,freq);
50dff777c9SBarry Smith     *flag = SAME_PRECONDITIONER;
513a40ed3dSBarry Smith     PetscFunctionReturn(0);
52dff777c9SBarry Smith   } else {
532d0c0e3bSBarry Smith     PLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %d\n",it,freq);
54dff777c9SBarry Smith     *flag = SAME_NONZERO_PATTERN;
55dff777c9SBarry Smith   }
5688389382SBarry Smith 
57522c5e43SBarry Smith 
58522c5e43SBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x1,0,0);
59522c5e43SBarry Smith   PetscStackPush("SNES user function");
60005c665bSBarry Smith   ierr = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
61522c5e43SBarry Smith   PetscStackPop;
62522c5e43SBarry Smith   snes->nfuncs++;
63522c5e43SBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x1,0,0);
643a40ed3dSBarry Smith   PetscFunctionReturn(0);
6591627157SBarry Smith }
6691627157SBarry Smith 
6791627157SBarry Smith 
6891627157SBarry Smith 
69