xref: /petsc/src/snes/interface/snesj2.c (revision 95d750ce60dde0400c72385d3bf7539054b7aeee)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
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 @*/
37*95d750ceSBarry Smith 
3863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
3991627157SBarry Smith {
4091627157SBarry Smith   MatFDColoring  color = (MatFDColoring) ctx;
41dfbe8321SBarry Smith   PetscErrorCode ierr;
4277431f27SBarry Smith   PetscInt       freq,it;
433a7fca6bSBarry Smith   Vec            f;
444a9d489dSBarry Smith   PetscErrorCode (*ff)(void),(*fd)(void);
45dff777c9SBarry Smith 
463a40ed3dSBarry Smith   PetscFunctionBegin;
47dff777c9SBarry Smith   ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr);
48dff777c9SBarry Smith   ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr);
49dff777c9SBarry Smith 
50d079835cSBarry Smith   if ((freq > 1) && ((it % freq))) {
51ae15b995SBarry Smith     ierr = PetscInfo2(color,"Skipping Jacobian recomputation, it %D, freq %D\n",it,freq);CHKERRQ(ierr);
52dff777c9SBarry Smith     *flag = SAME_PRECONDITIONER;
53dff777c9SBarry Smith   } else {
54ae15b995SBarry Smith     ierr = PetscInfo2(color,"Computing Jacobian, it %D, freq %D\n",it,freq);CHKERRQ(ierr);
5592abec31SBarry Smith     *flag = SAME_NONZERO_PATTERN;
564a9d489dSBarry Smith     ierr  = SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);CHKERRQ(ierr);
574a9d489dSBarry Smith     ierr  = MatFDColoringGetFunction(color,&fd,PETSC_NULL);CHKERRQ(ierr);
584a9d489dSBarry Smith     if (fd == ff) { /* reuse function value computed in SNES */
593a7fca6bSBarry Smith       ierr  = MatFDColoringSetF(color,f);CHKERRQ(ierr);
604a9d489dSBarry Smith     }
61005c665bSBarry Smith     ierr  = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
62d964b20dSBarry Smith   }
6332dfb669SBarry Smith   if (*J != *B) {
64194405b3SBarry Smith     ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
65194405b3SBarry Smith     ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
66194405b3SBarry Smith   }
673a40ed3dSBarry Smith   PetscFunctionReturn(0);
6891627157SBarry Smith }
6991627157SBarry Smith 
7091627157SBarry Smith 
7191627157SBarry Smith 
72