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

Computational physics

Understanding the physical laws that inspired the movie Arrival using Data Science tools

Image from the motion picture Arrival. The principle of stationary action has inspired the author of the book.
Image from the motion picture Arrival. The principle of stationary action has inspired the author of the book.

A few days after seeing the motion picture Arrival, while working on my previous paper Differentiable Programming, I discovered that the authors of JAX, a library implementing Automatic Differentiation, recommended the reading of Structure and Interpretation of Classical Mechanics. I was immediately struck by the clever way they explain the Principle of Stationary Action, that inspired Arrival.

This book has been published by MIT and reuses the same title to Structure and Interpretation of Computer Programs, that was also published by MIT. Both are available for free online!

Structure and Interpretation of Computer Programs, also known as the Wizard Book is a famous computer science book. I highly recommend its reading. As I had a really great time reading it, I was intrigued by this new book on Classical Mechanics that seemed to follow the same pattern.

As a former researcher in the mechanical field, I was not disappointed by this book; as like for SICP, I really enjoyed the reading of SICM. The overall idea of this book is to approach Classical Mechanics using the tools provided by computer science: Numerical analysis, Symbolic computation, Automatic Differentiation, …

In this article, we will see how to use the mathematical and numerical tools we are familiar with to compute rigid bodies trajectories, using the physics principle that inspired the movie Arrival: the Principle of Stationary Action.


Newton’s second law

The physics we are familiar with are essentially based on the vision of Newton. In his vision, based on a differential approach, motion and forces are related by the acceleration: Applying a force on a body changes its speed, i.e. the acceleration of the body is proportional to the force. And the more massive a body is, the lesser the force influences its speed: acceleration is inversely proportional to mass.

This relation is generally subsumed by his famous second law:

Newton second law of physics. Image by the author.
Newton second law of physics. Image by the author.

Using this formula, or slightly more sophisticated versions, it is absolutely possible to simulate the motion of rigid or deformable objects.

Principle of Stationary Action

However, in this paper, we will focus on another approach, developed by the french mathematician Maupertuis, refined by the brilliant Britannic mathematician Hamilton, further extended by the Russian mathematician Ostrogravsky and generalized by the french scientist Lagrange: the Principle of Least Action, generalized as the Principle of Stationary Action.

Also, note that this principle underlies the story of the fascinating book Story of your life by the American author Ted Chiang. This book has inspired the none less famous movie Arrival. But enough with this cultural digression. Let’s go back to math and physics.

I won’t give you the statement for this principle, that can be found in any good mechanic textbook. Instead, like in SICM, I will emphasize how we can use this principle to select physically acceptable motions amongst candidates trajectories using our preferred programming language: Python.

Indeed, this principle essentially states that any physically acceptable path minimizes a specific function. Or to be more accurate, any small variation around the acceptable path must leave the function unchanged, i.e. the path is stationary.

So, to find the right path, all we have to do is perform some minimization of an objective. Nothing easier, this is our daily job.


Programming physics

To compute these acceptable paths, all we need is :

  • A way to create candidates trajectories. For this, we need a function of time that return position. Plain python and some polynomial magic will to the job.
  • A way to extract characteristic features from the trajectories: usually their position, speed, acceleration and possibly higher-order derivative. We can use Automatic Differentiation for that.
  • A way to identify amongst the candidate trajectories the one that is the more physically plausible. We know, thanks to the Principle of Stationary Action, that this is accomplished by minimizing a function of the candidate trajectory characteristic features extracted by the previous step, with respect to the candidate trajectory. We refer to this function as the action, and it’s the integral between two timestamps of the Lagrangian, another function that enforces the physical law that we want to simulate.

Let’s be concrete, and put some code on these concepts.

Creating trajectories

There are various options to defines trajectories. We can for instance use splines, Bezier curves and Bernstein polynomials. But as we are dealing with a physical subject, we are going to use Lagrange polynomials.

These polynomials are pretty convenient: they control curves by forcing them to go through predefined control points. To do so, Lagrange has designed his polynomial so that the coefficient before each point x_i is 0.0 for any time t_k different of t_i, and is equal to 1.0 for t_i.

Here is an example of such a formula for the coefficient of the first control point x_0, for a curve with 3controls points.

An example of a Lagrangian polynomial coefficient. Image by the author.
An example of a Lagrangian polynomial coefficient. Image by the author.

Written in Python, this gives:

We can apply this code as follow, to get an example trajectory:

The resulting trajectory is shown below:

A trajectory going through 4 control points
A trajectory going through 4 control points

Note that the curve goes exactly through the control points.

Extract characteristic features

Now that we have a way to define curves using parameters as easy to handle as control points, we need a tool to extract characteristic features at any point of these curves.

As the features we need are position, speed or acceleration, i.e. derivatives of various order of the curve with respect to time, we can use Automatic Differentiation.

We define the following function, _localstate, that taking any function of time, computes its characteristic using the AD library JAX:

We can apply this function on the curve defined above. Looking at the curve tells us that the derivative must be null at t=1.5. Let’s check that with these few lines of code:

Everything works like a charm.

Identifying the right trajectory

Creating a candidate trajectory is now as straightforward as choosing a set of control points. We also have the tool to compute differential geometrical features automatically.

All we need now is a way to choose the right trajectory amongst candidates, or even better, to converge to the right one.

As written a few lines above, and stated by Maupertuis, we know for centuries that a physically acceptable trajectory minimizes what is called the Action.

The action is calculated between time to and t1 by integrating the Lagrangian of the candidate trajectory f. Image by the author.
The action is calculated between time to and t1 by integrating the Lagrangian of the candidate trajectory f. Image by the author.

L stands here for the Lagrangian, and Gamma is our function _localstate, in charge of feature extraction.

To perform the integration, we are going to use the standard scipy integrate method.

As a first try, we will compute the trajectory of a body freely falling. In this case, the body is only subject to the acceleration due to gravity. When dropping an object with a null initial speed, its y coordinate at time t is:

y, coordinate a time t. Image by the author
y, coordinate a time t. Image by the author

With this physical setup, the Lagrangian is defined as the difference between the kinetic energy and the potential energy due to gravity, i.e.:

Lagrangian when the body is subject to gravity only. Image by the author.
Lagrangian when the body is subject to gravity only. Image by the author.

Putting all these together, we get:

The action for the real trajectory is indeed minimal with respect to a perturbated curve.

Free Fall Example

Let’s now check that starting from any candidate trajectory, we can converge to the right one when minimizing the action. For this, we are going to use scipy.optimize.minimize function, using the geometric Nelder-Mead minimization method.

This lead to the following code:

As expected, we converge to the right curve, showing y coordinate at time t:

The initial curve is displayed in blue. After minimizing the action, we converge to the right trajectory. Image by the author.
The initial curve is displayed in blue. After minimizing the action, we converge to the right trajectory. Image by the author.

Free Motion Example

Let’s try another example, to be convinced. In this case, we will not only consider y, but also x.

We enforce positions of our body not only at t0 but also at t1. Hence we have two boundaries conditions:

  • t0: (0.0, 0.0)
  • t1: (0.0, 4.0)

This time we are interpolating 2D vectors not just float. To handle this, we are just going to use a Lagrangian polynomial for each dimension. Another option could have been to use a single polynomial and use 2D points as control points.

See the code below:

The equation for the real trajectory is defined in the real_trajectory function. It’s used for displaying some checkpoints, to ensure that we converge to the right motion, as shown below:

The blue curve, even though pretty far from the right trajectory successfully converge to the right motion. Image by the author.
The blue curve, even though pretty far from the right trajectory successfully converge to the right motion. Image by the author.

One again, everything works as expected.


Conclusion: use programming to teach physics (and math)

With these basic examples, we have shown that it’s possible to ease the understanding of pretty complex physical theories, using the same tools that we use for Data Science.

We have been able to illustrate the variational approach of classical mechanics with few lines of code, hence making it easier to understand the concepts behind these theoretical principles.

Introducing programming languages like python, with all their power and flexibility in teaching Physics is a very good way to help to grab complex subjects. With them, it’s straightforward to experiment with physics laws and understand their real meanings. It’s so easy nowadays to test a hypothesis, build a toy model, display curves, …

There is no better way than creating your own simulator to understand a complex phenomenon. Programming should be a tool used in any physics classroom.


Related Articles