<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Jul 18, 2018 at 10:15 PM David Knezevic <<a href="mailto:david.knezevic@akselos.com">david.knezevic@akselos.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jul 18, 2018 at 3:34 PM, Matthew Knepley <span dir="ltr"><<a href="mailto:knepley@gmail.com" target="_blank">knepley@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"><div class="gmail_quote"><span><div dir="ltr">On Wed, Jul 18, 2018 at 3:25 PM David Knezevic <<a href="mailto:david.knezevic@akselos.com" target="_blank">david.knezevic@akselos.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jul 18, 2018 at 1:59 PM, Matthew Knepley <span dir="ltr"><<a href="mailto:knepley@gmail.com" target="_blank">knepley@gmail.com</a>></span> wrote:<br><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"><span class="m_7065104706575120339m_8651620153512068023m_4623372581018041155gmail-"><div dir="ltr">On Wed, Jul 18, 2018 at 1:31 PM David Knezevic <<a href="mailto:david.knezevic@akselos.com" target="_blank">david.knezevic@akselos.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>I'm using SNES for a finite element contact solve, in which the sparsity pattern of the jacobian can change from one Newton iteration to the next (since the nodes on the contact surface move).</div><div><br></div><div>In order to handle this I figured the best way would be to destroy the jacobian matrix and re-allocate it with a new sparsity pattern inside each call to FormJacobian, does that seem like a reasonable approach in this context?</div></div></blockquote><div><br></div></span><div>Yes.</div><span class="m_7065104706575120339m_8651620153512068023m_4623372581018041155gmail-"><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> Also, I recall from an earlier discussion that this matrix re-allocation inside FormJacobian is supported by SNES, but I just wanted to confirm that?</div></div></blockquote><div><br></div></span><div>Yes.</div><span class="m_7065104706575120339m_8651620153512068023m_4623372581018041155gmail-"><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>Also, I was wondering if there is any example where the matrix is re-allocated inside SNES iterations so that I can make sure that I do it correctly?</div></div></blockquote><div><br></div></span><div>No, unfortunately. Contributions always welcome :)</div></div></div></blockquote><div><br></div><div><br></div><div>OK, as a test case I'd like to modify snes/tutorials/ex1.c to destroy and reallocate the jacobian matrix with the same sparsity pattern inside FormJacobian1 (once I can do that with the same sparsity pattern, then it should be straightforward to do the same thing with a modified sparsity pattern). To do that, I tried adding the following code inside <span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">FormJacobian1:</span></div><div><br></div><div><div>  ierr = MatDestroy(&B);</div></div></div></div></div></blockquote><div><br></div></span><div>You do not want to destroy the matrix. There would be no way to get another Mat back out of the function.</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 class="gmail_extra"><div class="gmail_quote"><div><div>  ierr = MatCreate(PETSC_COMM_WORLD,&B);CHKERRQ(ierr);</div></div></div></div></div></blockquote><div><br></div><div>And do not recreate it.</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 class="gmail_extra"><div class="gmail_quote"><div><div>  ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,2,2);CHKERRQ(ierr);</div></div></div></div></div></blockquote><div><br></div><div>Nor reset the sizes. However, you do want to call</div><div><br></div><div>  MatSetPreallocationXAIJ();</div><div><br></div><div>which should work</div><span><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div>  ierr = MatSetFromOptions(B);CHKERRQ(ierr);</div><div>  ierr = MatSetUp(B);CHKERRQ(ierr);</div></div></div></div></div></blockquote><div><br></div></span><div>  THanks,</div><div><br></div><div>    Matt</div></div></div></blockquote><div><br></div><div>Thanks! Calling <span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">MatSetPreallocationXAIJ inside FormJacobian works for me.</span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div><span style="font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">So I guess this means that <span style="text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">we create a new sparsity pattern "from scratch" </span>each time we call <span style="text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">MatSetPreallocationXAIJ and set the values in the matrix?</span></span></div></div></div></div></blockquote><div><br></div><div>Yes.</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 class="gmail_extra"><div class="gmail_quote"><div>Thanks,<br>David</div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" 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>