我正在尝试创建一个 Fortan 程序,当编译为可执行文件时,可以将文件作为参数(例如 progexe afile)。
我需要以这样的方式编写程序,以便传递的文件名 (afile) 在 Fortran 程序中可用。我不知道该怎么做。我在 Windows 上的 MingW 中使用 gfortran。
过去两天我在一个大型 Fortran 项目中调试了一个看似无意义的段错误。当我将代码移到自己的计算机上时,问题就开始了,段错误出现在代码的一部分中,而该部分代码多年来在其他几个系统上都运行良好。我最终找到了段错误的根源,但它是如此令人惊讶的意外(并且依赖于编译器),所以我决定将其发布在这里。
考虑以下 MWE:
program dafuq
implicit none
integer :: a=1
integer, parameter :: b=2
call foo(a,b)
end program dafuq
subroutine foo(a,b)
implicit none
integer, intent(inout) :: a, b
a=b !OK
b=a !causes segfault
end subroutine foo
Run Code Online (Sandbox Code Playgroud)
我可以访问两个 HPC 集群,它们与我的笔记本电脑一起允许我检查这些(有时有点旧)编译器:
事实证明,所有四个编译器都会对上述代码产生段错误,因为变量b被声明为parameter. 因此,在子例程中更改其值是违规的。intent我的问题是,只有最新的 gfortran 在编译期间显示警告(即使使用 -Wall),如果我省略子例程中的规范,该警告也会消失。我怀疑在 C++ 中使用const变量的相同设置会引发一个巨大的危险信号。
现在,为了使其更加晦涩,请考虑以下代码,其中使用数组而不是标量:
program dafuq_array
implicit none
integer :: a(2)=(/1,1/)
integer, parameter :: b(2)=(/2,2/)
call foo(a,b)
end …Run Code Online (Sandbox Code Playgroud) 我在这部分代码中遇到浮点溢出错误。有哪位大佬能帮我看看是什么原因吗?
do j=1,ny-1
do i=1,nx-1
sum = 0.0d0
do k=0,1000
n=2.0d0*dfloat(k)+ 1.0d0
sum = sum + ((dsinh(n*pi*x(i))*dcos(n*pi*y(j)))/((n*n*pi*pi)*dsinh(2*n*pi)))
end do
ue(i,j)= (x(i)/(4.0d0))- 4.0d0*sum
end do
end do
Run Code Online (Sandbox Code Playgroud) 是否有可能在 Fortran 中实现非常小的数字1E-1200?我知道如何用 python 来做,但是我的硕士论文代码运行得太慢了。我的主管推荐我使用 Fortran,但我不确定这是否是一个问题。
我想在 fortran90 中填充一个未知大小的数组。这是 MATLAB 中的等效代码:
for i=1:10
A[i] = i
end
Run Code Online (Sandbox Code Playgroud)
我知道我可以传递大小,但是如何在 fortran90 中执行此操作而不传递数组的大小。我读到我们可以使用指针,但我真的不知道如何处理指针
我正在使用某人的 Fortran 脚本,我对此进行了一些更改。现在我在执行它时遇到语法错误。我是 Fortran 的新手,所以无法弄清楚。
代码如下:
integer i_canorg
integer i
integer n
integer canorg(n)
real r1demdoms(n)
real r1supdoms(n)
real r1supdomcan(n)
real r1rivout(n)
real r1envflw(n)
if(canorg(i).ne.0)then
i0l_canorg=canorg(i)
call calc_supcan1(i_canorg,r1demdoms(i),r1supdoms(i),r1rivout(i_canorg),r1envflw(i_canorg),r1supdomcan(i))
end if
Run Code Online (Sandbox Code Playgroud)
代码很长,我另外加了if命令和i_canorg参数。
运行代码时出现以下错误:
348 | call calc_supcan1(i0lcanorg,r1demdoms(i),r1supdoms(i),r1rivout(i_canorg),r1envflw(i_canorg),r1supdomcan(i))
| 1
Error: Syntax error in argument list at (1)
Run Code Online (Sandbox Code Playgroud)
此语法错误背后的原因是什么?
我的矩阵计算是:C=CA*B
这里 C 是一个对称矩阵,所以我想通过只考虑上三角形然后取相反的 elelement 来加速这个计算。我使用了 OMP,发现我的实现比整个矩阵 C 的正常计算慢。
我还看到 C=C-AxB 的计算比 C=C+AxB 慢。
附上我的程序。请建议我!
Program testspeed
implicit none
integer nstate,nmeas,i,j,l
integer(kind=8) :: tclock1, tclock2, clock_rate
real(kind=8) :: elapsed_time
double precision, allocatable, dimension(:,:):: B,C,A
nstate =20000
nmeas=10000
allocate (B(nmeas,nstate),C(nstate,nstate),A(nstate,nmeas))
A=1d0
B=1d0
call system_clock(tclock1)
write(*,*) "1"
!$omp parallel do
do j = 1, nstate
do l = 1,nmeas
do i = 1, j
C(j,i) = C(j,i) - A(j,l)*B(l,i)
C(i,j)=C(j,i)
end do
end do
end do
!$omp end parallel do
write(*,*) "2" …Run Code Online (Sandbox Code Playgroud) fortran transpose openmp matrix-multiplication intel-fortran
我正在查看旧扫描论文中的一些 Fortran 代码。扫描质量不太好,所以我可能复印错了。我尝试使用在线 Fortran 编译器运行它,但它失败了。由于不熟悉 Fortran,我想知道是否有人可以指出语法没有意义的地方?该代码来自一篇关于沉积物动力学的论文:
Komar, PD 和 Miller, MC,1975。关于波浪和单向流下沉积物运动阈值的比较以及阈值实际评估的讨论:答复。沉积研究杂志,45(1)。
PROGRAM TSHOLD
REAL LI, LO
G = 981.0
PIE = 3.1416
RHOW = 1.00
READ (6O,1) DIAM, RHOS
1 FORMAT (2X, F6.3,2X, F5.3)
IF(DIAM .LT. 0.05) GO TO 5
A = 0.463 * PIE
B = 0.25
GO TO 7
5 A = 0.21
B = 0.50
7 PWR = 1.0 / (2.0 - B)
FAC = (A * (RHOS - RHOW) * G/(RHOW * PIE**B))**PWR
FAC1 = FAC …Run Code Online (Sandbox Code Playgroud) 我有以下行来删除 Fortran 中的高维数组:
real(8) X(48,20,7,3), Y(48,15,5)
Run Code Online (Sandbox Code Playgroud)
我的问题是我们如何解释 n 维数组?我想知道下标的含义。另外,如果可能的话,如果您能提供等效的代码来在 Matlab 中创建相同的 4D 和 3D 数组,我将不胜感激。
我曾尝试四处寻找这种解释,但只找到了一些例子,提到可以在不解释下标含义的情况下做到这一点。我非常感谢你的帮助。
如何使用 Fortran 创建例程和子程序之间的链接?
Program Example
common a,b,c,d,e
print*,"Enter a"
read*,a
print*,"Enter coefficient of x in f1(x)"
read*,b
print*,"Enter intercept in f1(x)"
read*,c
print*,"Enter coefficient of x in f2(x)"
read*,d
print*,"Enter intercept in f2(x)"
read*,e
Print*,f1(2.),f2(3.)
pause
end
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function f1(x)
common a,b,c
f1=a*x**2+b*x+c
return
end
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function f2(y)
common d,e
f2=d*y+e
return
end
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
这是一个例子。当我打印 f1(2.) 和 f2(3.) 时,第一个结果为 true,第二个结果为 false。