#include "petscmat.h" PetscErrorCode MatStoreValues(Mat mat)Collect on Mat
Create Jacobian matrix
Set linear terms into matrix
Apply boundary conditions to matrix, at this time matrix must have
final nonzero structure (i.e. setting the nonlinear terms and applying
boundary conditions again will not change the nonzero structure
ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
ierr = MatStoreValues(mat);
Call SNESSetJacobian() with matrix
In your Jacobian routine
ierr = MatRetrieveValues(mat);
Set nonlinear terms in matrix
// build linear portion of Jacobian
ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
ierr = MatStoreValues(mat);
loop over nonlinear iterations
ierr = MatRetrieveValues(mat);
// call MatSetValues(mat,...) to set nonliner portion of Jacobian
// call MatAssemblyBegin/End() on matrix
Solve linear system with Jacobian
endloop
When this is called multiple times it overwrites the previous set of stored values and does not allocated additional space.