I’ve already posted about NetLogo, but I want to return to it, because I’ve been very impressed with how ridiculously easy it is to construct sophisticated simulations of interesting models with it. My son Adam has been having a lot of fun with it, and he is able in the space of an hour or two, to create simulations of interesting models of his own devising, complete with easy-to-use GUI’s.

To illustrate how easy it is to build models, I’m going to use a familiar model that comes in the NetLogo library: the Ising Model of ferromagnetism on a square lattice, simulated using the Metropolis algorithm. (I simplified the code very slightly for clarity.)

In this model, there are spins at each node of a square lattice, that can point “up” or “down.” Spins like to have their neighbors point in the same direction. To compute the energy of a configuration of spins, we look at all pairs of neighboring spins, and add an energy of -1 if the two spins point in the same direction, and an energy of +1 if the two spins point in opposite directions. Boltzmann’s law tells us that each configuration should have a probability proportional to the exp(-*Energy[configuration] / T*), where T is the temperature.

The* Metropolis algorithm* is an algorithm for dynamically generating configurations of the model with the correct probability. One starts at some configuration, and picks a spin at random, and considers flipping it. If flipping it would reduce the energy or leave the energy unchanged, one makes the flip. Otherwise, one flips the spin anyways with a probability of exp(-E_{diff}/T), where E_{diff} is the amount that the energy will be increased by making the flip. Then pick another spin at random and continue.

It is not immediately obvious that the Metropolis algorithm generates configurations with probability given by Boltzmann’s Law, but one can prove that it does. The Metropolis algorithm is *not* the most efficient algorithm for generating samples from the correct probability distribution; much more efficient algorithms from that point of view are discussed in Werner Krauth’s book, which I previously reviewed.

There are many things to say about the Ising Model, but first let’s look at the NetLogo code for this model. It’s extremely short; the code (including the code controlling the GUI) is about the same length as the copyright notice that I’m appending because I only made small modifications to Uri Wilensky‘s code:

globals [sum-of-spins] patches-own [spin] to setup [initial-magnetization] clear-all ask patches [ ifelse initial-magnetization = 0 [set spin one-of [-1 1] ] [set spin initial-magnetization ] recolor ] set sum-of-spins sum [spin] of patches end to go ask one-of patches [ update ] tick if ticks mod 100 = 0 [ plotxy ticks (sum-of-spins / count patches) ] end to update let Ediff 2 * spin * sum [spin] of neighbors4 if (Ediff <= 0) or (temperature > 0 and (random-float 1.0 < exp ((- Ediff) / temperature))) [ set spin (- spin) set sum-of-spins sum-of-spins + 2 * spin recolor ] end to recolor ifelse spin = 1 [set pcolor blue + 2 ] [set pcolor blue - 2 ] end ; *** NetLogo 4.0beta5 Model Copyright Notice *** ; ; This model was created as part of the projects: ; PARTICIPATORY SIMULATIONS: NETWORK-BASED ; DESIGN FOR SYSTEMS LEARNING IN ; CLASSROOMS and/or INTEGRATED SIMULATION ; AND MODELING ENVIRONMENT. ; The project gratefully acknowledges the support of the ; National Science Foundation (REPP & ROLE programs) -- ; grant numbers REC #9814682 and REC-0126227. ; ; Copyright 2003 by Uri Wilensky. All rights reserved. ; ; Permission to use, modify or redistribute ; this model is hereby granted, ; provided that both of the following requirements ; are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit ; without permission from Uri Wilensky. ; Contact Uri Wilensky for appropriate licenses ; for redistribution for profit. ; ; To refer to this model in academic ; publications, please use: ; Wilensky, U. (2003). NetLogo Ising model. ; http://ccl.northwestern.edu/netlogo/models/Ising. ; Center for Connected Learning and Computer-Based Modeling, ; Northwestern University, Evanston, IL. ; ; In other publications, please use: ; Copyright 2003 Uri Wilensky. All rights reserved. ; See http://ccl.northwestern.edu/netlogo/models/Ising ; for terms of use. ; ; *** End of NetLogo 4.0beta5 Model Copyright Notice ***

In a future post (Edit: it’s here), I’ll step through this code and show you how it works (you do also need to click on a few buttons to set up the GUI). For now, though, I want to show you what an applet running this code looks like, and make some more comments about the Ising model that are best illustrated by running the applet. So please (assuming you have Java installed; and if you’re on Mac OS X Leopard, use Safari, there’s some problem when using Firefox) CONTINUE HERE.