如何指示子程序使用哪个模块可分配数组

How indicate to a subroutine, which of the module allocatable arrays use

我正在做的项目,需要通过一堆子程序和函数传递大量的数组,所以我选择了一个模块。

这些数组都是可分配的,它们不会造成太多麻烦,除非我必须明确说明子例程应该使用这些数组中的哪一个。 我将代码管理到 运行 的方式完全是矫枉过正。

它的工作方式:

Program Principal
 Use Info
 open(unit=1,file="dadose6h.dat")
 call get_data
 call sortsupply
 call sortcost
 call ratecapacity
end Program

Module Info
 integer i,j
 integer, dimension (:), allocatable :: supply
 integer, dimension (:), allocatable :: cost
 integer, dimension (:,:), allocatable :: capacity
 integer, dimension (:,:), allocatable :: demand
End module info


subroutine get_data
 use info
 read(1,*) j,i
 allocate (supply (j))
 allocate (cost (j))
 allocate (capacity (j,i))
 allocate (demand (j,i))
 read(1,*) supply
 read(1,*) cost
 read(1,*) capacity
 read(1,*) demand
end subroutine

Subroutine SortCost
 use info
 integer u
 !u=cost(1)
 !...
 print*, cost
End subroutine

Subroutine Sortsupply
 use info
 integer u
 !u=supply(1)
 !...
 print*, supply
End subroutine

Subroutine ratecapacity
 use info
 integer u
 !u=capacity(1,1)     
 !...
 print *, j,i
 print*, capacity
End subroutine

上面的例子除了正在排序的数组外,还有两个子程序sortcost和sortsupply是相等的。我真的想要一个独特的子程序来完成这两项任务。我只是不能宣布正确的方式。

应该是这样的:

Program Principal
 Use Info
 open(unit=1,file="dadose6h.dat")
 call get_data
 call sort(supply)
 call sort(cost)
 call rate(capacity)
end Program

Module Info
 integer i,j
 integer, dimension (:), allocatable :: supply
 integer, dimension (:), allocatable :: cost
 integer, dimension (:,:), allocatable :: capacity
 integer, dimension (:,:), allocatable :: demand
End module info


subroutine get_data
 use info
 read(1,*) j,i
 allocate (supply (j))
 allocate (cost (j))
 allocate (capacity (j,i))
 allocate (demand (j,i))
 read(1,*) supply
 read(1,*) cost
 read(1,*) capacity
 read(1,*) demand
end subroutine

Subroutine Sort(X)
 !use info
 !i dunno how the declaration should be
 integer u
 !u=X(1)
 !...
 print*, "Sort:",X
End subroutine

Subroutine rate(X)
 !use info
 !i dunno how the declaration should be neither
 integer u
 !u=X(1,1)
 !...
 print*, "rate:", X
End subroutine

我确实尝试了一些声明,但 none 工作,这些人有类似的问题 FORTRAN - allocatable array in subroutine , How to pass allocatable arrays to subroutines in Fortran,在第一个答案中,错误在意图中,但我已经尝试过(inout,out ,in) 但仍然不起作用。第二个有一个关于显式接口的答案,但我不会对分配状态做任何事情。

注意:我问这个问题的老师经常忽略这一点,问题不是关于将可分配数组传递给例程,我已经可以使用模块 Info 做到这一点,但是我需要显示子例程

我真的很感激谁能写出这两个子程序的声明。排序(一维数组)和速率(二维)。或者至少教我一下。

dadose6h.dat 文件的内容:

4 
7
1000 2000 2000 2000
100 100 100 100 
10 10 74 19
60 1 25 20
90 50 7 2
11 31 51 96
15 10 94 36
52 89 47 13
30 35 4 12
100 150 50 200
100 100 200 75
100 100 200 250
100 100 150 250
150 100 200 250
200 100 200 250
200 150 200 250

你的设计还是让我很困惑。我会这样做:

Module Info
 integer i,j
 integer, dimension (:), allocatable :: supply
 integer, dimension (:), allocatable :: cost
 integer, dimension (:,:), allocatable :: capacity
 integer, dimension (:,:), allocatable :: demand
End module info


Module Procedures

contains

  Subroutine Sort(X)
   integer :: X(:)

   integer u
   !u=X(1)
   !...
   print*, "Sort:",X
  End subroutine

End module Procedures


Program Principal
 Use Info
 Use Procedures

 open(unit=1,file="dadose6h.dat")
 call get_data
 call sort(supply)
 call sort(cost)
 call rate(capacity)
end Program

那是一个非常小的变化,所以我可能不明白你的描述。我看不出为什么排序过程需要带有数据的模块。