2017-05-07 164 views
-1

我已经从一本基本上运行善良的书中读取了这个Fortran程序,以适合测试某些数据并给出输出结果。代码和它的实际结果/输出给定为下:这个程序的Fortran代码给出错误

real*4 x(50),xc(50,20),omega(50) 
integer ir(50) 
real*8 xx 
c This code tests goodness of fit. 
n=47 
c The method of Bak, Nielsen, and Madsen is used. 
data (x(i), i=1,47)/ 18, 22, 26, 16, 19, 21, 18, 22, 
* 25, 31, 30, 34, 31, 25, 21, 24, 21, 28, 24, 26, 32, 
* 33, 36, 39, 32, 33, 42, 44, 43, 48, 50, 56, 57, 59, 
* 51, 49, 49, 57, 69, 72, 75, 76, 78, 73, 73, 75, 86/ 
do 999 icase=1,2 
c Parameter icase =1 or 2 denotes SDE model 1 or 2. 
xx=102038. 
m=8 
h=1.0 
do 10 j=1,m+1 
10 omega(j)=0.0 
kk=4 
akk=kk 
h=h/akk 
do 202 i=2,n 
xs=x(i-1) 
xe=x(i) 
do 202 j=1,m 
xk=xs 
do 252 k=1,kk 
call functs(icase,xk,f,g) 
call random(xx,rand1,rand2) 
252 xk=xk+h*f+sqrt(h)*g*rand1 
xc(i,j)=xk 
202 continue 
do 402 i=2,n 
irr=1 
do 302 j=1,m 
xe=x(i) 
xcalc=xc(i,j) 
if(xe.gt.xcalc) irr=irr+1 
302 continue 
402 ir(i)=irr 
do 502 i=2,n 
irr=ir(i) 
omega(irr)=omega(irr)+1.0 
502 continue 
chi2=0.0 
an=n 
am=m 
hlp=(an-1.0)/(am+1.0) 
do 602 j=1,m+1 
602 chi2=chi2+(omega(j)-hlp)**2/hlp 
write(6,100) icase,chi2 
100 format(5x,i7,5x,f9.2) 
999 continue 
stop 
end 
subroutine functs(icase,x,f,g) 
th1=3510.0 
th2=13500.0 
f=th1/(x*x) 
g=th2/(x*x) 
if(icase.eq.1) goto 17 
th1=.0361 
th2=.6090 
f=th1*x 
g=sqrt(th2*x) 
17 continue 
return 
end 
subroutine random(xx,rand1,rand2) 
real*8 xx,a,b,d,rng(2) 
a=16807. 
ib=2147483647 
b=ib 
do 55 i=1,2 
id=a*xx/b 
d=id 
xx=a*xx-d*b 
55 rng(i)=xx/b 
pi=3.141592654 
u1=rng(1) 
u2=rng(2) 
hlp=sqrt(-2.0*alog(u1)) 
rand1=hlp*cos(pi*2.0*u2) 
rand2=hlp*sin(pi*2.0*u2) 
return 
end 

输出是:

1 18.57 
2 4.09 

然而,即使使用网上很多Fortran编译器后,我没有得到这些结果。它会给出非标准类型声明等错误。

我需要帮助以获得与上述相同的输出。

+3

请给出确切的错误,它们出现的行,并显示你不想尝试解决它。 – Carcigenicate

+0

请使用一些缩进,空格和空行来使您的代码可读。 Seriusly,这太可怕了。并告诉我们你得到的确切的错误消息以及创建这些消息的编译器命令。 –

回答

3

该代码是使用(旧)Fortran 77风格编写的,并增加了一些常见扩展。由于它使用所谓的固定格式,因此源代码使用的列对于拥有正确的代码至关重要。特别是对于壳体:

  • 注释以c字符在第一列
  • 连续行被定义*在第六列定义
  • 标签必须使用第5列
  • 常规代码必须使用7-72列范围

正确缩进代码允许它在GNU gfortran(使用v.4.8.2测试)和Intel ifort(使用15.0.2版测试)上运行。要通知编译器,您希望对大多数编译器采用固定格式,那么您只需对源文件使用.f扩展名即可。否则,你有合适的编译器选项。对于gfortran,编译指定-ffixed-form。下面提供了(最小)缩进代码。

 real*4 x(50),xc(50,20),omega(50) 
     integer ir(50) 
     real*8 xx 
c This code tests goodness of fit. 
     n=47 
c The method of Bak, Nielsen, and Madsen is used. 
     data (x(i), i=1,47)/ 18, 22, 26, 16, 19, 21, 18, 22, 
    * 25, 31, 30, 34, 31, 25, 21, 24, 21, 28, 24, 26, 32, 
    * 33, 36, 39, 32, 33, 42, 44, 43, 48, 50, 56, 57, 59, 
    * 51, 49, 49, 57, 69, 72, 75, 76, 78, 73, 73, 75, 86/ 
     do 999 icase=1,2 
c Parameter icase =1 or 2 denotes SDE model 1 or 2. 
     xx=102038. 
     m=8 
     h=1.0 
     do 10 j=1,m+1 
10 omega(j)=0.0 
     kk=4 
     akk=kk 
     h=h/akk 
     do 202 i=2,n 
     xs=x(i-1) 
     xe=x(i) 
     do 202 j=1,m 
     xk=xs 
     do 252 k=1,kk 
     call functs(icase,xk,f,g) 
     call random(xx,rand1,rand2) 
252 xk=xk+h*f+sqrt(h)*g*rand1 
     xc(i,j)=xk 
202 continue 
     do 402 i=2,n 
     irr=1 
     do 302 j=1,m 
     xe=x(i) 
     xcalc=xc(i,j) 
     if(xe.gt.xcalc) irr=irr+1 
302 continue 
402 ir(i)=irr 
     do 502 i=2,n 
     irr=ir(i) 
     omega(irr)=omega(irr)+1.0 
502 continue 
     chi2=0.0 
     an=n 
     am=m 
     hlp=(an-1.0)/(am+1.0) 
     do 602 j=1,m+1 
602 chi2=chi2+(omega(j)-hlp)**2/hlp 
     write(6,100) icase,chi2 
100 format(5x,i7,5x,f9.2) 
999 continue 
     stop 
     end 
     subroutine functs(icase,x,f,g) 
     th1=3510.0 
     th2=13500.0 
     f=th1/(x*x) 
     g=th2/(x*x) 
     if(icase.eq.1) goto 17 
     th1=.0361 
     th2=.6090 
     f=th1*x 
     g=sqrt(th2*x) 
17 continue 
     return 
     end 
     subroutine random(xx,rand1,rand2) 
     real*8 xx,a,b,d,rng(2) 
     a=16807. 
     ib=2147483647 
     b=ib 
     do 55 i=1,2 
     id=a*xx/b 
     d=id 
     xx=a*xx-d*b 
55 rng(i)=xx/b 
     pi=3.141592654 
     u1=rng(1) 
     u2=rng(2) 
     hlp=sqrt(-2.0*alog(u1)) 
     rand1=hlp*cos(pi*2.0*u2) 
     rand2=hlp*sin(pi*2.0*u2) 
     return 
     end 

如果你想使用的在线资源,确保正确复制粘贴代码(与右缩进)编译和使用该选项的固定形式。例如在shell下面使用https://www.tutorialspoint.com/compile_fortran_online.php编译输入:gfortran -ffixed-form *.f95 -o main

既然Fortran 77风格现在已经很老了,如果你开始一个新的代码,我个人建议转向自由格式的源代码并使用更新的Fortran特性。使用现代风格可能重写代码如下:

module my_kinds 
    integer, parameter :: sp = selected_real_kind(9) 
    integer, parameter :: dp = selected_real_kind(18) 
end module my_kinds 

program test_from_book 
    use my_kinds 
    real(sp) :: x(50),xc(50,20),omega(50) 
    integer :: ir(50) 
    real(dp) :: xx 
    ! This code tests goodness of fit. 
    n=47 
    ! The method of Bak, Nielsen, and Madsen is used. 
    x = [ 18, 22, 26, 16, 19, 21, 18, 22, & 
      25, 31, 30, 34, 31, 25, 21, 24, 21, 28, 24, 26, 32, & 
      33, 36, 39, 32, 33, 42, 44, 43, 48, 50, 56, 57, 59, & 
      51, 49, 49, 57, 69, 72, 75, 76, 78, 73, 73, 75, 86, & 
      0 , 0, 0] 
    loop_999: do icase=1,2 
     ! Parameter icase =1 or 2 denotes SDE model 1 or 2. 
     xx=102038. 
     m=8 
     h=1.0 
     do j=1,m+1 
      omega(j)=0.0 
     enddo 
     kk=4 
     akk=kk 
     h=h/akk 
     loop_202: do i=2,n 
      xs=x(i-1) 
      xe=x(i) 
      do j=1,m 
       xk=xs 
       do k=1,kk 
        call functs(icase,xk,f,g) 
        call random(xx,rand1,rand2) 
        xk=xk+h*f+sqrt(h)*g*rand1 
       enddo 
       xc(i,j)=xk 
      enddo 
     enddo loop_202 
     loop_402: do i=2,n 
      irr=1 
      do j=1,m 
       xe=x(i) 
       xcalc=xc(i,j) 
       if(xe.gt.xcalc) irr=irr+1 
      enddo 
      ir(i)=irr 
     enddo loop_402 
     do i=2,n 
      irr=ir(i) 
      omega(irr)=omega(irr)+1.0 
     enddo 
     chi2=0.0 
     an=n 
     am=m 
     hlp=(an-1.0)/(am+1.0) 
     do j=1,m+1 
      chi2=chi2+(omega(j)-hlp)**2/hlp 
     enddo 
     write(6,100) icase,chi2 
     100 format(5x,i7,5x,f9.2) 
    enddo loop_999 
    stop 
end 

subroutine functs(icase,x,f,g) 
    th1=3510.0 
    th2=13500.0 
    f=th1/(x*x) 
    g=th2/(x*x) 
    if(icase.ne.1) then 
     th1=.0361 
     th2=.6090 
     f=th1*x 
     g=sqrt(th2*x) 
    endif 
end 

subroutine random(xx,rand1,rand2) 
    use my_kinds 
    real(dp) :: xx,a,b,d,rng(2) 
    a=16807. 
    ib=2147483647 
    b=ib 
    do i=1,2 
     id=a*xx/b 
     d=id 
     xx=a*xx-d*b 
     rng(i)=xx/b 
    enddo 
    pi=3.141592654 
    u1=rng(1) 
    u2=rng(2) 
    hlp=sqrt(-2.0*alog(u1)) 
    rand1=hlp*cos(pi*2.0*u2) 
    rand2=hlp*sin(pi*2.0*u2) 
end 
+0

谢谢,现代风格的代码运作良好! –