[petsc-users] Providing matrix vector product yourself for matrix free in SNES

Matthew Knepley knepley at gmail.com
Tue Dec 20 08:46:26 CST 2011

```On Tue, Dec 20, 2011 at 7:24 AM, <E.T.A.vanderWeide at utwente.nl> wrote:

> **
>
> Hi,
>
> I was wondering if it is possible to provide PETSc the matrix vector
> product yourself for a matrix free computation inside the SNES environment.
> The reason for asking is that the truncation error in the standard finite
> difference approach may lead to some noisy behavior for our application. We
> have found a nice way to get rid off the truncation error completely by
> using dual numbers for the creation of the Jacobian matrices. The same
> technique can be used for the matrix vector product inside SNES, but this
> means that the user has to provide this product to PETSc.
>
> To be more specific. I would like to solve the nonlinear problem F(U) = 0
> via the SNES environment using the matrix free option. So similarly to the
> function SNESSetFunction, which sets the function evaluation routine, I'm
> seeking for a function that sets the function to compute the matrix vector
> product dF/dU times V, where U and V are inputs. Is such a function
> available or is there a different way to provide the matrix vector product
> yourself?
>
No need to use the matrix-free option. Just provide a MATSHELL (
http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatCreateShell.html
)
as the Jacobian using SNESSetJacobian(). Then your FormJacobian() function
will get a vector 'x' as input. Update the context for the MATSHELL with
this vector in that function, which is all it would really do. Then when
the MatMult() is called on your shell matrix, compute the action using that
'x'
which is stored in the context.

Matt

> Thanks,
>
> Edwin
> ---------------------------------------------------
> Edwin van der Weide
> Department of Mechanical Engineering
> University of Twente
> Enschede, the Netherlands
>

--
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their