Maybe like this:
- Open the file before the loops
- Do the nested loops and write at the end of the loop
- Close the file
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
Please note that there are additional issues with your code:
p
is never initialized in
err=sqrt((x-xex)**2+(p-pex)**2+(y-yex)**2+(py-pyex)**2)
and
err=sqrt( (x-xex)**2+(p-pex)**2)
- The format of your write statement is insufficient:
k
might be as high as10000
but you try to represent it with three digits! Try using
write(11,*) k, x0, px0, errh
- There seem to be quite a number of similar variables like
pxex
<->pex
. In your question you ask forp0
which actually does not appear in your code. Maybe it would be a good idea to start by usingimplicit none
and manually specifying your variables... BTW: indentation does help ;-)