byl*_*byl 3 oop fortran gfortran
我正在使用旧的 Fortran 语言编写空气动力学代码(源代码在这里)。初始化的一部分是读取包含表面网格信息的文件,基本上是构成表面的一组三角形或四边形面板。我想使用单一类型来描述四边形和三角形面板panel。为此,我编写了两个初始化函数panel_init_3,panel_init_4它将根据面板有多少个顶点将数据加载到类型中。我想将它们绑定到我的panel类型,并根据参数的数量重载它们(即,如果它传递了一个整数和 3 个顶点对象,那么panel_init_3就会被调用,对于 4 个顶点对象也是如此。
这是该类型的源代码:
module panel_mod
use linked_list_mod
use vertex_mod
implicit none
type panel
! A panel with an arbitrary number of sides
integer :: N ! Number of sides/vertices
type(vertex_pointer),dimension(:),allocatable :: vertices
real,dimension(3) :: n_hat ! Normal vector
real :: A ! Surface area
contains
procedure :: init => panel_init_3, panel_init_4
procedure :: calc_area => panel_calc_area
procedure :: calc_normal => panel_calc_area
end type panel
contains
subroutine panel_init_3(this, v1, v2, v3)
! Initializes a 3-panel
implicit none
class(panel),intent(inout) :: this
type(vertex),intent(in),target :: v1, v2, v3
! Set number of sides
this%N = 3
! Allocate vertex array
allocate(this%vertices(this%N))
! Store info
this%vertices(1)%ptr => v1
this%vertices(2)%ptr => v2
this%vertices(3)%ptr => v3
! Calculate normal vec
! Calculate area
end subroutine panel_init_3
subroutine panel_init_4(this, v1, v2, v3, v4)
! Initializes a panel with 4 sides
implicit none
class(panel),intent(inout) :: this
type(vertex),intent(in),target :: v1, v2, v3, v4
! Set number of sides
this%N = 4
! Allocate vertex array
allocate(this%vertices(this%N))
! Store info
this%vertices(1)%ptr => v1
this%vertices(2)%ptr => v2
this%vertices(3)%ptr => v3
this%vertices(4)%ptr => v4
! Calculate normal vector
! Calculate area
end subroutine panel_init_4
subroutine panel_calc_area(this)
implicit none
class(panel),intent(inout) :: this
end subroutine panel_calc_area
subroutine panel_calc_normal(this)
implicit none
class(panel),intent(inout) :: this
end subroutine panel_calc_normal
end module panel_mod
Run Code Online (Sandbox Code Playgroud)
这个模块编译得很好。但是,当它用在这里时
...
! Initialize triangular panel
if (N == 3) then
call panels(i)%init(vertices(i1+1), vertices(i2+1), vertices(i3+1)) ! Need +1 because VTK is 0-indexed
! Initialize quadrilateral panel
else
call panels(i)%init(vertices(i1+1), vertices(i2+1), vertices(i3+1), vertices(i4+1))
end if
...
Run Code Online (Sandbox Code Playgroud)
我收到编译器消息
70 | call panels(i)%init(vertices(i1+1), vertices(i2+1), vertices(i3+1), vertices(i4+1))
| 1
Error: More actual than formal arguments in procedure call at (1)
Run Code Online (Sandbox Code Playgroud)
我是否超出了 Fortran 的能力,或者有没有办法做到这一点?我意识到我可以在不绑定和重载的情况下完成这一切,但我喜欢它的干净程度。我正在使用 gfortran 9.3.0。
在类型绑定过程声明语句中,可以在列表中声明多个绑定名称,因此我们可以有
type mytype
contains
procedure :: binding1, binding2
end type
Run Code Online (Sandbox Code Playgroud)
给出两个绑定名称。这些是单独的绑定。这些绑定名称解析为每个名称相同的过程。也就是说,我们可以写成
type mytype
contains
procedure :: binding1 => binding1, binding2 => binding2
end type
Run Code Online (Sandbox Code Playgroud)
绑定名称的列表性质是这样的
type mytype
contains
procedure :: binding => binding1, binding2
end type
Run Code Online (Sandbox Code Playgroud)
有列表中的项目binding=>binding1和binding2。这有类似的效果
type mytype
contains
procedure :: binding => binding1, binding2 => binding2
end type
Run Code Online (Sandbox Code Playgroud)
也就是说,我们仍然有两个不同的绑定名称,每个名称都绑定到一个过程。
我们没有具有binding特定程序的通用绑定名称binding1和binding2. 为此,我们需要显式创建一个泛型:
type mytype
contains
procedure :: binding1, binding2
generic :: binding => binding1, binding2
end type
Run Code Online (Sandbox Code Playgroud)
在此通用声明中,列表具有binding1和binding2作为通用 的具体细节的项目binding。Fortran 2018 7.5.5 中给出了这些列表/语句的精确语法规则。