diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dihedral_inf.h | 78 | ||||
-rw-r--r-- | lib/height.h | 75 | ||||
-rw-r--r-- | lib/ising.h | 1 | ||||
-rw-r--r-- | lib/orthogonal.h | 14 | ||||
-rw-r--r-- | lib/state.h | 1 | ||||
-rw-r--r-- | lib/wolff.h | 4 |
6 files changed, 138 insertions, 35 deletions
diff --git a/lib/dihedral_inf.h b/lib/dihedral_inf.h new file mode 100644 index 0000000..f7c4297 --- /dev/null +++ b/lib/dihedral_inf.h @@ -0,0 +1,78 @@ + +#include "types.h" +#include <cmath> +#include "height.h" + +template <class T> +struct dihedral_inf_t { bool is_reflection; T x; }; + +template <class T> +void init(dihedral_inf_t<T> *ptr) { + ptr->is_reflection = false; + ptr->x = (T)0; +} + +template <class T> +dihedral_inf_t<T> copy(dihedral_inf_t<T> r) { + return r; +} + +template <class T> +void free_spin(dihedral_inf_t<T> r) { + // do nothing! +} + +template <class T> +height_t<T> act(dihedral_inf_t<T> r, height_t<T> h) { + height_t<T> h2; + if (r.is_reflection) { + h2.x = r.x - h.x; + } else { + h2.x = r.x + h.x; + } + + return h2; +} + +template <class T> +dihedral_inf_t<T> act(dihedral_inf_t<T> r1, dihedral_inf_t<T> r2) { + dihedral_inf_t<T> r3; + + if (r1.is_reflection) { + r3.is_reflection = !(r2.is_reflection); + r3.x = r1.x - r2.x; + } else { + r3.is_reflection = r2.is_reflection; + r3.x = r1.x + r2.x; + } + + return r3; +} + +template <class T> +height_t<T> act_inverse(dihedral_inf_t<T> r, height_t<T> h) { + height_t<T> h2; + if (r.is_reflection) { + h2.x = r.x - h.x; + } else { + h2.x = h.x - r.x; + } + + return h2; +} + +template <class T> +dihedral_inf_t<T> act_inverse(dihedral_inf_t<T> r1, dihedral_inf_t<T> r2) { + dihedral_inf_t<T> r3; + + if (r1.is_reflection) { + r3.is_reflection = !(r2.is_reflection); + r3.x = r1.x - r2.x; + } else { + r3.is_reflection = r2.is_reflection; + r3.x = r2.x - r1.x; + } + + return r3; +} + diff --git a/lib/height.h b/lib/height.h index 16c4dc5..81d3a2d 100644 --- a/lib/height.h +++ b/lib/height.h @@ -2,61 +2,88 @@ #pragma once #include <cmath> +#include <stdio.h> #include "types.h" -// object definition +/* The following is the minimum definition of a spin class. + * + * The class must contain an M_t and an F_t for holding the sum of an + * integer number of spins and a double-weighted number of spins, + * respectively. + * + * void init(X_t *p); + * void free_spin(X_t p); + * void free_spin(M_t p); + * void free_spin(F_t p); + * X_t copy(X_t x); + * void add(M_t *x1, int factor, X_t x2); + * void add(F_t *x1, double factor, X_t x2); + * M_t scalar_multiple(int factor, X_t x); + * F_t scalar_multiple(double factor, X_t x); + * double norm_squared(F_t x); + * void write_magnetization(M_t M, FILE *outfile); + * + */ + template <class T> -struct height_t { T x; }; +struct height_t { + T x; + + typedef T M_t; + typedef double F_t; +}; -// init, copy, add, subtract, scalar_multiple, free_spin, and -// write_magnetization are necessary for the operation of wolff.h template <class T> -void init(height_t *ptr) { +void init(height_t<T> *ptr) { ptr->x = (T)0; } template <class T> -height_t <T> copy (height_t h) { - return h; +void free_spin(height_t<T> h) { + // do nothing! } template <class T> -void add (height_t <T> *h1, height_t <T> h2) { - h1->x += h2.x; +void free_spin(T h) { + // do nothing! } -template <class T> -void subtract (height_t <T> *h1, height_T <T> h2) { - h1->x -= h2.x; +void free_spin(double h) { + // do nothing! } template <class T> -height_t <T> scalar_multiple(v_t a, height_t <T> h) { - height_t <T> hm; - hm.x = a * h.x; +height_t<T> copy(height_t<T> h) { + return h; +} - return hm; +template <class T> +void add(T *h1, int a, height_t<T> h2) { + (*h1) += a * h2.x; } template <class T> -void free_spin (height_t <T> h) { +void add(double *h1, double a, height_t<T> h2) { + (*h1) += a * h2.x; } template <class T> -void write_magnetization(height_t <T> M, FILE *outfile) { - fwrite(&(M.x), sizeof(T), 1, outfile); +T scalar_multiple(int factor, height_t<T> h) { + return factor * h.x; } template <class T> -double correlation_component(height_t <T> h) { - return (double)h.x; +double scalar_multiple(double factor, height_t<T> h) { + return factor * h.x; } -// below here are not necessary for operation +double norm_squared(double h) { + return pow(h, 2); +} template <class T> -T dot(height_t <T> h1, height_t <T> h2) { - return (h1.x - h2.x) * (h1.x - h2.x); +void write_magnetization(T M, FILE *outfile) { + fwrite(&M, sizeof(T), 1, outfile); } diff --git a/lib/ising.h b/lib/ising.h index b4856c3..d956d88 100644 --- a/lib/ising.h +++ b/lib/ising.h @@ -19,6 +19,7 @@ * void add(M_t *x1, int factor, X_t x2); * void add(F_t *x1, double factor, X_t x2); * M_t scalar_multiple(int factor, X_t x); + * F_t scalar_multiple(double factor, X_t x); * double norm_squared(F_t x); * void write_magnetization(M_t M, FILE *outfile); * diff --git a/lib/orthogonal.h b/lib/orthogonal.h index 340ee2c..523fd54 100644 --- a/lib/orthogonal.h +++ b/lib/orthogonal.h @@ -144,7 +144,7 @@ orthogonal_t <q, T> act_inverse (orthogonal_t <q, T> m1, orthogonal_t <q, T> m2) } template <q_t q> -orthogonal_t <q, double> generate_rotation_uniform (gsl_rng *r, const state_t <orthogonal_t <q, double>, vector_t <q, double>> *s) { +orthogonal_t <q, double> generate_rotation_uniform (gsl_rng *r, vector_t <q, double> v) { orthogonal_t <q, double> ptr; ptr.is_reflection = true; ptr.x = (double *)calloc(q, sizeof(double)); @@ -166,7 +166,7 @@ orthogonal_t <q, double> generate_rotation_uniform (gsl_rng *r, const state_t <o } template <q_t q> -orthogonal_t <q, double> generate_rotation_perturbation (gsl_rng *r, const state_t <orthogonal_t <q, double>, vector_t <q, double>> *s, double epsilon) { +orthogonal_t <q, double> generate_rotation_perturbation (gsl_rng *r, vector_t <q, double> v, double epsilon) { orthogonal_t <q, double> m; vector_t <q, double> tmp_v; m.is_reflection = true; @@ -179,15 +179,15 @@ orthogonal_t <q, double> generate_rotation_perturbation (gsl_rng *r, const state for (q_t i = 0; i < q; i++) { tmp_v.x[i] = gsl_ran_ugaussian(r); - M2 += pow(s->M.x[i], 2); - tmpM += tmp_v.x[i] * s->M.x[i]; + M2 += pow(v.x[i], 2); + tmpM += tmp_v.x[i] * v.x[i]; } double v2 = 0; double factor = gsl_ran_ugaussian(r); for (q_t i = 0; i < q; i++) { - tmp_v.x[i] = (tmp_v.x[i] - tmpM * s->M.x[i] / M2) + epsilon * factor * s->M.x[i] / sqrt(M2); + tmp_v.x[i] = (tmp_v.x[i] - tmpM * v.x[i] / M2) + epsilon * factor * v.x[i] / sqrt(M2); v2 += tmp_v.x[i] * tmp_v.x[i]; } @@ -197,9 +197,7 @@ orthogonal_t <q, double> generate_rotation_perturbation (gsl_rng *r, const state tmp_v.x[i] /= mag_v; } - vector_t <q, double> v = act_inverse(s->R, tmp_v); - free(tmp_v.x); - m.x = v.x; + m.x = tmp_v.x; v2 = 0; diff --git a/lib/state.h b/lib/state.h index 8630810..3c6cafa 100644 --- a/lib/state.h +++ b/lib/state.h @@ -5,7 +5,6 @@ #include "types.h" #include "graph.h" -#include "ising.h" template <class R_t, class X_t> class state_t { diff --git a/lib/wolff.h b/lib/wolff.h index 4e93d01..8286024 100644 --- a/lib/wolff.h +++ b/lib/wolff.h @@ -3,14 +3,14 @@ #include "state.h" template <class R_t, class X_t> -void wolff(count_t N, state_t <R_t, X_t> *s, std::function <R_t(gsl_rng *, const state_t <R_t, X_t> *)> gen_R, std::function <void(const state_t <R_t, X_t> *)> measurements, gsl_rng *r, bool silent) { +void wolff(count_t N, state_t <R_t, X_t> *s, std::function <R_t(gsl_rng *, X_t s0)> gen_R, std::function <void(const state_t <R_t, X_t> *)> measurements, gsl_rng *r, bool silent) { if (!silent) printf("\n"); for (count_t steps = 0; steps < N; steps++) { if (!silent) printf("\033[F\033[JWOLFF: step %" PRIu64 " / %" PRIu64 ": E = %.2f, S = %" PRIv "\n", steps, N, s->E, s->last_cluster_size); v_t v0 = gsl_rng_uniform_int(r, s->nv); - R_t step = gen_R(r, s); + R_t step = gen_R(r, s->spins[v0]); flip_cluster <R_t, X_t> (s, v0, step, r); free_spin(step); |