Fortran90错误输出
我正在为大学里的一门课程做一个小程序,我几乎完成了,但不知怎么的,它并没有像我希望的那样工作 现在,输出文件gravity1.dat应该给我不等于0的值。但它不。。。在我计算g(surf)的最后一个公式中,其中一个变量是0。如果我尝试了我力所能及的一切来纠正它,但我似乎无法修复它Fortran90错误输出,fortran,fortran90,Fortran,Fortran90,我正在为大学里的一门课程做一个小程序,我几乎完成了,但不知怎么的,它并没有像我希望的那样工作 现在,输出文件gravity1.dat应该给我不等于0的值。但它不。。。在我计算g(surf)的最后一个公式中,其中一个变量是0。如果我尝试了我力所能及的一切来纠正它,但我似乎无法修复它 program gravity implicit none real(8) Lx,Ly,sx,sy,xsphere,ysphere,r,A,rho1,rho2,dx,G integer np,nel,nelx,nel
program gravity
implicit none
real(8) Lx,Ly,sx,sy,xsphere,ysphere,r,A,rho1,rho2,dx,G
integer np,nel,nelx,nely,i,nnx,nny,j,counter,nsurf
real(8),dimension(:),allocatable :: xcgrid
real(8),dimension(:),allocatable :: ycgrid
real(8),dimension(:),allocatable :: xgrid
real(8),dimension(:),allocatable :: ygrid
real(8),dimension(:),allocatable :: rho
real(8),dimension(:),allocatable :: xsurf, gsurf
real(8),dimension(:),allocatable :: ysurf
nnx=11.
nny=11.
Lx=10.
Ly=10.
nelx=nnx-1.
nely=nny-1.
nel=nelx*nely
np=nnx*nny
sx=Lx/nelx
sy=Ly/nely
xsphere=5.
ysphere=5.
r=3.
nsurf=7 !number of gravimeters
dx=Lx/(nsurf-1.)
!==========================================================
allocate(xgrid(np))
allocate(ygrid(np))
counter=0
do i=1,nnx
do j=1,nny
counter=counter+1
xgrid(counter)=dble(i-1)*sx
ygrid(counter)=dble(j-1)*sy
end do
end do
call write_two_columns(np,xgrid,ygrid,'grid_init1.dat')
!==========================================================
allocate(xcgrid(np))
allocate(ycgrid(np))
counter=0
do i=1,nnx-1
do j=1,nny-1
counter=counter+1
xcgrid(counter)=dble(i-1)*sx+0.5*sx
ycgrid(counter)=dble(j-1)*sy+0.5*sy
end do
end do
call write_two_columns(np,xcgrid,ycgrid,'gridc_init1.dat')
!==========================================================
allocate(rho(nel))
rho1=3000. !kg/m^3
rho2=3200. !kg/m^3
do i=1,nel
if (sqrt((xsphere-xcgrid(i))**2)+((ysphere-ycgrid(i))**2)<r) then
rho(i)=3200.
else
rho(i)=3000.
end if
end do
call write_three_columns(nel,xcgrid,ycgrid,rho,'inclusion1.dat')
!==========================================================
allocate(xsurf(nsurf))
allocate(ysurf(nsurf))
do i=1,nsurf
xsurf(i)=(i-1)*dx
ysurf(i)=ly
end do
call write_two_columns(nsurf,xsurf,ysurf,'surf_init1.dat')
!==========================================================
allocate(gsurf(nsurf))
G=0.000000000066738480 !m^3 kg^-1 s^-2
do i=1,nsurf
do j=1,nel
gsurf(i)=gsurf(i)+(-2.*G*(((rho(i)-rho1)*(ycgrid(counter)-ysurf(i)))/((xcgrid(counter)-xsurf(i))**2.+(ycgrid(counter)-ysurf(i))**2.))*sx*sy)
end do
end do
call write_two_columns(nsurf,xsurf,gsurf,'gravity1.dat')
deallocate(xgrid)
deallocate(ygrid)
deallocate(xcgrid)
deallocate(ycgrid)
deallocate(xsurf)
deallocate(ysurf)
deallocate(gsurf)
end program
和另一个子例程:
!===========================================
subroutine write_three_columns (nnn,xxx,yyy,zzz,filename)
implicit none
integer i,nnn
real(8) xxx(nnn),yyy(nnn),zzz(nnn)
character(LEN=*) filename
open(unit=123,file=filename,action='write')
do i=1,nnn
write(123,*) xxx(i),yyy(i),zzz(i)
end do
close(123)
end subroutine
!===========================================
它不应该是(rho(j)-rho1)
?您可以填写rho(1:nel)
,但只能使用rho(1:7)
顺便说一下,要小心变量初始化。。。将
real
s赋值给integer
s,并执行混合型算术。注意这一点,因为它可能会导致意外的结果。使用编译器来检测这些问题 我没有时间调试你的程序。但是如果你只是在学习,试着学习一些基本的Fortran习惯。使用缩进。你在某些地方使用它,但还不够。它大大提高了可读性。将子程序放在模块中。它将启用一些错误检查。然后,还可以使用假定形状数组(:,:)
,并且不需要传递数组的大小。不要使用real(8),它不便于携带,是一个“幻数”。使用kind(1.d0)
,选择的\u real\u kind
或rea64
。了解编译器的错误检查选项。您不必在Fortran95中取消分配可分配项。请注意,您的gsurf计算不使用j,而是使用计数器。这肯定会导致意外的行为。
!===========================================
subroutine write_three_columns (nnn,xxx,yyy,zzz,filename)
implicit none
integer i,nnn
real(8) xxx(nnn),yyy(nnn),zzz(nnn)
character(LEN=*) filename
open(unit=123,file=filename,action='write')
do i=1,nnn
write(123,*) xxx(i),yyy(i),zzz(i)
end do
close(123)
end subroutine
!===========================================