<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 30, 2020, at 10:11 PM, baikadi pranay <<a href="mailto:pranayreddy865@gmail.com" class="">pranayreddy865@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hello,<br class="">I have a couple of questions regarding SNESSetFunction usage, when programming in Fortran90.<br class=""><br class="">1) I have the following usage paradigm.<br class=""> call SNESSetFunction(snes,f_non,FormFunction,0,ierr)<br class=""> subroutine FormFunction(snes,x,r,dummy,ierr)<br class="">In the FormFunction subroutine, the function values are stored in the vector r. I see that these values are formed correctly. But when I use FormFunction in
SNESSetFunction(), the values are not getting populated into f_non and all of the values in f_non are zero. <br class=""></div></div></blockquote><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">Should the name of the variable used to store the function value be same in
SNESSetFunction and FormFunction?</div></div></blockquote><div><br class=""></div> It does not need to be the same, they are just the variables in each function</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""> And should I be calling the SNESComputeFunction() after calling SNESSetFunction()?<br class=""></div></div></blockquote><div><br class=""></div> No, that is a developer function called in PETSc, one would not need to call that.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><br class="">2) In the subroutine FormFunction, should the vector objects created be destroyed before ending the subroutine?<br class=""></div></div></blockquote><div><br class=""></div> What vectors? If you are creating any work vectors you need within the the FormFunction, yes those should be destroyed. But not the input and output functions.</div><div><br class=""></div><div> Here is any example from src/snes/tutorials/ex1f.F90 Note you call VecGetArrayF90() to access the arrays for the vectors, put the values into the arrays</div><div><br class=""></div><div><br class=""></div><div><div>subroutine FormFunction(snes,X,F,user,ierr)</div><div> implicit none</div><div><br class=""></div><div>! Input/output variables:</div><div> SNES snes</div><div> Vec X,F</div><div> PetscErrorCode ierr</div><div> type (userctx) user</div><div> DM da</div><div><br class=""></div><div>! Declarations for use with local arrays:</div><div> PetscScalar,pointer :: lx_v(:),lf_v(:)</div><div> Vec localX</div><div><br class=""></div><div>! Scatter ghost points to local vector, using the 2-step process</div><div>! DMGlobalToLocalBegin(), DMGlobalToLocalEnd().</div><div>! By placing code between these two statements, computations can</div><div>! be done while messages are in transition.</div><div> call SNESGetDM(snes,da,ierr);CHKERRQ(ierr)</div><div> call DMGetLocalVector(da,localX,ierr);CHKERRQ(ierr)</div><div> call DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX,ierr);CHKERRQ(ierr)</div><div> call DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX,ierr);CHKERRQ(ierr)</div><div><br class=""></div><div>! Get a pointer to vector data.</div><div>! - For default PETSc vectors, VecGetArray90() returns a pointer to</div><div>! the data array. Otherwise, the routine is implementation dependent.</div><div>! - You MUST call VecRestoreArrayF90() when you no longer need access to</div><div>! the array.</div><div>! - Note that the interface to VecGetArrayF90() differs from VecGetArray(),</div><div>! and is useable from Fortran-90 Only.</div><div><br class=""></div><div> call VecGetArrayF90(localX,lx_v,ierr);CHKERRQ(ierr)</div><div> call VecGetArrayF90(F,lf_v,ierr);CHKERRQ(ierr)</div><div><br class=""></div><div>! Compute function over the locally owned part of the grid</div><div> call FormFunctionLocal(lx_v,lf_v,user,ierr);CHKERRQ(ierr)</div><div><br class=""></div><div>! Restore vectors</div><div> call VecRestoreArrayF90(localX,lx_v,ierr);CHKERRQ(ierr)</div><div> call VecRestoreArrayF90(F,lf_v,ierr);CHKERRQ(ierr)</div><div><br class=""></div><div>! Insert values into global vector</div><div><br class=""></div><div> call DMRestoreLocalVector(da,localX,ierr);CHKERRQ(ierr)</div><div> call PetscLogFlops(11.0d0*user%ym*user%xm,ierr)</div><div><br class=""></div><div>! call VecView(X,PETSC_VIEWER_STDOUT_WORLD,ierr)</div><div>! call VecView(F,PETSC_VIEWER_STDOUT_WORLD,ierr)</div><div> return</div><div> end subroutine formfunction</div><div> end module f90module</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><br class="">Please let me know if you need any further information. Thank you in advance.<br class="">Best regards,<br class="">Pranay.<br class=""><br class=""></div><div hspace="streak-pt-mark" style="max-height:1px" class=""><img alt="" style="width:0px;max-height:0px;overflow:hidden" src="https://mailfoogae.appspot.com/t?sender=acHJhbmF5cmVkZHk4NjVAZ21haWwuY29t&type=zerocontent&guid=72a73203-6731-4e13-aa3e-75b68ac30ecf" class=""><font color="#ffffff" size="1" class="">ᐧ</font></div>
</div></blockquote></div><br class=""></body></html>