我有一个带有函数的模块,它带有一个起点和终点,并读入 .txt
某个浮点值 . 我希望该函数返回一个表,我不知道它在启动之前会有多大 .
我希望在主程序中使用此函数两次来制作第三个真实数组 . 但Fortran并不喜欢它 .
这是我的函数代码:
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
我希望在主程序中做这样的事情
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
然后将其写入另一个文件 .
我找到了:Share allocatable Arrays
FORTRAN - allocatable array in subroutine
但他们没有帮助我 .
有人认为也许http://www.stanford.edu/class/me200c/tutorial_90/09_modules.html更接近解决方案 .
但他们最后不想要一张 table ,他们使用 Prod_A = PRODUCT(A)
所以你不知道 a
的维度,但可以做产品或总和 . 但我想保持整体 .
2 回答
在主程序中,你应该能够声明一个可分配的数组
A
,如果你有一个Fortran 2003编译器,你可以:这是你想要的 . 这是赋值的分配,它是Fortran 2003的一部分 . 为什么你说“fortran不喜欢它”?具体的错误信息是什么?
对于不支持此编译器的编译器,将该过程作为子例程将更容易但更不优雅 . 在主程序和子程序中将数组声明为可分配的 . 使它成为
intent (out)
参数并在子例程中分配它 .附:除非您没有显示其他方面,否则为单个过程设置模块过程似乎毫无意义 . 我将省略接口和模块过程并使ReadDataPressure公开,以便直接调用它 .
错误是:
因为有
除了FileNameConstructed也是一个字符(50) . (所以我尝试将8 50 4放入50) . 但是在删除私有之前我无法看到它 . 所以,谢谢MSB . 你帮助了我很多 . 我改变了角色(4)的位置(因为它完全符合我的需要),所以它正在运行