【问题标题】:Ambiguous reference to variable对变量的模糊引用
【发布时间】:2015-09-04 18:09:09
【问题描述】:

所以我正在做 2 个链接到主程序的模块。第一个定义了所有变量,第二个定义了函数。

模块1:

module zmienne
 implicit none
 integer, parameter :: ngauss = 8
 integer, parameter :: out_unit=1000
 integer, parameter :: out_unit1=1001
 integer, parameter :: out_unit2=1002, out_unit3=1003
 real(10), parameter :: error=0.000001
 real(10):: total_calka, division,tot_old,blad
 real(10),parameter:: intrange=7.0
  real(10),dimension(ngauss),parameter::xx=(/-0.9602898565d0,&
  -0.7966664774d0,-0.5255324099d0,-0.1834346425d0,&
  0.1834346425d0,0.5255324099d0,0.7966664774d0,0.9602898565d0/)
  real(10),Dimension(ngauss),parameter::ww=(/0.1012285363d0,&
  0.2223810345d0,0.3137066459d0,0.3626837834d0,&
  0.3626837834d0,0.3137066459d0,0.2223810345d0,0.1012285363d0/)
 real(10) :: r, u, r6, tempred, f, r2, r1, calka,beta
 real(10) :: inte
 real :: start, finish
 integer:: i,j,irange
 real(10),dimension(ngauss)::x,w,integrand         
 end module zmienne

模块2

module in
  implicit none
  contains
     real(10) function inte(y,beta,r2,r1)
     real(kind=10)::r,beta,r6,r2,r1,u,y
     r=(r2-r1)*y+r1
     r6=(1.0/r)**6
     u=beta*r6*(r6-1.0d0)
     if (u>100.d0) then 
     inte=-1.0d0 
     else
     inte=exp(-u)-1.d0 
     endif
     inte=r*r*inte
     end function
end module in

虽然我这样称呼他们:

use zmienne; use in

我收到以下错误:

Name 'inte' at (1) is an ambiguous reference to 'inte' from module 'zmienne'

我已删除 module1 中的“inte”,但现在出现以下错误:

irange=inte(intrange/division)
           1
Error: Missing actual argument for argument 'beta' at (1)

主程序代码为:

 program wykres
 use zmienne; use in
 implicit none
 open(unit=out_unit, file='wykresik.dat', action='write', status='replace')
 open(unit=out_unit1, file='wykresik1.dat', action='write')
 open(unit=out_unit2, file='wykresik2.dat', action='write')
 open(out_unit3, file='wykresik3.dat', action='write')

! the gaussian points (xx) and weights (ww) are for the [-1,1] interval
! for [0,1] interval we have (vector instr.)
     x=0.5d0*(xx+1.0d0)
     w=0.5d0*ww
! plots 
   tempred = 1.0
   call cpu_time(start)
 do i=1,1000
    r=float(i)*0.01
    r6=(1.0/r)**6
    u=beta*r6*(r6-1.0)
    f=exp(-u/tempred)-1.0
    write(out_unit,*) r, u
    write(out_unit1,*)r, f
    write(out_unit2,*)r, r*r*f
end do
   call cpu_time(finish)
 print '("Time = ",f6.3," seconds.")',finish-start
! end of plots
! integration  1
 calka=0.0
 r1=0.0
 r2=0.5
    do i=1,ngauss
    r=(r2-r1)*x(i)+r1
    r6=(1.0/r)**6
    u=beta*r6*(r6-1.0d0)
! check for underflows
    if (u>100.d0) then 
    f=-1.0d0 
    else
    f=exp(-u)-1.d0 
    endif
! the array integrand is introduced in order to perform vector calculations below
    integrand(i)=r*r*f
    calka=calka+integrand(i)*w(i)
    enddo
    calka=calka*(r2-r1)

    write(*,*)calka
! end of integration    

! integration 2
  calka=0.0    
     do i=1,ngauss
     integrand(i)=inte(x(i),beta,r2,r1)
     calka=calka+integrand(i)*w(i)
     enddo
     calka=calka*(r2-r1)
! end of integration 2    
    write(*,*)calka

! vector integration  and analytical result  
    write(*,*)sum(integrand*w*(r2-r1)),-(0.5**3)/3.0

!**************************************************************



! tot_calka - the sum of integrals all integration ranges    
! dividion the initial length of the integration intervals    
!  tot_old - we will compare the results fro two consecutive divisions.
! at the beginning we assume any big number
!  blad - the difference between two consecutive integrations,
! at the beginning we assume any big number
! error - assumed precission, parameter, it is necassary for
! performing do-while loop 
    total_calka=0.0
    division=0.5
    tot_old=10000.0
    blad=10000.0
    do while (blad>error)
! intrange - the upper integration limit, it should be estimated
!  analysing the plot of the Mayer function. Here - 7.
! irange = the number of subintegrals we have to calculate
    irange=inte(intrange/division)
    total_calka=-(0.5**3)/3.0
!   the analytical result for the integration range [0,0.5]

! the loop over all the intervals, for each of them we calculate 
! lower and upper limits, r1 and r2
    do j=1,irange
    r1=0.5+(j-1)*division
    r2=r1+division
    calka=0.0
! the integral for a given interval   
    do i=1,ngauss
      integrand(i)=inte(x(i),beta,r2,r1)
     calka=calka+integrand(i)*w(i) 
    enddo
    total_calka=total_calka+calka*(r2-r1)
    enddo
! aux. output: number of subintervals, old and new integrals
        write(*,*) irange,division,tot_old,total_calka
    division=division/2.0
    blad=abs(tot_old-total_calka)
    tot_old=total_calka
! and the final error
       write(*,*) blad
    enddo

   open(1,file='calka.dat', access='append')
! the secod viarial coefficient=CONSTANT*total_calka, 
! CONSTANT is omitted here
   write(1,*)tempred,total_calka
   close(1)
end program wykres

【问题讨论】:

    标签: gcc fortran fortran90 gfortran intel-fortran


    【解决方案1】:

    inte 在两个模块中都声明了。

    更新。 inte(y,beta,r2,r1) 函数在模块in 中定义,并在主程序中使用。这个函数需要四个参数,但是这个调用

    irange=inte(intrange/division)
    

    只提供一个参数。我不确定在这种情况下是否应该使用此功能。尝试对变量和函数使用有意义的长名称以避免类似问题。

    【讨论】:

    • 谢谢你告诉我这么一件我还没有意识到的显而易见的事情。但现在我遇到了一个新错误。也不知道该怎么办。
    • 但是当整个东西被打包到一个没有任何模块的程序中时,代码可以流畅地运行。为什么在我即将用模块更清楚的时候呢?
    • 我建议先使用一些style guidelines 重构代码,然后阅读代码会容易得多。简单地将代码拆分为多个文件是不够的。
    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2023-03-17
    • 2021-11-23
    • 2019-11-01
    • 2016-02-16
    • 2016-09-11
    • 1970-01-01
    相关资源
    最近更新 更多