Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 嵌套的contains语句_Fortran - Fatal编程技术网

Fortran 嵌套的contains语句

Fortran 嵌套的contains语句,fortran,Fortran,我有一个这样的程序 program call one() contains one() some vars contains two() use the vars of one define its own vars contains

我有一个这样的程序

program
    call one()
    contains one()
                 some vars
                 contains two()
                              use the vars of one
                              define its own vars
                              contains three()
                                           use the vars of both one and two
这不会编译,因为Fortran只允许第一个contains语句

我使用这种设计来避免将
one()
的所有变量传递并重新键入
two()
three()

如何重写程序以实现变量共享

我无法在语句
callone()
之前定义所有变量
代码将很难管理,我需要功能:
父子例程无法访问内部子例程变量

也许解决办法是使用指针

call one(pointer_to_two)
然后在自己的模块中定义例程
two()

但由于我有限的Fortran技能,我发现这种设计很复杂。
我担心这会影响性能

我应该使用
公共块吗?
我在英特尔编译器中使用最新的Fortran方言

下面是“可编译”的示例

program nested_routines

    call one()

contains

    subroutine one()
        integer :: var_from_one = 10

        print *, 1
        call b()

        contains

            subroutine two()
                integer :: var_from_two = 20

                print *, 2, var_from_one
                call c()

            contains

                    subroutine three()
                        print *, 3, var_from_one, var_from_two
                    end subroutine

            end subroutine

    end subroutine

end module 

我建议将程序(子程序和函数)放在单个“包含”之后的模块中,并从主程序使用该模块。每个过程的局部变量将隐藏在它们的调用者中。与您的目标不同的是,您必须重新声明变量。我不喜欢继承另一个子例程中包含的所有变量,因为可能会错误地重用一个变量。如果您有几个在许多过程中共享的变量,那么适当的设计选择可能是使它们成为全局变量。Fortran>=90时,模块变量比普通块更适合作为全局变量。如果变量在数量有限的过程之间进行通信,通常最好使用参数,因为这样可以使信息流更清晰。

我建议将过程(子程序和函数)放在单个“包含”后的模块中,并从主程序使用该模块。每个过程的局部变量将隐藏在它们的调用者中。与您的目标不同的是,您必须重新声明变量。我不喜欢继承另一个子例程中包含的所有变量,因为可能会错误地重用一个变量。如果您有几个在许多过程中共享的变量,那么适当的设计选择可能是使它们成为全局变量。Fortran>=90时,模块变量比普通块更适合作为全局变量。如果变量在数量有限的过程之间进行通信,通常最好使用参数,因为这样可以使信息流更清晰。

如果每个函数特定变量都有一个单独的模块,每个函数实现都有一个单独的模块,这可能是可能的

根据使用层次,注意模块编译的顺序

此外,我不知道这样做对性能的影响

module m1_var
  implicit none
  contains
end module m1_var
!========================================================================================================================
module m2_var
  implicit none
  contains
end module m2_var
!========================================================================================================================
module m3_var
  implicit none
  contains
end module m3_var
!========================================================================================================================
module m3_sub
  implicit none
  contains
  subroutine s3()
  use m2_var !to see varibles
  !arguments

  !local

  !allocation

  !implementation

  end subroutine s3
end module m3_sub
!========================================================================================================================
module m2_sub
  implicit none
  contains
  subroutine s2()
  use m3_sub
  use m1_var
  !arguments

  !local

  !allocation

  !implementation
    call s3
  end subroutine s2
end module m2_sub
!========================================================================================================================
module m1_sub
  use m1_var
  implicit none
  contains
  subroutine s1()
  use m2_sub
  !arguments

  !local

  !allocation

  !implementation
!    call s2
  end subroutine s1
end module m1_sub
!========================================================================================================================
program nestmoduse
  use m1_sub
  implicit none
  call s1
end program nestmoduse

如果每个特定于函数的变量都有一个单独的模块,每个函数实现都有一个单独的模块,这可能是可能的

根据使用层次,注意模块编译的顺序

此外,我不知道这样做对性能的影响

module m1_var
  implicit none
  contains
end module m1_var
!========================================================================================================================
module m2_var
  implicit none
  contains
end module m2_var
!========================================================================================================================
module m3_var
  implicit none
  contains
end module m3_var
!========================================================================================================================
module m3_sub
  implicit none
  contains
  subroutine s3()
  use m2_var !to see varibles
  !arguments

  !local

  !allocation

  !implementation

  end subroutine s3
end module m3_sub
!========================================================================================================================
module m2_sub
  implicit none
  contains
  subroutine s2()
  use m3_sub
  use m1_var
  !arguments

  !local

  !allocation

  !implementation
    call s3
  end subroutine s2
end module m2_sub
!========================================================================================================================
module m1_sub
  use m1_var
  implicit none
  contains
  subroutine s1()
  use m2_sub
  !arguments

  !local

  !allocation

  !implementation
!    call s2
  end subroutine s1
end module m1_sub
!========================================================================================================================
program nestmoduse
  use m1_sub
  implicit none
  call s1
end program nestmoduse