0

我正在为大学的一门课程开发一个小程序,我几乎完成了,但不知何故,它不能像我希望的那样工作。

现在,输出文件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,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

!===========================================
4

1 回答 1

0

不应该(rho(j)-rho1)吗?你填rho(1:nel),但只用rho(1:7)

顺便说一句,小心你的变量初始化......你将reals 分配给integers,并进行混合类型算术。请注意这一点,因为它可能会导致意想不到的结果。使用您的编译器来检测这些问题。

于 2013-10-06T16:52:48.420 回答