<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Dear all, </div><div dir="ltr"><br></div><div>This request stems from the "How to add a source term for PETSCFV ?" thread.</div><div>Basically, I would like to output in a VTK file all the components of the gradient computed using the following piece of code (given sol is a global representation of the solution over the whole mesh described by the manager dm with a unique field fvm).</div><div><br></div><div><pre lang="c"><font face="arial, sans-serif"><span id="gmail-m_3741842111645541700gmail-LC1509" lang="c"><span>  ierr</span> <span>=</span> <span>PetscFVSetComputeGradients</span><span>(</span><span>fvm</span><span>,</span><span>PETSC_TRUE</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1511" lang="c">  <span>ierr</span> <span>=</span> <span>DMConvert</span><span>(</span><span>dm</span><span>,</span> <span>DMPLEX</span><span>,</span> <span>&</span><span>plex</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1512" lang="c">  <span>ierr</span> <span>=</span> <span>DMPlexGetDataFVM</span><span>(</span><span>plex</span><span>,</span> <span>fvm</span><span>,</span> <span>&</span><span>cellGeom</span><span>,</span> <span>&</span><span>faceGeom</span><span>,</span> <span>&</span><span>gradDM</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1513" lang="c">  <span>ierr</span> <span>=</span> <span>DMCreateLocalVector</span><span>(</span><span>plex</span><span>,</span><span>&</span><span>locX</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1514" lang="c">  <span>ierr</span> <span>=</span> <span>DMPlexInsertBoundaryValues</span><span>(</span><span>plex</span><span>,</span> <span>PETSC_TRUE</span><span>,</span> <span>locX</span><span>,</span> <span>0</span><span>.</span><span>0</span><span>,</span> <span>faceGeom</span><span>,</span> <span>cellGeom</span><span>,</span> <span>NULL</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1515" lang="c">  <span>ierr</span> <span>=</span> <span>DMGlobalToLocalBegin</span><span>(</span><span>plex</span><span>,</span> <span>sol</span><span>,</span> <span>INSERT_VALUES</span><span>,</span> <span>locX</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1516" lang="c">  <span>ierr</span> <span>=</span> <span>DMGlobalToLocalEnd</span>  <span>(</span><span>plex</span><span>,</span> <span>sol</span><span>,</span> <span>INSERT_VALUES</span><span>,</span> <span>locX</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1517" lang="c">  <span>ierr</span> <span>=</span> <span>DMCreateGlobalVector</span><span>(</span><span>gradDM</span><span>,</span> <span>&</span><span>grad</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1518" lang="c">  <span>ierr</span> <span>=</span> <span>DMPlexReconstructGradientsFVM</span><span>(</span><span>plex</span><span>,</span> <span>locX</span><span>,</span> <span>grad</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1519" lang="c">  <span>ierr</span> <span>=</span> <span>DMCreateLocalVector</span><span>(</span><span>gradDM</span><span>,</span> <span>&</span><span>locGrad</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1520" lang="c">  <span>ierr</span> <span>=</span> <span>DMGlobalToLocalBegin</span><span>(</span><span>gradDM</span><span>,</span> <span>grad</span><span>,</span> <span>INSERT_VALUES</span><span>,</span> <span>locGrad</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1521" lang="c">  <span>ierr</span> <span>=</span> <span>DMGlobalToLocalEnd</span><span>(</span><span>gradDM</span><span>,</span> <span>grad</span><span>,</span> <span>INSERT_VALUES</span><span>,</span> <span>locGrad</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1522" lang="c">  <span>ierr</span> <span>=</span> <span>VecDestroy</span><span>(</span><span>&</span><span>grad</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span></font></pre><pre lang="c"><font face="arial, sans-serif"><span>  ierr</span> <span>=</span> <span>PetscViewerCreate</span><span>(</span><span>PetscObjectComm</span><span>((</span><span>PetscObject</span><span>)gradDM</span><span>),</span> &<span>viewer</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></font></pre><pre lang="c"><pre lang="c"><font face="arial, sans-serif">  <span>ierr</span> <span>=</span> <span>PetscSNPrintf</span><span>(</span><span>filename</span><span>,</span><span>sizeof</span> <span>filename</span><span>,</span><span>"%s-%03D-gradient.vtu"</span><span>,</span><span>user</span><span>-></span><span>outputBasename</span><span>,</span><span>stepnum</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span><span lang="c"><br></span></font></pre><pre lang="c"><font face="arial, sans-serif"><span id="gmail-m_3741842111645541700gmail-LC1345" lang="c">  <span>ierr</span> <span>=</span> <span>PetscViewerSetType</span><span>(</span><span>viewer</span><span>,</span> <span>PETSCVIEWERVTK</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span>
<span id="gmail-m_3741842111645541700gmail-LC1346" lang="c">  <span>ierr</span> <span>=</span> <span>PetscViewerFileSetName</span><span>(</span><span>viewer</span><span>,</span> <span>filename</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span></font></pre></pre></div><div><pre lang="c"><font face="arial, sans-serif"><span id="gmail-m_3741842111645541700gmail-LC1464" lang="c">  <span>ierr</span> <span>=</span> <span>VecView</span><span>(locGrad</span><span>,</span><span>viewer</span><span>);</span><span>CHKERRQ</span><span>(</span><span>ierr</span><span>);</span></span></font>
</pre></div><div dir="ltr"><br></div><div>I am having troubles with a few things I think but I cannot figure out how to put it together in the right order ... </div><div>(i) I am not sure whether VecView should be called with a local or a global vector</div><div>(ii) I can see from the error log that gradDM does not have fields and thus when it goes through dmplexvtu.c it crashes and cannot write the file. I tried to add the fvm as a field, but it does not work either, it crashes on a memory corruption.</div><div dir="ltr"><br></div><div dir="ltr">Could somebody please advise ?<br></div><div dir="ltr"><br></div><div>Thank you very much in advance,</div><div dir="ltr"><br clear="all"><div><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><div><div><div><div>Thibault Bridel-Bertomeu<br>—<br></div></div></div></div>Eng, MSc, PhD</div><div>Research Engineer</div><div>CEA/CESTA</div><div>33114 LE BARP</div><div>Tel.: (+33)557046924</div><div>Mob.: (+33)611025322<br></div><div>Mail: <a href="mailto:thibault.bridelbertomeu@gmail.com" target="_blank">thibault.bridelbertomeu@gmail.com</a><br></div></div></div></div></div></div></div><br></div><div class="gmail_quote"><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"><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><br></div></div></div></div></div>
</blockquote></div></div>
</blockquote></div></div></div></div></div></div></div>