我正在尝试创建一个可供主程序和所有子程序全局访问的数据结构。数据结构是通过读取一些 .dat 文件构建的。
这种全局可访问性似乎适合模块。到目前为止,我的模块解决方案包括:1)全局定义数据类型;2)在模块中包含(包含)一堆子程序来打开/读取.dat文件;和 3) 从 .dat 文件构造数据类型。
理想情况下,我想在模块中一次性构建这个数据结构,然后让这个单一的数据结构全局访问。我不想每次调用模块过程时都打开/读取 .dat 文件。
例如。有没有办法从主程序中将数据结构声明为全局变量,然后调用模块过程来构建数据结构一次?
@罗斯。源代码:
module DataTypeModule
implicit none
type :: DatCube
integer :: NGrid(4)
double precision, allocatable :: tgrid(:)
end type DatCube
contains
subroutine DataArraySizes(NGrd)
implicit none
integer, intent(out) :: NGrd(4)
open(unit=15, file='./Data/DataHeader.txt', status='old')
read(15,*) NGrd(1)
read(15,*) NGrd(2)
read(15,*) NGrd(3)
read(15,*) NGrd(4)
close(15)
end subroutine DataArraySizes
subroutine DataTGrd(NGrd,tgrd)
implicit none
integer, intent(in) :: NGrd(4)
double precision, intent(out) :: tgrd(NGrd(1))
open(unit=16, file='./Data/tgrid.dat', status='old')
read(16,*) tgrd
close(16)
end subroutine DataTGrd
subroutine ConstructDataCube(DataCube)
implicit …Run Code Online (Sandbox Code Playgroud)