[petsc-users] Getting vectors of field variables of a DMPlex

Matthew Knepley knepley at gmail.com
Thu Jul 24 05:59:51 CDT 2025


On Thu, Jul 24, 2025 at 1:46 AM Andrea Irwin via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> Hi,
>
> I'm new to PETSc, and I'm trying to learn how to use the DMPlex object.
> I've learned that you can assign labeled "fields" to a PETScSection/DMPlex,
> but I'm a bit lost as to how to access the data corresponding to each field.
>
> The documentation suggests that data from different fields is stored in
> the same big vector, and you can access it with
> PetscSectionGetFieldOffset(). I've tried this in the snippet below (using
> petsc4py)
>
> # get points from the layout
> pstart, pend = section.getChart()
>
> # set vector values according to field
> arr = vec.getArray()
>
> for point in range(pstart, pend):
>     print(point)
>     offset_u = section.getFieldOffset(point, 0)
>     offset_v = section.getFieldOffset(point, 1)
>     offset_w = section.getFieldOffset(point, 2)
>
>     print(offset_u, offset_v, offset_w)
>
>     arr[offset_u] = 1
>
>     for d in range(num_comp[1]):
>         arr[offset_v + d] = 2
>
>     for d in range(num_comp[2]):
>         arr[offset_w + d] = 3
>
> Unfortunately, this either this doesn't work or I'm not using it right.
> The offsets are often equal when they should be different for different
> variables, and they get larger than the number of points in the vector.
> Despite being recommended directly in the documentation, there aren't any
> examples of it being used in context either.
>
1. The above looks like it should work. Feel free to send a small example
we can run.

2. It would be a good check to also call section.getFieldDof() to see if
that field has any unknowns on the point

3. You would expect dof offsets to be larger than the number of points.
Even if each field only has 1 dof, the offsets will be 3 * Npoints.

4. We do not normally use this method in examples because for the
situations we show, it is easier to use DMCreateSubDM() to pull out the
entire subvector, or to use DMPlexPoint*() to get a pointer directly to the
data.

> A previous entry in the mailing list suggests using DMCreateSubDM() to
> instead split the DM (and the vectors, in the process) up by field. There's
> also DMCreateFieldDecomposition(), which appears to do something similar.
>
> Is there a "canonical" way to access different fields? Does anyone have
> some simple examples of different field variables being declared,
> allocated, set, and accessed? I'm still new enough that I'm not even sure
> what questions to ask at this point, but any help would be appreciated.
>
There is no canonical way because there are many different reasons for
dof access.

Most of the examples, say in KSP, SNES, TS, and TAO are using the FE
assembly interface and not setting values by hand.

Most of the test/tutorials under Plex are directly setting coordinates, but
not fields. However,

  Plex tutorial ex6.c: Uses DMPlexPoint to look at spectral element
access patterns
  Plex test ex26: Uses CreateSubDM() extensively

  Thanks,

     Matt


> Thank you,
>
> Andrea Irwin
>


-- 
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!cn7IrTrgZMR2vAc2HvctUIDWZOfBdYI7H7njJ98SzhIKVtz8LCZqfDmhvC4PycTNu4HoKzTXpRyf04eSnfYU$  <https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!cn7IrTrgZMR2vAc2HvctUIDWZOfBdYI7H7njJ98SzhIKVtz8LCZqfDmhvC4PycTNu4HoKzTXpRyf0-nOcH0s$ >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250724/2ad8bf60/attachment.html>


More information about the petsc-users mailing list