-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid_XY.java
More file actions
183 lines (137 loc) · 3.71 KB
/
Grid_XY.java
File metadata and controls
183 lines (137 loc) · 3.71 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
package simulation;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
public class Grid_XY extends Grid{
private LinkedList<Point> _neighbors;
//public static final double _criticalTemperature = TODO ;
public Grid_XY(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));
// Initialize lattice with random spins
for(int i=0; i<D; i++){
for(int j=0; j<D; j++){
_points[i][j] = Math.random()*2*Math.PI;
}
}
}
/*
* Grow spin chain as specified in the Wolff Cluster algorithm
*
*/
private HashSet<Point> growChain(Point p0, double Ø){
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(-4*_ß*Math.cos(_points[p.x][p.y]-_points[t.x][t.y]))) ){
newSites.addFirst(t);
chain.add(t);
}
}
}
addedSites.clear();
addedSites.addAll(newSites);
}
return chain;
}
/*
* Flips each spin variable in the chain about an axis specified by Ø
*
*/
private void flipChain(HashSet<Point> chain, double Ø){
double k;
for (Point p : chain){
k = Math.PI + 2*Ø - this._points[p.x][p.y];
while (k > 2*Math.PI){
k -= 2*Math.PI;
}
this._points[p.x][p.y] = k;
}
}
/*
* Uses the Cluster Algorithm to sample new spin configuration
*
*/
public void clusterUpdate(){
double Ø = Math.random()*2*Math.PI;
this.flipChain(
this.growChain(
this.getRandomPosition()
,Ø)
,Ø);
}
public void metropolisUpdate(){
// TODO
}
/*
* Computes Magnetization for the current spin configuration
*
*/
public double getMagnetization(){
double Mx=0,
My=0;
for (int x=0; x<_D; x++){
for (int y=0; y<_D; y++){
Mx +=Math.cos(this.getValue(x, y));
My +=Math.sin(this.getValue(x, y));
}
}
return Math.sqrt(Mx*Mx+My*My);
}
/*
* Computes the Hamiltonian for the current spin configuration
*
*/
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 += Math.cos(_points[i][j]-_points[i+1][j]);
}
if (j<_D-1){
Hamiltonian += Math.cos(_points[i][j]*_points[i][j+1]);
}
}
}
return Hamiltonian;
}
/*
* Returns a new Grid_XY object that is produced by applying a block transformation
* to this Grid_XY object
*
*/
public Grid getBlockedGrid(){
Grid_XY blockedGrid = new Grid_XY(_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)
)/4;
blockedGrid.setValue(i, j, value);
}
}
return blockedGrid;
}
}