我有一个程序,我想用初始条件的分布来实现它(x0 = 0.1,0.2,...,1;对于p0也是如此)。所以我想要一个10x10(或nxn)点的网格。我想我必须使用双DO
周期:
do x0=0.1,1.0,0.1
do p0=0.1,1.0,0.1
...
enddo
enddo
此外,我想在单个文件中读取所有不同的输出(所以我有一个n * 3数组,其中n是点数)。
Program Main
Implicit real*8 (A-H,O-Z)
common ome,eps
duepi=8*datan(1.d0)
ome=sqrt(2.d0)
T_per=duepi/ome
eps=0.81
N_step=100
x0=0.4
y0=0
px0=1.0
py0=0
x=x0
y=y0
px=px0
py=py0
dt=T_per/N_step
E0=H(x,y,px,py)
k_max=100*N_step
k=0
t=0
errh=0
c---------
c integration loop
c--------
do k=1,k_max
call sym4(x,y,px,py,dt)
E= H(x,y,px,py)
errh=abs(E-E0)
t=k*dt
if(mod(k,N_step).eq.0) then
xex=x0*cos(t)+px0*sin(t)
pxex=-x0*sin(t)+px0*cos(t)
yex=y0*cos(t)+py0*sin(t)
pyex=-y0*sin(t)+py0*cos(t)
err=sqrt((x-xex)**2+(p-pex)**2+(y-yex)**2+(py-pyex)**2)
endif
enddo
do k=1,k_max
call sym4(x,y,px,py,-dt)
E= H(x,y,px,py)
errh=abs(E-E0)
t=t-dt
if(mod(k,N_step).eq.0) then
xex=cos(t)+sin(t)
pex=-sin(t)+cos(t)
err=sqrt( (x-xex)**2+(p-pex)**2)
endif
enddo
c--------
c if i put the OPEN-WRITE command here i have the last value of the two do cycle but i
c whant to write EVERY last values for every different initial conditions WITHOUT
c OVER-WRITE the file.
c--------
OPEN(unit=11, file="prova0.txt")
write(11,'(I3,8g15.6)') k, x0, px0, errh
end
subroutine f(x,y,fx,fy)
Implicit real*8 (A-H,O-Z)
common ome,eps
fx=-x*(1+eps*cos(ome*y))
fy= eps* ome*sin(ome*y)*x*x/2.d0
return
end
real*8 function H(x,y,px,py)
Implicit real*8 (A-H,O-Z)
common ome,eps
h=px*px/2.d0+ py +(1+eps*cos(ome*y))*x*x/2
return
end
subroutine sym2(x,y,px,py,dt)
Implicit real*8 (A-H,O-Z)
call f(x,y,fx,fy)
xnew= x+ px*dt + fx*dt**2/2.d0
ynew= y+ dt
call f(xnew,ynew,fxnew,fynew)
pxnew= px+ dt*(fx+fxnew )/2.d0
pynew= py+ dt*(fy+fynew )/2.d0
x=xnew
y=ynew
px=pxnew
py=pynew
end
subroutine sym4(x,y,px,py,dt)
Implicit real*8 (A-H,O-Z)
sq2=2**(1.d0/3.d0)
alpha= 1.d0/(2-sq2)
beta= sq2/(2-sq2)
dt1= dt*alpha
dt2=-dt*beta
call sym2(x,y,px,py,dt1)
call sym2(x,y,px,py,dt2)
call sym2(x,y,px,py,dt1)
return
end
非常感谢你的帮助!
答案 0 :(得分:2)
也许是这样的:
Program Main
Implicit real*8 (A-H,O-Z)
common ome,eps
duepi=8*datan(1.d0)
ome=sqrt(2.d0)
T_per=duepi/ome
eps=0.81
N_step=100
c Open the file
OPEN(unit=11, file="prova0.txt")
c Nested do loops
do iX0=1,10
do iP0=1,10
c x0=0.4
c Calc value for x0
x0 = real(iX0)/10.
y0=0
c px0=1.0
c Calc value for px0
px0 = real(iP0)/10.
py0=0
x=x0
y=y0
px=px0
py=py0
dt=T_per/N_step
E0=H(x,y,px,py)
k_max=100*N_step
k=0
t=0
errh=0
c---------
c integration loop
c--------
do k=1,k_max
call sym4(x,y,px,py,dt)
E= H(x,y,px,py)
errh=abs(E-E0)
t=k*dt
if(mod(k,N_step).eq.0) then
xex=x0*cos(t)+px0*sin(t)
pxex=-x0*sin(t)+px0*cos(t)
yex=y0*cos(t)+py0*sin(t)
pyex=-y0*sin(t)+py0*cos(t)
err=sqrt((x-xex)**2+(p-pex)**2+(y-yex)**2+(py-pyex)**2)
endif
enddo
do k=1,k_max
call sym4(x,y,px,py,-dt)
E= H(x,y,px,py)
errh=abs(E-E0)
t=t-dt
if(mod(k,N_step).eq.0) then
xex=cos(t)+sin(t)
pex=-sin(t)+cos(t)
err=sqrt( (x-xex)**2+(p-pex)**2)
endif
enddo
c--------
c Write results
write(11,'(I3,8g15.6)') k, x0, px0, errh
enddo ! iP0
enddo ! iX0
c Close file
close(11)
end
c ... omitted remaining program
请注意,您的代码还有其他问题:
p
永远不会在err=sqrt((x-xex)**2+(p-pex)**2+(y-yex)**2+(py-pyex)**2)
和
err=sqrt( (x-xex)**2+(p-pex)**2)
k
可能与10000
一样高,但您尝试用三位数表示它!尝试使用write(11,*) k, x0, px0, errh
pxex
< - > pex
。在您的问题中,您要求p0
实际上不会出现在您的代码中。也许最好先使用implicit none
并手动指定变量... BTW:缩进 帮助; - )