<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Nov 24, 2017 at 3:47 PM, Edoardo alinovi <span dir="ltr"><<a href="mailto:edoardo.alinovi@gmail.com" target="_blank">edoardo.alinovi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear petsc users,<div><br></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">I am new to petsc, but I am really enjoying it.</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">I am developing a CFD code in fortran and I have a (newby) question for you.</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">I have a subroutine that assembles the matrices arising from the finite volume method. They are sparse and the coefficients change every time step, but their sparsity remains unchanged (i.e. non-zeros entries are always in the same position).</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">In the subroutine, which is called every time step, I am basically following the manual and I do something like:</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- call MatCreate(PETSC_COMM_WORLD,A,<wbr>ierr)</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- call MatSetSizes(A,lm,lm,M,M,ierr)</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- call MatSetType(A, myType,ierr)</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- call MatMPIAIJSetPreallocation(A,d_<wbr>nz,PETSC_NULL_INTEGER, o_nz, PETSC_NULL_INTEGER,ierr)<span style="white-space:pre-wrap"> </span></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"> call MatSetUp(A,ierr)</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- call MatSetValues</font></div><div><span style="color:rgb(36,39,41);font-family:Arial,"Helvetica Neue",Helvetica,sans-serif">- call MatAssemblyBegin(A,MAT_FINAL_<wbr>ASSEMBLY,ierr)</span><br></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><div>- call MatAssemblyEnd(A,MAT_FINAL_<wbr>ASSEMBLY,ierr)</div></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- Solve the system</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">- call MatDestroy</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">Here is my question:</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">As you can note, every time step, I allocate the matrix, fill it, use it and the destroy it. Is there a way to avoid the matrix preallocation every time? I just would like to change matrix the entries and avoid the preallocation.</font></div></div></blockquote><div><br></div><div>Make the first 5 calls outside your timestep loop. At each step, (after the first timestep call MatZeroEntries()), MatSetValues() for all your values, and then matAssemblyBegin/End().</div><div>After all your timesteps, call MatDestroy(). If you use a factorization preconditioner, it will detect that you only change values, not structure.</div><div><br></div><div> Thanks,</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">Thank you for the help that you can give me,</font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif"><br></font></div><div><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif">Edoardo</font></div><div><br></div><div></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><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="http://www.caam.rice.edu/~mk51/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div>
</div></div>