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 @*/ 3763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultComputeJacobianColor(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx) 3891627157SBarry Smith { 3991627157SBarry Smith MatFDColoring color = (MatFDColoring) ctx; 40dfbe8321SBarry Smith PetscErrorCode ierr; 4177431f27SBarry Smith PetscInt freq,it; 423a7fca6bSBarry Smith Vec f; 434a9d489dSBarry Smith PetscErrorCode (*ff)(void),(*fd)(void); 44dff777c9SBarry Smith 453a40ed3dSBarry Smith PetscFunctionBegin; 46dff777c9SBarry Smith ierr = MatFDColoringGetFrequency(color,&freq);CHKERRQ(ierr); 47dff777c9SBarry Smith ierr = SNESGetIterationNumber(snes,&it);CHKERRQ(ierr); 48dff777c9SBarry Smith 49d079835cSBarry Smith if ((freq > 1) && ((it % freq))) { 50*ae15b995SBarry Smith ierr = PetscInfo2(color,"Skipping Jacobian recomputation, it %D, freq %D\n",it,freq);CHKERRQ(ierr); 51dff777c9SBarry Smith *flag = SAME_PRECONDITIONER; 52dff777c9SBarry Smith } else { 53*ae15b995SBarry Smith ierr = PetscInfo2(color,"Computing Jacobian, it %D, freq %D\n",it,freq);CHKERRQ(ierr); 5492abec31SBarry Smith *flag = SAME_NONZERO_PATTERN; 554a9d489dSBarry Smith ierr = SNESGetFunction(snes,&f,(PetscErrorCode (**)(SNES,Vec,Vec,void*))&ff,0);CHKERRQ(ierr); 564a9d489dSBarry Smith ierr = MatFDColoringGetFunction(color,&fd,PETSC_NULL);CHKERRQ(ierr); 574a9d489dSBarry Smith if (fd == ff) { /* reuse function value computed in SNES */ 583a7fca6bSBarry Smith ierr = MatFDColoringSetF(color,f);CHKERRQ(ierr); 594a9d489dSBarry Smith } 60005c665bSBarry Smith ierr = MatFDColoringApply(*B,color,x1,flag,snes);CHKERRQ(ierr); 61d964b20dSBarry Smith } 6232dfb669SBarry 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