Information Technology Reference
In-Depth Information
integer iunit
real * 8v
...
iunit = 22
open(iunit, name='sol.dat', status='unknown')
write(iunit, * ) 'some text and variables', v, ' and text'
...
close(iunit)
Note that a file is referenced by an integer, iunit here. Our dump subroutine
assumes that a file has already been opened, such that we can pass iunit as an
input argument and work with it inside the subroutine:
subroutine dump (iunit, u, n, t0, dt)
integer n, iunit
real * 8 u(0:n), t0, dt
integer i
real * 8 time
time = t0
do i = 0, n
write(iunit, * ) time, u(i)
time = time + dt
end do
return
end
The main program is listed next and commented upon afterward:
program ode
integer nmax, n, i, iunit
parameter (nmax=1000)
real * 8 dt, tstart, tstop, u0, u(0:nmax)
external f1
tstart = 0.0
write( * , * ) 'stop time:'
read( * , * ) tstop
write( * , * ) 'time step:'
read( * , * )dt
C
set a reasonable time step if dt<0:
if (dt .lt. 0.0) then
dt = 0.004
end if
C
check that the u array is sufficiently large:
n = tstop/dt
if (n .gt. nmax) then
write( * , * ) 'ERROR: too small time step'
end if
C
time integration:
u0 = 1.0
call heun (f1, u0, dt, n, u)
write( * , * ) 'end value =', u(n), ' error = ', u(n)-exp(-dt * n)
C
write solution to file (for plotting):
iunit = 21
open(iunit, name='sol.dat', status='unknown')
call dump (iunit, u, n, tstart, dt)
close (iunit)
end
Search WWH ::




Custom Search