fortran通过分配数组到主程序 [英] fortran pass allocated array to main procedure
问题描述
我有一个带有一个函数的模块,它有一个起点和终点,并读入一个 .txt
一些浮点值。
我希望函数返回一个表,我不知道它会在启动之前有多大。
我希望在函数中使用这个函数两次主要程序做出第三个真实数组。但是Fortran并不喜欢它。
这是我的代码:
模块ReadData
!在这部分中,您需要知道:
! - 开始(不能是第一或第二点)
! - 结束点
! - 文件名(在这里:cham / comp或flow)
!如果它不是AL026_Pd,则更改第40行
! - 它在文件上
隐含无
INTERFACE ReadP
模块程序ReadDataPressure
结束界面
private :: ReadDataPressure
包含
函数ReadDataPressure(whereabout,StartingPoint,EndingPoint)结果(P1)
!***************** *****
!**在变量中声明**
character(50):: whereabout!needed:cham / comp或flow
real(8):: StartingPoint,EndingPoint
!************************
!**声明使用的变量**
字符( 50):: FileNameConstructed
real(8):: deltat,CurrentTime,pressure
integer(8):: i,k
!******** **************
!**声明变量**
real(8),allocatable :: P1(:)
!程序本身的开始$ b $ b write(FileNameConstructed,'(a,a,a)')AL026_pd,whereabout,.txt
open(20,file = FileNameConstructed,status ='old',action ='read')
read(20,*)deltat,pressure
read(20,*)CurrentTime,pressure
deltat = CurrentTime-deltat $ b $!现在deltaT是循环计数器,但是我们在进程中丢失了两个可用的行
allocate(P1(1:int(((EndingPoint-StartingPoint) / deltat + 1))))
k = 1
do i = 0,int((EndingPoint-2 * deltat)/ deltat)
read(20,*)CurrentTime ,压力
if(CurrentTime> StartPoint)然后
P1(k)=压力
k = k + 1
write(*,*)p1(k)
end如果
end do
end function ReadDataPressure
End module
和我希望在主程序中做这样的事情。
a = ReadP(comp,350,750)
b = ReadP流量,350,750)
do i = 1; lenght_of_a
m_ox(i)= squarreroot(a(i)-b(i))
end do
end然后将其写入另一个文件。
我发现:共享可分配数组
FORTRAN - 子程序中的可分配数组
但他们没有帮助我。
有人认为可能是
http://www.stanford.edu/class /me200c/tutorial_90/09_modules.html
更接近该解决方案。
但他们不想在最后一张表,他们使用 Prod_A = PRODUCT(A)
,因此您不知道 a
的维数,但可以执行乘积或求和。但是我想保留它。
A
,如果您有Fortran 2003编译器,请执行: A = ReadDataPressure
这就是你想要的。这是分配,这是Fortran 2003的一部分。
为什么你说fortran不喜欢它?什么是特定的错误信息?
使用不支持这种编译器的编译器会使过程变成子例程,但会更简单。在主程序和子程序中声明该数组为可分配的。使它成为 intent(out)
参数并将其分配到子例程中。
除非你还没有显示其他方面,否则为单一程序设置模块程序似乎毫无意义。我将离开接口和模块过程,并使ReadDataPressure公开,以便直接调用它。
I have a module with a function which takes a starting and ending point and reads in a .txt
some float value.
I wish that function to return a table which I do not know how large it will be before it starts.
I wish to use this function twice in the main program to make a third real array. But Fortran doesn't like it much.
Here is my code for the function :
module ReadData
!in this part, you need to know :
! -the starting (cannot be the first or second point)
! -end point
! -the file name (here : cham/comp or flow)
! change line 40 in case it is not AL026_Pd anymore
! -where it is on the file
implicit none
INTERFACE ReadP
MODULE PROCEDURE ReadDataPressure
END INTERFACE
private :: ReadDataPressure
contains
function ReadDataPressure (whereabout,StartingPoint,EndingPoint) result (P1)
!**********************
!**decla in variables**
character(50) :: whereabout !needed : cham/comp or flow
real(8) :: StartingPoint,EndingPoint
!************************
!**decla used variables**
character(50) :: FileNameConstructed
real(8) :: deltat,CurrentTime,pressure
integer(8) :: i,k
!**********************
!**decla out variable**
real(8),allocatable :: P1(:)
!start of the programe itself
write (FileNameConstructed,'(a,a,a)') "AL026_pd",whereabout,".txt"
open(20,file=FileNameConstructed,status='old',action='read')
read (20,*) deltat,pressure
read (20,*) CurrentTime,pressure
deltat=CurrentTime-deltat
!now deltaT is the loop counter, but we "lost" two usable line in the process
allocate (P1(1:int(((EndingPoint-StartingPoint)/deltat+1))))
k=1
do i=0,int((EndingPoint-2*deltat)/deltat)
read (20,*) CurrentTime,pressure
if (CurrentTime>StartingPoint) then
P1(k)=pressure
k=k+1
write(*,*) p1(k)
end if
end do
end function ReadDataPressure
End module
and I wish to do something like this in the main program
a=ReadP(comp,350,750)
b=ReadP(flow,350,750)
do i=1; lenght_of_a
m_ox(i)=squarreroot(a(i)-b(i))
end do
end then write it in another file.
I found : Share allocatable Arrays
FORTRAN - allocatable array in subroutine
but they did not help me.
One thinks perhaps http://www.stanford.edu/class/me200c/tutorial_90/09_modules.html is closer to the solution.
But they don't want a table at the end, they use Prod_A = PRODUCT(A)
so you do not know the dimension of a
, but can do product or sum. But I want to keep it whole.
In the main program you should be able to declare an allocatable array A
and, if you have a Fortran 2003 compiler, do:
A = ReadDataPressure
Which is what you wish. This is allocation on assignment, which is part of Fortran 2003. Why do you say that "fortran doesn't like it"? What are the specific error messages?
With compilers that don't support this it will be easier though less elegant to make the procedure a subroutine. Declare the array as allocatable in both the main program and the subroutine. Make it an intent (out)
argument and allocate it in the subroutine.
P.S. Unless there are other aspects that you are not showing, setting up a module procedure for a single procedure seems pointless. I would leave off the interface and module procedure and make ReadDataPressure public so that it is directly called.
这篇关于fortran通过分配数组到主程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!