program main !======================================================================= ! Chapter 9, code 5: particle dynamics with fixed-step velocity Verlet. ! One particle moves in the gravitational field of a fixed central mass. ! The orbit is in the x,y plane; the central mass is at the origin. ! Acceleration: a = -mu*r/|r|^3, where mu = G*M. ! ! The default circular orbit uses scaled units: mu = 1, radius = 1. ! Its speed is 1 and its period is 2*pi. ! The same equations describe a satellite or a planet when appropriate ! units, mu, and initial conditions are supplied. ! ! To use another position-dependent force: ! 1. Edit parameters and initial data below. ! 2. Edit acceleration; disable or update the analytic comparison. ! Standard velocity Verlet here assumes acceleration depends on position, ! not velocity. Velocity-dependent drag requires another treatment. !======================================================================= implicit none integer, parameter :: data_unit = 20 double precision mu, t0, t_end, dt double precision r0(2), v0(2), rf(2), vf(2), rexact(2), vexact(2) double precision radius, omega, angle, tau, circular_speed integer steps, j logical check_exact character(len=10) names(2) external acceleration !-------------------- Parameters and initial data: edit here ------------- mu = 1.0d0 ! Gravitational parameter G*M t0 = 0.0d0 dt = 0.01d0 ! Fixed step size steps = 6283 ! About ten circular-orbit periods r0(1) = 1.0d0 ! Initial x r0(2) = 0.0d0 ! Initial y v0(1) = 0.0d0 ! Initial vx v0(2) = 1.0d0 ! Initial vy check_exact = .true. ! Check only a circular orbit starting on +x !------------------------------------------------------------------------ if (mu <= 0.0d0 .or. dt <= 0.0d0 .or. steps < 1) & stop 'Use positive mu and dt, and at least one step.' ! For a fixed step, choose the number of steps; the final time follows. t_end = t0+dble(steps)*dt if (check_exact) then if (r0(1) <= 0.0d0 .or. r0(2) /= 0.0d0 .or. v0(1) /= 0.0d0) & stop 'Disable check_exact for different initial conditions.' circular_speed = sqrt(mu/r0(1)) if (abs(abs(v0(2))-circular_speed) > 1.0d-12*circular_speed) & stop 'Disable check_exact for a noncircular orbit.' end if open(unit=data_unit, file='ch09_velocity_verlet_orbit.dat', & status='replace', action='write') write(*,'(a)') 'Fixed-step velocity Verlet: central gravitational field' write(*,'(a,es12.4)') 'Fixed step: ', dt write(*,'(a,f12.6)') 'Final time: ', t_end call velocity_verlet(acceleration,t0,r0,v0,dt,steps,mu,data_unit,rf,vf) close(data_unit) write(*,'(a,i8)') 'Steps: ', steps if (check_exact) then ! Exact circular orbit for these initial conditions; either direction. radius = r0(1) omega = v0(2)/radius tau = t_end-t0 angle = omega*tau rexact(1) = radius*cos(angle) rexact(2) = radius*sin(angle) vexact(1) = -radius*omega*sin(angle) vexact(2) = radius*omega*cos(angle) names(1) = 'x' names(2) = 'y' write(*,'(a10,2(1x,a18),1x,a12)') 'variable','numerical','exact','abs error' do j = 1,2 write(*,'(a10,2(1x,es18.10),1x,es12.4)') & names(j),rf(j),rexact(j),abs(rf(j)-rexact(j)) end do names(1) = 'vx' names(2) = 'vy' do j = 1,2 write(*,'(a10,2(1x,es18.10),1x,es12.4)') & names(j),vf(j),vexact(j),abs(vf(j)-vexact(j)) end do end if write(*,'(a)') 'Data file: ch09_velocity_verlet_orbit.dat' end program main subroutine acceleration(r,a,mu) !======================================================================= ! Input: position r(1:2), gravitational parameter mu = G*M. ! Output: the two acceleration components a(1:2). ! Change this subroutine for another position-dependent force. !======================================================================= implicit none double precision r(2), a(2), mu, distance distance = sqrt(r(1)**2+r(2)**2) if (distance == 0.0d0) stop 'Gravity is undefined at the origin.' a(1) = -mu*r(1)/distance**3 a(2) = -mu*r(2)/distance**3 end subroutine acceleration subroutine velocity_verlet(fcn,t0,r0,v0,dt,steps,mu,data_unit,rf,vf) !======================================================================= ! Integrate two-dimensional motion with FIXED-step velocity Verlet. ! Each step: update position, evaluate new acceleration, update velocity. ! Both position components must be updated before evaluating acceleration. ! Initial and all subsequent points are printed and saved. !======================================================================= implicit none integer steps, data_unit, i, j double precision t0, r0(2), v0(2), dt, mu, rf(2), vf(2) double precision t, r(2), v(2), a(2), anew(2) external fcn r = r0 v = v0 t = t0 call fcn(r,a,mu) write(*,'(a10,4(1x,a18))') 't','x','y','vx','vy' write(data_unit,'(a)') '# t x y vx vy' write(*,'(f10.6,4(1x,es18.10))') t,r,v write(data_unit,'(5(1x,es24.16))') t,r,v do i = 1,steps ! Position uses the current velocity and acceleration. do j = 1,2 r(j) = r(j)+dt*v(j)+0.5d0*dt**2*a(j) end do ! Evaluate acceleration at the new position. call fcn(r,anew,mu) ! Velocity uses the average of old and new accelerations. do j = 1,2 v(j) = v(j)+0.5d0*dt*(a(j)+anew(j)) end do a = anew t = t0+dble(i)*dt ! Indexed times avoid repeated-addition drift. write(*,'(f10.6,4(1x,es18.10))') t,r,v write(data_unit,'(5(1x,es24.16))') t,r,v end do rf = r vf = v end subroutine velocity_verlet