[petsc-users] Switching between sections of the same DM

Matthew Knepley knepley at gmail.com
Mon Feb 9 06:41:00 CST 2026


On Mon, Feb 9, 2026 at 6:29 AM Aldo Bonfiglioli <aldo.bonfiglioli at unibas.it>
wrote:

> Hi there,
>
> I am trying to switch btw two different sections (section_l(1:2)) defined
> on the same DM (one section for the dependent variable, the other for their
> gradient)
>
> !
> ! dependent variables and their nodal gradient
> !
>   PetscCall(DMSetLocalSection(dm, section_l(1), ierr)) ! dependent
> variables
>   PetscCall(DMGetLocalVector(dm, localu, ierr)) ! dependent variables
>   PetscCall(DMGlobaltoLocal(dm, u, INSERT_VALUES, localu, ierr))
>   PetscCall(VecSet(gradu, 0.d0, ierr))
>   PetscCall(DMSetLocalSection(dm, section_l(2), ierr)) ! gradient of the
> dependent variables
>   PetscCall(DMGetLocalVector(dm, localdu, ierr)) ! gradient of the
> dependent variables
>   PetscCall(VecSet(localdu, 0.d0, ierr))
> !
>   call innerloop(dm, localu, localdu, section_l, ierr)
>
> subroutine innerloop is supposed to do work on both Vecs.
>
> When trying to do so, I get the following error:
>
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: Object is in wrong state
> [0]PETSC ERROR: Clearing DM of local vectors that has a local vector
> obtained with DMGetLocalVector()
>
> as if two sections living simultaneously on the same DM are not allowed.
>
> Should I instead clone the DM and create the "second" section of the clone?
>
> Yes, this is exactly right. It is lightweight, and this is what clone is
intended for.

  Thanks,

     Matt

> Thanks,
>
> Aldo
>
> --
> Dr. Aldo Bonfiglioli
> Associate professor of Fluid Mechanics
> Dipartimento di Ingegneria
> Universita' della Basilicata
> V.le dell'Ateneo Lucano, 10 85100 Potenza ITALY
> tel:+39.0971.205203 fax:+39.0971.205215
> web: https://urldefense.us/v3/__http://docenti.unibas.it/site/home/docente.html?m=002423__;!!G_uCfscf7eWS!aoLehySjF-RiaGi_oyCRn_dYj6QvTlk5w028Xwvyvfl42tLcztJ0dySq0cpcEjn2g2b_md_LL-09Jcu-WXxH$  <https://urldefense.us/v3/__http://docenti.unibas.it/site/home/docente.html?m=002423__;!!G_uCfscf7eWS!YVPmxuMFuutpshSi89LXW-8W6oF1lauFcuN2HzqwcvecXbGkhDoRlhVqyIdHvFHEXN7_zUPSCbdoeElWRBtO1fQ_1q-v4pzF0QU$>
>
>

-- 
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://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!aoLehySjF-RiaGi_oyCRn_dYj6QvTlk5w028Xwvyvfl42tLcztJ0dySq0cpcEjn2g2b_md_LL-09JRBYgR6D$  <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!aoLehySjF-RiaGi_oyCRn_dYj6QvTlk5w028Xwvyvfl42tLcztJ0dySq0cpcEjn2g2b_md_LL-09JYjpzJhA$ >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20260209/cdd90101/attachment.html>


More information about the petsc-users mailing list