xref: /petsc/src/snes/interface/snesj2.c (revision 567444918ee22b3330960e77dec97fc980be57da)
191627157SBarry Smith 
2b45d2f2cSJed Brown #include <petsc-private/snesimpl.h>    /*I  "petscsnes.h"  I*/
31e25c274SJed Brown #include <petscdm.h>                   /*I  "petscdm.h"    I*/
491627157SBarry Smith 
54a2ae208SSatish Balay #undef __FUNCT__
68d359177SBarry Smith #define __FUNCT__ "SNESComputeJacobianDefaultColor"
791627157SBarry Smith /*@C
88d359177SBarry Smith     SNESComputeJacobianDefaultColor - Computes the Jacobian using
9b4fc646aSLois Curfman McInnes     finite differences and coloring to exploit matrix sparsity.
1091627157SBarry Smith 
11fee21e36SBarry Smith     Collective on SNES
12fee21e36SBarry Smith 
13c7afd0dbSLois Curfman McInnes     Input Parameters:
14c7afd0dbSLois Curfman McInnes +   snes - nonlinear solver object
15c7afd0dbSLois Curfman McInnes .   x1 - location at which to evaluate Jacobian
167fb41025SPeter Brune -   ctx - MatFDColoring context or NULL
17c7afd0dbSLois Curfman McInnes 
18c7afd0dbSLois Curfman McInnes     Output Parameters:
19c7afd0dbSLois Curfman McInnes +   J - Jacobian matrix (not altered in this routine)
20*56744491SBarry Smith -   B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
21c7afd0dbSLois Curfman McInnes 
2236851e7fSLois Curfman McInnes     Level: intermediate
2336851e7fSLois Curfman McInnes 
247fb41025SPeter Brune .notes: If the coloring is not provided through the context, this will first try to get the
257fb41025SPeter Brune         coloring from the DM.  If the DM type has no coloring routine, then it will try to
267fb41025SPeter Brune         get the coloring from the matrix.  This requires that the matrix have nonzero entries
277086a01eSPeter Brune         precomputed.  This is discouraged, as MatColoringApply() is not parallel by default.
28b0ae01b7SPeter Brune 
29b4fc646aSLois Curfman McInnes .keywords: SNES, finite differences, Jacobian, coloring, sparse
3091627157SBarry Smith 
318d359177SBarry Smith .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESComputeJacobianDefault()
32ab637aeaSJed Brown           MatFDColoringCreate(), MatFDColoringSetFunction()
33cb5b572fSBarry Smith 
3491627157SBarry Smith @*/
3595d750ceSBarry Smith 
36d1e9a80fSBarry Smith PetscErrorCode  SNESComputeJacobianDefaultColor(SNES snes,Vec x1,Mat J,Mat B,void *ctx)
3791627157SBarry Smith {
387fb41025SPeter Brune   MatFDColoring  color = (MatFDColoring)ctx;
39dfbe8321SBarry Smith   PetscErrorCode ierr;
404e269d77SPeter Brune   DM             dm;
414e269d77SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
424e269d77SPeter Brune   Vec            F;
434e269d77SPeter Brune   void           *funcctx;
44335efc43SPeter Brune   MatColoring    mc;
454e269d77SPeter Brune   ISColoring     iscoloring;
46b0ae01b7SPeter Brune   PetscBool      hascolor;
47aa2f1b4eSJed Brown   PetscBool      solvec,matcolor = PETSC_FALSE;
48dff777c9SBarry Smith 
493a40ed3dSBarry Smith   PetscFunctionBegin;
507fb41025SPeter Brune   if (color) PetscValidHeaderSpecific(color,MAT_FDCOLORING_CLASSID,6);
5194ab13aaSBarry Smith   else {ierr  = PetscObjectQuery((PetscObject)B,"SNESMatFDColoring",(PetscObject*)&color);CHKERRQ(ierr);}
5222d28d08SBarry Smith   ierr  = SNESGetFunction(snes,&F,&func,&funcctx);CHKERRQ(ierr);
534e269d77SPeter Brune   if (!color) {
544e269d77SPeter Brune     ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
55b0ae01b7SPeter Brune     ierr = DMHasColoring(dm,&hascolor);CHKERRQ(ierr);
56924833adSPeter Brune     matcolor = PETSC_FALSE;
57c3138ed3SPeter Brune     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_fd_color_use_mat",&matcolor,NULL);CHKERRQ(ierr);
58c3138ed3SPeter Brune     if (hascolor && !matcolor) {
59b412c318SBarry Smith       ierr = DMCreateColoring(dm,IS_COLORING_GLOBAL,&iscoloring);CHKERRQ(ierr);
6094ab13aaSBarry Smith       ierr = MatFDColoringCreate(B,iscoloring,&color);CHKERRQ(ierr);
614e269d77SPeter Brune       ierr = MatFDColoringSetFunction(color,(PetscErrorCode (*)(void))func,funcctx);CHKERRQ(ierr);
624e269d77SPeter Brune       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6394ab13aaSBarry Smith       ierr = MatFDColoringSetUp(B,iscoloring,color);CHKERRQ(ierr);
64f86b9fbaSHong Zhang       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
65b0ae01b7SPeter Brune     } else {
6694ab13aaSBarry Smith       ierr = MatColoringCreate(B,&mc);CHKERRQ(ierr);
67335efc43SPeter Brune       ierr = MatColoringSetDistance(mc,2);CHKERRQ(ierr);
68335efc43SPeter Brune       ierr = MatColoringSetType(mc,MATCOLORINGSL);CHKERRQ(ierr);
69335efc43SPeter Brune       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
70335efc43SPeter Brune       ierr = MatColoringApply(mc,&iscoloring);CHKERRQ(ierr);
71335efc43SPeter Brune       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7294ab13aaSBarry Smith       ierr = MatFDColoringCreate(B,iscoloring,&color);CHKERRQ(ierr);
73b0ae01b7SPeter Brune       ierr = MatFDColoringSetFunction(color,(PetscErrorCode (*)(void))func,(void*)funcctx);CHKERRQ(ierr);
74b0ae01b7SPeter Brune       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7594ab13aaSBarry Smith       ierr = MatFDColoringSetUp(B,iscoloring,color);CHKERRQ(ierr);
76f86b9fbaSHong Zhang       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
77b0ae01b7SPeter Brune     }
7894ab13aaSBarry Smith     ierr = PetscObjectCompose((PetscObject)B,"SNESMatFDColoring",(PetscObject)color);CHKERRQ(ierr);
794108615fSPeter Brune     ierr = PetscObjectDereference((PetscObject)color);CHKERRQ(ierr);
804a9d489dSBarry Smith   }
8195862db2SPeter Brune 
82c89319d2SPeter Brune   /* F is only usable if there is no RHS on the SNES and the full solution corresponds to x1 */
83c89319d2SPeter Brune   ierr = VecEqual(x1,snes->vec_sol,&solvec);CHKERRQ(ierr);
84c89319d2SPeter Brune   if (!snes->vec_rhs && solvec) {
85432bb422SPeter Brune     ierr = MatFDColoringSetF(color,F);CHKERRQ(ierr);
8695862db2SPeter Brune   }
87d1e9a80fSBarry Smith   ierr = MatFDColoringApply(B,color,x1,snes);CHKERRQ(ierr);
8894ab13aaSBarry Smith   if (J != B) {
8994ab13aaSBarry Smith     ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9094ab13aaSBarry Smith     ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
91194405b3SBarry Smith   }
923a40ed3dSBarry Smith   PetscFunctionReturn(0);
9391627157SBarry Smith }
94