diff options
-rw-r--r-- | lib/cluster.h | 10 | ||||
-rw-r--r-- | lib/graph.c | 67 | ||||
-rw-r--r-- | lib/graph.cpp | 37 | ||||
-rw-r--r-- | lib/graph.h | 27 | ||||
-rw-r--r-- | lib/state.h | 13 |
5 files changed, 55 insertions, 99 deletions
diff --git a/lib/cluster.h b/lib/cluster.h index 3261969..03b0ac0 100644 --- a/lib/cluster.h +++ b/lib/cluster.h @@ -36,11 +36,7 @@ void flip_cluster(state_t <R_t, X_t> *state, v_t v0, R_t r, gsl_rng *rand) { si_new = act (r, si_old); } - v_t nn = state->g->v_i[v + 1] - state->g->v_i[v]; - - for (v_t i = 0; i < nn; i++) { - v_t vn = state->g->v_adj[state->g->v_i[v] + i]; - + for (v_t vn : state->g.v_adj[v]) { X_t sj; if (vn != state->nv) { @@ -101,7 +97,7 @@ void flip_cluster(state_t <R_t, X_t> *state, v_t v0, R_t r, gsl_rng *rand) { } } - if (v == state->g->nv - 1) { + if (v == state->nv) { free_spin(state->R); state->R = R_new; } else { @@ -109,7 +105,7 @@ void flip_cluster(state_t <R_t, X_t> *state, v_t v0, R_t r, gsl_rng *rand) { state->spins[v] = si_new; } - if (v != state->g->nv - 1) { // count the number of non-external sites that flip + if (v != state->nv) { // count the number of non-external sites that flip nv++; } } diff --git a/lib/graph.c b/lib/graph.c deleted file mode 100644 index a1edf13..0000000 --- a/lib/graph.c +++ /dev/null @@ -1,67 +0,0 @@ - -#include "graph.h" - -graph_t *graph_create_square(D_t D, L_t L) { - v_t nv = pow(L, D); - v_t ne = D * nv; - - v_t *v_i = (v_t *)malloc((nv + 1) * sizeof(v_t)); - - for (v_t i = 0; i < nv + 1; i++) { - v_i[i] = 2 * D * i; - } - - v_t *v_adj = (v_t *)malloc(2 * D * nv * sizeof(v_t)); - - for (v_t i = 0; i < nv; i++) { - for (D_t j = 0; j < D; j++) { - v_adj[v_i[i] + 2 * j] = pow(L, j + 1) * (i / ((v_t)pow(L, j + 1))) + fmod(i + pow(L, j), pow(L, j + 1)); - v_adj[v_i[i] + 2 * j + 1] = pow(L, j + 1) * (i / ((v_t)pow(L, j + 1))) + fmod(pow(L, j+1) + i - pow(L, j), pow(L, j + 1)); - } - } - - graph_t *g = (graph_t *)malloc(sizeof(graph_t)); - - g->ne = ne; - g->nv = nv; - g->v_i = v_i; - g->v_adj = v_adj; - - return g; -} - -graph_t *graph_add_ext(const graph_t *G) { - graph_t *tG = (graph_t *)calloc(1, sizeof(graph_t)); - - tG->nv = G->nv + 1; - tG->ne = G->ne + G->nv; - - tG->v_i = (v_t *)malloc((tG->nv + 1) * sizeof(v_t)); - tG->v_adj = (v_t *)malloc(2 * tG->ne * sizeof(v_t)); - - for (v_t i = 0; i < G->nv + 1; i++) { - tG->v_i[i] = G->v_i[i] + i; - } - - tG->v_i[tG->nv] = 2 * tG->ne; - - for (v_t i = 0; i < G->nv; i++) { - v_t nn = G->v_i[i + 1] - G->v_i[i]; - - for (v_t j = 0; j < nn; j++) { - tG->v_adj[tG->v_i[i] + j] = G->v_adj[G->v_i[i] + j]; - } - - tG->v_adj[tG->v_i[i] + nn] = G->nv; - tG->v_adj[tG->v_i[G->nv] + i] = i; - } - - return tG; -} - -void graph_free(graph_t *g) { - free(g->v_i); - free(g->v_adj); - free(g); -} - diff --git a/lib/graph.cpp b/lib/graph.cpp new file mode 100644 index 0000000..021873d --- /dev/null +++ b/lib/graph.cpp @@ -0,0 +1,37 @@ + +#include "graph.h" + +graph_t::graph_t(D_t D, L_t L) { + nv = pow(L, D); + ne = D * nv; + + v_adj.resize(nv); + + for (std::vector<v_t> v_adj_i : v_adj) { + v_adj_i.reserve(2 * D); + } + + for (v_t i = 0; i < nv; i++) { + for (D_t j = 0; j < D; j++) { + v_adj[i].push_back(pow(L, j + 1) * (i / ((v_t)pow(L, j + 1))) + fmod(i + pow(L, j), pow(L, j + 1))); + v_adj[i].push_back(pow(L, j + 1) * (i / ((v_t)pow(L, j + 1))) + fmod(pow(L, j+1) + i - pow(L, j), pow(L, j + 1))); + } + } +} + +void graph_t::add_ext() { + for (std::vector<v_t> v_adj_i : v_adj) { + v_adj_i.push_back(nv); + } + + v_adj.resize(nv + 1); + v_adj[nv].reserve(nv); + + for (v_t i = 0; i < nv; i++) { + v_adj[nv].push_back(i); + } + + ne += nv; + nv += 1; +} + diff --git a/lib/graph.h b/lib/graph.h index beb7f4c..a4732fb 100644 --- a/lib/graph.h +++ b/lib/graph.h @@ -2,27 +2,20 @@ #pragma once #include <inttypes.h> -#include <math.h> +#include <cmath> #include <stdlib.h> +#include <vector> #include "types.h" -#ifdef __cplusplus -extern "C" { -#endif +class graph_t { + public: + v_t ne; + v_t nv; + std::vector<std::vector<v_t>> v_adj; -typedef struct { - v_t ne; - v_t nv; - v_t *v_i; - v_t *v_adj; -} graph_t; + graph_t(D_t D, L_t L); + void add_ext(); -graph_t *graph_create_square(D_t D, L_t L); -graph_t *graph_add_ext(const graph_t *G); -void graph_free(graph_t *h); - -#ifdef __cplusplus -} -#endif +}; diff --git a/lib/state.h b/lib/state.h index 3c6cafa..3cef157 100644 --- a/lib/state.h +++ b/lib/state.h @@ -13,7 +13,7 @@ class state_t { L_t L; v_t nv; v_t ne; - graph_t *g; + graph_t g; double T; X_t *spins; R_t R; @@ -29,12 +29,10 @@ class state_t { std::function <double(X_t, X_t)> J; std::function <double(X_t)> H; - state_t(D_t D, L_t L, double T, std::function <double(X_t, X_t)> J, std::function <double(X_t)> H) : D(D), L(L), T(T), J(J), H(H) { - graph_t *h = graph_create_square(D, L); - nv = h->nv; - ne = h->ne; - g = graph_add_ext(h); - graph_free(h); + state_t(D_t D, L_t L, double T, std::function <double(X_t, X_t)> J, std::function <double(X_t)> H) : D(D), L(L), T(T), J(J), H(H), g(D, L) { + nv = g.nv; + ne = g.ne; + g.add_ext(); spins = (X_t *)malloc(nv * sizeof(X_t)); for (v_t i = 0; i < nv; i++) { init (&(spins[i])); @@ -58,7 +56,6 @@ class state_t { } ~state_t() { - graph_free(g); for (v_t i = 0; i < nv; i++) { free_spin(spins[i]); } |