xref: /petsc/src/snes/interface/snesj2.c (revision 3a7fca6b988f1faea172e46abdf950477dbfaf76)
1*3a7fca6bSBarry Smith /*$Id: snesj2.c,v 1.30 2001/05/29 15:39:07 bsmith Exp bsmith $*/
291627157SBarry Smith 
3e090d566SSatish Balay #include "src/mat/matimpl.h"      /*I  "petscmat.h"  I*/
4e090d566SSatish Balay #include "src/snes/snesimpl.h"    /*I  "petscsnes.h"  I*/
591627157SBarry Smith 
64a2ae208SSatish Balay #undef __FUNCT__
74a2ae208SSatish Balay #define __FUNCT__ "SNESDefaultComputeJacobianColor"
891627157SBarry Smith /*@C
92d0c0e3bSBarry Smith     SNESDefaultComputeJacobianColor - Computes the Jacobian using
10b4fc646aSLois Curfman McInnes     finite differences and coloring to exploit matrix sparsity.
1191627157SBarry Smith 
12fee21e36SBarry Smith     Collective on SNES
13fee21e36SBarry Smith 
14c7afd0dbSLois Curfman McInnes     Input Parameters:
15c7afd0dbSLois Curfman McInnes +   snes - nonlinear solver object
16c7afd0dbSLois Curfman McInnes .   x1 - location at which to evaluate Jacobian
17c7afd0dbSLois Curfman McInnes -   ctx - coloring context, where ctx must have type MatFDColoring,
18c7afd0dbSLois Curfman McInnes           as created via MatFDColoringCreate()
19c7afd0dbSLois Curfman McInnes 
20c7afd0dbSLois Curfman McInnes     Output Parameters:
21c7afd0dbSLois Curfman McInnes +   J - Jacobian matrix (not altered in this routine)
22c7afd0dbSLois Curfman McInnes .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
23c7afd0dbSLois Curfman McInnes -   flag - flag indicating whether the matrix sparsity structure has changed
24c7afd0dbSLois Curfman McInnes 
25dff777c9SBarry Smith     Options Database Keys:
262d0c0e3bSBarry Smith .  -mat_fd_coloring_freq <freq> - Activates SNESDefaultComputeJacobianColor()
27dff777c9SBarry Smith 
2836851e7fSLois Curfman McInnes     Level: intermediate
2936851e7fSLois Curfman McInnes 
30b4fc646aSLois Curfman McInnes .keywords: SNES, finite differences, Jacobian, coloring, sparse
3191627157SBarry Smith 
32b4fc646aSLois Curfman McInnes .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
332d0c0e3bSBarry Smith           TSDefaultComputeJacobianColor(), MatFDColoringCreate(),
34cb5b572fSBarry Smith           MatFDColoringSetFunction()
35cb5b572fSBarry Smith 
3691627157SBarry Smith @*/
37454a90a3SBarry Smith int SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
3891627157SBarry Smith {
3991627157SBarry Smith   MatFDColoring color = (MatFDColoring) ctx;
40dff777c9SBarry Smith   int           ierr,freq,it;
41*3a7fca6bSBarry Smith   Vec           f;
42dff777c9SBarry Smith 
433a40ed3dSBarry Smith   PetscFunctionBegin;
44dff777c9SBarry Smith   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
45dff777c9SBarry Smith   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
46dff777c9SBarry Smith 
47dff777c9SBarry Smith   if ((freq > 1) && ((it % freq) != 1)) {
48d964b20dSBarry Smith     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Skippingsnesj2.c and  Jacobian, it %d, freq %d\n",it,freq);
49dff777c9SBarry Smith     *flag = SAME_PRECONDITIONER;
50dff777c9SBarry Smith   } else {
51b0a32e0cSBarry Smith     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %d\n",it,freq);
5292abec31SBarry Smith     *flag = SAME_NONZERO_PATTERN;
53*3a7fca6bSBarry Smith     ierr  = SNESGetFunction(snes,&f,0,0);CHKERRQ(ierr);
54*3a7fca6bSBarry Smith     ierr  = MatFDColoringSetF(color,f);CHKERRQ(ierr);
5592abec31SBarry Smith     ierr  = PetscLogEventBegin(SNES_FunctionEval,snes,x1,0,0);CHKERRQ(ierr);
56522c5e43SBarry Smith     PetscStackPush("SNES user function");
57005c665bSBarry Smith     ierr  = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
58522c5e43SBarry Smith     PetscStackPop;
59522c5e43SBarry Smith     snes->nfuncs++;
60d964b20dSBarry Smith     ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x1,0,0);CHKERRQ(ierr);
61d964b20dSBarry Smith   }
62194405b3SBarry Smith   if (J != B) {
63194405b3SBarry Smith     ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
64194405b3SBarry Smith     ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
65194405b3SBarry Smith   }
663a40ed3dSBarry Smith   PetscFunctionReturn(0);
6791627157SBarry Smith }
6891627157SBarry Smith 
6991627157SBarry Smith 
7091627157SBarry Smith 
71