Computational Physics
Code, algorithms and simulations for understanding the physical world
Computation is how we bridge the gap between theory and experiment. This section covers the tools and methods I use daily — from MDAnalysis scripts to machine learning models and Monte Carlo simulations.
MDAnalysis Guide
Practical Python tutorials for analysing molecular dynamics trajectories using the MDAnalysis library.
Machine Learning
Applying ML methods to molecular simulation data — from dimensionality reduction to predicting membrane properties.
Monte Carlo Methods
Random sampling, Metropolis algorithms and their applications in statistical physics and membrane modelling.
MDAnalysis Guide
MDAnalysis is a Python library for analysing molecular dynamics trajectories. It is flexible, well-documented and integrates naturally with NumPy and matplotlib.
Planned tutorials:
- Getting started — installing MDAnalysis and loading a trajectory
- Atom selections — using the selection language to pick specific molecules
- Measuring distances and angles — between atoms, residues and lipid headgroups
- Density profiles — computing electron density and mass density across the bilayer
- Order parameters — calculating lipid tail order parameters (S_CD)
- Leaflet identification — using LeafletFinder to separate upper and lower leaflets
- Lateral diffusion — computing mean squared displacement and diffusion coefficients
- Membrane thickness — mapping bilayer thickness across the membrane surface
- Custom analysis — writing your own analysis classes
Example snippet:
import MDAnalysis as mda
from MDAnalysis.analysis import rdf
# Load simulation
u = mda.Universe('system.tpr', 'trajectory.xtc')
# Select phosphorus atoms (headgroup markers)
phosphorus = u.select_atoms('name P')
# Print number of lipids
print(f"Number of lipids: {len(phosphorus)}")
Full tutorials with example data are coming soon.
Machine Learning
Machine learning offers powerful tools for extracting patterns from the high-dimensional data produced by molecular simulations.
Topics to be covered:
- Dimensionality reduction — PCA and t-SNE applied to lipid conformations
- Clustering — identifying distinct membrane states from simulation trajectories
- Neural networks — predicting membrane properties from lipid composition
- Autoencoders — learning compact representations of bilayer configurations
- Gaussian process regression — building surrogate models for expensive simulations
Each topic will include Python code using scikit-learn, PyTorch or TensorFlow, with a focus on making the concepts accessible to physicists who are new to ML.
First tutorials are being drafted.
Monte Carlo Methods
Monte Carlo methods use random sampling to solve problems that are deterministic in principle but intractable in practice. They are fundamental to statistical physics and are widely used in membrane modelling.
Planned content:
- The Metropolis algorithm — deriving and implementing the basic MC method
- Ising model — simulating phase transitions on a 2D lattice
- Lattice models of membranes — modelling lipid domains and phase separation
- Wang-Landau sampling — estimating the density of states
- Kinetic Monte Carlo — simulating rare events in membrane dynamics
Example: 2D Ising model
import numpy as np
def metropolis_step(lattice, beta):
N = lattice.shape[0]
i, j = np.random.randint(0, N, size=2)
spin = lattice[i, j]
neighbours = (
lattice[(i+1)%N, j] + lattice[(i-1)%N, j] +
lattice[i, (j+1)%N] + lattice[i, (j-1)%N]
)
delta_E = 2 * spin * neighbours
if delta_E < 0 or np.random.random() < np.exp(-beta * delta_E):
lattice[i, j] *= -1
return lattice
Full implementations with visualisations are on the way.