From 1160baa61bad605cf8a1d583e8ae356a54a942df Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Thu, 26 Jul 2018 16:18:40 -0400 Subject: many changes, including new spin spaces and groups and cleaning up core library code --- lib/angle.h | 48 ++++++++++++++++++++++++++++++++++++++++ lib/circle_group.h | 46 +++++++++++++++++++++++++++++++++++++++ lib/cluster.h | 16 ++++++++------ lib/dihedral.h | 1 - lib/state.h | 6 ++--- lib/torus.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/vector.h | 4 ++-- 7 files changed, 172 insertions(+), 13 deletions(-) create mode 100644 lib/angle.h create mode 100644 lib/circle_group.h create mode 100644 lib/torus.h (limited to 'lib') diff --git a/lib/angle.h b/lib/angle.h new file mode 100644 index 0000000..c3f128e --- /dev/null +++ b/lib/angle.h @@ -0,0 +1,48 @@ +#pragma once + +#include "types.h" + +#include +#include "vector.h" + +class angle_t { + public: + double x; + + typedef vector_t<2, double> M_t; + typedef vector_t<2, double> F_t; + + angle_t() : x(0) {} + angle_t(double x) : x(x) {} + + inline vector_t<2, double> operator*(v_t a) const { + vector_t<2, double>M; + M[0] = a * cos(x); + M[1] = a * sin(x); + + return M; + } + + inline vector_t<2, double> operator*(double a) const { + vector_t<2, double>M; + M[0] = a * cos(x); + M[1] = a * sin(x); + + return M; + } +}; + +inline vector_t<2,double>& operator+=(vector_t<2,double>& M, const angle_t& theta) { + M[0] += cos(theta.x); + M[1] += sin(theta.x); + + return M; +} + +inline vector_t<2,double>& operator-=(vector_t<2,double>& M, const angle_t& theta) { + M[0] -= cos(theta.x); + M[1] -= sin(theta.x); + + return M; +} + diff --git a/lib/circle_group.h b/lib/circle_group.h new file mode 100644 index 0000000..cb7cadd --- /dev/null +++ b/lib/circle_group.h @@ -0,0 +1,46 @@ +#pragma once + +#include "angle.h" + +class circle_group_t { + public: + bool is_reflection; + double x; + + circle_group_t() : is_reflection(false), x(0) {} + circle_group_t(bool x, double y) : is_reflection(x), x(y) {} + + angle_t act(const angle_t& theta) const { + if (is_reflection) { + return angle_t(fmod(2 * M_PI + x - theta.x, 2 * M_PI)); + } else { + return angle_t(fmod(x + theta.x, 2 * M_PI)); + } + } + + circle_group_t act(const circle_group_t& r) const { + if (is_reflection) { + return circle_group_t(!r.is_reflection, fmod(2 * M_PI + x - r.x, 2 * M_PI)); + } else { + return circle_group_t(r.is_reflection, fmod(x + r.x, 2 * M_PI)); + } + } + + angle_t act_inverse(const angle_t& theta) const { + if (is_reflection) { + return act(theta); + } else { + return angle_t(fmod(2 * M_PI + theta.x - x, 2 * M_PI)); + } + } + + circle_group_t act_inverse(const circle_group_t& r) const { + if (is_reflection) { + return act(r); + } else { + return circle_group_t(r.is_reflection, fmod(2 * M_PI + r.x - x, 2 * M_PI)); + } + } +}; + + diff --git a/lib/cluster.h b/lib/cluster.h index c5f2be7..f948586 100644 --- a/lib/cluster.h +++ b/lib/cluster.h @@ -31,7 +31,9 @@ void flip_cluster(state_t *state, v_t v0, R_t r, gsl_rng *rand) { R_old = state->R; marks[v] = true; - if (v == state->nv) { + bool v_is_ghost = (v == state->nv); + + if (v_is_ghost) { R_new = r.act(R_old); } else { si_old = state->spins[v]; @@ -40,19 +42,18 @@ void flip_cluster(state_t *state, v_t v0, R_t r, gsl_rng *rand) { for (const v_t &vn : state->g.v_adj[v]) { X_t sj; + bool vn_is_ghost = (vn == state->nv); - if (vn != state->nv) { + if (!vn_is_ghost) { sj = state->spins[vn]; } double prob; - bool is_ext = (v == state->nv || vn == state->nv); - - if (is_ext) { + if (v_is_ghost || vn_is_ghost) { X_t rs_old, rs_new; v_t non_ghost; - if (vn == state->nv) { + if (vn_is_ghost) { rs_old = R_old.act_inverse(si_old); rs_new = R_old.act_inverse(si_new); non_ghost = v; @@ -61,6 +62,7 @@ void flip_cluster(state_t *state, v_t v0, R_t r, gsl_rng *rand) { rs_new = R_new.act_inverse(sj); non_ghost = vn; } + double dE = state->H(rs_old) - state->H(rs_new); #ifdef FINITE_STATES prob = H_probs[state_to_ind(rs_old)][state_to_ind(rs_new)]; @@ -97,7 +99,7 @@ void flip_cluster(state_t *state, v_t v0, R_t r, gsl_rng *rand) { } } - if (v == state->nv) { + if (v_is_ghost) { state->R = R_new; } else { state->spins[v] = si_new; diff --git a/lib/dihedral.h b/lib/dihedral.h index 5588afa..8d0472b 100644 --- a/lib/dihedral.h +++ b/lib/dihedral.h @@ -21,7 +21,6 @@ class dihedral_t { } } - dihedral_t act(dihedral_t r) const { if (this->is_reflection) { return dihedral_t(!(r.is_reflection), ((q + this->x) - r.x) % q); diff --git a/lib/state.h b/lib/state.h index eefa0cb..5abf65b 100644 --- a/lib/state.h +++ b/lib/state.h @@ -27,10 +27,10 @@ class state_t { std::vector precomputed_cos; std::vector precomputed_sin; - std::function J; - std::function H; + std::function J; + std::function H; - state_t(D_t D, L_t L, double T, std::function J, std::function H) : D(D), L(L), g(D, L), T(T), R(), J(J), H(H) { + state_t(D_t D, L_t L, double T, std::function J, std::function H) : D(D), L(L), g(D, L), T(T), R(), J(J), H(H) { nv = g.nv; ne = g.ne; g.add_ext(); diff --git a/lib/torus.h b/lib/torus.h new file mode 100644 index 0000000..2aead52 --- /dev/null +++ b/lib/torus.h @@ -0,0 +1,64 @@ + +#pragma once + +#include +#include +#include "types.h" + +template +class torus_t : public std::array { + public: + typedef std::array M_t; + typedef std::array F_t; + + torus_t() { + this->fill(0); + } + + inline torus_t operator*(v_t a) const { + torus_t x; + for (q_t i = 0; i < n; i++) { + x[i] = a * (*this)[i]; + } + + return x; + } + + inline torus_t operator*(double a) const { + torus_t x; + for (q_t i = 0; i < n; i++) { + x[i] = a * (*this)[i]; + } + + return x; + } + + inline torus_t& operator+=(const torus_t& x) { + for (q_t i = 0; i < n; i++) { + (*this)[i] += x[i]; + } + } + + inline torus_t& operator-=(const torus_t& x) { + for (q_t i = 0; i < n; i++) { + (*this)[i] -= x[i]; + } + } +}; + +template +double norm_squared(const torus_t& x) { + double tmp = 0; + for (const double& xi : x) { + tmp += pow(xi, 2); + } + return tmp; +} + +void write_magnetization(const torus_t& x, FILE *outfile) { + for (const double& xi : x) { + float tmp_xi = (float)xi; + fwrite(&tmp_xi, sizeof(float), 1, outfile); + } +} + diff --git a/lib/vector.h b/lib/vector.h index beee1a7..2f4077a 100644 --- a/lib/vector.h +++ b/lib/vector.h @@ -109,7 +109,7 @@ void write_magnetization(vector_t M, FILE *outfile) { } template -T dot(vector_t v1, vector_t v2) { +T dot(const vector_t & v1, const vector_t & v2) { T prod = 0; for (q_t i = 0; i < q; i++) { @@ -120,7 +120,7 @@ T dot(vector_t v1, vector_t v2) { } template -double H_vector(vector_t v1, T *H) { +double H_vector(const vector_t & v1, T *H) { vector_t H_vec(H); return (double)(dot (v1, H_vec)); } -- cgit v1.2.3-70-g09d2