[petsc-users] Questions about PetscObjectCompose and DM user context

Matthew Knepley knepley at gmail.com
Mon Nov 19 02:41:26 CST 2012

On Sun, Nov 18, 2012 at 4:10 PM, Blaise A Bourdin <bourdin at lsu.edu> wrote:
> Hi,
> Following on last week's discussion with Jed on petsc-dev, I am looking a moving the DM's associated to parameters in a PDE from a user context to the DM associated with the solution field, using PetscObjectCompose.
> Let's imagine for a second that I want to solve the PDE
>         -div (k(x) \cdot u(x) ) = f + Boundary conditions,
> where k(x) is a a vector-valued coefficient and u(x) is the field I am solving for.
> Assume that the DM for u is dmU and the one for K is dmK, and that they have been created.
> Once both DMs are created, I can stash dmK inside dmU, using
>         PetscObjectCompose((PetscObject) dmU,"SomeName",(PetscObject) dmK);
> At this point, my understanding is that I can safely destroy dmK without causing a memory link, am I right?


> In my jacobian evaluation, I get a copy of dmK using PetscObjectQuery with the proper name:
>         PetscObjectQuery((PetscObject) dmU,"SomeName",(PetscObject*) &dmK);
> Again, I _can_ safely call DMDestroy(&dmK) at the end of the jacobian. Can I simply let dmK fall out of scope?

You do not destroy after a query. Just let it go out of scope.

> I assume that dmK gets automatically destroyed when dmU is destroyed, right?


> Finally, as far as I understand, I can either set the user context to the Jacobian function explicitly in the call to SNESSetJacobian or attach it to the DM using DMSetApplicationContext, or to the SNES using SNESSetApplicationContext. What would be the pros and cons of each technique? The same question would apply to DMCoarsenHookAdd.

I have not figured out what is the best yet. We are still
experimenting. I tend to favor
setting the DM describing a solution with SNESSetDM(), which you can
always get back.
Now all vectors that come from DMs have it attached, so you can use
VecGetDM(). Then
if you need auxiliary data for the solve, I think SNESSetAppCtx() is
right for that.



> Regards,
> Blaise
> --
> Department of Mathematics and Center for Computation & Technology
> Louisiana State University, Baton Rouge, LA 70803, USA
> Tel. +1 (225) 578 1612, Fax  +1 (225) 578 4276 http://www.math.lsu.edu/~bourdin

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

More information about the petsc-users mailing list