Skip to content

Instantly share code, notes, and snippets.

@skalarproduktraum
Created January 12, 2012 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skalarproduktraum/1600613 to your computer and use it in GitHub Desktop.
Save skalarproduktraum/1600613 to your computer and use it in GitHub Desktop.
1D Ising model with periodic boundary conditions on the BeagleBone's LEDs
var bb = require('./bonescript');
leds = [bone.USR0, bone.USR1, bone.USR2, bone.USR3];
stepDuration = 200;
arrayLength = 4;
coupling = 0.5;
temperature = 1.0;
setup = function() {
for(i = 0; i < arrayLength; i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], HIGH);
console.log("LED" + i + " is in output mode.");
}
};
isingStep = function(isingArray, coupling, temperature) {
var picked = Math.round(Math.random()*isingArray.length);
var pickedNext = picked+1;
// implement periodic boundary conditions
if(pickedNext >= isingArray.length) {
picked = 3;
pickedNext = 0;
}
var energy = coupling*isingArray[picked]*isingArray[pickedNext];
var energyNew = (-1)*coupling*isingArray[picked]*isingArray[pickedNext];
console.log("E=", energy, " E_new=", energyNew);
if(energyNew <= energy) {
isingArray[picked] = isingArray[picked]*(-1);
} else {
prob = Math.exp(-Math.abs(energy-energyNew));
console.log("exp(-\beta ∂E)=" + prob);
if(Math.random() >= prob) {
isingArray[picked] = isingArray[picked]*(-1);
}
}
return isingArray;
};
spinToLED = function(index, state) {
if(state == 1) {
digitalWrite(leds[index], HIGH);
} if(state == -1) {
digitalWrite(leds[index], LOW);
}
};
isingRun = function(arraySize, duration, coupling, temperature) {
arr = [];
for(i = 0; i<arraySize; i++) {
arr[i] = Math.round(Math.random());
if(arr[i] === 0) {
arr[i] = -1;
}
spinToLED(i, arr[i]);
}
console.log("initial config:", arr);
while(true) {
arr = isingStep(arr, coupling, temperature);
console.log(arr);
for(i = 0; i < arraySize; i++) {
spinToLED(i, arr[i]);
delay(duration);
}
}
};
setup();
isingRun(arrayLength, stepDuration, coupling, temperature);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment