-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlattice.cpp
222 lines (200 loc) · 5.45 KB
/
lattice.cpp
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
#include "lattice.h"
Lattice::Lattice()
{
std::cout << "Generating lattice sites" << std::endl;
size = 100;
external_field = 0;
coupling = 1;
temperature = 1;
M = 0;
n_sites = pow(size, 2);
for (size_t i = 0; i < n_sites; i++)
{
if (rnd::random_double() > 0.5)
{
latticeMat.push_back(1);
M++;
}
else
{
latticeMat.push_back(-1);
M--;
}
}
calcEnergy();
}
Lattice::Lattice(int _size)
{
std::cout << "Generating lattice sites" << std::endl;
size = _size;
external_field = 0;
coupling = 1;
temperature = 1;
M = 0;
n_sites = pow(size, 2);
for (size_t i = 0; i < n_sites; i++)
{
if (rnd::random_double() > 0.5)
{
latticeMat.push_back(1);
M++;
}
else
{
latticeMat.push_back(-1);
M--;
}
}
calcEnergy();
}
Lattice::Lattice(int _size, float external_h, float J, float temp)
{
std::cout << "Generating lattice sites" << std::endl;
size = _size;
external_field = external_h;
coupling = J;
temperature = temp;
M = 0;
n_sites = pow(size, 2);
for (size_t i = 0; i < n_sites; i++)
{
if (rnd::random_double() > 0.5)
{
latticeMat.push_back(1);
M++;
}
else
{
latticeMat.push_back(-1);
M--;
}
}
calcEnergy();
}
/*public*/
/*gets*/
int Lattice::getSize(){return size;}
int Lattice::getNSites(){return n_sites;}
intVector Lattice::getLatticeMatrix(){return latticeMat;}
int Lattice::getValueFromCoordinates(int i, int j){return latticeMat[size * i + j];}
int Lattice::getMag(){return M;}
float Lattice::getEnergy(){return E;};
/*sets*/
void Lattice::setStorageOption(bool option){storeMat = option;}
/*prints*/
void Lattice::printLatticeMatrix()
{
for (size_t i = 0; i < size; i++)
{
for (size_t j = 0; j < size; i++)
{
printf("%d ", latticeMat[i * size + j]);
}
printf("\n");
}
}
/*Changes*/
void Lattice::analyseSite(int row, int col)
{
int new_row, new_col;
int deltaM, environmentSpins;
float contribution;
float delta_energy, probability_threshold, _switch;
float k_boltzmann = 1;
deltaM = -2 * latticeMat[row * size + col];
environmentSpins = (latticeMat[ising::periodic(row - 1, size) * size + col]
+ latticeMat[ising::periodic(row + 1, size) * size + col]
+ latticeMat[row * size + ising::periodic(col - 1, size)]
+ latticeMat[row * size + ising::periodic(col + 1, size)]);
environmentSpins *= -2 * latticeMat[row * size + col];
delta_energy = -coupling * environmentSpins - external_field * deltaM;
if (delta_energy <= 0 || rnd::random_float() < exp(- delta_energy / (k_boltzmann * temperature)))
{
latticeMat[row * size + col] *= -1;
M += deltaM;
E += delta_energy;
}
}
void Lattice::timeStep()
{
int j_idx, i_idx;
for (size_t i = 0; i < n_sites; i++)
{
i_idx = rnd::random_int(0, size);
j_idx = rnd::random_int(0, size);
analyseSite(i_idx,j_idx);
}
}
void Lattice::simulate(int cycles)
{
FILE *Mlog, *Elog;
std::string MatLog;
int count = 0;
// #pragma omp parallel for num_threads(8)
for (size_t cycle = 0; cycle <= cycles + 1; cycle++)
{
Lattice::timeStep();
count++;
// if (storeMat == true && (count % 500 == 0) || count == 1)
// {
// MatLog = "./data/matrix_T_" + std::to_string(temperature) + "_cycle_" + std::to_string(count) + ".dat";
// std::ofstream file(MatLog);
// for (size_t i=0; i<size; i++)
// {
// for (size_t j = 0; j < size; j++)
// {
// file << latticeMat[i* size + j] << " ";
// }
// file << "\n";
// }
// file.close();
// }
}
calcMag();
Mlog = fopen("./data/magnetisation.dat", "a+");
fprintf(Mlog, "%f \t %d\n", temperature, abs(M));
fclose(Mlog);
Elog = fopen("./data/energy.dat", "a+");
fprintf(Elog, "%f \t %f\n", temperature, E);
fclose(Elog);
if (storeMat == true)
{
MatLog = "./data/ising_T_" + std::to_string(temperature) + ".dat";
std::ofstream file(MatLog);
for (size_t i=0; i<size; i++)
{
for (size_t j = 0; j < size; j++)
{
file << latticeMat[i* size + j] << " ";
}
file << "\n";
}
file.close();
}
}
void Lattice::calcEnergy()
{
int nn_count = 0; // nearest neighbour count
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
{ if(latticeMat[i * size + j]==latticeMat[(i+1)* size + j])
nn_count++;
else
nn_count--;
if(latticeMat[i * size + j]==latticeMat[i * size + j + 1])
nn_count++;
else
nn_count--;
}
E = -coupling*nn_count - external_field*M;
}
void Lattice::calcMag()
{
M = 0;
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
{
M += latticeMat[i* size + j];
}
printf("Mag : %d\n", abs(M));
}