1 #define PETSCSNES_DLL 2 3 #include "private/snesimpl.h" /*I "petscsnes.h" I*/ 4 5 #undef __FUNCT__ 6 #define __FUNCT__ "SNESDefaultComputeJacobianColor" 7 /*@C 8 SNESDefaultComputeJacobianColor - Computes the Jacobian using 9 finite differences and coloring to exploit matrix sparsity. 10 11 Collective on SNES 12 13 Input Parameters: 14 + snes - nonlinear solver object 15 . x1 - location at which to evaluate Jacobian 16 - ctx - coloring context, where ctx must have type MatFDColoring, 17 as created via MatFDColoringCreate() 18 19 Output Parameters: 20 + J - Jacobian matrix (not altered in this routine) 21 . B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 22 - flag - flag indicating whether the matrix sparsity structure has changed 23 24 Level: intermediate 25 26 .keywords: SNES, finite differences, Jacobian, coloring, sparse 27 28 .seealso: SNESSetJacobian(), SNESTestJacobian(), SNESDefaultComputeJacobian() 29 TSDefaultComputeJacobianColor(), MatFDColoringCreate(), 30 MatFDColoringSetFunction() 31 32 @*/ 33 34 PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx) 35 { 36 MatFDColoring color = (MatFDColoring) ctx; 37 PetscErrorCode ierr; 38 Vec f; 39 PetscErrorCode (*ff)(void),(*fd)(void); 40 41 PetscFunctionBegin; 42 *flag = SAME_NONZERO_PATTERN; 43 ierr = SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);CHKERRQ(ierr); 44 ierr = MatFDColoringGetFunction(color,&fd,PETSC_NULL);CHKERRQ(ierr); 45 if (fd == ff) { /* reuse function value computed in SNES */ 46 ierr = MatFDColoringSetF(color,f);CHKERRQ(ierr); 47 } 48 ierr = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr); 49 if (*J != *B) { 50 ierr = MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 51 ierr = MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 52 } 53 PetscFunctionReturn(0); 54 } 55 56 57 58