xref: /petsc/src/snes/interface/snesj2.c (revision d964b20dc2fa1f18ce27b7cb91c75aaaf77dc742)
1*d964b20dSBarry Smith /*$Id: snesj2.c,v 1.28 2001/05/15 02:50:15 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;
41dff777c9SBarry Smith 
423a40ed3dSBarry Smith   PetscFunctionBegin;
43dff777c9SBarry Smith   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
44dff777c9SBarry Smith   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
45dff777c9SBarry Smith 
46dff777c9SBarry Smith   if ((freq > 1) && ((it % freq) != 1)) {
47*d964b20dSBarry Smith     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Skippingsnesj2.c and  Jacobian, it %d, freq %d\n",it,freq);
48dff777c9SBarry Smith     *flag = SAME_PRECONDITIONER;
49dff777c9SBarry Smith   } else {
50b0a32e0cSBarry Smith     PetscLogInfo(color,"SNESDefaultComputeJacobianColor:Computing Jacobian, it %d, freq %d\n",it,freq);
51*d964b20dSBarry Smith     *flag = SAME_NONZERO_PATTE  if (J != B) {
52*d964b20dSBarry Smith     ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
53*d964b20dSBarry Smith     ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54*d964b20dSBarry Smith   }RN;
55*d964b20dSBarry Smith     ierr = PetscLogEventBegin(SNES_FuninterfacectionEval,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++;
60*d964b20dSBarry Smith     ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x1,0,0);CHKERRQ(ierr);
61*d964b20dSBarry 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