如何将过程指针用于具有不同参数个数的子例程 [英] How to use procedure pointers for subroutines with different number of arguments
问题描述
我正在开发Fortran计算科学计划。我想使用过程指针来分配问题中的边界条件,如下面的主程序所示:
程序main
使用边界
隐式无
bc1 => boundaryA
bc2 =>边界B
bc3 => boundaryC
call comp_boundary
结束程序
我在边界模块中定义了所有边界操作boundaryA / B / C
module boundary
implicit none
procedure(boundary_type),pointer :: bc1,bc2,bc3
抽象接口
子程序boundary_type(i)
integer :: i
结束子程序
结束接口
包含
子程序boundaryA(i)
integer :: i
print *,'Boundary A at',i
结束子程序
子程序boundaryB(i)
整数:: i
print *,'边界B at',i
结束子程序
子程序boundaryC(i)
整数:: i
print *,'边界C at',
结束子程序
子程序comp_boundary
调用bc1(1)
调用bc2(2)
调用bc3(3)
结束子程序
结束模块
这个效果很好。
但是我的问题是,如果说 boundaryC
没有一个输入参数,而是两个,那么我的定义对于抽象接口 boundary_type
现在不起作用。
有可能使用过程指针来处理这种情况吗?或者任何其他方式?
您可以通过 OPTIONAL
论据。这更多的是一种学术解决方案,因为总会有痛苦的节节。正如高性能Mark 指出,您决定使用一个或两个参数需要在某个地方完成。
尽管如此,可选
参数需要添加这个全部的子程序,因此可能并不是真正的解决方案,因为所有的子程序都发生了根本性的变化。这与您给出的解决方案基本相同,只是使用了不同的过程参数集。
模块边界
IMPLICIT NONE
PROCEDURE(boundary_type),POINTER :: bc1,bc2
抽象接口
SUBROUTINE boundary_type(i,j)
INTEGER :: i
INTEGER,可选:: j
END SUBROUTINE boundary_type
END INTERFACE
CONTAINS
子程序boundaryA(i,j)
INTEGER: :i
INTEGER,可选:: j
PRINT *,'边界A at',i
END SUBROUTINE boundaryA
子程序boundaryB(i,j)
INTEGER :: i
INTEGER,OPTIONAL :: j
PRINT *,'边界B at',i,j
END子程序boundaryB
子程序comp_boundary
CALL bc1(1)
CALL bc2(2,3)
END SUBROUTINE comp_boundary
END MODULE boundary
注意: th e子程序 boundaryB
must always 被两个参数调用,因为没有检查 j $ c的可用性$ c>(使用
PRESENT
)
I'm developing a Fortran program for scientific computing. I want to use procedure pointers to assign the boundary conditions in the problem, shown in the following main program
program main
use boundary
implicit none
bc1 => boundaryA
bc2 => boundaryB
bc3 => boundaryC
call comp_boundary
end program
I define all the boundary operations "boundaryA/B/C" in a "boundary" module
module boundary
implicit none
procedure(boundary_type), pointer :: bc1,bc2,bc3
abstract interface
subroutine boundary_type(i)
integer :: i
end subroutine
end interface
contains
subroutine boundaryA(i)
integer :: i
print*, 'Boundary A at ',i
end subroutine
subroutine boundaryB(i)
integer :: i
print*, 'Boundary B at ',i
end subroutine
subroutine boundaryC(i)
integer :: i
print*, 'Boundary C at',i
end subroutine
subroutine comp_boundary
call bc1(1)
call bc2(2)
call bc3(3)
end subroutine
end module
This works well.
But my question is that if, say, boundaryC
has not one input argument, but two, then my definition for the abstract interface boundary_type
doesn't work now.
Is that possible to use the procedure pointer to deal with this case? Or any other way around?
You could achieve this with an OPTIONAL
argument. This is more an academic solution as there is always conservation of misery. As the comment of High Performance Mark states, your decision to use one or two arguments will need to be made somewhere.
Nonetheless, the OPTIONAL
argument would require to add this too all subroutines and could therefore be not really the solution you request as all subroutines are essentially changed. It is essentially the same solution you gave, with just different set of procedure arguments.
MODULE boundary
IMPLICIT NONE
PROCEDURE(boundary_type), POINTER :: bc1,bc2
ABSTRACT INTERFACE
SUBROUTINE boundary_type(i,j)
INTEGER :: i
INTEGER, OPTIONAL :: j
END SUBROUTINE boundary_type
END INTERFACE
CONTAINS
SUBROUTINE boundaryA(i,j)
INTEGER :: i
INTEGER, OPTIONAL :: j
PRINT *, 'Boundary A at ',i
END SUBROUTINE boundaryA
SUBROUTINE boundaryB(i,j)
INTEGER :: i
INTEGER, OPTIONAL :: j
PRINT *, 'Boundary B at ',i,j
END SUBROUTINE boundaryB
SUBROUTINE comp_boundary
CALL bc1(1)
CALL bc2(2,3)
END SUBROUTINE comp_boundary
END MODULE boundary
Note: the subroutine boundaryB
must always be called with both arguments as no check is done for the availability of j
(using PRESENT
)
这篇关于如何将过程指针用于具有不同参数个数的子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!