【问题标题】:Fortran: How to make multiple procedures share the same procedure interfaceFortran:如何使多个程序共享同一个程序接口
【发布时间】:2016-01-22 07:14:26
【问题描述】:

我的代码看起来像

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。如何使这两个程序共享相同的界面(例如使用模块)?我必须使用过程指针吗?

【问题讨论】:

    标签: fortran fortran2003


    【解决方案1】:

    您可以在模块中定义诸如abstract interfaces 之类的可重用“函数类型”:

    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
    

    使用函数pointers不是必须的,但你可以用同样的方式声明它们:procedure(der), pointer :: funPtr => myFun

    【讨论】:

    • 谢谢,这成功了。我在没有“抽象”的情况下尝试它,当然它没有用。
    猜你喜欢
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    相关资源
    最近更新 更多