Lattice $\phi^4$ Simulation

The lattice simulation code is written in C++ and comes with an interface to Sage (a Python-based computer algebra system) for analysis. The source code is on github.

Getting Sage

If you are using Linux or OSX then it is lengthy but simple to compile yourself. Note that you need xcode + command line tools on OSX. On Windows you can run Sage in a pre-packaged virtual machine.

Alternatively, you can run it “in the cloud” just with a browser, no local installation of anything required. I have prepared a public worksheet here.

The Lattice Code

You have to download and compile the lattice code regardless of where you run Sage. Its simple! Download the source code (the dollar sign is the prompt and might look different on your system, you should only type in what comes after the dollar sign in the first line):

$ git clone
Cloning into 'lattice_phi4'...
remote: Counting objects: 30, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 30 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (30/30), done.
Checking connectivity... done.

This creates a directory, change into it and compile in the Sage environment:

$ sage -sh -c make
cython --cplus phi4.pyx
g++ -O3 -fPIC -fwrapv -fno-strict-aliasing -Wall -Werror -pedantic -std=c++11 -g -pg -I/mnt/disk/home/release/Sage/local/include -I/mnt/disk/home/release/Sage/local/include/python2.7   -c -MMD -MP -o phi4.o phi4.cpp
g++ -O3 -fPIC -fwrapv -fno-strict-aliasing -Wall -Werror -pedantic -std=c++11 -g -pg -I/mnt/disk/home/release/Sage/local/include -I/mnt/disk/home/release/Sage/local/include/python2.7   -c -MMD -MP -o lattice.o lattice.cpp
g++ -O3 -fPIC -fwrapv -fno-strict-aliasing -Wall -Werror -pedantic -std=c++11 -g -pg -I/mnt/disk/home/release/Sage/local/include -I/mnt/disk/home/release/Sage/local/include/python2.7   -c -MMD -MP -o simulation.o simulation.cpp
g++ -O3 -fPIC -fwrapv -fno-strict-aliasing -Wall -Werror -pedantic -std=c++11 -g -pg -I/mnt/disk/home/release/Sage/local/include -I/mnt/disk/home/release/Sage/local/include/python2.7   -c -MMD -MP -o explicit_instantiation.o explicit_instantiation.cpp
g++ -shared -pthread -o phi4.o lattice.o simulation.o explicit_instantiation.o -lgsl -lgslcblas -lm -lstdc++
python ./
Equilibrate: 397ms
1000 -0.00533875140543 0.477598774092 nan
(-0.7396239746356059, 0.7584699698779787)
[32, 92, 110, 97, 57, 40, 29, 20, 9, 12, 15, 12, 23, 15, 26, 36, 62, 115, 116, 66, 15]
1000 0.477598774092 0.477598774092 0.785273642688
1000 0.253847538236 0.253847538236 0.661684664824
1.01627893794 1.016279
25.7240551779 25.724055
32 x 32 dense matrix over Real Double Field

Your First Simulation

Run Sage (in the same directory as above). You’ll get a “sage:” prompt.

$ sage
│ Sage Version 6.4.rc1, Release Date: 2014-11-01                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
┃ Warning: this is a prerelease version, and it may be unstable.     ┃

The Simulation class is the main interface to the C++ code. Note that anything after the #-sign is a comment in Python.

sage: from phi4 import Simulation
sage: sim = Simulation(-1.26, 1.0, equilibrate=1000)
Equilibrate: 348ms
sage:    # run for 1000 iterations
sage: sim.abs_phi().samples()

Get help by adding a questionmark:

sage: Simulation?

Pretty Pictures

There is a helper class to produce various plots:

sage: from phi4 import Simulation
sage: from phase_plot import PhasePlot
sage: susz = PhasePlot(32, 32, observable=Simulation.susceptibility, equilibrate=1000, iterations=1000)
sage: susz.mass_squared = srange(-2, 0, 0.05)
sage: susz.plot()