Create Your Own Spring Network Simulation (With Python)

Philip Mocz
4 min readMar 20, 2021

For today’s recreational coding exercise, we will simulate a spring network: an array of nodes connected by springs that obey Hooke’s Law. The spring system will fall down due to gravity and bounce off the floor. We use a particle algorithm similar to our N-body simulation.

You may find the accompanying Python code on github.

Before we begin, below is a gif of what running our simulation looks like:

Animation of our spring network simulation

Force Calculation

We will assume a system of N particles (nodes), indexed by i=1,…,N. Each node has a:

  • position rᵢ = [ xᵢ, yᵢ, zᵢ ],
  • velocity v = [ vx, vyᵢ, vzᵢ ]

The nodes are arranged in a grid, connected by springs. Each spring connects a pair of nodes i and j, and are characterized by:

  • rest length Lᵢⱼ ,
  • spring coefficient k

Then, each node will feel an acceleration along the direction of any spring that is stretched/compressed from its rest length. We model this with Hooke’s Law:

where the sum is taken over all points j that are connected to i. Hooke’s Law says that a spring feels a restoring force that is linearly proportional to how much it is displaced from its ‘relaxed’ position…

--

--

Philip Mocz

Computational Physicist. Sharing intro tutorials on creating your own computer simulations! Harvard ’12 (A.B), ’17 (PhD). Connect with me @PMocz