Projects / extras

Bead Lab

Bead Lab is an iPhone app with eight simulations that run live on the phone, from a Lennard-Jones liquid to polymer blends and patterns that form on their own. Each one has sliders and a short page on the model behind it. Most of them also plot one quantity as they run, and in some you can heat, stir or paint the system with your finger. The simulations are written from scratch in Swift, and the Gray-Scott patterns run on the phone's GPU.

the code on GitHub →

The eight simulations

in three groups, as in the app

One of them in your browser

the same model as in the app

The Gray-Scott model is two chemicals that react and diffuse. One feeds on the other, and depending on the feed and kill rates, the pattern settles into spots, stripes or a maze. Here it runs in your browser. In the app, it runs on the phone's GPU, on a grid of 512 × 512 cells.

The other models are explained, with simulations you can run here, on the learn pages for molecular dynamics, dissipative particle dynamics, polymers in solution, Monte Carlo and diffusion.

How it is built

Swift, SwiftUI and Metal

The app

  • Simulations in plain Swift

    The simulations have no screen code, and all of them share one small protocol: sliders, a step, a picture and a touch. The particle simulations split their force loops over all CPU cores.

  • Screens in SwiftUI

    The sliders, the live plot and the page on each model.

  • Gray-Scott on the GPU

    A Metal compute shader updates the 512 × 512 grid 16 times per frame.

The checks

  • Fast enough for the screen

    One frame of any CPU simulation takes 2 to 7 ms on a 4-core test machine, well inside the 16 ms of a 60 Hz screen.

  • Physics tests

    Ten engine tests check, for example, that the thermostats hold their temperature, that the Brownian motion gives back the D you set, that DPD blends separate at high aAB, and that Cahn-Hilliard keeps the total composition.

  • Built on every push

    GitHub Actions runs the tests and builds the app for the iPhone simulator.

Trying it

built with Xcode

Bead Lab is not in the App Store. To run it on an iPhone, you build it with Xcode on a Mac, signed with a free Apple Account. With a free account, the app stays on the phone for seven days, and then it has to be installed again. The code is on GitHub, with the steps to build it.

References

for the models in the app

  1. Dissipative particle dynamics: bridging the gap between atomistic and mesoscopic simulation
    R. D. Groot and P. B. Warren. J. Chem. Phys. 107, 4423 (1997).
  2. Role of repulsive forces in determining the equilibrium structure of simple liquids
    J. D. Weeks, D. Chandler and H. C. Andersen. J. Chem. Phys. 54, 5237 (1971).
  3. Equation of state calculations by fast computing machines
    N. Metropolis, A. W. Rosenbluth, M. N. Rosenbluth, A. H. Teller and E. Teller. J. Chem. Phys. 21, 1087 (1953).
  4. Free energy of a nonuniform system. I. Interfacial free energy
    J. W. Cahn and J. E. Hilliard. J. Chem. Phys. 28, 258 (1958).
  5. Complex patterns in a simple system
    J. E. Pearson. Science 261, 189 (1993).