<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 26, 2018 at 3:10 PM, Smith, Barry F. <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</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"><span class="gmail-"><br>
<br>
> On Jan 26, 2018, at 2:15 PM, Kong, Fande <<a href="mailto:fande.kong@inl.gov">fande.kong@inl.gov</a>> wrote:<br>
><br>
><br>
><br>
> On Mon, Jan 8, 2018 at 2:15 PM, Smith, Barry F. <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:<br>
><br>
><br>
> > On Jan 8, 2018, at 2:59 PM, Alexander Lindsay <<a href="mailto:alexlindsay239@gmail.com">alexlindsay239@gmail.com</a>> wrote:<br>
> ><br>
> > Is there any elegant way to tell whether SNESComputeFunction is being called under different conceptual contexts?<br>
> ><br>
> > E.g. non-linear residual evaluation vs. Jacobian formation from finite differencing vs. Jacobian-vector products from finite differencing?<br>
><br>
> Under normal usage with the options database no.<br>
><br>
> Hi Barry,<br>
><br>
> How difficult to provide an API? Is it possible?<br>
><br>
><br>
><br>
> If you have some reason to know you could write three functions and provide them to SNESSetFunction(), MatMFFDSetFunction(), and MatFDColoringSetFunction(). Note that these functions have slightly different calling sequences but you can have all of them call the same underlying common function if you are only interested in, for example, how many times the function is used for each purpose.<br>
><br>
> If we use this way for the Jacobian-free Newton, the function evaluation will be called twice at the first linear iteration because the computed residual vector at the nonlinear step is not reused. Any way to reuse the function residual of the Newton step instead of recomputing a new residual at the first linear iteration?<br>
<br>
</span> It does reuse the function evaluation. Why do you think it does not? If you look at MatMult_MFFD() you will see the lines of code<br>
<br>
/* compute func(U) as base for differencing; only needed first time in and not when provided by user */<br>
if (ctx->ncurrenth == 1 && ctx->current_f_allocated) {<br>
ierr = (*ctx->func)(ctx->funcctx,U,F)<wbr>;CHKERRQ(ierr);<br>
}<br>
<br>
since the if is satisfied it does not compute the function at the base location. To double check I ran src/snes/examples/tutorials/<wbr>ex19 with -snes_mf in the debugger and verified that the "extra" function evaluations are not done.<br></blockquote><div><br></div><div>In MatAssemblyEnd_SNESMF,<br><br> if (j->func == (PetscErrorCode (*)(void*,Vec,Vec))SNESComputeFunction) {<br> ierr = SNESGetFunction(snes,&f,NULL,NULL);CHKERRQ(ierr);<br> ierr = MatMFFDSetBase_MFFD(J,u,f);CHKERRQ(ierr);<br> } else {<br> /* f value known by SNES is not correct for other differencing function */<br> ierr = MatMFFDSetBase_MFFD(J,u,NULL);CHKERRQ(ierr);<br> }<br><br><br></div><div>Will hit ierr = MatMFFDSetBase_MFFD(J,u,NULL);CHKERRQ(ierr), because SNES and MAT have different function pointers. <br><br></div><div>In MatMFFDSetBase_MFFD(Mat J,Vec U,Vec F), <br><br> if (F) {<br> if (ctx->current_f_allocated) {ierr = VecDestroy(&ctx->current_f);CHKERRQ(ierr);}<br> ctx->current_f = F;<br> ctx->current_f_allocated = PETSC_FALSE;<br> } else if (!ctx->current_f_allocated) {<br> ierr = MatCreateVecs(J,NULL,&ctx->current_f);CHKERRQ(ierr);<br><br> ctx->current_f_allocated = PETSC_TRUE;<br> }<br></div><div><br></div><div>Because F=NULL, we then have ctx->current_f_allocated = PETSC_TRUE.<br><br></div><div>Then, the following if statement is true:<br><br> if (ctx->ncurrenth == 1 && ctx->current_f_allocated) {<br> ierr = (*ctx->func)(ctx->funcctx,U,F);CHKERRQ(ierr);<br> }<br><br><br></div><div>Fande,<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">
<br>
Barry<br>
<br>
<br>
><br>
> Fande,<br>
><br>
><br>
><br>
> Barry<br>
><br>
><br>
><br>
> ><br>
> > Alex<br>
><br>
><br>
<br>
</blockquote></div><br></div></div>