<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El jue., 12 de sep. de 2019 a la(s) 17:01, Dave May (<a href="mailto:dave.mayhem23@gmail.com">dave.mayhem23@gmail.com</a>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br><div>Please always use "reply-all" so that your messages go to the list.<br>This is standard mailing list <span class="gmail-m_-4918740516533092344gmail-il" style="background-color:rgb(255,255,255)">etiquette</span>.  It is important to preserve<br>threading for people who find this discussion later and so that we do<br>not waste our time re-answering the same questions that have already<br>been answered in private side-conversations.  You'll likely get an<br>answer faster that way too. <br></div></div></div></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 12 Sep 2019 at 22:26, Emmanuel Ayala <<a href="mailto:juaneah@gmail.com" target="_blank">juaneah@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">Thank you for the answer.<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El jue., 12 de sep. de 2019 a la(s) 15:21, Dave May (<a href="mailto:dave.mayhem23@gmail.com" target="_blank">dave.mayhem23@gmail.com</a>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 12 Sep 2019 at 20:21, Emmanuel Ayala via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi everyone, it would be great if someone can give me a hint for this issue, i have been trying to figure out how to solve it, but i did not succeed<br><br>I'm using DMDA to generate a 3D mesh (DMDA_ELEMENT_Q1). I'm trying to fill a MPI matrix with some values wich are related to the dofs of each element node, moreover i need to set this values based on the element number. Something like:<br><br>mpi_A(total_elements X total_dofs)<br><br>                                                                    total_dofs<br>row_0 (element_0)                        a_0 a_1 a_2 ... a_23<br>row_1 (element_1)                                                     a_0 a_1 a_2 ... a_23<br>row_2 (element_2)                                                                                    a_0 a_1 a_2 ... a_23<br>.<br>.<br>.<br>row_n (element_n)                                     a_0 a_1 a_2 ... a_23 <br><br>The element number is related to the row index. And the matrix values are set depending of the DOFs related to the element.<br><br>With DMDAGetElements i can read the LOCAL nodes connected to the element and then the DOFs associated to the element. I can handle the local and global relations with DMGetLocalToGlobalMapping, MatSetLocalToGlobalMapping and MatSetValuesLocal. BUT i CAN NOT understand how to know the element number in LOCAL or GLOBAL contex. DMDAGetElements gives the NUMBER OF ELEMENTS owned in the local process, but there is not any information about the local or global ELEMENT NUMBER.<br><br>How to know the local or global element number related to the data provided by DMDAGetElements? <br></div></blockquote><div><br></div><div>The DMDA defines cells of the same type (quads (2D) or hex (3D), hence every cell defines the same number of vertices. <br></div></div></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><div><div>DMDAGetElements(DM dm,PetscInt *nel,PetscInt *nen,const PetscInt *e[])</div></div><div><div>nel<span style="white-space:pre-wrap">        </span>- number of local elements</div><div>nen<span style="white-space:pre-wrap">    </span>- number of element nodes</div><div>e<span style="white-space:pre-wrap">       </span>- the local indices of the elements' vertices</div></div><div><br></div><div>e[] defines the ordering of the elements. e[] is an array containing all of the element-vertex maps. Since each element in the DMDA has the same number of vertices, the first nen values in e[] correspond to the vertices (local index) associated with the first element. The next nen values in e[] correspond to the vertices of the second element.  The vertices for any (local) element with the index "cid" can be sought via e[nen*cid + i] where i would range from 0 to nen-1.</div><div><br></div></div></div></div></div></div></blockquote><div><br></div><div>You are right. I can handle the local information, i think the idea is:</div><div><div style="color:rgb(51,51,51);background-color:rgb(253,246,227);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-weight:normal;font-size:12px;line-height:16px;white-space:pre-wrap"><div><span style="color:rgb(51,51,51)"><br></span></div><div><span style="color:rgb(51,51,51)">    </span><span style="color:rgb(133,153,0)">for</span><span style="color:rgb(51,51,51)"> ( PetscInt i </span><span style="color:rgb(133,153,0)">=</span><span style="color:rgb(51,51,51)"> </span><span style="color:rgb(211,54,130)">0</span><span style="color:rgb(51,51,51)">; i </span><span style="color:rgb(133,153,0)"><</span><span style="color:rgb(51,51,51)"> nel; i</span><span style="color:rgb(133,153,0)">++</span><span style="color:rgb(51,51,51)"> )</span></div><div><span style="color:rgb(51,51,51)">            </span><span style="color:rgb(133,153,0)">for</span><span style="color:rgb(51,51,51)"> (PetscInt j </span><span style="color:rgb(133,153,0)">=</span><span style="color:rgb(51,51,51)"> </span><span style="color:rgb(211,54,130)">0</span><span style="color:rgb(51,51,51)">; j </span><span style="color:rgb(133,153,0)"><</span><span style="color:rgb(51,51,51)"> nen; j</span><span style="color:rgb(133,153,0)">++</span><span style="color:rgb(51,51,51)">)</span></div><div><span style="color:rgb(51,51,51)">                </span><span style="color:rgb(38,139,210)">PetscSynchronizedPrintf</span><span style="color:rgb(51,51,51)">(PETSC_COMM_WORLD,</span><span style="color:rgb(42,161,152)">"local element </span><span style="color:rgb(203,75,22)">%d</span><span style="color:rgb(42,161,152)"> : e[</span><span style="color:rgb(203,75,22)">%d</span><span style="color:rgb(42,161,152)">] = </span><span style="color:rgb(203,75,22)">%d\n</span><span style="color:rgb(42,161,152)">"</span><span style="color:rgb(51,51,51)">, i, j, e</span><span style="color:rgb(51,51,51)">[i</span><span style="color:rgb(133,153,0)">*</span><span style="color:rgb(51,51,51)">nen</span><span style="color:rgb(133,153,0)">+</span><span style="color:rgb(51,51,51)">j]);</span></div><div><span style="color:rgb(51,51,51)"><br></span></div></div> </div><div>BUT, it does not give information regarding to the ELEMENT identifier (number). I need the element number to ordering the elements inside of a MPI matrix. I want to access to each element data by means of the matrix row . I mean, in the row_0 there is the information (spreading through the columns) of the element_0.</div></div></div></blockquote><div><br></div><div>I think this is a mis-understanding. The element number is not related to a row in the matrix. </div></div></div></div></div></div></blockquote><div><br></div><div>Sorry, <span class="gmail-tlid-translation gmail-translation" lang="en"><span title="" class="gmail-">I did not express myself very well: I WANT TO CREATE a matrix which let me "access to each element data by means of the matrix row".<br></span></span></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 dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>The element is associated with vertices (basis functions), and each vertex (basis) in the DMDA is given a unique index. The index of that basis corresponds to a row (column) if it's a test (trial) function. So if you have any element defined by the array e[], you know how to insert values into a matrix by using the vertex indices. </div><div><br></div></div></div></div></div></div></blockquote><div><br></div><div>OK, i got it.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div></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 dir="ltr"><div class="gmail_quote"><div><br></div><div>The element vertices are numbered starting from 0, for each process. It does not give information about the element number.<br></div><div><br></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 dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>Why would you ever want, or need, the global element number? What is the use case?</div></div></div></div></div></div></blockquote><div> </div><div></div><div>I'm performing topology optimization, and it is part of gradient computation. I'm already have the analytic gradient.</div><div>I need the global element number to link nodal displacements with the element.</div></div></div></blockquote><div><br></div><div>Any nodal displacement, except those at the corners of your physical domain are associated with multiple elements. There isn't a one-to-one map between nodes and elements. </div><div><br></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 dir="ltr"><div class="gmail_quote"><div>I can use a local element number just if I have a equivalence between this local number and the global element number.</div></div></div></blockquote><div><br></div><div>I obviously don't understand what you want to do.</div><div><br></div></div></div></div></div></div></blockquote><div><br></div><div>Actually this is (below) the information that i need, thanks! Let me try it! :)<br></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 dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div></div><div>However, here is one way to achieve what you are asking for: specifically relating local element indices to global indices.</div><div>Assuming by "global element number" you are referring to what PETSc calls the "natural" ordering, then the dumbest way to convert from the local element index to the natural element index is the following: </div><div>(i) upon creation, use DMSetUniformCoordinates to define a unit 1 box;</div><div>(ii) compute the cell dimensions dx, dy associated with your uniform grid layout; </div><div>(iii) traverse through the e[] array return by DMDAGetElements. For each element, get the vertices and compute the centroid cx, cy and then compute<br></div><div>  PetscInt J = (PetscInt)(cy/dy);</div><div>  PetscInt I = (PetscInt)(cx/dx);</div><div>  PetscInt natural_id = I + J * mx;</div><div>where mx is the number of elements in the i direction in your domain (not the sub-domain).</div><div>You can determine mx by calling</div><div>  DMDAGetInfo(dm,NULL,&mx,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);<br></div><div>(iv) after you have computed natural_id for every element, layout the global coordinates of the DMDA however you want. Following that, you must call the following</div><div><br></div><div>  DM dm, cdm;</div><div>  Vec coor,lcoor;</div><div><br></div><div>  DMGetCoordinateDM(dm,&cdm);</div><div>  DMGetCoordinates(dm,&coor);</div><div>  DMGetCoordinatesLocal(dm,&lcoor);</div><div></div><div>  DMGlobalToLocal(cmd,coor,INSERT_VALUES,lcoor);</div><div><br></div><div>The above must be executed to ensure the new coordinates values are propagated to coords associated with your local sub-domain.</div><div><br></div><div>I doubt this will solve your _actual_ problem.</div></div></div></div></div></div></blockquote><div><br></div><div><br></div><div><br></div><div>Thanks for your time!</div><div>Best regards.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div> </div><div>Thanks,</div><div>Dave</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><div>Thanks,</div><div>Dave</div><div><br></div><div><br></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 dir="ltr"><br>Thank you.</div></blockquote></div></div></div></div></div></blockquote><div><br></div><div>Thanks! <br></div></div></div></blockquote></div></div></div></div></div></blockquote><div><br></div><div> </div></div></div>