-
Notifications
You must be signed in to change notification settings - Fork 0
/
ising.js
493 lines (415 loc) · 11.6 KB
/
ising.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// some debugging function
let show_runtime = false;
function run_function_and_measure_runtime(func,name)
{
let t0 = performance.now();
func();
let t1 = performance.now();
if (show_runtime)
console.log("function " + name +" needed " + (t1-t0)+ " milliseconds");
}
// nicer display on retina
function retina(canv,cont,w,h)
{
if (window.devicePixelRatio){
canv
.attr('width', w * window.devicePixelRatio)
.attr('height', h * window.devicePixelRatio)
.style('width', w + 'px')
.style('height', h + 'px');
cont.scale(window.devicePixelRatio, window.devicePixelRatio);
}
}
function histogram(sizes)
{
let hist = {};
sizes.forEach(function(s){
if (!hist.hasOwnProperty(s))
hist[s] = 1;
else
hist[s]++;
});
let x = [];
let y = [];
Reflect.ownKeys(hist).forEach(function(key){
x.push(+key);
y.push(+hist[key]);
});
return { 'x': x, 'y': y };
}
// explorable definitions
//
var use_growing_occupation = true;
var sites = []; // one-dimensional array containing all sites (1 if occupied, 0 if not)
let pixel_width = 3; // width of one site in pixels
let flipped_spins = {};
let use_2d_fourier = false;
var local_field = "none";
let T = 2.0, // occupation probability
sidelength = 128; // how many sites per side of the square
let N = sidelength*sidelength; // total number of sites
let n_clusters = 0; // current number of clusters
let color = d3.scaleOrdinal(d3.schemeDark2); // colorscheme
let colors = d3.range(8).map(tmp => color(tmp));
colors[1] = d3.color(colors[1]).brighter().brighter().hex();
var width = sidelength*pixel_width, // canvas width
height = sidelength*pixel_width; // canvas height
var plot_width = width/2, plot_height=width/2;
var canvas = d3.select('#ising_container')
.append('canvas')
.attr('width', width)
.attr('height', height);
var ctx = canvas.node().getContext('2d');
var transform = d3.zoomIdentity;
retina(canvas,ctx,width,height);
// ============== ising functions ===============
/*
function initialize_ising_canvas() {
canvas
//.call(d3.drag().subject(dragsubject).on("drag", dragged))
.call(d3.zoom().scaleExtent([1, 8]).on("zoom", zoomed))
.call(draw);
}
*/
let mouseX = -1,
mouseY = -1;
canvas.on('mouseout', function () {
mouseX = -1;
mouseY = -1;
})
.on('mousemove', function () {
mouseX = d3.event.layerX || d3.event.offsetX;
mouseY = d3.event.layerY || d3.event.offsetY;
})
function zoomed() {
transform = d3.event.transform;
draw();
}
var maintimer;
var plottimer;
function start_ising() {
init();
maintimer = d3.timer(function(elapsed) {
update();
draw_updated();
//maintimer.stop();
});
plottimer = d3.interval(function(elapsed) {
analyze_magnetization();
plot_magnetization();
},100);
}
function stop_ising()
{
maintimer.stop();
plottimer.stop();
}
function reset()
{
//stop_ising();
T = 2;
m_x_minus.length = 0;
m_y_minus.length = 0;
m_x_plus.length = 0;
m_y_plus.length = 0;
reset_fourier();
init();
analyze_magnetization();
plot_magnetization();
//start_ising();
}
function update() {
flipped_spins = {};
for(let i=0; i<N; i++)
{
let I = Math.floor(Math.random()*N);
let s_xy = sites[I];
let pos = coords(I);
let x = pos[0];
let y = pos[1];
let S = 0;
for(let dr = -1; dr<2; dr += 2)
{
let _x = 0;
let _y = dr;
S += sites[index(x+_x,y+_y)];
_x = dr;
_y = 0;
S += sites[index(x+_x,y+_y)];
}
let dE = 2*s_xy*S;
if ((dE < 0) || (Math.random() < Math.exp(-dE/T)))
{
sites[I] *= -1;
flipped_spins[I] = { x:x, y:y, s: -s_xy}
}
}
if ((local_field != "none") && (mouseX>-1))
{
let x = Math.floor(mouseX/pixel_width),
y = Math.floor(mouseY/pixel_width);
let spin;
if (local_field == "positive")
spin = +1;
if (local_field == "negative")
spin = -1;
let radius = 5;
for (let _x = x - radius; _x <= x+radius; ++_x)
for (let _y = y - radius; _y <= y+radius; ++_y)
{
if (Math.sqrt(Math.pow(_x - x,2)+Math.pow(_y - y,2))<radius)
{
let I = index(_x,_y);
let pos = coords(I);
sites[I] = spin;
flipped_spins[I] = { x:pos[0], y:pos[1], s: spin}
}
}
}
}
function draw() {
ctx.save();
// delete all that was drawn
let bg_val = 0;
ctx.fillStyle = "rgb("+bg_val+","+bg_val+","+bg_val+")";
ctx.fillRect( 0, 0, width, height );
ctx.translate(transform.x, transform.y);
ctx.scale(transform.k, transform.k);
// draw the sites according to their clusters
for(let x = 0; x < sidelength; x++)
{
for(let y = 0; y < sidelength; y++)
{
ctx.fillStyle = colors[(sites[index(x,y)]+1)/2];
ctx.fillRect( x*pixel_width, y*pixel_width, pixel_width, pixel_width );
}
}
ctx.restore();
}
function draw_updated() {
ctx.save();
ctx.translate(transform.x, transform.y);
ctx.scale(transform.k, transform.k);
Reflect.ownKeys(flipped_spins).forEach(function (key) {
let site = flipped_spins[key];
ctx.fillStyle = colors[(site.s+1)/2];
ctx.fillRect( site.x*pixel_width, site.y*pixel_width, pixel_width, pixel_width );
});
ctx.restore();
}
function index(i,j){
if (i<0)
i += sidelength;
if (j<0)
j += sidelength;
return (i%sidelength) * sidelength + (j%sidelength);
}
function coords(i){
return [ Math.floor(i/sidelength), i%sidelength ];
}
function init(){
sites.length = 0;
let p = 0.95;
for(let i=0; i<sidelength; i++)
{
for(let j=0; j<sidelength; j++)
{
if (Math.random() < p)
sites.push(1);
else
sites.push(-1);
}
}
draw();
}
// ================= plot Magnetization ===========
//
//
var magnetization_canvas = d3.select('#magnetization_container')
.append('canvas')
.attr('width', plot_width)
.attr('height', plot_height);
var m_ctx = magnetization_canvas.node().getContext('2d');
retina(magnetization_canvas,m_ctx,plot_width,plot_height);
var m_pl = new simplePlot(m_ctx,plot_width,plot_height,{margin:30,fontsize:14,fastScatter:true});
m_pl.xlabel('temperature');
m_pl.ylabel('magnetization');
m_pl.xlimlabels(['1','5']);
m_pl.ylimlabels(['-1','+1']);
m_pl.xlim([1,5]);
m_pl.ylim([-1,1]);
var m_x_plus = [];
var m_y_plus = [];
var m_x_minus = [];
var m_y_minus = [];
var M;
function analyze_magnetization()
{
M = d3.mean(sites);
if (M >= 0)
{
m_x_plus.push(T);
m_y_plus.push(M);
}
else
{
m_x_minus.push(T);
m_y_minus.push(M);
}
}
function plot_magnetization()
{
let plus_color = colors[1];
//let plus_color = d3.color(colors[1]);
//plus_color.opacity = 0.3;
//plus_color = plus_color.toString();
let minus_color = colors[0];
//let minus_color = d3.color(colors[0]);
//minus_color.opacity = 0.3;
//minus_color = minus_color.toString();
if (m_x_plus.length > 0)
m_pl.scatter('plus',m_x_plus,m_y_plus,{marker:'s',markercolor:plus_color,markerradius:2},false);
if (m_x_minus.length > 0)
m_pl.scatter('minus',m_x_minus,m_y_minus,{marker:'s',markercolor:minus_color,markerradius:2},false);
m_pl.scatter('systemmarker',[T],[M],{marker:'o',markercolor:'rgba(255,255,255,0.0)', markerradius:10,markeredgewidth:1,markeredgecolor:'rgba(102,102,102,1.0)'},false);
m_pl.plot('zero',[1,5],[0,0])
}
// =================== FOURIER ANALYSIS 2D CORRELATION LENGTH =====================
//
let corr_length = [];
let corr_length_2d = [];
let mean_corr_length = d3.range(N).map(s => 0.0);
let fourier_measurements = 0;
let last_corr_lengths = [];
let max_measurements = 20;
function compute_correlation_length() {
let h_hat = [];
let dims = [ sidelength, sidelength ];
// transform
Fourier.transform(sites, h_hat);
// manipulate
for(let i=0; i<h_hat.length;++i)
{
h_hat[i].real = Math.pow(h_hat[i].real,2) + Math.pow(h_hat[i].imag,2);
h_hat[i].imag = 0.0;
}
// invert
corr_length_2d = [];
Fourier.invert(h_hat, corr_length_2d);
/*
//corr_length = x_corr_length.map(c => c);
for(let j=0; j<sidelength/2; ++j)
{
let initial = corr_length_2d[index(0,j)];
for(let i=sidelength-1; i>=0; --i)
{
//corr_length[index(i,j)] = x_corr_length[index(i,j)];
corr_length_2d[index(i+1,j)] = corr_length_2d[index(i,j)];
}
corr_length_2d[index(sidelength-1,j)] = initial;
}
*/
//console.log(corr_length);
//
//fourier_measurements += 1;
//console.log(mean_corr_length.length);
//mean_corr_length = corr_length.map(function(c, i) {
// return mean_corr_length[i]*(fourier_measurements-1)/fourier_measurements
// + c/fourier_measurements;
//});
}
function compute_correlation_length_1d() {
corr_length = d3.range(sidelength).map(tmp=>0);
for (var row=0; row<sidelength; row++)
{
for (var col=0; col<sidelength; col++)
corr_length[row] += corr_length_2d[index(row,col)]/sidelength;
}
// flip such that the circle is in the center
//corr_length = Fourier.halfshift(Fourier.halfshift(corr_length,dims),dims);
//corr_length = x_corr_length.map(c => c);
//let tmp = [];
//for(var i=sidelength/2; i<sidelength; ++i)
// tmp.push(corr_length[i]);
//for(var i=0; i<sidelength/2; ++i)
// tmp.push(corr_length[i]);
//corr_length = tmp;
corr_length = corr_length.slice(0,sidelength/2);
last_corr_lengths.push(corr_length);
if (last_corr_lengths.length > max_measurements)
last_corr_lengths = last_corr_lengths.slice(1,last_corr_lengths.length);
}
//console.log(corr_length);
function reset_fourier()
{
if (use_2d_fourier)
{
mean_corr_length = sites.map(s => 0.0);
fourier_measurements = 0;
}
else
{
last_corr_lengths.length = 0;
}
}
var corr_timer;
function start_fourier()
{
if (use_2d_fourier)
{
corr_timer = d3.interval(function(){
},250);
}
else
{
corr_timer = d3.interval(function(){
compute_correlation_length();
compute_correlation_length_1d();
draw_corr_1d();
//corr_timer.stop();
},100);
}
}
function stop_fourier()
{
corr_timer.stop();
}
if (use_2d_fourier)
{
var corr_canvas = d3.select('#correlation_container')
.append('canvas')
.attr('width', width)
.attr('height', height);
var corr_ctx = corr_canvas.node().getContext('2d');
retina(corr_canvas,corr_ctx,width,height);
start_fourier();
}
else
{
var corr_canvas = d3.select('#correlation_container')
.append('canvas')
.attr('width', plot_width)
.attr('height', plot_height)
;
var corr_ctx = corr_canvas.node().getContext('2d');
retina(corr_canvas,corr_ctx,plot_width,plot_height);
var corr_pl = new simplePlot(corr_ctx,plot_width,plot_height,{margin:30,fontsize:14});
//corr_pl.xscale("log");
//corr_pl.yscale("log");
corr_pl.ylimlabels(['','']);
corr_pl.xlabel('distance to neighbor');
corr_pl.ylabel('correlation');
corr_pl.xlimlabels(['0','L/2'])
start_fourier();
}
function draw_corr_1d() {
let y = corr_length.map(tmp => 0);
for (var row=0; row<last_corr_lengths.length; row++)
{
for (var col=0; col<sidelength; col++)
y[col] += last_corr_lengths[row][col]/last_corr_lengths.length;
}
let x = d3.range(sidelength/2);
corr_pl.ylim(d3.extent(y),false);
corr_pl.plot('corr',x,y,{linewidth:3,linecolor:'#888'});
}