The world’s leading publication for data science, AI, and ML professionals.

How Apollo 8 Won the Moon Race

Simulating the free-return trajectory with Python

Quick Success Data Science

Fifty-five years ago this December, the Apollo 8 crew became the first humans to leave Earth’s sphere of influence and travel to another celestial body. This month, Frank Boorman, the commander of that mission, passed away at age 95. To remember this historic event and its distinguished crew, I’ll review a computer simulation of the Apollo 8 flight path. Simulations like this are useful for mission planning, as teaching aids, and for consumption by the press and general public.

Why was Apollo 8 so Important?

In the summer of 1968, America was losing the space race. The Soviet Zond spacecraft appeared moon-ready, the CIA had photographed a giant Soviet N-1 rocket sitting on its launch pad, and the Americans’ troubled Apollo program still needed three more test flights.

But in August of that year, NASA manager George Low had an audacious idea. Let’s go to the moon now. Instead of more tests in Earth’s orbit, circle the moon in December and let that be the test!

At that moment, the space race was essentially over. Less than a year later, the Soviets had capitulated, and Neil Armstrong had taken his great leap for all mankind.

The decision to take the Apollo 8 spacecraft to the moon was hardly trivial. In 1967, three men had died in the Apollo 1 capsule, and multiple unmanned missions had blown up or otherwise failed.

Against this backdrop and with so much at stake, everything hinged on the concept of the Free Return Trajectory. The mission was designed so that if the service module engine failed to fire, the ship would simply swing around the moon and return to the Earth like a boomerang. The elegant beauty of this concept was even incorporated into the official mission patch.

In this article, we’ll explore the free-return trajectory using a computer simulation built with the Python programming language. Although not necessary, if you want to play along with the simulation as you read, you can download the source code and supporting files from this GitHub repository. You’ll need the files _apollo_8_freereturn.py, _earth100×100.gif, and _moon27×27.gif.

Understanding the Apollo 8 Mission

The goal of the Apollo 8 mission was merely to circle the moon, so there was no need to take a lunar lander component. The astronauts traveled in the command and service modules, collectively known as the CSM.

In the fall of 1968, the CSM engine had been tested in the earth’s orbit only, and there were legitimate concerns about its reliability. To orbit the moon, the engine would have to fire twice, once to slow the spacecraft to enter lunar orbit and then again to leave orbit.

With the free-return trajectory, if the first maneuver failed, the astronauts could still coast home. As it turned out, the engine fired perfectly both times, and Apollo 8 orbited the moon 10 times. (The ill-fated Apollo 13, however, made great use of its free-return trajectory!)

The Free Return Trajectory

While calculating a free-return trajectory requires a lot of intense mathematics (it is rocket science, after all), you can still use a few simplified parameters to diagram one in two dimensions, as shown below.

To simulate the free return in 2D we need a few key values: the starting position of the CSM (R₀), the velocity and orientation of the CSM (V₀), and the phase angle between the CSM and the moon (gamma₀). The phase angle, also called the lead angle, is the change in the orbital time position of the CSM required to get from a starting position to a final position.

The translunar injection velocity (V₀) is a propulsive maneuver used to set the CSM on a trajectory to the moon. It’s achieved from a parking orbit around the earth, where the spacecraft performs internal checks and waits until the phase angle with the moon is optimal. At this point, the third stage of the Saturn V rocket fires and falls away, leaving the CSM to coast to the moon.

Because the moon is moving, you have to predict its future position or lead it, like shooting skeet with a shotgun. This requires knowing the phase angle (gamma₀) at the time of translunar injection.

Leading the moon is a little different from shooting a shotgun, however, as space is curved, and you need to factor in the gravity of the earth and the moon. The tug of these two bodies on the spacecraft creates perturbations that are difficult to calculate. So difficult, in fact, that the calculation has earned its own special name in the field of physics: the three-body problem.

The Three-Body Problem

The three-body problem is the challenge of predicting the behavior of three interacting bodies. Isaac Newton’s gravity equations work great for predicting the behavior of two orbiting bodies, such as the earth and the moon, but add one more body to the mix, whether a spacecraft, comet, moon, or so on, and things get complicated. Newton was never able to encapsulate the behavior of three or more bodies into a simple equation. For 275 years — even with kings offering prizes for a solution — the world’s greatest mathematicians worked the problem in vain.

The issue is that the three-body problem can’t be solved using simple algebraic expressions or integrals. Calculating the impact of multiple gravitational fields requires numerical iteration on a scale that’s impractical without a high-speed computer, such as your laptop.

In 1961, Michael Minovitch, a summer intern at the Jet Propulsion Laboratory, found the first numerical solution using an IBM 7090 mainframe, at the time the fastest computer in the world. He discovered that mathematicians could reduce the number of computations needed to solve a restricted three-body problem, like our earth-moon-CSM problem, by using a patched conic method.

The Patched Conic Method

The patched conic method is an analytical approximation that assumes you’re working with a simple two-body problem while the spacecraft is in the earth’s gravitational sphere of influence and another when you’re within the moon’s sphere of influence. It’s a rough, "back-of-the-envelope" calculation that provides reasonable estimates of departure and arrival conditions, reducing the number of choices for initial velocity and position vectors. All that’s left is to refine the flight path with repeated computer simulations.

Because researchers have already found and documented the Apollo 8 mission’s patched conic solution, we don’t need to calculate it. I’ve already adapted it to the 2D simulation we’ll be viewing here.

If you download the source code, you can experiment with alternative solutions by varying parameters such as R₀ and V₀.

The Simulation

The free-return trajectory simulation will capture the fundamentals of the flight path while making several approximations for "viewing pleasure." These involve running the simulation in 2D, ignoring Apollo 8’s ten orbits of the moon, and distorting both the time and distance scales. As a simplification, neither the Earth nor the moon rotates.

Time Scale Distortions

In 1968, a round-trip to the moon took about six days. No one wants to watch this in real time, so the simulation increments the time unit by 0.001 with each loop. This means that, when running the simulation for 4,100 loops, each time step represents about two minutes of time in the real world. The longer the time step, the faster the simulation but the less accurate the results, as small errors compound over time.

You can optimize the time step in flight plan simulations by first running a small step for maximum accuracy and then using the results to find the largest time step that yields a similar result.

Distance Scale Distortions

The simulation is not to scale. Although the Earth and Moon images will have the correct relative sizes, the distance between the two is smaller than the actual distance, so the phase angle has to be adjusted accordingly. I’ve reduced the distance in the model because outer space is big. Really big. If you want to show the simulation to scale and fit it all on your computer monitor, then you must settle for a ridiculously tiny earth and moon.

To keep the two bodies recognizable, the simulation uses larger, properly scaled images with the distance between them reduced. This configuration will be more relatable to the viewer while still permitting replication of the free-return trajectory.

Because the earth and the moon are closer together in the simulation, the moon’s orbital velocity will be faster than in real life, as per Kepler’s second law of planetary motion. To compensate for this, the moon’s starting position is designed to reduce the phase angle appropriately.

As a final nod to viewability, the size of the CSM is drastically increased. At scale, it would be smaller than a pixel.

Running the Simulation

The simulation tracks Apollo 8 from the moment the Saturn V rocket’s third stage stops firing for translunar injection until the splashdown of the command module in the Pacific Ocean. The initial acceleration represents the only time the spacecraft needs to fire its thrusters during a free return maneuver.

NOTE: The animation below is a GIF, as required by Medium. Image quality is better when running the program directly from the Python script.

If you uncomment the self.pendown() option in the Python script you can trace the journey by drawing a line behind the CSM and moon. The "figure 8" pattern of the free-return trajectory is clearly visible, as well as the spacecraft’s close approach to the moon, at an altitude of only 69 miles (110 km).

If you play around with the simulation inputs, you’ll find that the slightest tweaks to the parameters can cause the mission to go awry. Negative outcomes include crashing into the moon, entering Earth’s atmosphere too steeply, or continuing off into space forever.

To simulate a moon crash, change the Vo_X variable, representing the V₀ translunar injection velocity, from its initial value of 485 to 475.

While this sensitivity is partly a function of the limited degrees of freedom in the small simulation, the same issues persist in the real world. At critical moments, space travel becomes a game of seconds and centimeters, which is why computers are required to fire thrusters for important maneuvers like orbital insertion burns.

Simulating Gravity Propulsion

Michael Minovitch’s solution to the three-body problem led him to another significant discovery: gravity propulsion. Known also as the Gravity Assist and the slingshot maneuver, this technique involves using the gravity field of a large planet to either accelerate or decelerate a spacecraft into a different orbit. It was most famously used in the 1970s to send the Voyager spacecraft on their grand tours of the solar system.

You can simulate gravity propulsion by setting the Vo_X variable to a value between 520 and 540. This will cause the CSM to pass behind the moon and steal some of its momentum, increasing the ship’s velocity and deflecting its flight path.

Interestingly, accelerating the CSM in this way will slow down the moon in its orbit, as the laws of physics dictate that energy must be conserved. The effect on the moon is negligible, however, due to the tremendous differences in the mass of the two objects.

Summary

The Python simulation referenced in this article lets you experiment with the famous free-return trajectory that convinced NASA to gamble and go to the moon earlier than planned. The nice thing about simulations is that, if you fail, you can live to try again. NASA runs countless simulations for all its proposed missions. The results help NASA choose between competing flight plans, find the most efficient routes, decide what to do if things go wrong, and much more.

Simulations are especially important for outer solar system exploration, where great distances make real-time communications impossible. The timing of key events, such as firing thrusters, taking photographs, or dropping probes, are all preprogrammed based on meticulous simulations.

References and Further Reading

Real-world Python: A Hacker’s Guide to Solving Problems with Code (No Starch Press, 2020) by Lee Vaughan, dedicates a chapter to a detailed explanation of the free-return simulation code.

Apollo 8: The Thrilling Story of the First Mission to the Moon (Henry Holt and Co., 2017), by Jeffrey Kluger, covers the historic Apollo 8 mission from its unlikely beginning to its "unimaginable triumph."

PBS Nova How Apollo 8 Left Earth Orbit is a short video clip on the Apollo 8 translunar injection maneuver, marking the first time that humans left the earth’s orbit and traveled to another celestial body.

NASA Voyager 1 & 2 Owner’s Workshop Manual (Haynes, 2015), by Christopher Riley, Richard Corfield, and Philip Dolling, provides interesting background on the three-body problem and Michael Minovitch’s many contributions to space travel.

The Wikipedia _Gravity Assist_ page contains lots of interesting animations of various gravity-assist maneuvers and historic planetary flybys that you can reproduce with the Apollo 8 simulation referenced in this article.

Chasing New Horizons: Inside the Epic First Mission to Pluto (Picador, 2018), by Alan Stern and David Grinspoon, documents the importance – and ubiquity – of simulations in NASA missions.

Thanks

Thanks for reading and please follow me for more Quick Success Data Science articles in the future.


Related Articles