IsingCocoa
This application provides an interface for running simulations of the Ising model of magnetically interacting spins on a 2-dimensional lattice. It is intended as a demonstration of the possibilities for interactive exploration of scientific models - the computing power available in modern machines is sufficient to perform sophisticated calculations on the fly as the parameters are changed. The original version of this program was written for X11 using the UIM/X user-interface development tool while I was an employee at Visual Edge Software in 1991.
The Ising model is a model for a ferromagnetic material. Imagine a two-dimensional array of spins, each one of which can be either "up" or "down" and is free to flip between these two states. Suppose that it is energetically favourable for the spins to be aligned - ie. each spin prefers to be pointing in the same direction as its neigbours.
At high temperature, there is so much energy around that the spins don't much mind the extra energy-cost of being mis-aligned, but as the temperature drops, the energy-cost becomes more and more important. Thus at high temperature, the spins tend to a random configuration with little correlation between the orientation of neighbouring spins, but at low temperature, the spins tend to align themselves with their neighbours, resulting in domains where all the spins are "up" and domains where all the spins are "down".
In the simulation, we start out with a random configuration of spins and then let them evolve according to their own dynamics. Each spin is represented by one pixel on the screen, with the "up" spins shown as white and the "down" spins shown as black. In order to avoid problems with the boundaries of the lattice, we consider the left edge to be joined onto the right edge, and the top edge to be joined onto the bottom edge.
The sliders in the Ising window allow you to vary the temperature and ambient magnetic field while the simulation is in progress. The temperature is shown in units of the spin-spin interaction energy. Statistics on the update rates for the calculation ("model") and display ("view") are available in a drawer that can be opened via the View menu. (The update rates are shown in both frames per second (fps) and millions of spin updates per second (Msps)).
download the IsingCocoa executable (Requires OS X, version 10.3 or higher)
download the IsingCocoa project files (licensed under the GPL)
(Requires the Apple Developer Tools - I used XCode version 2.0)
Technical Points of Interest
- We take advantage of the fact that a spin has only two states (and hence can be stored in a single bit) by storing several spins in one long integer. The code to update these spins can then be efficiently expressed in terms of bitwise operations. This is referred to as multi-spin coding and I believe this idea came from Michael Creutz.
-
This project illustrates several aspects of programming
for Mac OS X using the Cocoa API:
- NSDocument architecture
- separate NSWindowController's for each type of window or sheet
- auxiliary information displayed in an NSDrawer
- the use of sheets for getting parameters from the user
- a toolbar with state-dependent icons
- interactive controls (e.g. a slider) in the toolbar
- calculation in a separate thread
- synchronizing use of shared variables via NSLock
- image drawn via NSBitmapImageRep in a subclass of NSView
- display updates done via NSTimer
- using NSEventTrackingRunLoopMode to get events while mouse is down
- maintaining aspect ratio of a view (as opposed to a window) via tricky code in the window resizing methods, and the use of performSelector:withObject:afterDelay to wrap up after the resizing has finished
- adding resizing methods to NSWindow via an Objective-C category
- the use of an Objective-C protocol in keeping multiple views in sync
- multi-spin coding technique to achieve a 32 times parallelism (32 = # of bits in an int)
- faster random numbers via inlining of BSD code for random()
- use of gcc's "-fast" flag on a per-file basis (in gcc 4.0 it is not supported for Objective-C code, so it was only used for the .c files)