-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathising_model2.html
More file actions
254 lines (215 loc) · 7.86 KB
/
Copy pathising_model2.html
File metadata and controls
254 lines (215 loc) · 7.86 KB
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
<!-- Author: Emma Poliakova 2020 -->
<!-- Ising model cellular automata simulation using html canvas -->
<!DOCTYPE html>
<html>
<head>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
.column {
float: left;
width: 515px;
}
.row:after {
content: "";
display: table;
clear: both;
}
</style>
</head>
<body onload='draw()'>
<div class="row1">
<div class="column">
<div>
<canvas id='canvas' width='505' height='505'></canvas>
</div>
<div id=buttons>
<button class="button" id="start" onclick="setUp()">Set Up</button>
<button class="button" id="play" onclick="play()">Play</button>
<button class="button" id="stop" onclick="stop()">Stop</button>
<button class="button" id="step" onclick="step()">Step</button>
</div>
<div class="slidecontainer">
<p>Temperature: <span id="temperature"></span></p>
<input type="range" min="0" max="500" value="0" class="slider" id="myRange">
</div>
</div>
<div class="column">
<div id='ising_plot'></div>
<div id='ising_plot2'></div>
</div>
</div>
<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var size = 202;
var length = 201;
var square_size = 2.5;
var cells = createArray(size, size);
var game;
var T = 0;
var x = 0;
var spin_min = 0;
var spin_plus = 0;
var counter = 1;
var iter_count = [];
var slider = document.getElementById("myRange");
var output = document.getElementById("temperature");
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value/100;
T = this.value/100;}
// canvas frame and initial set up
function draw() {
if (canvas.getContext) {
var context = canvas.getContext('2d');
context.strokeStyle='black';
context.moveTo(square_size, canvas.width-square_size);
context.lineTo(canvas.width-square_size, canvas.width-square_size);
context.moveTo(canvas.width-square_size, square_size);
context.lineTo(canvas.width-square_size, canvas.width-square_size);
context.moveTo(square_size, square_size);
context.lineTo(canvas.width-square_size, square_size);
context.moveTo(square_size, square_size);
context.lineTo(square_size,canvas.width-square_size);
context.stroke();
setUp();
drawGraph();
}
}
// create n x n array
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[length-1 - i] = createArray.apply(this, args);
}
return arr;
}
// generate and draw cells with +1 or -1 spin
function setUp(){
var spin;
for (var i=0 ;i<size; i+=1) {
cells[i][0] = 0;
cells[i][length] = 0;
}
for (var j=0 ; j<size; j+=1) {
cells[0][j] = 0;
cells[length][j] = 0;
}
for (var i=1 ; i<length; i+=1) {
for (var j=1 ;j<length; j+=1) {
spin = Math.floor(Math.random() * 2);
if (spin == 0){
spin_min = spin_min + 1;
cells[i][j] = -1;
context.beginPath();
context.fillStyle = "#40ade8";
context.fillRect(i*square_size, j*square_size, square_size, square_size); }
else {
spin_plus = spin_plus + 1;
cells[i][j] = 1;
context.beginPath();
context.fillStyle = "white";
context.fillRect(i*square_size, j*square_size, square_size, square_size);
}
}
}
drawGraph(spin_plus, spin_min);
}
// calculate energy for all cells in the array and draw new spins
function step(){
spin_plus = 0;
spin_min = 0;
iter_count.push(counter);
counter = counter + 1;
for (var i=1 ; i<length; i+=1) {
for (var j=1 ;j<length; j+=1) {
var E = -1*cells[i][j]*(cells[i-1][j] + cells[i+1][j] + cells[i][j-1] + cells[i][j+1]);
if (E>0){
cells[i][j] = -1 * cells[i][j];}
else {
var r = Math.floor(Math.random() * 1000)/1000;
if (r < Math.pow(Math.E, (2*E/T))){
cells[i][j] = -1 * cells[i][j];}
}
}
}
for (var i=1 ; i<length; i+=1) {
for (var j=1 ;j<length; j+=1) {
if (cells[i][j] == -1){
spin_min = spin_min + 1;
context.beginPath();
context.fillStyle = "#40ade8";
context.fillRect(i*square_size, j*square_size, square_size, square_size); }
else {
spin_plus = spin_plus + 1;
context.beginPath();
context.fillStyle = "white";
context.fillRect(i*square_size, j*square_size, square_size, square_size);}
}
}
//Plotly.extendTraces(ising_plot, {x : [[spin_plus]], y: [[spin_min]]}, [0])
Plotly.extendTraces(ising_plot2, {y: [[spin_plus], [spin_min]]}, [0, 1])
}
function drawGraph(x, y){
/**var trace1 = {
x: [x],
y: [y],
type: 'scatter',
name: 'Spin',
line: {
color: 'blue'}
};
var layout = {
height: 350,
width: 430,
title: 'Ratio of the two states',
xaxis: {
title: 'Positive state +1',
},
yaxis: {
title: 'Negative state -1',
}
};
*/
var trace2 = {
x: iter_count,
y: [y],
type: 'scatter',
name: 'State +1',
line: {
color: '#adadad'}
};
var trace3 = {
x: iter_count,
y: [y],
type: 'scatter',
name: 'State -1',
line: {
color: 'blue'}
};
var layout2 = {
height: 350,
width: 430,
title: 'Comparison of the two states',
xaxis: {
title: 'Time steps',
},
yaxis: {
title: 'Number of cells',
}
};
//Plotly.newPlot('ising_plot', [trace1], layout);
Plotly.newPlot('ising_plot2', [trace2, trace3], layout2);
};
function play(){
game = setInterval(step, 150);
}
function stop(){
clearInterval(game);
}
</script>
</body>
</html>