[petsc-users] Getting vectors of field variables of a DMPlex
Andrea Irwin
andrea.irwin at vuw.ac.nz
Thu Jul 24 00:46:03 CDT 2025
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.
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.
Thank you,
Andrea Irwin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20250724/8dcc30c6/attachment-0001.html>
More information about the petsc-users
mailing list