-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid_Ising.java
More file actions
165 lines (120 loc) · 3.39 KB
/
Grid_Ising.java
File metadata and controls
165 lines (120 loc) · 3.39 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
package simulation;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
public class Grid_Ising extends Grid{
private LinkedList<Point> _neighbors;
public static final double _criticalTemperature = 2 / Math.log(1 + Math.sqrt(2));
public Grid_Ising(int D) {
_D = D;
_points = new double[D][D];
_neighbors = new LinkedList<Point>();
_neighbors.add(new Point(0,1));
_neighbors.add(new Point(0,-1));
_neighbors.add(new Point(1,0));
_neighbors.add(new Point(-1,0));
for(int i=0; i<D; i++){
for(int j=0; j<D; j++){
double P = Math.random();
if(P>.5){
this._points[i][j] = -1;
}else{
this._points[i][j] = 1;
}
}
}
}
//<><><><><><><><><><><><><><><><><> Begin Cluster Algorithm <><><><><><><><><><><><><><><><><><><>
private HashSet<Point> growChain(Point p0){
LinkedList<Point> newSites = new LinkedList<Point>(),
addedSites = new LinkedList<Point>();
HashSet<Point> chain = new HashSet<Point>();
chain.add(p0);
addedSites.add(p0);
Point t;
while(addedSites.size() > 0){
newSites.clear();
for (Point p : addedSites){
for(Point n : _neighbors){
t = new Point(p.x+n.x, p.y+n.y);
if (t.x >= 0
&& t.y >= 0
&& t.x < _D
&& t.y < _D
&& !chain.contains(t)
&& Math.random() < (1 - Math.exp(-2*_ß*this.getValue(t.x, t.y)*this.getValue(p.x, p.y))) ){
newSites.addFirst(t);
chain.add(t);
}
}
}
addedSites.clear();
addedSites.addAll(newSites);
}
return chain;
}
private void flipChain(HashSet<Point> chain){
for (Point p : chain){
this._points[p.x][p.y] *= -1;
}
}
public void clusterUpdate(){
this.flipChain(
this.growChain(
this.getRandomPosition()
)
);
}
//<><><><><><><><><><><><><><><><><><> End Cluster Algorithm <><><><><><><><><><><><><><><><><><><>
public void metropolisUpdate(){
Point p = this.getRandomPosition();
double Hi = this.getHamiltonian(),
Hf;
_points[p.x][p.y] *= -1;
Hf = this.getHamiltonian();
if (Math.random() > Math.min(1, Math.exp(_ß*(Hf-Hi) ) ) ){
_points[p.x][p.y] *= -1;
}
}
public double getHamiltonian(){
double Hamiltonian = 0;
for (int i=0; i<_D; i++){
for (int j=0; j<_D; j++){
if (i<_D-1){
Hamiltonian += _points[i][j]*_points[i+1][j];
}
if (j<_D-1){
Hamiltonian += _points[i][j]*_points[i][j+1];
}
}
}
return Hamiltonian;
}
public double getMagnetization(){
int M=0;
for (int x=0; x<_D; x++){
for (int y=0; y<_D; y++){
M+=this.getValue(x, y);
}
}
return M;
}
// Returns a 2x2 blocked copy of this grid
public Grid getBlockedGrid(){
Grid_Ising blockedGrid = new Grid_Ising(_D/2);
double value;
for (int i = 0; i<_D/2; i++){
for (int j = 0; j<_D/2; j++){
value = this.getValue(2*i, 2*j)
+this.getValue(2*i+1, 2*j)
+this.getValue(2*i, 2*j+1)
+this.getValue(2*i+1, 2*j+1);
if (value > 0) {value = 1;}
else if (value < 0) {value = -1;}
else {value = -1 + 2*Math.round(Math.random());}
blockedGrid.setValue(i, j, value);
}
}
return blockedGrid;
}
}