【发布时间】:2018-08-24 09:45:04
【问题描述】:
我已经写了Fortran代码来计算距离然后排序,但是调用可执行命令时出现了一些问题。
这里是代码
program sort
implicit none
character CN*8,O*7
integer j,iconf,nconf
integer i,m
integer n,nmax,num
parameter (n=5)
double precision xbox,rq
parameter (nmax=3091,nconf=1)
double precision atom(nmax),id(nmax),ox(nmax),oy(nmax),oz(nmax)
double precision xij,yij,zij,rij,t
double precision r(n,n)
open(unit=1,status='unknown',file='a.gro')
do iconf= 1,nconf
read(1,*)
read(1,*)
do i=1,n
read(1,'(A8,A7,1i5,3f8.3)')CN,O,num,ox(i),oy(i),oz(i)
enddo
read(1,*)xbox
open(unit=3,file='dist.txt')
do i=1,n
do j=1,n
if(i .ne. j) then
xij=ox(i)-ox(j)
yij=oy(i)-oy(j)
zij=oz(i)-oz(j)
xij=xij - nint(xij/xbox)*xbox
yij=yij - nint(yij/xbox)*xbox
zij=zij - nint(zij/xbox)*xbox
r(i,j)=dsqrt(xij**2 + yij**2 + zij**2)
write(3,'(i3,2x,i3,4x,f17.15)') i,j, r(i,j)
call execute_command_line(sort -t, -k1 -g r(i,j))
write(*,*)
endif
enddo
enddo
enddo
END program
输入文件是a.gro
Generated by trjconv : 360 water t= 1000.00000
216
1water OW1 1 0.764 0.617 0.582
2water OW1 2 0.865 1.469 1.696
3water OW1 3 0.423 1.400 1.324
4water OW1 4 0.381 1.464 0.392
5water OW1 5 1.279 0.872 0.131
1.87759 1.87759 1.87759
输出文件 3,dist.txt
1 2 1.148553302245917
1 3 1.131341681367747
1 4 0.948787647474397
1 5 0.730514202462895
2 1 1.148553302245917
2 3 0.581815262776768
2 4 0.750524142249935
2 5 0.790896648178509
3 1 1.131341681367747
3 2 0.581815262776768
3 4 0.935138492417032
3 5 1.216627908647504
4 1 0.948787647474397
4 2 0.750524142249935
4 3 0.935138492417032
4 5 1.106792211754311
5 1 0.730514202462895
5 2 0.790896648178509
5 3 1.216627908647504
5 4 1.106792211754311
所以,我想对r(i,j) 进行排序,保持 i 相同 j 不同。但是调用行在 fortran 代码中不起作用。
即将发生的错误
tetra.f(48): error #6413: This global name is invalid in this context. [SORT]
call execute_command_line(sort -t, -k1 -g r(i,j))
----------------------------------^
tetra.f(48): error #6404: This name does not have a type, and must have an explicit type. [K1]
call execute_command_line(sort -t, -k1 -g r(i,j))
--------------------------------------------^
tetra.f(48): error #6404: This name does not have a type, and must have an explicit type. [GR]
call execute_command_line(sort -t, -k1 -g r(i,j))
------------------------------------------------^
tetra.f(48): error #6362: The data types of the argument(s) are invalid. [EXECUTE_COMMAND_LINE]
call execute_command_line(sort -t, -k1 -g r(i,j))
---------------------------------------^
tetra.f(48): error #6362: The data types of the argument(s) are invalid. [EXECUTE_COMMAND_LINE]
call execute_command_line(sort -t, -k1 -g r(i,j))
-----------------------------------------------^
compilation aborted for tetra.f (code 1)
请告诉我如何在 Fortran 代码中执行 shell 命令。
【问题讨论】:
-
execute_command_line的参数必须是字符串。但我实际上不明白,你想做什么。使用 bash 对 Fortran 数组进行排序?这是一个非常糟糕的主意。 -
是的....我正在尝试使用 bash 排序为 fortran 数组。我想排序保持 i 相同 j 不同。但是这种排序我做不到。
-
不要尝试这样做。
-
@claudiasmith 您可以查看 William Press 等人的 Fortran 数字食谱一书。在这本书中仍然可以找到最好的排序例程,而且这本书写得很好。本书还附带一张CD,其中包含这些算法的所有源代码。大多数图书馆应该让您免费访问本书和源代码。正如 Vladimir F 所说,您不需要为 Fortran 中的此类琐碎任务调用外部应用程序。否则,一旦问题规模失控或平台发生变化,您就会遇到麻烦。
-
我刚刚注意到,数字配方的作者已经在网上免费提供 Fortran 和 C 版本的书籍here。如果我几年前没记错的话,源代码也可以在同一个网站上免费获得,但我现在在任何地方都找不到。
标签: linux shell fortran system-calls intel-fortran