Fortran Coder

查看: 23082|回复: 3

[子程序] function可以返回一个数组吗

[复制链接]

4

帖子

1

主题

0

精华

入门

F 币
32 元
贡献
19 点

规矩勋章

发表于 2017-3-14 22:11:49 | 显示全部楼层 |阅读模式
本帖最后由 jlliu 于 2017-3-14 22:58 编辑

楼主小白,今天刚接触fortran。
如题,我想知道可以用function返回一个数组吗?

虽然说用subroutine可以实现这样的功能,但这样用起来有点不直观。

我定义函数的时候没有报错,但是在主函数里面用的时候就报错了。

哪位可以帮忙看看吗?
[Fortran] 纯文本查看 复制代码
!
! PURPOSE: Entry point for the console application.
!
!****************************************************************************
module ini
implicit none
real*8,parameter::T=10.,wx=6.,wy=6.,wz=11., pi = 3.141592653589793239
integer,parameter::N=3
!T是温度,单位为微K,N是原子数,6,6,11刻画了原子的构型
contains

function ran() !returns random number between 0 - 1 
implicit none 
integer , save :: flag = 0
double precision :: ran 
if(flag==0) then 
call random_seed()
flag = 1 
endif 
call random_number(ran) ! built in fortran 90 random number function 
end function ran

function normal(mean,sigma) 
implicit none 
integer :: flag 
double precision, parameter :: pi = 3.141592653589793239 
double precision :: u1, u2, y1, y2, normal, mean, sigma 
save flag 
data flag /0/ 
u1 = ran(); u2 = ran() 
if (flag.eq.0) then 
y1 = sqrt(-2.0d0*log(u1))*cos(2.0d0*pi*u2) 
normal = mean + sigma*y1 
flag = 1 
else 
y2 = sqrt(-2.0d0*log(u1))*sin(2.0d0*pi*u2) 
normal = mean + sigma*y2 
flag = 0 
endif 
end function normal 
!The above codes are made in Fortran 90 language, if you have any question, you may write to [url=mailto:sealin2008@hotmail.com]sealin2008@hotmail.com[/url] 

subroutine position0(R0)
implicit none
real*8::R0(N,3),x,y
integer ::i
do i=1,N
do
x=ran()
y=ran()
if((2*x-1)**2+(2*y-1)**2<=1) exit
end do
R0(i,1)=wx*(2.0*x-1.0)
R0(i,2)=wy*(2.0*y-1.0)
end do
do i=1,N
R0(i,3)=normal(0.D0,wz/2.0)
end do
end subroutine position0

subroutine Velocity(V)
implicit none
real*8::V(N,3),r1,r2,r3
integer::i
do i=1,N
r1=ran()*2.*pi
r2=ran()*2.-1.0
r3=normal(0.d0,dsqrt(1.3806504E4*T/(87*1.65053878)))
V(i,1)=r3*sqrt(1-r2*r2)*cos(r1)
V(i,2)=r3*sqrt(1-r2*r2)*sin(r1)
V(i,3)=r3*r2
end do

end subroutine Velocity

function position(i,t,R0,V)
implicit none
real*8::R0(N,3),V(N,3),t,position(3)
integer::i
position(1)=R0(i,1)+t*V(i,1)
position(2)=R0(i,2)+t*V(i,2)
position(3)=R0(i,3)+t*V(i,3)
end function position
end module ini


program main
use ini
implicit none
real*8::R0(N,3),V(N,3)
integer ::i
call position0(R0)
call Velocity(V)
Open( 12 , File = 'out.txt' )
Do i = 1 , N
write( 12 , * ) R0(i,1),R0(i,2),R0(i,3)
End Do
Do i = 1 , N
write( 12 , * ) V(i,1),V(i,2),V(i,3)
End Do
Close( 12 )


end program main

953

帖子

0

主题

0

精华

大师

F 币
180 元
贡献
73 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
发表于 2017-3-14 22:22:53 | 显示全部楼层
[Fortran] 纯文本查看 复制代码
  R0 = position0()
  V = Velocity()

 Function position0() result(R0)
    implicit none
    real*8::R0(N,3),x,y
    integer ::i
    do i=1,N
      do
        x=ran()
        y=ran()
        if((2*x-1)**2+(2*y-1)**2<=1) exit
      end do
      R0(i,1)=wx*(2.0*x-1.0)
      R0(i,2)=wy*(2.0*y-1.0)
    end do
    do i=1,N
      R0(i,3)=normal(0.D0,wz/2.0)
    end do
  end Function position0

  Function Velocity() result(V)
    implicit none
    real*8::V(N,3),r1,r2,r3
    integer::i
    do i=1,N
      r1=ran()*2.*pi
      r2=ran()*2.-1.0
      r3=normal(0.d0,dsqrt(1.3806504E4*T/(87*1.65053878)))
      V(i,1)=r3*sqrt(1-r2*r2)*cos(r1)
      V(i,2)=r3*sqrt(1-r2*r2)*sin(r1)
      V(i,3)=r3*r2
    end do
  end Function Velocity

4

帖子

1

主题

0

精华

入门

F 币
32 元
贡献
19 点

规矩勋章

 楼主| 发表于 2017-3-14 22:50:54 | 显示全部楼层
非常感谢!
回复

使用道具 举报

4

帖子

1

主题

0

精华

入门

F 币
32 元
贡献
19 点

规矩勋章

 楼主| 发表于 2017-3-14 22:59:17 | 显示全部楼层
问题解决啦,但是不知道在哪儿点出已解决。。。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 01:03

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表