Fortran 函数不向主程序返回值
我从另一个函数 rter3
调用了一个函数 integ
。我已经从 integ
函数打印了 integ
值,并且它打印了合法值。但是,当我在 rter3
函数中打印 integ
值时,它会打印 0.00
。我已尽我所能去理解它为什么会这样做,但无法弄清楚。请帮我一下。谢谢。
function rter3(z,c1,nx1,xarray,xlocation,angl3r,gamr,probr,aaa,da1)
use data
integer:: z,o,nx1,lc
double precision:: c1(nx1),w1,w2,rter3,angl3r,gamr,probr(100),aaa,da1,rati2,xarray(nx1),xlocation,div1
double precision:: variable
dx=0.1
rati2=20.0
!write(*,*) z,o,nx1,xlocation,angl3,gam
w1=div1(c1(z-1),c1(z),dx)
w2=div1(c1(z),c1(z+1),dx)
variable=integ(nx1,c1,xarray,xlocation,dx,angl3r,gamr,probr,aaa,da1,w1,w2)
rter3=rati2*variable
write(*,*) "rter3=", rter3, variable, rati2, xlocation, gam, angl3, da1
end function rter3
function integ(nxx,hfield,xfield,loc1,diff,angl3,gam,prob,afield,dprob,ab2,af2)
implicit none
integer :: l,nxx,dui,kk,indx,indt
double precision:: du(101),prob(100),am1,a,ap1,angl3,gam,diff,dprob,integ,function12,afield(100),xfield(nxx)
double precision:: loc1,loc2,hfield(nxx),am2,ap2,ab2,af2
!write(*,*) 'test loop', loc1, diff, angl3, gam, dprob
do kk=1,100
do l=1,nxx
if (0.95*xfield(l)>=loc1-afield(kk).and. 1.05*xfield(l)<loc1-afield(kk)) then
dui=l
!call exit()
endif
enddo
!dui= loc1-kk
!write(*,*) "Entered integ", dui
!write(*,*) dui
if (dui<=3) then
dui=3
endif
if (dui>= nxx-2) then
dui=nxx-2
endif
am1=hfield(dui-1)
am2=hfield(dui-2)
a=hfield(dui)
ap2=hfield(dui+2)
ap1=hfield(dui+1)
du(kk)=abs(prob(kk)*(function12(am2,am1,ap1,ap2,diff,angl3,gam,ab2,af2))*dprob)
enddo
integ=sum(du)
write(*,*) "integration value=", integ, nxx, loc1, diff, angl3, gam, dprob
end function integ
所以,这些是供您参考的单独功能。如果您觉得有什么问题,请告诉我。整个代码非常大,但是如果您觉得需要查看它,我将附上它。再次感谢。
I have called a function integ
from another function rter3
. I have printed integ
values from the integ
function and it prints legitimate values. But when I print integ
values at rter3
function, it prints 0.00
. I have tried all I could to understand why it does that but could not figure out. Please help me out here. Thanks.
function rter3(z,c1,nx1,xarray,xlocation,angl3r,gamr,probr,aaa,da1)
use data
integer:: z,o,nx1,lc
double precision:: c1(nx1),w1,w2,rter3,angl3r,gamr,probr(100),aaa,da1,rati2,xarray(nx1),xlocation,div1
double precision:: variable
dx=0.1
rati2=20.0
!write(*,*) z,o,nx1,xlocation,angl3,gam
w1=div1(c1(z-1),c1(z),dx)
w2=div1(c1(z),c1(z+1),dx)
variable=integ(nx1,c1,xarray,xlocation,dx,angl3r,gamr,probr,aaa,da1,w1,w2)
rter3=rati2*variable
write(*,*) "rter3=", rter3, variable, rati2, xlocation, gam, angl3, da1
end function rter3
function integ(nxx,hfield,xfield,loc1,diff,angl3,gam,prob,afield,dprob,ab2,af2)
implicit none
integer :: l,nxx,dui,kk,indx,indt
double precision:: du(101),prob(100),am1,a,ap1,angl3,gam,diff,dprob,integ,function12,afield(100),xfield(nxx)
double precision:: loc1,loc2,hfield(nxx),am2,ap2,ab2,af2
!write(*,*) 'test loop', loc1, diff, angl3, gam, dprob
do kk=1,100
do l=1,nxx
if (0.95*xfield(l)>=loc1-afield(kk).and. 1.05*xfield(l)<loc1-afield(kk)) then
dui=l
!call exit()
endif
enddo
!dui= loc1-kk
!write(*,*) "Entered integ", dui
!write(*,*) dui
if (dui<=3) then
dui=3
endif
if (dui>= nxx-2) then
dui=nxx-2
endif
am1=hfield(dui-1)
am2=hfield(dui-2)
a=hfield(dui)
ap2=hfield(dui+2)
ap1=hfield(dui+1)
du(kk)=abs(prob(kk)*(function12(am2,am1,ap1,ap2,diff,angl3,gam,ab2,af2))*dprob)
enddo
integ=sum(du)
write(*,*) "integration value=", integ, nxx, loc1, diff, angl3, gam, dprob
end function integ
So, those are the individual functions for your reference. Please let me know if you feel something is wrong. The whole code is very big but if you feel you need to see it, I shall attach it. Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rter3函数如何“知道”integ函数的接口?两者都在同一个模块中吗?如果不是,则 rter3 可能不知道该接口并且正在使用隐式类型。由于您没有在 rter3 中使用“隐式无”,因此可能会这样做,在这种情况下, integ 的位将被解释为整数,而它们应该被解释为双精度 - 该值将显示不正确。我的建议:始终将程序放在模块中。然后你就可以在同一个模块中使用过程了……编译器可以检查参数一致性。如果从主程序或另一个模块调用子例程或函数,请“使用”该模块来获取参数检查。使用“隐式无”。如果您忘记在源代码中包含隐式 none,请另外使用执行相同操作的编译器选项(例如,gfortran 的 -fimplicit-none)。
How does the rter3 function "know" the interface of the integ function? Are both in the same module? If not, perhaps rter3 doesn't know the interface and is using implicit typing. Since you didn't use "implicit none" in rter3 it might do that, in which case the bits of integ would be interpreted as an integer, while they are supposed to be interpreted as double precision -- the value would appear incorrectly. My suggestions: always place your procedures in a module. Then you are OK with procedues in the same module ... the compiler can check argument consistency. If you call a subroutine or function from a main program or another module, "use" the module to obtain the argument checking. Use "implicit none". In case you forget to include implicit none in your source code, additionally use the compiler option that does the same thing (e.g., -fimplicit-none for gfortran).