Lesson 1.3 · 12 min
Newton's law in code
A force alone does not move an atom: it changes the velocity, and the velocity changes the position. In code, that becomes two lines repeated over and over.
- How F = ma turns a force into motion
- The two update lines at the heart of every MD step
- How to store and update many atoms at once with arrays
The idea
In Lesson 1.2 we found the force between atoms. Now we need to turn that force into motion.
Newton's second law says F = ma, or a = F/m. A force gives an atom an acceleration: a change in its velocity. The same push changes a light atom's velocity a lot and a heavy atom's velocity only a little.
A computer cannot follow time smoothly, so it moves forward in tiny jumps of length Δt. In each jump:
- Compute the force on the atom.
- Get the acceleration: a = F/m.
- Update the velocity: new v = v + a·Δt.
- Update the position: new x = x + v·Δt.
Repeat this many times and the dots join up into a trajectory. In 3D, each position, velocity and force has three numbers (x, y, z), so for N atoms we store N × 3 arrays and update them all together.
The algorithm
One MD step for N atoms, written with arrays of shape (N, 3):
F = compute_forces(pos) # (N, 3): from Lesson 1.2 a = F / mass # mass has shape (N, 1) vel = vel + a * dt # update all velocities pos = pos + vel * dt # move all atoms
- Order matters. Here we update the velocity first and then use the new velocity to move. This version (semi-implicit Euler) is simple and surprisingly stable.
- Arrays, not loops. One line updates every atom. In NumPy this is much faster than a Python loop over atoms.
- Mass as a column. Storing masses as an (N, 1) column lets each atom's force row be divided by its own mass.
The step size Δt is a trade-off: small steps follow the true path closely but cost more steps. Lesson 1.4 looks at how to choose it, and Lesson 1.5 replaces this update with the more accurate velocity Verlet.