Fortran:如何使多个过程共享相同的过程接口

时间:2016-01-21 22:45:36

标签: fortran fortran2003

我的代码看起来像

subroutine sub1(f)
    interface
        function f(x)
            (description of f)
        end function f
    end interface

    (do something with f)
end subroutine sub1

subroutine sub2(f)
    interface
        function f(x)
            (description of f)
        end function f
    end interface

    (do something with f)
end subroutine sub2

但是,两个子例程sub1sub2都使用相同的接口用于虚函数f。如何使这两个过程共享相同的接口(例如使用模块)?我必须使用程序指针吗?

1 个答案:

答案 0 :(得分:3)

您可以在模块中将此类可重复使用的“函数类型”定义为abstract interface

module m
    implicit none
    abstract interface
        function der(x,y) result(yDot)
            real, intent(in) :: x, y
            real :: yDot
        end function
    end interface
end module

subroutine integrateEuler(derY,x0,xf,y)
    use m
    real, intent(in) :: x0, xf
    real, intent(inout) :: y
    procedure(der) :: derY
    ! code here
end subroutine

subroutine integrateRKF45(derY,x0,xf,y)
    use m
    real, intent(in) :: x0, xf
    real, intent(inout) :: y
    procedure(der) :: derY
    ! code here
end subroutine

不需要使用函数指针,但您可以用相同的方式声明它们:procedure(der), pointer :: funPtr => myFun