Hello <br><br>I'm wondering if it is possible to put an external procedure reference in a ctx() in fortran.<br><br> I'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'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'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'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'm only using Fortran since I'm much more familiar with it then with C.<br>
<br>Sorry there isn't much to go on, but any suggestions would be greatly appreciated.<br><br>Gaetan<br>