【发布时间】:2018-06-12 09:13:33
【问题描述】:
我正在编写一个小代码,它将从 2.txt 中读取数字,这是一堆随机实数,将其保存在二维数组中并将信息传递给子程序。在子例程中,我将此数组的特定维度分配给另一个矩阵以保存以供将来计算。
implicit none
double precision, allocatable :: x1(:,:)
integer :: nstep, nheatoms, k, i, j
open(unit=999,file="2.txt",status="old")
nstep = 5
nheatoms = 2
allocate(x1(3,nheatoms))
do k = 1, nstep
do i = 1, 3
do j = 1, nheatoms
read(999,*) x1(i,j)
end do
read(999,*)
end do
call diffusion(nstep,nheatoms,x1)
end do
stop
end
subroutine diffusion(nstep,nheatoms,qhecent)
implicit none
integer, intent (in) :: nheatoms, nstep
double precision :: qhecent(3,nheatoms)
integer :: j
double precision, allocatable :: qhestep(:,:)
integer :: nstepdiff, ncross
integer, save :: l = 0
double precision :: diff
l = l + 1
allocate(qhestep(nstep,nheatoms))
do j = 1, nheatoms
qhestep(l,j) = qhecent(3,j)
end do
if (l .gt. 1) then
do j = 1, nheatoms
write(*,*)qhestep(l,j), qhestep(l-1,j)
end do
end if
end subroutine
2.txt文件如下:
1.0
2.1
3.2
-1.1
-2.2
-3.3
5.0
3.5
4.4
1.9
2.1
1.5
6.0
3.5
4.4
1.9
2.8
2.5
6.0
3.5
4.4
1.9
2.1
3.2
6.0
3.5
4.4
1.9
-4.3
7.9
现在如果我用 gfortran 编译,大多数时候我得到的输出是:
2.1000000000000001 -2.2000000000000002
1.5000000000000000 -3.2999999999999998
2.7999999999999998 2.1000000000000001
2.5000000000000000 1.5000000000000000
2.1000000000000001 2.7999999999999998
3.2000000000000002 2.5000000000000000
-4.2999999999999998 2.1000000000000001
7.9000000000000004 3.2000000000000002
这是预期的。但是如果我多次运行代码,输出中会出现一个未知的数字,例如:
2.1000000000000001 -2.2000000000000002
1.5000000000000000 -3.2999999999999998
2.7999999999999998 1.2882297539194267E-231
2.5000000000000000 0.0000000000000000
2.1000000000000001 2.7999999999999998
3.2000000000000002 2.5000000000000000
-4.2999999999999998 2.1000000000000001
7.9000000000000004 3.2000000000000002
如果我用 ifort 编译,那么我得到的输出有两个零,这是错误的。
2.10000000000000 -2.20000000000000
1.50000000000000 -3.30000000000000
2.80000000000000 0.000000000000000E+000
2.50000000000000 0.000000000000000E+000
2.10000000000000 2.80000000000000
3.20000000000000 2.50000000000000
-4.30000000000000 2.10000000000000
7.90000000000000 3.20000000000000
我必须强调,大多数时候在 ifort 上编译会得到正确的结果,就像在 gfortran 的情况下一样。我在 Mac OS High Sierra 上。 提前致谢。
【问题讨论】:
-
请包含文件
2.txt(而不是图像)。 “但如果只是代码几次”是什么意思。您正在使用哪些编译标志(和编译器版本)。闻起来有点像未初始化的变量。 -
“但如果只是代码多次”:抱歉打错了。应该是“但如果只是多次运行代码”。
-
@albert 我没有使用任何编译标志。对于 gfortran,我使用的是 6.3.0 版本,对于 ifort,我使用的是 18.0.2。
-
编译时示例立即崩溃。最好是首先开始检查输入是否是您期望的。语句
read(999,*) x1(i,j)读取完整的行并仅存储该行的第一个元素。不需要read(999,*)行。也许qhestep应该被初始化,但也要检查你使用的索引。 -
也许全局
qhestep而不是本地qhestep或本地保存qhestep可能会做一些好事。
标签: fortran gfortran intel-fortran fortran95