从 Fortran 中的文件读取函数指针
我试图从外部指定应该调用哪个函数。在以下代码中,使用所选包络高斯调用 getEnvelope
。然而,我想从使用特定信封的文件中读取而不是编码 env =>;高斯
。
有办法做到这一点吗?或者还有其他首选方式吗?
module laser
implicit none
abstract interface
double precision function envelope(t, tau) result(f)
double precision, intent(in) :: t, tau
end function envelope
end interface
contains
double precision function getEnvelope(env, t, tau) result(f)
procedure(envelope) :: env
double precision, intent(in) :: t, tau
f = env(t, tau)
end function getEnvelope
double precision function gaussian(t, tau) result(f)
double precision, intent(in) :: t, tau
f = exp(-2.d0*log(2.d0)*(t/tau)**2.d0)
end function gaussian
end module laser
program main
use laser
implicit none
procedure(envelope), pointer :: env
double precision :: f
double precision :: t, tau
env => gaussian
t = 1.d0
tau = 200.d0
f = getEnvelope(env, t, tau)
end program main
I am trying to externally specify which function should be called. In the following code, getEnvelope
is called with the chosen envelope gaussian
. I would however want to read from a file which specific envelope is used rather than coding env => gaussian
.
Is there a way to do this? Or is there another preferred way?
module laser
implicit none
abstract interface
double precision function envelope(t, tau) result(f)
double precision, intent(in) :: t, tau
end function envelope
end interface
contains
double precision function getEnvelope(env, t, tau) result(f)
procedure(envelope) :: env
double precision, intent(in) :: t, tau
f = env(t, tau)
end function getEnvelope
double precision function gaussian(t, tau) result(f)
double precision, intent(in) :: t, tau
f = exp(-2.d0*log(2.d0)*(t/tau)**2.d0)
end function gaussian
end module laser
program main
use laser
implicit none
procedure(envelope), pointer :: env
double precision :: f
double precision :: t, tau
env => gaussian
t = 1.d0
tau = 200.d0
f = getEnvelope(env, t, tau)
end program main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论