将例程名称分配给 Fortran 中的其他例程



我有一个迭代大量时间(几个小时)的求解器,我正在尝试从主循环中删除几个 if 语句以节省时间。

我在这里基本上要做的是创建一个例程,updateGhosts,它指向一个分配的例程。此例程属于派生数据类型,其中包含几个其他属性和例程。我想使用一个例程 setGhosts 将 updateGhost 设置为 ghostOne 或 ghostTwo,这将是正确更新某些条件的例程。

我似乎无法找出一种导致代码编译的方法,尽管我已经尝试了几种不同的东西都无济于事。

为了简单起见,我试图尽可能减少代码示例,但实际上类型GridPoint和BlockType有更多的参数需要处理,因此简单的重构不是一种选择。

以下是简化的代码:

module BlockModule
  implicit none
  type GridPoint
    real(kind=8) :: x, y, T
  end type GridPoint
  type BlockType
    integer :: BC
    type (GridPoint) :: Points(0:102,0:102)
  contains
    procedure :: setGhosts, updateGhosts
    procedure :: ghostOne, ghostTwo
  end type BlockType
contains
  subroutine setGhosts(this)
    class(BlockType), intent(inout) :: this
    if (this%BC == -1) then
      ! We want to assign updateGhosts to ghostOne.
      this%updateGhosts => this%ghostOne
    else
      ! We want to assign updateGhosts to ghostTwo.
      this%updateGhosts => this%ghostTwo
    end if
  end subroutine
  ! Routine that will be either ghostOne or ghostTwo.
  subroutine updateGhosts(this)
    class(BlockType), intent(inout) :: this
  end subroutine
  ! Routine will do something.
  subroutine ghostOne(this)
    class(BlockType), intent(inout) :: this
  end subroutine
  ! Routine will do something completely different, with same inputs.
  subroutine ghostTwo(this)
    class(BlockType), intent(inout) :: this
  end subroutine
end module

如何分配例程名称以指向 Fortran90/95/03 中的其他例程?(可能的最旧版本是理想的,但不是必需的。抱歉,如果之前问过类似的问题,我尝试了搜索,但我不太确定我需要寻找什么。

感谢您的阅读!

(问题已在评论中回答。请参阅没有答案的问题,但问题在评论中得到解决(或在聊天中扩展) )

@SuperCow写道:

这有帮助吗?过程指针,派生类型

OP写道:

是的!这就是诀窍。

相关内容

最新更新