xref: /petsc/src/snes/interface/snesj2.c (revision 97eb6af2371cd04b71d75d3ff32186e92fb81114)
191627157SBarry Smith 
2c6db04a5SJed Brown #include <private/snesimpl.h>    /*I  "petscsnes.h"  I*/
391627157SBarry Smith 
44a2ae208SSatish Balay #undef __FUNCT__
54a2ae208SSatish Balay #define __FUNCT__ "SNESDefaultComputeJacobianColor"
691627157SBarry Smith /*@C
72d0c0e3bSBarry Smith     SNESDefaultComputeJacobianColor - Computes the Jacobian using
8b4fc646aSLois Curfman McInnes     finite differences and coloring to exploit matrix sparsity.
991627157SBarry Smith 
10fee21e36SBarry Smith     Collective on SNES
11fee21e36SBarry Smith 
12c7afd0dbSLois Curfman McInnes     Input Parameters:
13c7afd0dbSLois Curfman McInnes +   snes - nonlinear solver object
14c7afd0dbSLois Curfman McInnes .   x1 - location at which to evaluate Jacobian
15c7afd0dbSLois Curfman McInnes -   ctx - coloring context, where ctx must have type MatFDColoring,
16c7afd0dbSLois Curfman McInnes           as created via MatFDColoringCreate()
17c7afd0dbSLois Curfman McInnes 
18c7afd0dbSLois Curfman McInnes     Output Parameters:
19c7afd0dbSLois Curfman McInnes +   J - Jacobian matrix (not altered in this routine)
20c7afd0dbSLois Curfman McInnes .   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
21c7afd0dbSLois Curfman McInnes -   flag - flag indicating whether the matrix sparsity structure has changed
22c7afd0dbSLois Curfman McInnes 
2336851e7fSLois Curfman McInnes     Level: intermediate
2436851e7fSLois Curfman McInnes 
25b4fc646aSLois Curfman McInnes .keywords: SNES, finite differences, Jacobian, coloring, sparse
2691627157SBarry Smith 
27b4fc646aSLois Curfman McInnes .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian()
28ab637aeaSJed Brown           MatFDColoringCreate(), MatFDColoringSetFunction()
29cb5b572fSBarry Smith 
3091627157SBarry Smith @*/
3195d750ceSBarry Smith 
327087cfbeSBarry Smith PetscErrorCode  SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
3391627157SBarry Smith {
3491627157SBarry Smith   MatFDColoring  color = (MatFDColoring) ctx;
35dfbe8321SBarry Smith   PetscErrorCode ierr;
363a7fca6bSBarry Smith   Vec            f;
374a9d489dSBarry Smith   PetscErrorCode (*ff)(void),(*fd)(void);
38dff777c9SBarry Smith 
393a40ed3dSBarry Smith   PetscFunctionBegin;
40a8a26c1eSJed Brown   PetscValidHeaderSpecific(color,MAT_FDCOLORING_CLASSID,6);
4192abec31SBarry Smith   *flag = SAME_NONZERO_PATTERN;
424a9d489dSBarry Smith   ierr  = SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);CHKERRQ(ierr);
434a9d489dSBarry Smith   ierr  = MatFDColoringGetFunction(color,&fd,PETSC_NULL);CHKERRQ(ierr);
44*97eb6af2SJed Brown   if (fd == ff && !snes->vec_rhs) { /* reuse function value computed in SNES */
453a7fca6bSBarry Smith     ierr  = MatFDColoringSetF(color,f);CHKERRQ(ierr);
464a9d489dSBarry Smith   }
47005c665bSBarry Smith   ierr  = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr);
4832dfb669SBarry Smith   if (*J != *B) {
49194405b3SBarry Smith     ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
50194405b3SBarry Smith     ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
51194405b3SBarry Smith   }
523a40ed3dSBarry Smith   PetscFunctionReturn(0);
5391627157SBarry Smith }
5491627157SBarry Smith 
5591627157SBarry Smith 
5691627157SBarry Smith 
57