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
Module Fortran派生类型是否可以包含来自不同模块的组件?_Module_Fortran_Derived Types - Fatal编程技术网

Module Fortran派生类型是否可以包含来自不同模块的组件?

Module Fortran派生类型是否可以包含来自不同模块的组件?,module,fortran,derived-types,Module,Fortran,Derived Types,我正在开发一个派生类型。它不断增长,使它所在的模块变得笨重。有没有办法将模块分解为多个模块,并让派生类型从各个模块获取其组件?正如@francescalus所指出的,您可以使用其他模块的派生类型来 创建新的派生类型。我觉得他的例子有点短,所以我写了一篇 我希望这个小例子能让你对这样的事情有个了解 我可以工作。也许这个例子的篇幅比严格必要的要长,但我确实是 我玩得很开心 我的派生类型描述旅行计划,包括行李和行程。 它附带一个子例程,将打印给定的旅行计划 module travel use Lugg

我正在开发一个派生类型。它不断增长,使它所在的模块变得笨重。有没有办法将模块分解为多个模块,并让派生类型从各个模块获取其组件?

正如@francescalus所指出的,您可以使用其他模块的派生类型来 创建新的派生类型。我觉得他的例子有点短,所以我写了一篇 我希望这个小例子能让你对这样的事情有个了解 我可以工作。也许这个例子的篇幅比严格必要的要长,但我确实是 我玩得很开心

我的派生类型描述旅行计划,包括行李和行程。 它附带一个子例程,将打印给定的旅行计划

module travel
use Luggage
use Routing
   type tTravel
       type(tItinerary) :: trip
       type(tSuitcase)  :: suitcase
   end type tTravel
contains
   subroutine printTravel(travel)
   implicit none
       type(tTravel), intent(in) :: travel
       print '(a)','    Luggage:'
       call printSuitcase(travel%suitcase)
       print '(a)','    Itinerary:'
       call printItinerary(travel%trip)
   end subroutine printTravel
end module travel
旅行计划的两个组成部分,行李和行程,每个都有自己的 单元首先,行李模块:

module Luggage
   type tSuitcase
       integer :: socks = 2
       integer :: shirts = 1
       integer :: underwear = 1
       integer :: raincoats = 0
   end type tSuitcase
contains
   subroutine printSuitcase(suitcase)
   implicit none
       type(tSuitcase), intent(in) :: suitcase
       print '(i10,a)', suitcase%socks,'  socks'
       print '(i10,a)', suitcase%shirts,'  shirts'
       print '(i10,a)', suitcase%underwear,'  underwear'
       print '(i10,a)', suitcase%raincoats,'  raincoats'
   end subroutine printSuitcase
end module Luggage
接下来是行程模块:

module Routing
   integer,          parameter :: &
     HOME=1,     MONACO=2,   IBIZA=3,     BIARRITZ=4, &
     nDESTINATIONS=4
   character(len=8), parameter :: destination_names(nDESTINATIONS) = (/ &
     'Home    ', 'Monaco  ', 'Ibiza   ', 'Biarritz' /)
   integer, parameter :: maxTripLen = 100

   type tItinerary
       integer  :: length = 0
       integer  :: destinations(maxTripLen)
   end type tItinerary
contains
   subroutine addDestination(trip,destination)
   implicit none
       type(tItinerary), intent(inout) :: trip
       integer,          intent(in)    :: destination
       if (destination<1 .or. destination>nDESTINATIONS) &
          stop('illegal destination')
       if (trip%length >= maxTripLen) stop('Trip too long')
       trip%length = trip%length + 1
       trip%destinations(trip%length) = destination
   end subroutine AddDestination

   subroutine printItinerary(trip)
   implicit none
       type(tItinerary), intent(in) :: trip
       integer :: i
       if (trip%length==0) then
          print '(a)','        Empty itinerary'
       else
          print '(100(a))','        '//trim(destination_names(trip%destinations(1))), &
             ('-',trim(destination_names(trip%destinations(i))), i=2,trip%length)
       end if
   end subroutine printItinerary
end module Routing

正如@francescalus指出的,您可以使用来自其他模块的派生类型来 创建新的派生类型。我觉得他的例子有点短,所以我写了一篇 我希望这个小例子能让你对这样的事情有个了解 我可以工作。也许这个例子的篇幅比严格必要的要长,但我确实是 我玩得很开心

我的派生类型描述旅行计划,包括行李和行程。 它附带一个子例程,将打印给定的旅行计划

module travel
use Luggage
use Routing
   type tTravel
       type(tItinerary) :: trip
       type(tSuitcase)  :: suitcase
   end type tTravel
contains
   subroutine printTravel(travel)
   implicit none
       type(tTravel), intent(in) :: travel
       print '(a)','    Luggage:'
       call printSuitcase(travel%suitcase)
       print '(a)','    Itinerary:'
       call printItinerary(travel%trip)
   end subroutine printTravel
end module travel
旅行计划的两个组成部分,行李和行程,每个都有自己的 单元首先,行李模块:

module Luggage
   type tSuitcase
       integer :: socks = 2
       integer :: shirts = 1
       integer :: underwear = 1
       integer :: raincoats = 0
   end type tSuitcase
contains
   subroutine printSuitcase(suitcase)
   implicit none
       type(tSuitcase), intent(in) :: suitcase
       print '(i10,a)', suitcase%socks,'  socks'
       print '(i10,a)', suitcase%shirts,'  shirts'
       print '(i10,a)', suitcase%underwear,'  underwear'
       print '(i10,a)', suitcase%raincoats,'  raincoats'
   end subroutine printSuitcase
end module Luggage
接下来是行程模块:

module Routing
   integer,          parameter :: &
     HOME=1,     MONACO=2,   IBIZA=3,     BIARRITZ=4, &
     nDESTINATIONS=4
   character(len=8), parameter :: destination_names(nDESTINATIONS) = (/ &
     'Home    ', 'Monaco  ', 'Ibiza   ', 'Biarritz' /)
   integer, parameter :: maxTripLen = 100

   type tItinerary
       integer  :: length = 0
       integer  :: destinations(maxTripLen)
   end type tItinerary
contains
   subroutine addDestination(trip,destination)
   implicit none
       type(tItinerary), intent(inout) :: trip
       integer,          intent(in)    :: destination
       if (destination<1 .or. destination>nDESTINATIONS) &
          stop('illegal destination')
       if (trip%length >= maxTripLen) stop('Trip too long')
       trip%length = trip%length + 1
       trip%destinations(trip%length) = destination
   end subroutine AddDestination

   subroutine printItinerary(trip)
   implicit none
       type(tItinerary), intent(in) :: trip
       integer :: i
       if (trip%length==0) then
          print '(a)','        Empty itinerary'
       else
          print '(100(a))','        '//trim(destination_names(trip%destinations(1))), &
             ('-',trim(destination_names(trip%destinations(i))), i=2,trip%length)
       end if
   end subroutine printItinerary
end module Routing

正如我在第一篇评论中指出的,在您没有回复的部分中,如果您确实需要组件作为派生类型的实际组件,而不是另一个派生类型的组件,那么您可以使用类型扩展,因此如果您希望避免将类型进一步构造为树的话

请注意,一般来说,这不是一个好主意,有一个大单位类型,但据我所知,这是你所要求的,所以这里是我的答案

module mod1

  type part1
    ...many components
  end type
end module

module mod2
  use mod1
  type, extends(part1) :: part2
    ...many other components
  end type
end module

module the_actual_type_mod
  use mod2
  type, extends(part2) :: the_actual_type
    ...many other components
  end type
end module
提到的另一种方式是包括。结果并不相等,但就你的目的而言,几乎相等

module the_actual_type_mod
  use mod2
  type the_type
    include "part1.f90"
    include "part2.f90"
    include "part3.f90"
  end type
end module

正如我在第一篇评论中指出的,在您没有回复的部分中,如果您确实需要组件作为派生类型的实际组件,而不是另一个派生类型的组件,那么您可以使用类型扩展,因此如果您希望避免将类型进一步构造为树的话

请注意,一般来说,这不是一个好主意,有一个大单位类型,但据我所知,这是你所要求的,所以这里是我的答案

module mod1

  type part1
    ...many components
  end type
end module

module mod2
  use mod1
  type, extends(part1) :: part2
    ...many other components
  end type
end module

module the_actual_type_mod
  use mod2
  type, extends(part2) :: the_actual_type
    ...many other components
  end type
end module
提到的另一种方式是包括。结果并不相等,但就你的目的而言,几乎相等

module the_actual_type_mod
  use mod2
  type the_type
    include "part1.f90"
    include "part2.f90"
    include "part3.f90"
  end type
end module

没有什么比得上派生类型元素。你是说组件吗?你考虑过类型扩展继承吗?你是否考虑过你的程序设计,如果它导致这样的野兽?更正。是使用moda,仅限:a;t型;x型;末端类型;结束是可能的。这就是你的意思吗?它并不总是一个流行的选择,但如果你的文件越来越大,你可以考虑使用包含。虽然我没有太多的经验,子模块可能是另一个改进/替代。我有大约40k行,一个模块中有结构和常量,另一个模块中有函数,还有一些其他模块中有子程序。。。他们都在图书馆里。主程序导入所需的内容。将函数与子例程分离会大大加快编译速度,并且大多数修改都发生在子例程模块中。没有什么比派生类型元素更好的了。你是说组件吗?你考虑过类型扩展继承吗?你是否考虑过你的程序设计,如果它导致这样的野兽?更正。是使用moda,仅限:a;t型;x型;末端类型;结束是可能的。这就是你的意思吗?它并不总是一个流行的选择,但如果你的文件越来越大,你可以考虑使用包含。虽然我没有太多的经验,子模块可能是另一个改进/替代。我有大约40k行,一个模块中有结构和常量,另一个模块中有函数,还有一些其他模块中有子程序。。。他们都在图书馆里。主程序导入所需的内容。将函数与子例程分离会导致编译时间大大加快,并且大多数修改都发生在子例程模块中。