我有以下问题.我想在Fortran90中编写一个程序,我希望能够像这样调用:
./program.x < main.in > main.out
Run Code Online (Sandbox Code Playgroud)
除了"main.out"(我可以在调用程序时设置其名称),还必须编写辅助输出,我希望它们具有与"main.in"或"main.out"类似的名称(它们是实际上并没有被称为"主要"); 但是,当我使用时:
INQUIRE(UNIT=5,NAME=sInputName)
Run Code Online (Sandbox Code Playgroud)
sInputName的内容变为"Stdin"而不是文件名.有没有办法获取程序调用时链接到stdin/stdout的文件名?
如果我分配一个F90指针会发生什么:
real, pointer :: abc(:)
allocate abc (nx*ny*nz)
Run Code Online (Sandbox Code Playgroud)
我将abc传递给子程序,在那里我将其重新定义为
real arg1(nx,ny,xz)
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常.
但如果我重新定义为2D数组,我会得到一个段错误.
real arg1(nx,ny)
Run Code Online (Sandbox Code Playgroud)
使用上面重新排序的数组,它应该工作.为什么会失败?任何帮助将不胜感激.
谢谢.
这是试图解决3*3的线性方程并打印出结果,但它在注释行中出现问题:
我在程序之外定义了LinearSolution模块,我应该在里面定义它吗?有什么不同?
为什么它说这个语句是递归的,你知道,当我使用这些语句作为普通的子程序而不是模块子程序时,它们被证实是好的.
module LinearSolution
type LAE
integer::N
double precision,dimension(:,:),allocatable::A
double precision,dimension( :),allocatable::B
contains
procedure,nopass::RowReduction
end type LAE
contains
subroutine RowReduction
double precision::C
do k=1,N
do i=k+1,N
if(A(k,k)/=0) then
C=A(i,k)/A(k,k)
B(i)=B(i)-B(k)*C !error: Statement Function is recursive
do j=k+1,N
A(i,j)=A(i,j)-A(k,j)*C !error: Statement Function is recursive
end do
end if
end do
end do
do k=N,1,-1
do i=k-1,1,-1
if(A(k,k)/=0) then
C=A(i,k)/A(k,k)
B(i)=B(i)-B(k)*C !error: Statement Function is recursive
end if
end do
end do
do k=1,N
if(A(k,k)/=0) then
B(k)=B(k)/A(k,k) !error: Statement Function is …Run Code Online (Sandbox Code Playgroud) 我花了几个小时在互联网上寻找解决这个问题的方法,却找不到任何东西.我一直在尝试将未格式化的输出写入CSV输出文件,该文件具有多个不同长度和多种数据类型的非常长的行.我正在尝试首先编写一个长标题,指示将在下面写入的变量,用逗号分隔.然后在下面的行上,我正在写标题中指定的值.但是,通过顺序访问,长输出行被分成多个较短的行,这不是我所希望的.我尝试在open语句中使用recl控制行长度,但是在输出后只添加了一堆乱码文本和符号,但仍然出现相同的问题.我也试过使用直接访问,但行的长度不一样,所以也不行.我已经阅读过在Fortran2003中使用流i/o,但我使用的是Fortran90,所以这也不行.我正在使用Fortran 90和使用FTN95编译器的Plato IDE.我在下面添加了一个类似于我想要做的示例程序,使用数组和一些虚拟文本,我在下面包含了说明问题的输出.任何人都知道每个写声明我只能一行吗?任何帮助将不胜感激.任何人都知道每个写声明我只能一行吗?任何帮助将不胜感激.任何人都知道每个写声明我只能一行吗?任何帮助将不胜感激.
module types
integer, parameter :: dp=selected_real_kind(15)
end module types
program blah
use types
use inputoutput
implicit none
integer :: i
character(50)::fileNm
integer :: unitout2=20
real(dp), dimension(100) :: bigArray
fileNm='predictout2.csv'
open(unit=unitout2,file=fileNm,status="replace")
do i=1,100
bigArray(i)=i
end do
write(unitout2,*)"word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,&
&word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,&
&word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word"
write(unitout2,*)bigArray
close(unitout2)
end program
Run Code Online (Sandbox Code Playgroud)
这是上面程序的输出(没有recl):
word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word
,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,wo
rd,word,word,word,word,word
1.00000000000 2.00000000000 3.00000000000 4.00000000000
5.00000000000 6.00000000000 7.00000000000 8.00000000000
9.00000000000 10.0000000000 11.0000000000 12.0000000000
13.0000000000 14.0000000000 15.0000000000 16.0000000000
17.0000000000 18.0000000000 19.0000000000 20.0000000000
21.0000000000 22.0000000000 23.0000000000 24.0000000000
25.0000000000 26.0000000000 27.0000000000 28.0000000000
29.0000000000 30.0000000000 31.0000000000 …Run Code Online (Sandbox Code Playgroud) 我不知道这个自由形式的Fortran程序有什么问题.它无法正确处理其命令行参数.
如果我使用静态数组作为命令行参数而不是数组,它可以工作allocatable.
此外,这是一个很好的第一个Fortran程序吗?这是Fortran有用的问题类型吗?我已经知道C,C++和一点点D.
module fibonacci
use ISO_FORTRAN_ENV
implicit none
contains
subroutine output_fibonacci(ordinal)
! Declare variables
integer, parameter :: LongInt = selected_int_kind (38)
integer, intent(in) :: ordinal
integer :: count
! integer (kind=LongInt) :: count, compare=2
integer (kind=LongInt), dimension(2,2) :: matrix, initial
matrix=reshape((/ 1, 1, 1, 0 /), shape(matrix))
initial=reshape((/ 1, 0, 0, 1 /), shape(initial))
count = ordinal
! Do actual computations
do while (count > 0)
! If the exponent is odd, then the output matrix
! …Run Code Online (Sandbox Code Playgroud) 例如,我想以 2 为增量从 1 循环到 500。但是,对于每 8 个循环,我想跳过接下来的 18 个循环(使 do 变量增加 18)。我怎么做?
我的代码是:
event = 0
do i = 1,500,2
event = event + 1
if (event .eq. 8) then
i = i + 18
event = 0
endif
enddo
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误消息:“DO 主体中的 do 变量不应出现在变量定义上下文中”。基本上我不能改变循环中的变量“i”。那么我应该如何编写代码来实现它呢?
谢谢。
我想在Fortran 90中使用系统命令来执行以下命令:
command = awk '{print "C"NR,$1,$2,$3}' filename1 > filename2
call system(trim(command))
Run Code Online (Sandbox Code Playgroud)
这里我的filename1和filename2是Fortran 90程序中的变量.但问题是任何字符都可以分配给一个包含在撇号之间的变量,而我的变量也应该由撇号组成.我不知道如何在Fortran 90中输入它.
以下是数据的编写方式(浮点数的2-D矩阵.我不确定大小).
open(unit=51,file='rmsd/'//nn_output,form='unformatted',access='direct',status='replace',&
recl=Npoints*sizeofreal)
!a bunch of code omitted
write(51,rec=idx-nstart+1) (real(dist(jdx)),jdx=1,Npoints)
Run Code Online (Sandbox Code Playgroud)
f = open(inputfilename,'rb')
field = np.fromfile(f,dtype='float64')
Run Code Online (Sandbox Code Playgroud)
但结果不正确.矩阵的对角线应为0(或非常接近),因为它是相似矩阵.我尝试了不同dtype的但我仍然无法得到正确的结果.
编辑:这是我得到的输出
array([ 1.17610188e+01, 2.45736970e+02, 7.79741823e+02, ...,
9.52930627e+05, 8.93743127e+05, 7.64186127e+05])
Run Code Online (Sandbox Code Playgroud)
我还没有为重塑而烦恼,因为值应该在0到20之间.
编辑2:这是指向文件前100行的链接:https: //drive.google.com/file/d/0B2Mz7CoRS5g5SmRxTUg5X19saGs/view?usp =sharing
编辑3:文件顶部的声明
integer,parameter :: real_kind=8
!valuables for MPI
integer :: comm, nproc, myid, ierr
integer,allocatable :: idneigh(:)
real :: tmp
real,allocatable :: traj(:,:)
real(real_kind),allocatable :: dist(:)
real(real_kind),allocatable :: xx(:,:),yy(:,:),rot(:),weight(:)
character(200) :: nn_traj, nn_output, nn_neigh
integer,parameter :: sizeofreal=4,sizeofinteger=4
Run Code Online (Sandbox Code Playgroud) 我想循环N次迭代,但是在特定条件下应该"跳过"一些迭代.我知道我可以使用goto语句来完成它,例如:
do i = 1, N
if condition(i) goto 14
! Execute my iteration if condition(i) is false
14 continue
end do
Run Code Online (Sandbox Code Playgroud)
但我有点害怕这些goto陈述,我想知道是否有另一种解决方案(我使用的是fortran 90,但对任何解决方案都感兴趣,即使它需要更新的版本).
使用gfortran --version== GNU Fortran(Homebrew GCC 8.2.0)8.2.0进行编译
当我编写以下测试代码时,我得到了这些函数MOD并MODULO给出了相同的结果。但是,据我从多个来源了解,MOD应该给出余数,并且MODULO应该给出整数商。
program test
implicit none
print *, mod(17,3)
print *, mod(17.5,5.5)
print *, mod(17.5d0,5.5)
print *, mod(17.5,5.5d0)
print *, modulo(17,3)
print *, modulo(17.5,5.5)
print *, modulo(17.5d0,5.5)
print *, modulo(17.5,5.5d0)
end program test
Run Code Online (Sandbox Code Playgroud)
打印输出:
2
1.00000000
1.0000000000000000
1.0000000000000000
2
1.00000000
1.0000000000000000
1.0000000000000000
结果是所有余数,MOD应该做些什么,但是MODULO在这种情况下不应该打印出模数,5然后再打印三个3以不同的精度吗?