[petsc-users] Change Amat in FormJacobian

Matthew Knepley knepley at gmail.com
Sun Jun 13 11:21:05 CDT 2021


On Sun, Jun 13, 2021 at 10:55 AM Anton Popov <popov at uni-mainz.de> wrote:

> Hi,
>
> I want a simple(?) thing. I want to decide and be able to assign the
> Jacobian matrix (Amat) on the fly within the FormJacobian function (i.e.
> during Newton iteration) to one of the following options:
>
> 1) built-in MFFD approximation
> 2) assembled preconditioner matrix (Pmat)
>
> I have not found this scenario demonstrated in any example, therefore
> I’m asking how to do that.
>
> Currently I do the following:
>
> 1) setup Amat as a shell matrix with a MATOP_MULT operation that simply
> retrieves a matrix object form its context and calls MatMult on it.
>
> 2) if I need MFFD, I put a matrix generated with MatCreateSNESMF in the
> Amat context (of course I also call MatMFFDComputeJacobian before that).
>
> 3) if I need Pmat, I simply put Pmat in the Amat context.
>
> 4) call MatAssemblyBegin/End on Amat
>
> So far so good.
>
> However, shell Amat and assembled Pmat generate a problem if Galerkin
> multigrid is requested as a preconditioner (I just test on 1 CPU):
>
> [0]PETSC ERROR: MatPtAP requires A, shell, to be compatible with P,
> seqaij (Misses composed function MatPtAP_shell_seqaij_C)
> [0]PETSC ERROR: #1 MatPtAP()
> [0]PETSC ERROR: #2 MatGalerkin()
> [0]PETSC ERROR: #3 PCSetUp_MG()
> [0]PETSC ERROR: #4 PCSetUp()
> [0]PETSC ERROR: #5 KSPSetUp()
> [0]PETSC ERROR: #6 KSPSolve()
> [0]PETSC ERROR: #7 SNESSolve_NEWTONLS()
> [0]PETSC ERROR: #8 SNESSolve()
>
> It seems like PETSc tries to apply Galerkin coarsening to the shell Amat
> matrix instead of the assembled Pmat. Why?
>
> Pmat is internally generated by SNES using a DMDA attached to SNES, so
> it has everything to define Galerkin coarsening. And it actually works
> if I just get rid of the shell Amat.
>
> I can get around this problem by wrapping a PC object in a shell matrix
> and pass it as Pmat. This is a rather ugly approach, so I wonder how to
> resolve the situation in a better way, if it is possible.
>

Hi Anton,

You are correct that there is no specific test, so I can believe that a bug
might be lurking here.
I believe the way it is supposed to work is that you set the type of
Galerkin coarsening that you
want


https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/PC/PCMGSetGalerkin.html

So I am thinking you want 'pmat' only, and you would be in charge of making
the coarse MFFD operator
and telling PCMG about it. I could imagine that you might want us to
automatically do that, but we would
need some way to know that it is MFFD, and with the scheme above we do not
have that.

What is the advantage of switching representations during the Newton
iteration?

  Thanks,

     Matt


> Thanks.
> Anton
>
-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20210613/04c673df/attachment.html>


More information about the petsc-users mailing list