Hello <br><br>I&#39;m wondering if it is possible to put an external procedure reference in a ctx() in fortran.<br><br> I&#39;m in the process of writing a Newton--Krylov solver for an aero-structural system. My two different codes are wrapped with python and so each code is called through python for residual and preconditioning operations. Nominally this would be a good use of petsc4py but it doesn&#39;t allow for PCShell so it is no use to me. <br>
<br>I then wrote up the solver in Fortran and attempted to use callbacks to python for computing the required information. Using f2py, I can pass my two call back functions cb1 and cb2 fortran. A schematic of the code is below:<br>
<br>subroutine solver(cb1, cb2)<br><br>   ! cb1 and cb2 are python callbacks set using f2py<br>   external cb1,cb2<br>   petscFortranAddress ctx(2)<br><br>   ! I would like to do the following, but this doesn&#39;t compile<br>
   ! ctx(1) = cb1<br>   ! ctx(2) = cb2<br><br>  call SNESCreate(comm,snes,ierr)<br>  call SNESSetFunction(snes,resVec,FormFunction,ctx,ierr)<br><br>  call KSPGetPC(ksp,pc,ierr)<br>  call PCSetType(pc,PCSHELL,ierr)<br><br>
  call PCShellSetContext(pc,ctx,ierr)<br>  call PCShellSetApply(pc,applyaspc_fortran,ierr)<br><br>end subroutine solver<br><br>subroutine applyaspc_fortran(pc,inputVec,outputVec,ierr)<br><br>  PC      pc<br>  Vec     inputVec,outputVec<br>
  PetscFortranAddress ctx(2)<br>  external func<br> 
<br>  call PCShellGetContext(pc,ctx,ierr)<br>  func = ctx(2)<br><br>  call VecGetArrayF90(inputVec,states_in,ierr)<br>  call VecGetArrayF90(outputVec,states_out,ierr)<br><br>  ! Call the callback to python<br>  call func(states_in,states_out,shape(states_in))<br>
<br>  call VecRestoreArrayF90(inputVec,states_in,ierr)<br>  call VecRestoreArrayF90(outputVec,states_out,ierr)<br>end subroutine applyaspc_fortran<br><br><br> In general, in Fortran, is it possible to put an external function reference in a module such that I wouldn&#39;t have to try to pass it through the application ctx? I realize this may be impossible to do in Fortran. Would such a procedure be possible in C? I&#39;m only using Fortran since I&#39;m much more familiar with it then with C.<br>
<br>Sorry there isn&#39;t much to go on, but any suggestions would be greatly appreciated.<br><br>Gaetan<br>