Fortran Mini Project
In this project you'll build a small projectile simulator, one step at a time: a ball launched from the ground, flying through the air with gravity and air resistance. It's a miniature version of the codes Fortran was invented for, and it brings together most of the course: kinds, modules, derived types, type-bound procedures, functions passed as arguments, array intrinsics and formatted tables.
By the end the program will:
- integrate the equations of motion with a fourth-order Runge–Kutta method;
- print a trajectory table;
- detect the landing point precisely;
- search for the launch angle that gives the longest range with air resistance.
Every step is a complete program that you can run and modify.
Step 1: The physics without drag
Without air resistance, the motion has an exact solution, which we'll use later to test the simulator. A ball launched at speed v₀ and angle θ has flight time T = 2v₀ sin θ / g, range R = v₀² sin 2θ / g, and maximum height H = (v₀ sin θ)² / (2g).
We start with a module for the shared kind and constants:
module physics
use iso_fortran_env, only: dp => real64
implicit none
real(dp), parameter :: g = 9.81_dp ! m/s^2
real(dp), parameter :: pi = 4.0_dp * atan(1.0_dp)
contains
pure function deg2rad(deg) result(rad)
real(dp), intent(in) :: deg
real(dp) :: rad
rad = deg * pi / 180.0_dp
end function deg2rad
end module physics
program exact_solution
use physics
implicit none
real(dp) :: v0, theta
v0 = 30.0_dp
theta = deg2rad(40.0_dp)
print '(a, f8.3, a)', 'flight time: ', 2 * v0 * sin(theta) / g, ' s'
print '(a, f8.3, a)', 'range: ', v0**2 * sin(2 * theta) / g, ' m'
print '(a, f8.3, a)', 'max height: ', (v0 * sin(theta))**2 / (2 * g), ' m'
end program exact_solutionflight time: 3.931 s
range: 90.349 m
max height: 18.953 m
Step 2: A state type and the equations of motion
The state of the ball is its position and velocity. We store it as a derived type and write the right-hand side of the equations of motion: the derivative of the state. With quadratic air drag, the acceleration is
a = (0, −g) − k |v| v, where k = ½ ρ C_d A / m
module projectile
use iso_fortran_env, only: dp => real64
implicit none
real(dp), parameter :: g = 9.81_dp
type :: state
real(dp) :: x = 0, y = 0, vx = 0, vy = 0
end type state
contains
pure function derivative(s, k) result(d)
type(state), intent(in) :: s
real(dp), intent(in) :: k ! drag coefficient per unit mass, 1/m
type(state) :: d
real(dp) :: speed
speed = hypot(s%vx, s%vy)
d%x = s%vx ! dx/dt = vx
d%y = s%vy
d%vx = -k * speed * s%vx ! dvx/dt
d%vy = -g - k * speed * s%vy
end function derivative
end module projectile
program check_derivative
use projectile
implicit none
type(state) :: s, d
s = state(x=0, y=0, vx=20, vy=15) ! speed 25 m/s
d = derivative(s, 0.0_dp)
print '(a, 4f9.3)', 'no drag: ', d
d = derivative(s, 0.002_dp)
print '(a, 4f9.3)', 'with drag: ', d
end program check_derivativeno drag: 20.000 15.000 -0.000 -9.810
with drag: 20.000 15.000 -1.000 -10.560
With drag, the horizontal acceleration is −0.002 × 25 × 20 = −1.0 m/s², as expected. The -0.000 in the first line is a signed zero: -k * speed * s%vx with k = 0 evaluates to −0.0, which compares equal to 0.0 but keeps its sign when printed.
Step 3: Operators and a Runge–Kutta step
The RK4 formula combines several states: s + h/2·k₁ and so on. Overloading + and * for the state type lets us write it exactly like the textbook. We then fly the ball without drag and compare with the exact answer from step 1:
module projectile
use iso_fortran_env, only: dp => real64
implicit none
real(dp), parameter :: g = 9.81_dp
real(dp), parameter :: pi = 4.0_dp * atan(1.0_dp)
type :: state
real(dp) :: x = 0, y = 0, vx = 0, vy = 0
end type state
interface operator(+)
module procedure add_states
end interface
interface operator(*)
module procedure scale_state
end interface
contains
pure function add_states(a, b) result(c)
type(state), intent(in) :: a, b
type(state) :: c
c = state(a%x + b%x, a%y + b%y, a%vx + b%vx, a%vy + b%vy)
end function add_states
pure function scale_state(h, a) result(c)
real(dp), intent(in) :: h
type(state), intent(in) :: a
type(state) :: c
c = state(h * a%x, h * a%y, h * a%vx, h * a%vy)
end function scale_state
pure function derivative(s, k) result(d)
type(state), intent(in) :: s
real(dp), intent(in) :: k
type(state) :: d
real(dp) :: speed
speed = hypot(s%vx, s%vy)
d = state(s%vx, s%vy, -k * speed * s%vx, -g - k * speed * s%vy)
end function derivative
pure function rk4_step(s, h, k) result(next)
type(state), intent(in) :: s
real(dp), intent(in) :: h, k
type(state) :: next, k1, k2, k3, k4
k1 = derivative(s, k)
k2 = derivative(s + (h / 2) * k1, k)
k3 = derivative(s + (h / 2) * k2, k)
k4 = derivative(s + h * k3, k)
next = s + (h / 6) * (k1 + 2.0_dp * k2 + 2.0_dp * k3 + k4)
end function rk4_step
end module projectile
program test_rk4
use projectile
implicit none
type(state) :: s
real(dp) :: v0, theta, t, h, t_exact
integer :: steps
v0 = 30.0_dp
theta = 40.0_dp * pi / 180.0_dp
t_exact = 2 * v0 * sin(theta) / g
h = 0.01_dp
s = state(0, 0, v0 * cos(theta), v0 * sin(theta))
t = 0
steps = nint(t_exact / h) ! fly for the exact flight time (rounded to whole steps)
do while (steps > 0)
s = rk4_step(s, h, 0.0_dp)
t = t + h
steps = steps - 1
end do
print '(a, f8.3, a, f10.3)', 't = ', t, ' x = ', s%x
print '(a, f10.3)', 'exact x at that time: ', v0 * cos(theta) * t
print '(a, es10.2)', 'difference: ', abs(s%x - v0 * cos(theta) * t)
end program test_rk4t = 3.930 x = 90.317
exact x at that time: 90.317
difference: 5.54E-13
Without drag the motion is a polynomial in time, which RK4 integrates exactly up to rounding error, a reassuring test of the implementation.
Step 4: Landing detection and a trajectory table
The ball lands somewhere between two time steps. When y changes sign, we interpolate linearly between the last two states to estimate the landing point. We also record a table row every 0.5 s.
module projectile
use iso_fortran_env, only: dp => real64
implicit none
real(dp), parameter :: g = 9.81_dp
real(dp), parameter :: pi = 4.0_dp * atan(1.0_dp)
type :: state
real(dp) :: x = 0, y = 0, vx = 0, vy = 0
end type state
interface operator(+)
module procedure add_states
end interface
interface operator(*)
module procedure scale_state
end interface
contains
pure function add_states(a, b) result(c)
type(state), intent(in) :: a, b
type(state) :: c
c = state(a%x + b%x, a%y + b%y, a%vx + b%vx, a%vy + b%vy)
end function add_states
pure function scale_state(h, a) result(c)
real(dp), intent(in) :: h
type(state), intent(in) :: a
type(state) :: c
c = state(h * a%x, h * a%y, h * a%vx, h * a%vy)
end function scale_state
pure function derivative(s, k) result(d)
type(state), intent(in) :: s
real(dp), intent(in) :: k
type(state) :: d
real(dp) :: speed
speed = hypot(s%vx, s%vy)
d = state(s%vx, s%vy, -k * speed * s%vx, -g - k * speed * s%vy)
end function derivative
pure function rk4_step(s, h, k) result(next)
type(state), intent(in) :: s
real(dp), intent(in) :: h, k
type(state) :: next, k1, k2, k3, k4
k1 = derivative(s, k)
k2 = derivative(s + (h / 2) * k1, k)
k3 = derivative(s + (h / 2) * k2, k)
k4 = derivative(s + h * k3, k)
next = s + (h / 6) * (k1 + 2.0_dp * k2 + 2.0_dp * k3 + k4)
end function rk4_step
end module projectile
program trajectory
use projectile
implicit none
type(state) :: s, prev
real(dp) :: v0, theta, k, h, t, frac, x_land, t_land, y_max
integer :: step, report_every
v0 = 30.0_dp
theta = 40.0_dp * pi / 180.0_dp
k = 0.01_dp ! a light ball, e.g. a tennis ball
h = 0.01_dp
report_every = 50 ! one row every 0.5 s
s = state(0, 0, v0 * cos(theta), v0 * sin(theta))
t = 0
y_max = 0
print '(a6, 4a10)', 't (s)', 'x (m)', 'y (m)', 'vx', 'vy'
print '(f6.2, 4f10.3)', t, s
step = 0
do
prev = s
s = rk4_step(s, h, k)
t = t + h
step = step + 1
y_max = max(y_max, s%y)
if (s%y < 0.0_dp) exit
if (mod(step, report_every) == 0) print '(f6.2, 4f10.3)', t, s
end do
frac = prev%y / (prev%y - s%y) ! where between the two steps y = 0
t_land = t - h + frac * h
x_land = prev%x + frac * (s%x - prev%x)
print '(a)', repeat('-', 46)
print '(a, f8.3, a)', 'landed after ', t_land, ' s'
print '(a, f8.3, a)', 'range ', x_land, ' m'
print '(a, f8.3, a)', 'max height ', y_max, ' m'
end program trajectory t (s) x (m) y (m) vx vy
0.00 0.000 0.000 22.981 19.284
0.50 10.731 7.830 20.120 12.278
1.00 20.258 12.460 18.094 6.379
1.50 28.906 14.318 16.558 1.139
2.00 36.859 13.671 15.283 -3.664
2.50 44.207 10.715 14.116 -8.098
3.00 50.978 5.638 12.968 -12.140
----------------------------------------------
landed after 3.413 s
range 56.131 m
max height 14.383 m
Compare with step 1: air resistance cut the range from about 90 m to about 56 m and the flight time from 3.93 s to 3.41 s, and the descent is steeper than the climb, which is the characteristic asymmetric shape of a real trajectory.
Printing s with 4f10.3 prints all four components of the derived type in order.
Step 5: The best launch angle with drag
Without drag, 45° gives the longest range. With drag, the optimum is lower. Let's wrap the simulation in a function and scan the angles from 30° to 50°, then use maxloc to pick the best:
module projectile
use iso_fortran_env, only: dp => real64
implicit none
real(dp), parameter :: g = 9.81_dp
real(dp), parameter :: pi = 4.0_dp * atan(1.0_dp)
type :: state
real(dp) :: x = 0, y = 0, vx = 0, vy = 0
end type state
interface operator(+)
module procedure add_states
end interface
interface operator(*)
module procedure scale_state
end interface
contains
pure function add_states(a, b) result(c)
type(state), intent(in) :: a, b
type(state) :: c
c = state(a%x + b%x, a%y + b%y, a%vx + b%vx, a%vy + b%vy)
end function add_states
pure function scale_state(h, a) result(c)
real(dp), intent(in) :: h
type(state), intent(in) :: a
type(state) :: c
c = state(h * a%x, h * a%y, h * a%vx, h * a%vy)
end function scale_state
pure function derivative(s, k) result(d)
type(state), intent(in) :: s
real(dp), intent(in) :: k
type(state) :: d
real(dp) :: speed
speed = hypot(s%vx, s%vy)
d = state(s%vx, s%vy, -k * speed * s%vx, -g - k * speed * s%vy)
end function derivative
pure function rk4_step(s, h, k) result(next)
type(state), intent(in) :: s
real(dp), intent(in) :: h, k
type(state) :: next, k1, k2, k3, k4
k1 = derivative(s, k)
k2 = derivative(s + (h / 2) * k1, k)
k3 = derivative(s + (h / 2) * k2, k)
k4 = derivative(s + h * k3, k)
next = s + (h / 6) * (k1 + 2.0_dp * k2 + 2.0_dp * k3 + k4)
end function rk4_step
pure function flight_range(v0, angle_deg, k) result(x_land)
real(dp), intent(in) :: v0, angle_deg, k
real(dp) :: x_land, theta, frac
real(dp), parameter :: h = 0.005_dp
type(state) :: s, prev
theta = angle_deg * pi / 180.0_dp
s = state(0, 0, v0 * cos(theta), v0 * sin(theta))
do
prev = s
s = rk4_step(s, h, k)
if (s%y < 0.0_dp) exit
end do
frac = prev%y / (prev%y - s%y)
x_land = prev%x + frac * (s%x - prev%x)
end function flight_range
end module projectile
program best_angle
use projectile
implicit none
integer, parameter :: n = 21
real(dp) :: angles(n), range_drag(n), range_vacuum(n)
integer :: i, best
angles = [(30.0_dp + i, i = 0, n - 1)] ! 30, 31, ..., 50 degrees
do concurrent (i = 1:n)
range_drag(i) = flight_range(30.0_dp, angles(i), 0.01_dp)
range_vacuum(i) = flight_range(30.0_dp, angles(i), 0.0_dp)
end do
print '(a8, 2a12)', 'angle', 'no drag', 'drag'
do i = 1, n, 4
print '(f8.1, 2f12.3)', angles(i), range_vacuum(i), range_drag(i)
end do
best = maxloc(range_drag, dim=1)
print '(a, f5.1, a, f8.3, a)', 'best angle with drag: ', angles(best), ' deg (', range_drag(best), ' m)'
best = maxloc(range_vacuum, dim=1)
print '(a, f5.1, a, f8.3, a)', 'best angle in vacuum: ', angles(best), ' deg (', range_vacuum(best), ' m)'
end program best_angle angle no drag drag
30.0 79.452 52.610
34.0 85.063 54.716
38.0 89.018 55.886
42.0 91.241 56.155
46.0 91.687 55.550
50.0 90.349 54.097
best angle with drag: 41.0 deg ( 56.171 m)
best angle in vacuum: 45.0 deg ( 91.743 m)
Because flight_range is pure, the angle scan can safely use do concurrent: every angle is simulated independently, so a parallelising compiler could run them on different cores.
Where to go next
Ideas to extend the project:
- Wind: add a constant wind speed
wand use the velocity relative to the air,vx - w, in the drag term. - Bisection for a target: use the root-finding lesson to find the angle that lands exactly at 60 m.
- Output to a file: write the trajectory to a CSV file with
open(newunit=u, file='trajectory.csv', status='replace')and plot it with your favourite tool. - Object-oriented version: turn
stateand the integrator into a type with type-boundstepandrunprocedures, and make the force model an abstract type so you can plug in different physics (lift, spin, altitude-dependent air density).
Test yourself
Why can RK4 reproduce the no-drag trajectory almost exactly?
- Because the step is very small
- Without drag the motion is a low-degree polynomial in time, which RK4 integrates exactly
- Because gravity is constant
- It can't; the error is large
How does the program find the landing point between two time steps?
- It reduces the step size to zero
- It linearly interpolates between the last state above ground and the first one below
- It uses the exact formula
- It rounds to the nearest step
Why is it safe to use do concurrent for the angle scan?
- Because angles are integers
- Each iteration calls only the pure function
flight_rangeand writes its own array element - Because n is small
- It is never safe