-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyMc.h
332 lines (298 loc) · 11.3 KB
/
anyMc.h
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#ifndef ANYMC_H_
#define ANYMC_H_
#include "MyLattice.h"
#include <cmath>
namespace lat
{
static std::random_device __AnyMc_rd;
static std::default_random_engine __MP_e(__AnyMc_rd());
static std::default_random_engine __wolff_e(__AnyMc_rd());
static std::default_random_engine __wolff_ei(__AnyMc_rd());
#pragma region 声明坐标结构
struct position2d
{
int x;
int y;
};
struct position3d
{
int x;
int y;
int z;
};
#pragma endregion
#pragma region 声明函数
#pragma region 辅助函数
//生成等间距列表
std::vector<double> linspace(double a, double b, int num);
//数组求模取平均
double absmean(const std::vector<double> &);
//数组求平均
double mean(const std::vector<double> &vec);
#pragma endregion
#pragma region metropolis算法相关函数
template <typename Type>
bool MPAcceptOrNot(const position2d &p, const Lattice2D<Type> &model, const double beta, const Type &rvec);
template <typename Type>
void MetroPolisProcess(Lattice2D<Type> &model, const double beta);
#pragma endregion
#pragma region wolff算法相关函数
//计算是否接受
template <typename Type>
bool WolffAcceptOrNot(const position2d &p, const position2d &lastp, const Lattice2D<Type> &model, const double beta, const Type &rvec);
//递归地扩充集团
template <typename Type>
void recursiveExpand(const position2d &p, const position2d &lastp, Lattice2D<Type> &model, Lattice2D<int> &label, int &count, const double beta, const Type &rvec);
template <typename Type>
int WolffProcess(Lattice2D<Type> &model, const double beta);
#pragma endregion
#pragma region 具体系统相关的函数,需要用户自定义
//定义反射矢量选取方法
template <typename Type>
Type& chooseRvec(Type &); //用户自定义
//定义反射方法
template <typename Type>
Type reflect(const Type &orign, const Type &rvec); //用户自定义
//定义自旋对能量
template <typename Type>
double caculatePairEnergy(const Type &sigma1, const Type &sigma2); //用户自定义
//定义总磁矩
template <typename Type>
Type caculateMagnitude(const Lattice2D<Type> &model);
#pragma endregion
#pragma region 计算物理量
//计算总能量
template <typename Type>
double caculateEnergy(const Lattice2D<Type> &model);
//计算自旋能量
template <typename Type>
double caculateSpinEnergy(position2d p, const Lattice2D<Type> &model);
//给定反射参数计算试探自旋能量
template <typename Type>
double caculateSpinEnergy(position2d p, const Lattice2D<Type> &model, Type rvec);
//计算binder ratio
double caculateBiner(const std::vector<double> &Magnitude, int begin = 0, int end = -1);
#pragma endregion
#pragma endregion 声明函数
#pragma region 定义函数
#pragma region 预定义具体模型相关函数
template <typename Type>
Type& chooseRvec(Type &obj)
{
obj = Type();
return obj;
}
template <typename Type>
Type reflect(const Type &orign, const Type &rvec)
{
return orign - 2 * (orign * rvec) * rvec;
}
template <typename Type>
double caculatePairEnergy(const Type &sigma1, const Type &sigma2)
{
return -sigma1 * sigma2;
}
template <typename Type>
Type caculateMagnitude(const Lattice2D<Type> &model)
{
int scale = model.Scale();
Type Mag;
for (size_t x = 0; x < scale; x++)
{
for (size_t y = 0; y < scale; y++)
{
Mag += model.Get(x, y);
}
}
return Mag;
}
#pragma endregion
#pragma region 辅助函数
std::vector<double> linspace(double a, double b, int num)
{
double space = (b - a) / (num - 1);
std::vector<double> vec(num);
for (size_t i = 0; i < num; i++)
{
vec[i] = (a + i * space);
}
return vec;
}
double absmean(const std::vector<double> &vec)
{
double mean = 0;
for (auto i : vec)
{
mean += abs(i);
}
mean /= vec.size();
return mean;
}
double mean(const std::vector<double> &vec)
{
double mean = 0;
for (auto i : vec)
{
mean += i;
}
mean /= vec.size();
return mean;
}
double caculateBiner(const std::vector<double> &MeanMagnitude, int begin, int end)
{
if (end == -1)
{
end = MeanMagnitude.size();
}
double m1 = 0;
long double m2 = 0;
long double m4 = 0;
for (auto i = MeanMagnitude.begin() + begin; i < MeanMagnitude.begin() + end; i++)
{
double m = *i;
double mm = m * m;
m1 += m;
m2 += mm;
m4 += mm * mm;
}
int num = end - begin;
m1 /= num;
m2 /= num;
m4 /= num;
return 1.5 - m4 / (m2 * m2 * 2);
}
#pragma endregion
#pragma region Wolff算法相关
template <typename Type>
bool WolffAcceptOrNot(const position2d &p, const position2d &lastp, const Lattice2D<Type> &model, const double beta, const Type &rvec)
{
if (std::isinf(beta))
{
return true;
}
Type newone = model.Get(p.x, p.y);
Type lastone = model.Get(lastp.x, lastp.y);
double OldEnergy = caculatePairEnergy(newone, lastone);
double NewEnergy = caculatePairEnergy(reflect(newone, rvec), lastone);
double delta = -NewEnergy + OldEnergy;
double probability = exp(-beta * delta);
static std::uniform_real_distribution<double> __wolff_ur;
double randp = __wolff_ur(__wolff_e);
return (randp > probability);
}
template <typename Type>
void recursiveExpand(const position2d &p, const position2d &lastp, Lattice2D<Type> &model, Lattice2D<bool> &label, int &count, const double beta, const Type &rvec)
{
bool stat = label.Get(p.x, p.y);
if (!stat) //对本次扩张中未访问过的节点
{
if (count == 0 || WolffAcceptOrNot(p, lastp, model, beta, rvec))
{
//接受节点加入
label.Set(p.x, p.y, true);
count++;
//翻转节点
model.Set(p.x, p.y, reflect(model.Get(p.x, p.y), rvec));
//递归地向最近邻节点扩张
recursiveExpand(position2d{p.x + 1, p.y}, position2d{p.x, p.y}, model, label, count, beta, rvec);
recursiveExpand(position2d{p.x - 1, p.y}, position2d{p.x, p.y}, model, label, count, beta, rvec);
recursiveExpand(position2d{p.x, p.y + 1}, position2d{p.x, p.y}, model, label, count, beta, rvec);
recursiveExpand(position2d{p.x, p.y - 1}, position2d{p.x, p.y}, model, label, count, beta, rvec);
}
else //拒绝加入直接返回
{
return;
}
}
else //遇到已访问的节点则返回
{
return;
}
}
template <typename Type>
int WolffProcess(Lattice2D<Type> &model, const double beta)
{
int scale = model.Scale();
//选择随机反射矢量
Type rvec;
chooseRvec(rvec);
//选择随机种子
std::uniform_int_distribution<int> __wolff_uui(0, scale - 1);
position2d seed{__wolff_uui(__wolff_ei), __wolff_uui(__wolff_ei)};
//初始化晶格标记
Lattice2D<bool> label(scale, false);
//翻转选取结点
int count = 0;
recursiveExpand(seed, seed, model, label, count, beta, rvec);
return count;
}
#pragma endregion
#pragma region MetroPolis算法相关
template <typename Type>
bool MPAcceptOrNot(const position2d &p, const Lattice2D<Type> &model, const double beta, const Type &rvec)
{
if (std::isinf(beta))
{
return false;
}
double OldEnergy = caculateSpinEnergy(p, model);
double NewEnergy = caculateSpinEnergy(p, model, rvec);
double delta = NewEnergy - OldEnergy;
double probability = exp(-beta * delta);
static std::uniform_real_distribution<double> __MP_ur;
double randp = __MP_ur(__MP_e);
return (randp < probability);
}
template <typename Type>
void MetroPolisProcess(Lattice2D<Type> &model, const double beta)
{
int scale = model.Scale();
std::uniform_int_distribution<int> __MP_ui(0, scale - 1);
position2d seed{__MP_ui(__MP_e), __MP_ui(__MP_e)};
Type rvec;
chooseRvec(rvec);
if (MPAcceptOrNot(seed, model, beta, rvec))
{
model.Set(seed.x, seed.y, reflect(model.Get(seed.x, seed.y), rvec));
}
}
#pragma endregion
#pragma region 计算物理量
template <typename Type>
double caculateEnergy(const Lattice2D<Type> &model)
{
int scale = model.Scale();
double Energy = 0;
for (size_t x = 0; x < scale; x++)
{
for (size_t y = 0; y < scale; y++)
{
Energy += caculateSpinEnergy(position2d{x, y}, model);
}
}
return Energy / 2;
}
template <typename Type>
double caculateSpinEnergy(position2d p, const Lattice2D<Type> &model)
{
Type orign = model.Get(p.x, p.y);
double energy = (caculatePairEnergy(orign, model.Get(p.x - 1, p.y)) +
caculatePairEnergy(orign, model.Get(p.x + 1, p.y)) +
caculatePairEnergy(orign, model.Get(p.x, p.y + 1)) +
caculatePairEnergy(orign, model.Get(p.x, p.y - 1)));
return energy;
}
template <typename Type>
double caculateSpinEnergy(position2d p, const Lattice2D<Type> &model, Type rvec)
{
Type changed = reflect(model.Get(p.x, p.y), rvec);
double energy = (caculatePairEnergy(changed, model.Get(p.x - 1, p.y)) +
caculatePairEnergy(changed, model.Get(p.x + 1, p.y)) +
caculatePairEnergy(changed, model.Get(p.x, p.y + 1)) +
caculatePairEnergy(changed, model.Get(p.x, p.y - 1)));
return energy;
}
#pragma endregion
#pragma endregion
} // namespace lat
#endif