Fortran90错误输出

时间:2013-10-06 16:28:05

标签: fortran fortran90

我正在为一所大学的课程开设一个小程序而且我差不多完成了但不管怎么说它不起作用,因为我想让它起作用。

现在,输出文件gravity1.dat应该给我不等于0的值。但它没有...在最后一个公式中我计算g(冲浪)的某个地方,其中一个变量是0.如果尝试了几乎所有的东西我有权纠正它,但我似乎无法解决它。

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_two_columns (nnn,xxx,yyy,filename)
implicit none
integer i,nnn
real(8) xxx(nnn),yyy(nnn)
character(LEN=*) filename

open(unit=123,file=filename,action='write')
do i=1,nnn
write(123,*) xxx(i),yyy(i)
end do
close(123)
end subroutine

和另一个子程序:

!===========================================

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

!===========================================

1 个答案:

答案 0 :(得分:0)

不应该是(rho(j)-rho1)吗?您填写rho(1:nel),但仅使用rho(1:7)

顺便说一下,要小心你的变量初始化...你将real分配给integer s,并做混合类型的算术。小心这一点,因为它可能会导致意想不到的结果。使用编译器检测这些问题。