This repository was archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuscep.cpp
More file actions
142 lines (118 loc) · 3.89 KB
/
suscep.cpp
File metadata and controls
142 lines (118 loc) · 3.89 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
#include <iostream>
#include <cmath>
#include <vector>
#include <cassert>
using namespace std;
using std::scientific;
// lattice size
const int dim = 2;
const int L = 3;
// needs to be known at compile time
const int vol = pow(L,dim);
const long numberOfConfigs = pow(2,vol);
// array which stores the neighbors of sites
int nbrAr[vol][dim];
// defines a class SpinConfig which stores and manipulates configurations;
// and some small functions
#include "spinConfClass.h"
// output data for a given beta, to be plotted
struct outData {
double tanhBeta;
double beta;
double energy;
double susc;
double suscEnergy;
};
//void printConfigData(configData);
//void printVector(const vector <int>);
//void printBoolAr(const bool[], const int);
// we label vectors of coordinates (x1, ..., xd) by an integer
vector <int> intToCoordinates(int);
int coordinatesToInt(vector <int>);
int main () {
// number of steps in tanh(beta)
int betaSteps = 100;
cout << "\nLattice dimensions: " << L << "^" << dim << " = " << vol << ", "
<< numberOfConfigs << " configurations.\n";
cout << "Number of bonds is " << vol*dim << ".\n\n";
// populate the array of neighbors
for(int x=0; x<vol; x++) {
vector <int> xCoords = intToCoordinates(x);
for(int d=0; d<dim; d++) {
auto newCoords = xCoords;
newCoords[d] = (newCoords[d]+1) % L;
nbrAr[x][d] = coordinatesToInt(newCoords);
}
}
cout << "Running through all configurations...\n";
// go through all configurations and compute observables
vector <configData> configDataAr;
for(int n=0; n<numberOfConfigs; n++) {
SpinConfig currentConfig(n);
auto cd = currentConfig.fetchData();
configDataAr.push_back(cd);
}
cout << endl;
// list of values of tanh(beta) for which we'll compute
double tanhBetaList[betaSteps];
{
double deltaTanhBeta = 1.0f/betaSteps;
for(int i=0; i< betaSteps; i++) tanhBetaList[i] = deltaTanhBeta*i;
}
cout << "Computing observables for a range of values of tanh(beta)...\n\n";
vector <outData> outputData;
cout << scientific; // formatting output
for(int i=0; i<betaSteps; i++) {
double tanhBeta = tanhBetaList[i];
double beta = atanh(tanhBeta);
outData od;
od.tanhBeta = tanhBeta, od.beta = beta;
// running tally of observables as we go through
// the list of configurations:
double Z = 0, energy = 0, susc = 0, suscEn = 0;
for(configData cd : configDataAr) {
double wt = exp(-beta*cd.energy); // Boltzmann weight
Z += wt;
energy += cd.energy * wt;
susc += cd.susc * wt;
suscEn += cd.suscEnergy * wt;
};
energy /= Z;
susc /= Z;
suscEn /= Z;
od.energy = energy;
od.susc = susc;
od.suscEnergy = susc*energy - suscEn;
outputData.push_back(od);
cout << od.tanhBeta << " " << od.beta << " " << od.energy << " " << od.susc << " " << od.suscEnergy << endl;
}
cout << "\nDone.\n\n";
}
// void printConfigData(configData cd) {
// cout << "[Energy, susceptibiltiy, mixed] = [" << cd.energy << ", " << cd.susc << ", " << cd.suscEnergy << "]\n";
// }
// void printVector(const vector <int>& v) {
// for(int p : v) std::cout << p << " ";
// std::cout << std::endl;
// }
vector <int> intToCoordinates(int n) {
assert(n >= 0 && n < vol);
vector <int> out = {};
while(n > 0) {
int tp = n % L;
out.push_back(tp);
n = (n-tp)/L;
}
while(out.size() < dim) out.push_back(0);
return out;
}
int coordinatesToInt(vector <int> v) {
int out = 0, mult = 1;
while(v.size() > 0) {
out += v.front()*mult;
v.erase(v.begin());
mult *= L;
}
assert(out >= 0 && out < vol);
return out;
}