<div dir="ltr"><div>In that case, from your original message, it seems like the issue might be in the <br></div><div><br></div><div> // populate edofF, edofT <br></div><div><br></div><div>Or, perhaps, it's simply due to the fact that the numbering/ordering depends on the number of ranks. This is illustrated in this image in the manual: <a href="https://petsc.org/release/docs/manual/vec/#fig-daao">https://petsc.org/release/docs/manual/vec/#fig-daao</a></div><div><br></div><div>This is for DMDA but DMStag does something similar. There is a "natural" ordering, which is the ordering you'd get with a single rank. There's also "PETSc ordering" which depends on the number of ranks - in this case each rank's portion of the vector is a contiguous range. <br></div><div><br></div><div>If this might be the explanation for your problem, note that for DMDA there are already some helper functions which can map "PETSc" to "natural" ordering. While these obviously require a lot of communication between ranks and so you'd want to avoid them in production runs, these are useful in situations where you want to directly compare vectors on different numbers of ranks for diagnostic or I/O purposes.</div><div><br></div><div>E.g. see this man page:<br></div><div><a href="https://petsc.org/release/docs/manualpages/DMDA/DMDANaturalToGlobalBegin.html">https://petsc.org/release/docs/manualpages/DMDA/DMDANaturalToGlobalBegin.html</a></div><div><br></div><div>I haven't implemented the equivalent for DMStag, but if it's needed I can look at adding it.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Fr., 22. Apr. 2022 um 17:28 Uhr schrieb Carl-Johan Thore <<a href="mailto:carl-johan.thore@liu.se">carl-johan.thore@liu.se</a>>:<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>
<div>Thanks for the quick replies!</div>
<div><br>
</div>
<div>Using dmstag for the temperature, or even for both fields is a possibility of course. Preferably however I'd like to keep the temp. code as it is and be able to quickly switch between different methods for the flow, based on dmda, dmstag and so on, so
I'll try a bit more with the dmda-dmstag approach first. </div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<br>
<div></div>
<br>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-8400698669009399192divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Patrick Sanan <<a href="mailto:patrick.sanan@gmail.com" target="_blank">patrick.sanan@gmail.com</a>><br>
<b>Sent:</b> 22 April 2022 17:04<br>
<b>To:</b> Barry Smith <<a href="mailto:bsmith@petsc.dev" target="_blank">bsmith@petsc.dev</a>><br>
<b>Cc:</b> Carl-Johan Thore <<a href="mailto:carl-johan.thore@liu.se" target="_blank">carl-johan.thore@liu.se</a>>; <a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a> <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br>
<b>Subject:</b> Re: [petsc-users] Combining DMDA and DMStag</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr"><br>
</div>
<br>
<div>
<div dir="ltr">Am Fr., 22. Apr. 2022 um 16:04 Uhr schrieb Barry Smith <<a href="mailto:bsmith@petsc.dev" target="_blank">bsmith@petsc.dev</a>>:<br>
</div>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div><br>
</div>
We would need more details as to exactly what goes wrong to determine any kind of fix; my guess would be that the layout of the velocity vectors and temperature vectors is slightly different since the DMDA uses nelx+1 while the stag uses nelx and may split
things up slightly differently in parallel. You could try very small problems with say 2 or 4 ranks and put known values into the vectors and look at the ghost point update locations and exact locations in the local and global vectors to make sure everything
is where you expect it to be.
<div><br>
</div>
<div> There is also the possibility of using a DMStag for the temperature instead of DMDA since DMDA provides essentially a subset of the functionality of DMDA to get a more consistent layout of the unknowns in the two vectors.</div>
<div><br>
</div>
</div>
</blockquote>
<div><br>
This was going to be my first suggestion as well - one way to ensure compatibility would be to use DMStag for everything. E.g. you could create your temperature DMStag with only vertex/corner (dof30 degrees of freedom, and then create one or more a "compatible"
DMStags (same elements on each MPI rank) for your other fields, using DMStagCreateCompatibleDMStag() .</div>
<div><br>
</div>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div></div>
<div> Finally, one could use a single DMStag with all the unknowns but treat subvectors of the unknowns differently in your discretization and solve process. So assign velocity values (I guess) to the cell faces and temperature to the cell vertices, this will
give consistent parallel decomposition of values but you will have to manage the fact that the unknowns are interlaced into a single vector so your solver portions of the code may need to "pull" out appropriate subvectors.</div>
<div><br>
</div>
<div> Barry</div>
<div><br>
<div><br>
<blockquote type="cite">
<div>On Apr 22, 2022, at 9:45 AM, Carl-Johan Thore <<a href="mailto:carl-johan.thore@liu.se" target="_blank">carl-johan.thore@liu.se</a>> wrote:</div>
<br>
<div>
<div style="font-family:Helvetica;font-size:18px;font-style:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">Hi!<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"><u></u> <u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">I'm working on a convection-diffusion heat transfer problem. The temperature<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">is discretized using standard Q1 elements and a DMDA. The flow is modelled<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">using a stabilized Q1-Q0 method for which DMStag seemed like a good choice. The codes for the temperature<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">and flow work fine separately (both in serial and parallel), but when combined and running<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">in parallel, a problem sometimes arises in the assembly of the thermal system matrix.<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">Here’s a rough sketch of the combined code:<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"><u></u> <u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">// Create dmda for thermal problem and dmstag for flow problem<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">DMDACreate3d(PETSC_COMM_WORLD, bx, by, bz, DMDA_STENCIL_BOX, nelx+1, nely+1, nelz+1, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE,<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> 1, stencilWidth, 0, 0, 0, &dmthermal); <u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">…<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">// A bit of code to adjust Lx,Ly,Lz so that dmthermal and dmflow are compatible in the sense of having the same<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">// local elements<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">…<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">DMStagCreate3d(PETSC_COMM_WORLD, bx, by, bz, nelx, nely, nelz, md,nd,pd,<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif;text-indent:65.2pt">
<span lang="EN-US">3,0,0,0,DMSTAG_STENCIL_BOX,stencilWidth,Lx,Ly,Lz,&dmflow); <u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> <u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">PetscInt edofT[8]; // 8-noded element with 1 temp DOF per node<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">DMStagStencil edofF[24]; // 8 nodes with 3 velocity DOFs each<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"><u></u> <u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">// Assemble thermal system matrix K<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">for (PetscInt e=0 ...) // Loop over local elements<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">{<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif;text-indent:65.2pt">
<span lang="EN-US">// Populate edofF, edofT<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> // Get element velocities in ue from local velocity vector uloc<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> DMStagVecGetValuesStencil(dmflow,uloc,24,edof,ue);<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> ...<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> Ke = Ke_diffusion + Ke_convection(ue)<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> ...<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"> MatSetValuesLocal(K, 8, edofT, 8, edofT, Ke, ADD_VALUES);<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">}<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"><u></u> <u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">This always works fine in serial, but depending on the mesh and the number of ranks,<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">we don't always get the correct values in the element velocity vector ue. I suspect<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">this has something to do with the ordering of the elements and/or the DOFs, because<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">the elements in the global velocity vector are always the same but their order may change<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">(judging from the output of VecView at least).<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"><u></u> <u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">Is it possible to ensure compatibility between the dm:s, or find some kind of mapping<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US">between them, so that something along the lines of the code above always works?<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span lang="EN-US"><u></u> <u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span>Kind regards,<u></u><u></u></span></div>
<div style="margin:0cm;font-size:11pt;font-family:Calibri,sans-serif"><span>Carl-Johan</span></div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote></div>