<div dir="ltr"><div dir="ltr">On Thu, Jul 24, 2025 at 1:46 AM Andrea Irwin via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>> wrote:</div><div class="gmail_quote gmail_quote_container"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>




<div>
<p>Hi,</p>
<p>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.</p>
<p>The documentation suggests that data from different fields is stored in the same big vector, and you can access it with
<font face="Courier New, Courier, monospace">PetscSectionGetFieldOffset()</font>. I've tried this in the snippet below (using petsc4py)</p>
<p><font face="Courier New, Courier, monospace" size="2"># get points from the layout<br>
pstart, pend = section.getChart()<br>
<br>
# set vector values according to field<br>
arr = vec.getArray()<br>
<br>
for point in range(pstart, pend):<br>
    print(point)<br>
    offset_u = section.getFieldOffset(point, 0)<br>
    offset_v = section.getFieldOffset(point, 1)<br>
    offset_w = section.getFieldOffset(point, 2)<br>
<br>
    print(offset_u, offset_v, offset_w)<br>
<br>
    arr[offset_u] = 1<br>
<br>
    for d in range(num_comp[1]):<br>
        arr[offset_v + d] = 2<br>
<br>
    for d in range(num_comp[2]):<br>
        arr[offset_w + d] = 3</font><br>
</p>
<p>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.<br></p></div></blockquote><div>1. The above looks like it should work. Feel free to send a small example we can run.</div><div><br></div><div>2. It would be a good check to also call section.getFieldDof() to see if that field has any unknowns on the point</div><div><br></div><div>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.</div><div><br></div><div>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.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><p>
</p>
<p>A previous entry in the mailing list suggests using <font face="Courier New, Courier, monospace">
DMCreateSubDM()</font> to instead split the DM (and the vectors, in the process) up by field. There's also
<font face="Courier New, Courier, monospace">DMCreateFieldDecomposition()</font>, which appears to do something similar.</p>
<p>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.</p></div></blockquote><div>There is no canonical way because there are many different reasons for dof access.</div><div><br></div><div>Most of the examples, say in KSP, SNES, TS, and TAO are using the FE assembly interface and not setting values by hand.</div><div><br></div><div>Most of the test/tutorials under Plex are directly setting coordinates, but not fields. However,</div><div><br></div><div>  Plex tutorial ex6.c: Uses DMPlexPoint to look at spectral element access patterns</div><div>  Plex test ex26: Uses CreateSubDM() extensively</div><div><br></div><div>  Thanks,</div><div><br></div><div>     Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<p>Thank you,</p>
<p>Andrea Irwin<br>
</p>
</div>

</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="https://urldefense.us/v3/__http://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!cn7IrTrgZMR2vAc2HvctUIDWZOfBdYI7H7njJ98SzhIKVtz8LCZqfDmhvC4PycTNu4HoKzTXpRyf0-nOcH0s$" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>