Fortran - 显式接口

Ger*_*ine 9 fortran compilation explicit-interface

我对Fortran很新,而且对于我的研究,我需要让一个模型运行的怪物,所以我正在学习,因为我正在进行.如果我问一个"愚蠢"的问题,我很抱歉.我正在尝试编译(Mac OSX,从命令行),我已经设法解决了一些问题,但现在我遇到了一些我不确定如何解决的问题.我想我得到了错误背后的想法,但同样,不知道如何解决.

模型很大,所以我只会发布我认为相关的代码部分(虽然我可能是错的).我有一个包含几个子程序的文件,它以:

    !==========================================================================================!
!    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
!==========================================================================================!
!==========================================================================================!
Run Code Online (Sandbox Code Playgroud)

我得到了错误信息

budget_utils.f90:20.54:

真实的,外部:: compute_co2_storage 1个
错误:伪参数过程"compute_co2_storage"的"csite"在(1)具有一个需要此过程显式接口的属性

(我得到了很多,但它们基本上都是一样的).现在,看一下ed_state_vars.f90(在子程序中"使用"),我发现

!============================================================================!
!============================================================================!
  !---------------------------------------------------------------------------!  
  ! 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
Run Code Online (Sandbox Code Playgroud)

等等 - 这相当于另外500行左右.所以为了达到这一点,似乎原始子程序需要一个显式接口来处理它的程序,以便能够使用(虚拟)参数csite.再一次,我对Fortran很新,但我真的想了解它是如何"思考"的.我一直在寻找具有显式界面的意义,何时(以及如何!)使用它等等.但我无法弄清楚它在我的情况下是如何应用的.我应该使用不同的编译器(英特尔?).任何提示?

编辑:所以在所有程序中csite声明一个target并且从声明中type(site type)包含一大堆pointers,如中所述sitetype.但是在所有程序中sitetype正在use从另一个模块(ed_state_vars.f90)中正确地进行操作.所以我仍然困惑为什么它给我显式接口错误?

M. *_* B. 16

"显式接口"表示将过程(子例程或函数)的接口声明给编译器.这允许编译器检查对过程的调用和实际过程之间的参数的一致性.这可以找到很多程序员的错误.您可以使用interface语句写出接口,但有一个更容易的方法:将过程放入一个模块,并将use该模块放置在调用它的任何其他实体中 - 来自主程序或任何本身不在模块.但是,您不是use来自同一模块中的另一个过程的过程 - 它们彼此自动相同.

将过程放入模块会自动使编程器知道其接口,并在use编辑时进行交叉检查.与编写接口相比,这更容易,也更不容易出错.使用接口,您必须复制过程参数列表.然后,如果您修改过程,您还必须修改调用(当然!)以及界面.

interface使用"高级"参数时,需要显式接口(语句或模块).否则编译器不知道生成正确的调用

如果您有一个useed 过程,则不应该用它来描述external.external现代Fortran中很少使用- 因此,删除external属性,将所有过程放入模块中,然后将use它们放入模块中.

  • 不过,将一百万行模型转换为模块可能相当困难。 (3认同)