-
Notifications
You must be signed in to change notification settings - Fork 0
/
ising-live.cpp
147 lines (123 loc) · 5.54 KB
/
ising-live.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
//
// Created by chris on 16.07.21.
//
#include "Simulation.h"
#include "utils.h"
void runIsingLive() {
SpinLattice2level sl(100);
// create arrays to save measurements to
std::vector<std::vector<float>> energyMetro;
std::vector<std::vector<float>> energyHB;
std::vector<std::vector<float>> energyWolff;
// Register the GUI
// You can specify the dimensions of the window
RuntimeGUI gui(1000, 1200);
gui.waitTime = 1;
gui.notify(sl);
const int numEnsembles = 1;
const int numSteps = 1000;
const float temp = 2.0f / std::log(1.0f + std::sqrt(2.0f));
// reserve memory
energyMetro.reserve(numEnsembles);
energyHB.reserve(numEnsembles);
energyWolff.reserve(numEnsembles);
/// Start simulation
for (int i = 0; i < numEnsembles; i++) {
sl.initCold();
energyMetro.push_back(std::vector<float>{(float) sl.calcEnergy()});
for (int j = 0; j < numSteps; ++j) {
metropolisSweep(sl, temp, 10);
if (j == 0)
energyMetro[i].reserve(numSteps);
energyMetro[i].push_back(sl.calcEnergy());
if (j % 20 == 0)
gui.notify(sl);
}
}
for (int i = 0; i < numEnsembles; i++) {
sl.initCold();
energyHB.push_back(std::vector<float>{(float) sl.calcEnergy()});
for (int j = 0; j < numSteps; ++j) {
heatBathSweep(sl, temp, 10);
if (j == 0)
energyHB[i].reserve(numSteps);
energyHB[i].push_back(sl.calcEnergy());
if (j % 20 == 0)
gui.notify(sl);
}
}
for (int i = 0; i < numEnsembles; i++) {
sl.initCold();
energyWolff.push_back(std::vector<float>{(float) sl.calcEnergy()});
for (int j = 0; j < numSteps; ++j) {
wolffSweep(sl, temp, 10);
if (j == 0)
energyWolff[i].reserve(numSteps);
energyWolff[i].push_back(sl.calcEnergy());
if (j % 20 == 0)
gui.notify(sl);
}
}
/// we can save a screenshot of the gui to disk (so you also can generate videos with ffmpeg)
//gui.saveImageToDisk("2levelIsing.png", "./results/");
/// ---------------------------------------------------------------------------------------------------------------
/// Plot and calculate measured energy
/// ---------------------------------------------------------------------------------------------------------------
auto axesEnergy = CvPlot::makePlotAxes();
for (size_t i = 0; i < energyMetro.size(); i++) {
if (i == 0) {
axesEnergy.create<CvPlot::Series>(energyMetro[i], "-g").setMarkerType(
CvPlot::MarkerType::Circle).setMarkerSize(
10).setLineType(CvPlot::LineType::None).setName("Metropolis (T=T_c)");
axesEnergy.create<CvPlot::Series>(energyHB[i], "-r").setMarkerType(
CvPlot::MarkerType::Circle).setMarkerSize(
10).setLineType(CvPlot::LineType::None).setName("HeatBath (T=T_c)");
axesEnergy.create<CvPlot::Series>(energyWolff[i], "-b").setMarkerType(
CvPlot::MarkerType::Circle).setMarkerSize(
10).setLineType(CvPlot::LineType::None).setName("Wolff (T=T_c)");
} else {
axesEnergy.create<CvPlot::Series>(energyMetro[i], "-g").setMarkerType(
CvPlot::MarkerType::Circle).setMarkerSize(
10).setLineType(CvPlot::LineType::None);
axesEnergy.create<CvPlot::Series>(energyHB[i], "-r").setMarkerType(
CvPlot::MarkerType::Circle).setMarkerSize(
10).setLineType(CvPlot::LineType::None);
axesEnergy.create<CvPlot::Series>(energyWolff[i], "-b").setMarkerType(
CvPlot::MarkerType::Circle).setMarkerSize(
10).setLineType(CvPlot::LineType::None);
}
}
axesEnergy.xLabel("simulation steps n").yLabel("normalized energies");
axesEnergy.title("Energies algorithms in 2D Ising");
axesEnergy.create<Legend>()._parentAxes = &axesEnergy;
CvPlot::show("plotPhysics", axesEnergy);
//
std::cout << "mean(metropolis)= " << mean(energyMetro) << std::endl;
std::cout << "mean(heatBath)= " << mean(energyHB) << std::endl;
std::cout << "mean(wolff)= " << mean(energyWolff) << std::endl;
/// ---------------------------------------------------------------------------------------------------------------
/// Calculate and plot autocorrelation
/// ---------------------------------------------------------------------------------------------------------------
auto axesAC = CvPlot::makePlotAxes();
for (int i = 0; i < numEnsembles; ++i) {
std::vector<float> ac_metro = autoCorr(energyMetro[i]);
normalize(ac_metro);
std::vector<float> ac_HB = autoCorr(energyHB[i]);
normalize(ac_HB);
std::vector<float> ac_wolff = autoCorr(energyWolff[i]);
normalize(ac_wolff);
if (i == 0) {
axesAC.create<CvPlot::Series>(ac_metro, "-g").setName("Metropolis (T=T_c)");
axesAC.create<CvPlot::Series>(ac_HB, "-r").setName("HeatBath (T=T_c)");
axesAC.create<CvPlot::Series>(ac_wolff, "-b").setName("Wolff (T=T_c)");
}
}
axesAC.xLabel("simulation steps n").yLabel("normalized autocorrelation");
axesAC.title("Autocorrelations algorithms in 2D Ising");
axesAC.create<Legend>()._parentAxes = &axesAC;
CvPlot::show("autocorr", axesAC);
cv::waitKey(0);
}
int main() {
runIsingLive();
}