[petsc-users] matrix-free SNES with preconditioner

Matthew Knepley knepley at gmail.com
Wed Sep 5 20:37:39 CDT 2018


On Wed, Sep 5, 2018 at 9:34 PM Oleksandr Koshkarov <olk548 at mail.usask.ca>
wrote:

> Hello all,
>
> I got lost a little with petsc and want to clarify something.
>
> I want to do my nonlinear solves with matrix free method, but I want a
> real matrix preconditioner and invert it with LU.
>
> without preconditioner it is simple:
>
> Mat Jmf;
>
> MatCreateSNESMF(snes,&Jmf);
>
> SNESSetJacobian(snes,Jmf,Jmf,MatMFFDComputeJacobian,0);
>
>
> But with preconditioner, I am lost:
>
> Mat Jmf,J;
>
> MatCreateSNESMF(snes,&Jmf);
>
> DMCreateMatrix(da,&J);
>
> SNESSetJacobian(snes,Jmf,J,FromJacobian,0);
>
> //And here I have problems:
>
> PetscErrorCode FormJacobian(SNES snes,Vec X,Mat Jmf,Mat J,void *ptr){
>
> // I am constructing it only once in a while, not every nonlinear solve
> (this snes is inside time stepper).
>
> if (sometimes) {  MatSetValuesStencil(J,...); ...    }
>
>    MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
>    MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
>
> // assembling Jmf
>    MatAssemblyBegin(Jmf,MAT_FINAL_ASSEMBLY);
>    MatAssemblyEnd(Jmf,MAT_FINAL_ASSEMBLY);
>
>    return(0);
> }
>
> Here I have two concerns:
>
> 1) I am changing J only once in a while, because it is expansive to do
> otherwise. Am I doing it correctly? During iteration when I am not
> changing J, will petsc know this?


Yes, every matrix has a state variable that is updated when values are
changed.
The LU looks at this state variable to decide whether to refactor.


> I do not want petsc to perform LU
> every time when I am not changing J. And when I will change J, will it
> work?


Yes, but it is easy to check in the log. Use -log_view and look at the
number of calls
to PCLUFactorNumeric.


> (My snes is inside time stepper, and I want to construct
> preconditioner only say every 100 time steps)
>
> 2) What about Jmf? how can I let petsc know that it should use matrix
> free finite difference? Will it work if I let it as it is? (I think so,
> but I am not sure - my information source is
> ts/examples/tutorials/ex15.c, but I do not understand it fully)
>

It appears you have it correct. I always use MMS to make sure my solver is
doing what I expect.

  Thanks,

    Matt


> Thank you and best regards,
>
> Alex.
>
>

-- 
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/20180905/dc1e3058/attachment.html>


More information about the petsc-users mailing list