From bd4f181a67aec822d1e420db2b12a10812b371ae Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Wed, 11 Dec 2024 11:36:39 +0100 Subject: Moved coordinate calculation to construction --- percolation.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'percolation.cpp') diff --git a/percolation.cpp b/percolation.cpp index a5c2a36..f012b07 100644 --- a/percolation.cpp +++ b/percolation.cpp @@ -26,6 +26,7 @@ class Vertex { public: std::vector bonds; unsigned index; + std::vector coordinates; }; class Cluster : public std::list> {}; @@ -60,8 +61,8 @@ public: unsigned squaredDistance(unsigned vi, unsigned vj) const { unsigned sd = 0; for (unsigned i = 0; i < D; i++) { - int x1 = (vi / uPow(L, i)) % uPow(L, i + 1); - int x2 = (vj / uPow(L, i)) % uPow(L, i + 1); + int x1 = vertices[vi].coordinates[i]; + int x2 = vertices[vj].coordinates[i]; unsigned Δx = abs(x1 - x2); if (Δx > L / 2) { Δx = L - Δx; @@ -72,11 +73,14 @@ public: } /* Initialize a square lattice */ - Graph(unsigned D, unsigned L) : D(D), L(L), vertices(uPow(L, D)), multiplicities(D * L * L / 4) { + 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); -- cgit v1.2.3-70-g09d2