Hello,
I want to pass different subroutines/functions to a finite different subroutine for computation, without doing the FD implementation for every possible function again.
Basically one module stores plenty of functions, which can also be composed for more complex behavior. The programmer then picks what he needs from this module and uses it in main_function.
Then, the derivative has to be computed with finite differences. Therefore, it makes sense to me to have one routine doing the FD scheme and only passing the function to it that is needed, so I do not need to implement the FD method for each possible function that comes up.
The main_function is called by the main programm and is stored in a separate shared library. I tried to do the following:
subroutine main_function(arg1, arg2, arg3)
use module1, only: sub
use fd_module
implicit none
real*8 :: arg1, arg2, arg3
! Do stuff
call fd_sub(arg1, arg2, sub)
return
The way the main_function is called is fixed in the main program. Module1 will contain several similar routines to sub, all taking the same imput and producing the same output. (In this case, taking strain and parameters in and return mechanical stress)
The sub is defined in its module like
module module1
implicit none
contains
subroutine sub1(a1, a2, a3)
real*8 :: a1, a2, a3
! do stuff
end subroutine sub1
end module
The next part is where I will get a segmentation fault in run-time, which lets me assume something is wrong with how I actually call the subroutine:
module fd_module
implicit none
interface
subroutine sub_interface(a1, a2, a3)
real*8 a1, a2, a3
end subroutine sub_interface
end interface
contains
subroutine fd_sub(a1, a2, a3, sub)
real*8 a1, a2, a3
real*8 b1, b2
procedure(sub_interface), pointer :: sub
! Do stuff, obtain more values bi
! ...
call sub(b1, b2)
end subroutine
The compiler (gfortran) has nothing to complain, on runtime the program comes to a halt at call sub(b1, b2, sub)
and exits shortly after with a segmentation fault.
I am relatively new to fortran, coming from years of Julia and Python, so that pointer/interface thing does leave me confused.
Thanks for any advice.