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
|
#include <stack>
#include <list>
#include <cstdlib>
#include <getopt.h>
#include "eigen/Eigen/Dense"
#include "randutils/randutils.hpp"
#include "pcg-cpp/include/pcg_random.hpp"
using Rng = randutils::random_generator<pcg32>;
using Real = double;
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;
using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
class Vertex;
class HalfEdge {
public:
unsigned index;
Vertex& neighbor;
HalfEdge(unsigned index, Vertex& v) : index(index), neighbor(v) {};
};
class Vertex {
public:
std::vector<HalfEdge> bonds;
unsigned index;
std::vector<unsigned> coordinates;
};
class Cluster : public std::list<std::reference_wrapper<const Vertex>> {};
unsigned uPow(unsigned x, unsigned a) {
unsigned result = 1;
while (a) {
if (a & 1) {
result *= x;
}
a >>= 1;
x *= x;
}
return result;
}
class Graph {
public:
unsigned D;
unsigned L;
std::vector<Vertex> vertices;
std::vector<unsigned> multiplicities;
unsigned Nv() const {
return vertices.size();
}
unsigned Ne() const {
return D * Nv();
}
unsigned squaredDistance(unsigned vi, unsigned vj) const {
unsigned sd = 0;
for (unsigned i = 0; i < D; i++) {
int x1 = vertices[vi].coordinates[i];
int x2 = vertices[vj].coordinates[i];
unsigned Δx = abs(x1 - x2);
if (Δx > L / 2) {
Δx = L - Δx;
}
sd += Δx * Δx;
}
return sd;
}
/* Initialize a square lattice */
Graph(unsigned D, unsigned L) : D(D), L(L), vertices(uPow(L, D)), multiplicities((D * L * L) / 4) {
for (unsigned i = 0; i < Nv(); i++) {
vertices[i].coordinates.resize(D);
vertices[i].index = i;
vertices[i].bonds.reserve(2 * D);
for (unsigned j = 0; j < D; j++) {
vertices[i].coordinates[j] = (i / uPow(L, j)) % L;
unsigned n1 = uPow(L, j + 1) * (i / uPow(L, j + 1)) + (i + uPow(L, j)) % uPow(L, j + 1);
unsigned n2 = uPow(L, j + 1) * (i / uPow(L, j + 1)) + (uPow(L, j + 1) + i - uPow(L, j)) % uPow(L, j + 1);
unsigned e1 = j * Nv() + i;
unsigned e2 = j * Nv() + n2;
HalfEdge he1(e1, vertices[n1]);
HalfEdge he2(e2, vertices[n2]);
vertices[i].bonds.push_back(he1);
vertices[i].bonds.push_back(he2);
}
}
for (const Vertex& v1 : vertices) {
for (const Vertex& v2 : vertices) {
unsigned dist = squaredDistance(v1.index, v2.index);
if (dist > 0) {
multiplicities[dist - 1]++;
}
}
}
}
std::vector<Cluster> markClusters(const std::vector<short unsigned>& activeEdges) const {
std::vector<Cluster> clusters;
std::vector<unsigned> indicies(Nv());
unsigned nClusters = 0;
for (const Vertex& v : vertices) {
if (indicies[v.index] == 0) {
nClusters++;
clusters.push_back({});
std::stack<std::reference_wrapper<const Vertex>> inCluster;
inCluster.push(v);
while (!inCluster.empty()) {
const Vertex& vn = inCluster.top();
inCluster.pop();
if (indicies[vn.index] == 0) {
indicies[vn.index] = nClusters;
clusters[nClusters - 1].push_back(vn);
}
for (const HalfEdge& e : vn.bonds) {
if (activeEdges[e.index] && indicies[e.neighbor.index] == 0) {
inCluster.push(e.neighbor);
}
}
}
}
}
return clusters;
}
Matrix laplacian(const std::vector<short unsigned>& activeEdges) const {
Matrix L = Matrix::Zero(Nv(), Nv());
for (const Vertex& v : vertices) {
for (const HalfEdge& e : v.bonds) {
if (activeEdges[e.index]) {
L(v.index, v.index) += 1;
L(v.index, e.neighbor.index) -= 1;
}
}
}
return L;
}
};
int main(int argc, char* argv[]) {
Rng r;
unsigned D = 2;
unsigned L = 8;
double p = 0.5;
unsigned n = 10;
int opt;
while ((opt = getopt(argc, argv, "D:L:p:n:")) != -1) {
switch (opt) {
case 'D':
D = atoi(optarg);
break;
case 'L':
L = atoi(optarg);
break;
case 'p':
p = atof(optarg);
break;
case 'n':
n = (unsigned)atof(optarg);
break;
default:
exit(1);
}
}
Graph G(D, L);
for (unsigned trials = 0; trials < n; trials++) {
std::vector<short unsigned> activeEdges(G.Ne());
for (short unsigned& edge : activeEdges) {
edge = r.uniform(0.0, 1.0) < p;
}
std::vector<Cluster> clusters = G.markClusters(activeEdges);
Matrix laplacian = G.laplacian(activeEdges);
std::vector<Real> conductivities(L * L * D / 4);
for (const Cluster& c : clusters) {
std::vector<unsigned> inds;
inds.reserve(c.size());
for (const Vertex& v : c){
inds.push_back(v.index);
}
Matrix subLaplacian = laplacian(inds, inds);
subLaplacian(0,0)++; /* Set voltage of first node to zero */
Matrix subLaplacianInv = subLaplacian.inverse();
for (unsigned i = 0; i < c.size(); i++) {
for (unsigned j = 0; j < i; j++) {
Vector input = Vector::Zero(inds.size());
input(i) = 1;
input(j) = -1;
Vector output = subLaplacianInv * input;
double ΔV = std::abs(output(i) - output(j));
conductivities[G.squaredDistance(inds[i], inds[j]) - 1] += 1 / ΔV;
}
}
}
for (unsigned i = 0; i < conductivities.size(); i++) {
if (G.multiplicities[i] != 0) {
std::cout << conductivities[i] / G.multiplicities[i] << " ";
} else {
std::cout << 0 << " ";
}
}
std::cout << std::endl;
}
return 0;
}
|