-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCgrid.isotropy.hpp
75 lines (63 loc) · 1.77 KB
/
MCgrid.isotropy.hpp
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
// MCgrid.example.hpp
// Isotropic coarsening algorithm for 2D and 3D Monte Carlo methods
// Questions/comments to jgruber@andrew.cmu.edu (Jason Gruber)
#ifndef MCGRID_UPDATE
#define MCGRID_UPDATE
#include"MCgrid.hpp"
namespace MMSP{
void MCgrid2D::update(int steps)
{
MCgrid2D& grid = *this;
int n = nx[0]*nx[1];
const float kT = 0.75;
for (int step=0; step<steps; step++) {
for (int h=0; h<n; h++) {
int x = rand()%nx[0];
int y = rand()%nx[1];
int spin1 = grid[x][y];
std::vector<int> neighbors = nonzero(x,y);
int spin2 = neighbors[rand()%neighbors.size()];
if (spin1!=spin2) {
float dE = -1.0;
for (int i=-1; i<=1; i++)
for (int j=-1; j<=1; j++) {
int spin = grid.neighbor(x,y,i,j);
dE += (spin!=spin2)-(spin!=spin1);
}
float num = static_cast<float>(rand())/static_cast<float>(RAND_MAX);
if (dE<=0.0) grid[x][y] = spin2;
else if (dE>0.0 && num<exp(-dE/kT)) grid[x][y] = spin2;
}
}
}
}
void MCgrid3D::update(int steps)
{
MCgrid3D& grid = *this;
int n = nx[0]*nx[1]*nx[2];
const float kT = 1.5;
for (int step=0; step<steps; step++) {
for (int h=0; h<n; h++) {
int x = rand()%nx[0];
int y = rand()%nx[1];
int z = rand()%nx[2];
int spin1 = grid[x][y][z];
std::vector<int> neighbors = nonzero(x,y,z);
int spin2 = neighbors[rand()%neighbors.size()];
if (spin1!=spin2) {
float dE = -1.0;
for (int i=-1; i<=1; i++)
for (int j=-1; j<=1; j++)
for (int k=-1; k<=1; k++) {
int spin = grid.neighbor(x,y,z,i,j,k);
dE += (spin!=spin2)-(spin!=spin1);
}
float num = static_cast<float>(rand())/static_cast<float>(RAND_MAX);
if (dE<=0.0) grid[x][y][z] = spin2;
else if (dE>0.0 && num<exp(-dE/kT)) grid[x][y][z] = spin2;
}
}
}
}
} // namespace MMSP
#endif