[petsc-users] DMPlex doubt

Brandon Denton bldenton at buffalo.edu
Sat Jul 24 20:31:20 CDT 2021

Good Evening Miguel,

I've successfully used the following to get the coordinates of all
nodes/vertices from a DMPlex. The steps I use are as follow

----- CODE ------
    Vec                      coordinates;         // Define a  Petsc vector
to store the coordinates.
    PetscInt               cdm;                    // Define a PetscInt to
store coordinate dimension of DMPlex
    PetscInt              *vSize;                 // PetscInt to store
number of elements in vector (used below)
    PetscErrorCode   ierr;

    ierr = DMGetCoordinateDM(dm, &cdm); CHKERRQ(ierr);               // Get
coordinate dimension of dm
    ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);    //
Populate Vector with (x, y, z) coordinates of all nodes/vectors in DM
    ierr = VecGetSize(coordinates, &vSize);         // Get Number of
elements in coordinates vector

    PetscScalar               coords[vSize];            // Define array
where the (x, y, z) values will be stored
    const PetscInt            ix[vSize];                   // Define array
to store indices of coordinates you'd like to get values for

    // Load ix[] with indices of Vector coordinate[] you want values for.
In your case, you would like all of them
    for (int ii = 0; ii < vSize; ++ii){
         ix[ii] = ii;      // Note: Petsc uses 0-based indexing

    ierr = VecGetValues(coordinates, vSize, ix, coords);       // Get (x,
y, z) values from Vector coordinates and store in coords[] array.

    // All (x, y, z) coordinates for all nodes/vertices should now be in
the coords[] array.
    // They are stored interlaced. i.e. (x_0, y_0, z_0, x_1, y_1, z_1, ....
x_n-1, y_n-1, z_n-1)

    // Print out each nodes (x, y, z) coordinates to screen. I assuming 3
    for (int ii = 0; ii < vSize; ii+3){
         ierr = PetscPrintf(PETSC_COMM_SELF, "     Node %d :: (x,y,z) =
(%lf, %lf, %lf) \n", coords[ii], coords[ii+1],coords[ii+2]);

---- END OF CODE  ----

Please forgive any coding errors/typos that may be above but the technique
should work. I don't claim that this is the most elegant solution.

Good Luck


On Sat, Jul 24, 2021 at 9:58 AM Miguel Angel Tapia <miguel.td19 at outlook.com>

> Hello. I am a master's student in Mexico. I am currently working on a
> project in which we are implementing DMPlex in a code for electromagnetic
> modeling. Right now I am working on understanding the tool in C. But I'm
> stuck on something and that's why my next doubt:
> I am trying to get the coordinates of the nodes of a mesh in DMPlex. I
> already understood how the DAG is structured, how to obtain the nodes that
> make up some point. But the ordering of the nodes changes in DMPlex. So I
> need to know the coordinates of each node to compare them with my initial
> mesh and confirm that the same nodes form the same point in the software I
> am using as well as in the DMPlex DAG.
> It would be great if you could guide me a bit on how to do this or
> indicate which DMPlex examples would be good to review or which examples
> solve something similar to my situation.
> Thank you in advance. Regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20210724/8c69d219/attachment.html>

More information about the petsc-users mailing list