Fortran - 显式接口 [英] Fortran - explicit interface

查看:395
本文介绍了Fortran - 显式接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Fortran的新手,对于我的研究,我需要得到一个模型的怪物跑,所以我正在学习,因为我一直在。所以对不起,如果我问一个愚蠢的问题。
我试图编译(Mac OSX,从命令行),我已经设法解决一些事情,但现在我遇到了一些我不知道如何解决。我想我得到的错误背后的想法,但是,再次,不知道如何解决。

I'm very new to Fortran, and for my research I need to get a monster of a model running, so I am learning as I am going along. So I'm sorry if I ask a "stupid" question. I'm trying to compile (Mac OSX, from the command line) and I've already managed to solve a few things, but now I've come across something I am not sure how to fix. I think I get the idea behind the error, but again, not sure how to fix.

这个模型是巨大的,所以我只会发布我认为是相关的(虽然我可以错了)。我有一个包含几个子程序的文件,开头是:

The model is huge, so I will only post the code sections that I think are relevant (though I could be wrong). I have a file with several subroutines, that starts with:

    !==========================================================================================!
!    This subroutine simply updates the budget variables.                                  !
!------------------------------------------------------------------------------------------!
subroutine update_budget(csite,lsl,ipaa,ipaz)

   use ed_state_vars, only : sitetype     ! ! structure
   implicit none

   !----- Arguments -----------------------------------------------------------------------!
   type(sitetype)  , target     :: csite
   integer         , intent(in) :: lsl
   integer         , intent(in) :: ipaa
   integer         , intent(in) :: ipaz
   !----- Local variables. ----------------------------------------------------------------!
   integer                      :: ipa
   !----- External functions. -------------------------------------------------------------!
   real            , external   :: compute_water_storage
   real            , external   :: compute_energy_storage
   real            , external   :: compute_co2_storage
   !---------------------------------------------------------------------------------------!


   do ipa=ipaa,ipaz
      !------------------------------------------------------------------------------------!
      !      Computing the storage terms for CO2, energy, and water budgets.               !
      !------------------------------------------------------------------------------------!
      csite%co2budget_initialstorage(ipa) = compute_co2_storage(csite,ipa)
      csite%wbudget_initialstorage(ipa)   = compute_water_storage(csite,lsl,ipa)
      csite%ebudget_initialstorage(ipa)   = compute_energy_storage(csite,lsl,ipa)
   end do

   return
end subroutine update_budget
!==========================================================================================!
!==========================================================================================!

我收到的错误信息

budget_utils.f90:20.54:

budget_utils.f90:20.54:

real,external :: compute_co2_storage
1

错误:虚拟参数'csite' 'compute_co2_storage'在(1)有一个属性,需要为这个过程显式接口

real , external :: compute_co2_storage 1
Error: Dummy argument 'csite' of procedure 'compute_co2_storage' at (1) has an attribute that requires an explicit interface for this procedure

(我得到一大堆他们,但他们基本上是一样的)。现在,查看ed_state_vars.f90(在子程序中是已使用),我找到

(I get a bunch of them, but they are essentially all the same). Now, looking at ed_state_vars.f90 (which is "used" in the subroutine), I find

!============================================================================!
!============================================================================!
  !---------------------------------------------------------------------------!  
  ! Site type:
  ! The following are the patch level arrays that populate the current site.
  !---------------------------------------------------------------------------!  

type sitetype


     integer :: npatches

     !  The global index of the first cohort in all patches
     integer,pointer,dimension(:) :: paco_id

     ! The number of cohorts in each patch
     integer,pointer,dimension(:) :: paco_n

     ! Global index of the first patch in this vector, across all patches
     ! on the grid

     integer :: paglob_id

     ! The patches containing the cohort arrays
     type(patchtype),pointer,dimension(:) :: patch

等等 - 这去一个另外500行左右。
所以为了达到这一点,似乎原来的子程序需要一个明确的接口为其过程,以便能够使用(dummy)参数csite。再次,我是Fortran的新,但我真的试图了解它如何思考。我一直在寻找什么意思,有一个明确的接口,什么时候(以及如何!)使用它等,但我不知道它如何适用于我的情况。我应该使用不同的编译器(Intel?)。任何提示?

Etc etc - this goes one for another 500 lines or so. So to get to the point, it seems like the original subroutine needs an explicit interface for its procedures in order to be able to use the (dummy) argument csite. Again, I am SO NEW to Fortran, but I am really trying to understand how it "thinks". I have been searching what it means to have an explicit interface, when (and how!) to use it etc. But I can't figure out how it applies in my case. Should I maybe use a different compiler (Intel?). Any hints?

编辑:因此 csite 声明为目标在所有过程中并从声明 type(site type)包含一整套指针 sitetype 中。但是 sitetype 正在正确使用 d从另一个模块( ed_state_vars.f90 )。所以我仍然困惑为什么它给我明确的接口错误?

So csite is declared a target in all procedures and from the declaration type(site type) contains a whole bunch of pointers, as specified in sitetype. But sitetype is being properly used from another module (ed_state_vars.f90) in all procedures. So I am still confused why it gives me the explicit interface error?

推荐答案

显式接口意味着向编译器声明过程的接口(子例程或函数)。这允许编译器检查对过程的调用和实际过程之间的参数的一致性。这可以发现很多程序员的错误。你可以使用 interface 语句编写接口,但是有一个更容易的方法:将过程放入一个模块中,并使用 use 该模块从任何其他实体调用它 - 从主程序或任何程序本身不在模块中。但是你不会使用来自同一个模块中的另一个过程的过程 - 它们彼此自动知道。

"explicit interface" means that the interface to the procedure (subroutine or function) is declared to the compiler. This allows the the compiler to check consistency of arguments between calls to the procedure and the actual procedure. This can find a lot of programmer mistakes. You can do this writing out the interface with an interface statement but there is a far easier method: place the procedure into a module and use that module from any other entity that calls it -- from the main program or any procedure that is itself not in the module. But you don't use a procedure from another procedure in the same module -- they are automatically known to each other.

将过程放入模块会自动使其接口为编译器所知,并且当使用 ed时可用于交叉检查。这比编写界面更容易,更不容易犯错误。使用接口,您必须复制过程参数列表。

Placing a procedure into a module automatically makes its interface known to the compiler and available for cross-checking when it is useed. This is easier and less prone to mistakes than writing an interface. With an interface, you have to duplicate the procedure argument list. Then if you revise the procedure, you also have to revise the calls (of course!) but also the interface.

一个显式的接口(接口) 语句或模块)是必需的,当您使用高级参数。否则编译器不知道生成正确的调用

An explicit interface (interface statement or module) is required when you use "advanced" arguments. Otherwise the compiler doesn't know to generate the correct call

如果你有一个使用你不应该用 external 来描述它。在现代Fortran中很少使用 external - 因此,删除外部属性,将所有的过程成为一个模块,使用

If you have a procedure that is useed, you shouldn't describe it with external. There are very few uses of external in modern Fortran -- so, remove the external attributes, put all of your procedures into a module, and use them.

这篇关于Fortran - 显式接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆