function - 使用接口(interface)将函数作为参数传递给子例程在 Plato Fortran 90 中不起作用

标签 function fortran fortran90 subroutine plato

我创建了一个在 Linux 机器上使用的 Fortran 90 程序,并使用 gfortran 进行编译。它在带有 gfortran 的 Linux 机器上运行良好,但提供了错误

错误 327 - 在 SECANTMETHOD 的接口(interface)(来自 MODULE SECMETH)中,第九个虚拟参数 (F) 的类型为 REAL(KIND=2) FUNCTION,而实际参数的类型为 REAL(KIND=2) )

使用 Plato 编译器 (FTN95) 时。有谁知道我需要如何更改代码才能在柏拉图中工作?我尝试阅读此错误,其中提到了一些指针,但从我的尝试来看,这不起作用。我已经找到了一些解决方法,但它们使子例程不再接受任何函数作为参数 - 这几乎没有用。任何帮助将不胜感激。我的代码如下。

!--! A module to define a real number precision.
module types
  integer, parameter :: dp=selected_real_kind(15)
end module types

module secFuncs
  contains

  function colebrookWhite(T)
    use types

    real(dp) :: colebrookWhite
    real(dp), intent(in) :: T

    colebrookwhite=25-T**2

    return
  end function colebrookWhite
end module secFuncs

module secMeth
  contains

  subroutine secantMethod(xolder,xold,xnew,epsi1,epsi2,maxit,exitFlag,numit,f)
    use types
    use secFuncs
    implicit none

    interface
      function f(T)
        use types
        real(dp) :: f
        real(dp), intent(in) :: T
      end function f
    end interface

    real(dp), intent(in) :: epsi1, epsi2
    real(dp), intent(inout) :: xolder, xold
    real(dp), intent(out) :: xnew
    integer, intent(in) :: maxit
    integer, intent(out) :: numit, exitFlag
    real(dp) :: fxold, fxolder, fxnew
    integer :: i

    fxolder = f(xolder)
    fxold = f(xold)

    i = 0

    do
      i = i + 1

      xnew = xold - fxold*(xold-xolder)/(fxold-fxolder)

      fxnew = f(xnew)

      if (i == maxit) then
        exitFlag = 1
        numit = i
        return
      else if (abs(fxnew) < epsi1) then
        exitFlag = 2
        numit = i
        return
      else if (abs(xnew - xold) < epsi2) then
        exitFlag = 3
        numit = i
        return
      end if

      xolder = xold
      xold = xnew
      fxolder = fxold
      fxold = fxnew
    end do
  end subroutine secantMethod

end module secMeth

program secantRoots
  use types
  use secMeth
  use secFuncs
  implicit none

  real(dp) :: x1, x2, xfinal, epsi1, epsi2
  integer :: ioerror, maxit, numit, exitFlag

  do
    write(*,'(A)',advance="no")"Please enter two initial root estimates, 2epsi's, and maxit: "
    read(*,*,iostat=ioerror) x1, x2, epsi1, epsi2, maxit

    if (ioerror /= 0) then
      write(*,*)"Invalid input."
    else
      exit
    end if
  end do

  call secantMethod(x1,x2,xfinal,epsi1,epsi2,maxit,exitFlag,numit,colebrookWhite)

  if (exitFlag == 1) then
    write(*,*)"The maximum number of iterations was reached."
  else if (exitFlag == 2) then
    write(*,'(a,f5.3,a,i3,a)')"The root is ", xfinal, ", which was reached in ", numit, " iterations."
  else if (exitFlag == 3) then
    write(*,'(a,i3,a)')"There is slow or no progress at ", numit, " iterations."
  end if

end program secantRoots

最佳答案

当前的 gfortran 检测到对 secantMethod 过程的调用中的错误,其中在 colebrookWhite 函数名称后面有括号,但没有参数列表。

如果您想将函数作为参数传递(而不是计算函数的结果)(这就是您想要在此处执行的操作),则不要在函数名称后面添加括号对。

call secantMethod(x1,x2,xfinal,epsi1,epsi2,maxit,exitFlag,numit,colebrookWhite )
!                                                                             ^

关于function - 使用接口(interface)将函数作为参数传递给子例程在 Plato Fortran 90 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25956179/

相关文章:

C++虚函数问题

python - 为什么变量不能是全局的但函数却可以?

javascript - 如何从不同的按钮多次调用一个函数?

matlab - 建模与仿真编程语言

c - 从 Fortran 运行时 METIS 段错误

c++ - 在定义它的函数之外操作局部静态指针变量

module - Fortran 模块中类型之间的循环依赖

macos - 关闭 6 核 Intel Xeon 中的超线程

arrays - 如何在 Fortran 90 中连接两个数组

fortran - 本地初始化变量规则也适用于指针吗?