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 --- CMakeLists.txt | 4 + 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 +- src/wolff_O2.cpp | 278 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wolff_On.cpp | 2 +- src/wolff_cgm.cpp | 2 +- src/wolff_clock.cpp | 4 +- src/wolff_dgm.cpp | 4 +- src/wolff_ising.cpp | 4 +- src/wolff_potts.cpp | 4 +- 15 files changed, 464 insertions(+), 23 deletions(-) create mode 100644 lib/angle.h create mode 100644 lib/circle_group.h create mode 100644 lib/torus.h create mode 100644 src/wolff_O2.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fe564f7..12fe087 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ add_executable(wolff_4potts src/wolff_potts.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(wolff_7potts src/wolff_potts.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(wolff_3clock src/wolff_clock.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(wolff_5clock src/wolff_clock.cpp ${CPPSOURCES} ${CSOURCES}) +add_executable(wolff_planar2 src/wolff_O2.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(wolff_planar src/wolff_On.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(wolff_heisenberg src/wolff_On.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(analyze_correlations src/analyze_correlations.cpp ${CPPSOURCES} ${CSOURCES}) @@ -54,6 +55,7 @@ if (${GLUT} MATCHES "GLUT-NOTFOUND") target_link_libraries(wolff_5clock cblas gsl m) target_link_libraries(wolff_heisenberg cblas gsl m) target_link_libraries(wolff_planar cblas gsl m) + target_link_libraries(wolff_planar2 cblas gsl m) else() target_link_libraries(wolff_ising cblas gsl m glut GL GLU) target_link_libraries(wolff_dgm cblas gsl m glut GL GLU) @@ -65,6 +67,7 @@ else() target_link_libraries(wolff_5clock cblas gsl m glut GL GLU) target_link_libraries(wolff_heisenberg cblas gsl m glut GL GLU) target_link_libraries(wolff_planar cblas gsl m glut GL GLU) + target_link_libraries(wolff_planar2 cblas gsl m glut GL GLU) target_compile_definitions(wolff_ising PUBLIC HAVE_GLUT) target_compile_definitions(wolff_dgm PUBLIC HAVE_GLUT) target_compile_definitions(wolff_cgm PUBLIC HAVE_GLUT) @@ -74,6 +77,7 @@ else() target_compile_definitions(wolff_3clock PUBLIC HAVE_GLUT) target_compile_definitions(wolff_5clock PUBLIC HAVE_GLUT) target_compile_definitions(wolff_planar PUBLIC HAVE_GLUT) + target_compile_definitions(wolff_planar2 PUBLIC HAVE_GLUT) target_compile_definitions(wolff_heisenberg PUBLIC HAVE_GLUT) endif() 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)); } diff --git a/src/wolff_O2.cpp b/src/wolff_O2.cpp new file mode 100644 index 0000000..63ca0a7 --- /dev/null +++ b/src/wolff_O2.cpp @@ -0,0 +1,278 @@ + +#include +#include + +#ifdef HAVE_GLUT +#include +#endif + +#include +#include + +#include +#include +#include +#include + +typedef circle_group_t orthogonal_R_t; +typedef angle_t vector_R_t; +typedef state_t On_t; + +double H_modulated(vector_R_t v, int order, double mag) { + return mag * cos(order * v.x); +} + +double theta(double *v) { + double x = v[0]; + double y = v[1]; + + if (x == 0) { + if (y >= 0) { + return M_PI / 2; + } else { + return - M_PI / 2; + } + } else { + double val = atan(y / x); + + if (x < 0.0 && y > 0.0) { + return M_PI + val; + } else if ( x < 0.0 && y < 0.0 ) { + return - M_PI + val; + } else { + return val; + } + } +} + +int main(int argc, char *argv[]) { + + count_t N = (count_t)1e7; + + D_t D = 2; + L_t L = 128; + double T = 2.26918531421; + double *H_vec = (double *)calloc(MAX_Q, sizeof(double)); + + bool silent = false; + bool use_pert = false; + bool N_is_sweeps = false; + bool draw = false; + unsigned int window_size = 512; + + bool modulated_field = false; + unsigned int order = 1; + + int opt; + q_t H_ind = 0; + double epsilon = 1; + +// unsigned char measurement_flags = measurement_energy | measurement_clusterSize; + + unsigned char measurement_flags = 0; + + while ((opt = getopt(argc, argv, "N:D:L:T:H:spe:mo:M:Sdw:")) != -1) { + switch (opt) { + case 'N': // number of steps + N = (count_t)atof(optarg); + break; + case 'D': // dimension + D = atoi(optarg); + break; + case 'L': // linear size + L = atoi(optarg); + break; + case 'T': // temperature + T = atof(optarg); + break; + case 'H': // external field. nth call couples to state n + H_vec[H_ind] = atof(optarg); + H_ind++; + break; + case 's': // don't print anything during simulation. speeds up slightly + silent = true; + break; + case 'p': + use_pert = true; + break; + case 'e': + epsilon = atof(optarg); + break; + case 'm': + modulated_field = true; + break; + case 'M': + measurement_flags ^= 1 << atoi(optarg); + break; + case 'o': + order = atoi(optarg); + break; + case 'S': + N_is_sweeps = true; + break; + case 'd': +#ifdef HAVE_GLUT + draw = true; + break; +#else + printf("You didn't compile this with the glut library installed!\n"); + exit(EXIT_FAILURE); +#endif + case 'w': + window_size = atoi(optarg); + break; + default: + exit(EXIT_FAILURE); + } + } + + unsigned long timestamp; + + { + struct timespec spec; + clock_gettime(CLOCK_REALTIME, &spec); + timestamp = spec.tv_sec*1000000000LL + spec.tv_nsec; + } + + const char *pert_type; + + std::function gen_R; + + if (use_pert) { + gen_R = [=] (gsl_rng *r, const angle_t& t) -> circle_group_t { + circle_group_t rot; + rot.is_reflection = true; + + unsigned int x = gsl_rng_uniform_int(r, order); + double amount = epsilon * gsl_ran_ugaussian(r); + + rot.x = fmod(2 * M_PI * (1.0 + (double)x / (double)order + amount), 2 * M_PI); + + return rot; + }; + pert_type = "PERTURB"; + } else { + gen_R = [=] (gsl_rng *r, const angle_t& t) -> circle_group_t { + circle_group_t rot; + rot.is_reflection = true; + rot.x = 2 * M_PI * gsl_rng_uniform(r); + + return rot; + }; + pert_type = "UNIFORM"; + } + + FILE *outfile_info = fopen("wolff_metadata.txt", "a"); + + fprintf(outfile_info, "<| \"ID\" -> %lu, \"MODEL\" -> \"%s\", \"q\" -> %d, \"D\" -> %" PRID ", \"L\" -> %" PRIL ", \"NV\" -> %" PRIv ", \"NE\" -> %" PRIv ", \"T\" -> %.15f, \"FIELD_TYPE\" -> ", timestamp, ON_strings[2], 2, D, L, (v_t)pow(L, D), D * (v_t)pow(L, D), T); + if (modulated_field) { + fprintf(outfile_info, "\"MODULATED\", \"ORDER\" -> %d, \"H\" -> %.15f, ", order, H_vec[0]); + } else { + fprintf(outfile_info, "\"VECTOR\", \"H\" -> {"); + for (q_t i = 0; i < 2; i++) { + fprintf(outfile_info, "%.15f", H_vec[i]); + if (i < 2 - 1) { + fprintf(outfile_info, ", "); + } + } + fprintf(outfile_info, "}, "); + } + + fprintf(outfile_info, "\"GENERATOR\" -> \"%s\"", pert_type); + + if (use_pert) { + fprintf(outfile_info, ", \"EPS\" -> %g", epsilon); + } + + fprintf(outfile_info, " |>\n"); + + fclose(outfile_info); + + FILE **outfiles = measure_setup_files(measurement_flags, timestamp); + + std::function other_f; + uint64_t sum_of_clusterSize = 0; + + if (N_is_sweeps) { + other_f = [&] (const On_t *s) { + sum_of_clusterSize += s->last_cluster_size; + }; + } else if (draw) { +#ifdef HAVE_GLUT + // initialize glut + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(window_size, window_size); + glutCreateWindow("wolff"); + glClearColor(0.0,0.0,0.0,0.0); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D(0.0, L, 0.0, L); + + other_f = [&] (const On_t *s) { + glClear(GL_COLOR_BUFFER_BIT); + for (v_t i = 0; i < pow(L, 2); i++) { + vector_R_t v_tmp = s->R.act_inverse(s->spins[i]); + double saturation = 0.7; + double value = 0.9; + double chroma = saturation * value; + glColor3f(chroma * hue_to_R(v_tmp.x) + (value - chroma), chroma * hue_to_G(v_tmp.x) + (value - chroma), chroma * hue_to_B(v_tmp.x) + (value - chroma)); + glRecti(i / L, i % L, (i / L) + 1, (i % L) + 1); + } + glFlush(); + }; +#endif + } else { + other_f = [] (const On_t *s) {}; + } + + std::function measurements = measure_function_write_files(measurement_flags, outfiles, other_f); + + std::function J = [] (const angle_t& t1, const angle_t& t2) -> double { + return cos(t1.x - t2.x); + }; + + std::function H; + + if (modulated_field) { + H = [=] (const angle_t& t) -> double { + return H_vec[0] * cos(order * t.x); + }; + } else { + double mag = 0; + for (q_t i = 0; i < 2; i++) { + mag += pow(H_vec[i], 2); + } + mag = sqrt(mag); + double t0 = theta(H_vec); + H = [=] (const angle_t& t) -> double { + return mag * cos(t0 + t.x); + }; + } + + // initialize random number generator + gsl_rng *r = gsl_rng_alloc(gsl_rng_taus2); + gsl_rng_set(r, rand_seed()); + + state_t s(D, L, T, J, H); + + if (N_is_sweeps) { + count_t N_rounds = 0; + printf("\n"); + while (sum_of_clusterSize < N * s.nv) { + printf("\033[F\033[J\033[F\033[JWOLFF: sweep %" PRIu64 " / %" PRIu64 ": E = %.2f, S = %" PRIv "\n", (count_t)((double)sum_of_clusterSize / (double)s.nv), N, s.E, s.last_cluster_size); + wolff (N, &s, gen_R, measurements, r, silent); + N_rounds++; + } + printf("\033[F\033[J\033[F\033[JWOLFF: sweep %" PRIu64 " / %" PRIu64 ": E = %.2f, S = %" PRIv "\n\n", (count_t)((double)sum_of_clusterSize / (double)s.nv), N, s.E, s.last_cluster_size); + } else { + wolff (N, &s, gen_R, measurements, r, silent); + } + + measure_free_files(measurement_flags, outfiles); + free(H_vec); + gsl_rng_free(r); + + return 0; +} + diff --git a/src/wolff_On.cpp b/src/wolff_On.cpp index 5458860..cbde498 100644 --- a/src/wolff_On.cpp +++ b/src/wolff_On.cpp @@ -206,7 +206,7 @@ int main(int argc, char *argv[]) { std::function measurements = measure_function_write_files(measurement_flags, outfiles, other_f); - std::function H; + std::function H; if (modulated_field) { H = std::bind(H_modulated, std::placeholders::_1, order, H_vec[0]); diff --git a/src/wolff_cgm.cpp b/src/wolff_cgm.cpp index 7613691..ec3ae36 100644 --- a/src/wolff_cgm.cpp +++ b/src/wolff_cgm.cpp @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) { gsl_rng_set(r, rand_seed()); // define spin-spin coupling - std::function , height_t)> Z = [] (height_t h1, height_t h2) -> double { + std::function &, const height_t&)> Z = [] (const height_t& h1, const height_t& h2) -> double { return -pow(h1.x - h2.x, 2); }; diff --git a/src/wolff_clock.cpp b/src/wolff_clock.cpp index bc2c5d1..376eaec 100644 --- a/src/wolff_clock.cpp +++ b/src/wolff_clock.cpp @@ -78,12 +78,12 @@ int main(int argc, char *argv[]) { gsl_rng_set(r, rand_seed()); // define spin-spin coupling - std::function , potts_t)> Z = [] (potts_t s1, potts_t s2) -> double { + std::function &, const potts_t&)> Z = [] (const potts_t& s1, const potts_t& s2) -> double { return cos(2 * M_PI * (double)(s1.x + POTTSQ - s2.x) / (double)POTTSQ); }; // define spin-field coupling - std::function )> B = [=] (potts_t s) -> double { + std::function &)> B = [=] (const potts_t& s) -> double { return H_vec[s.x]; }; diff --git a/src/wolff_dgm.cpp b/src/wolff_dgm.cpp index 2583704..d00cae5 100644 --- a/src/wolff_dgm.cpp +++ b/src/wolff_dgm.cpp @@ -75,12 +75,12 @@ int main(int argc, char *argv[]) { gsl_rng_set(r, rand_seed()); // define spin-spin coupling - std::function , height_t)> Z = [] (height_t h1, height_t h2) -> double { + std::function &, const height_t&)> Z = [] (const height_t& h1, const height_t& h2) -> double { return -pow(h1.x - h2.x, 2); }; // define spin-field coupling - std::function )> B = [=] (height_t h) -> double { + std::function &)> B = [=] (const height_t& h) -> double { return -H * pow(h.x, 2);; }; diff --git a/src/wolff_ising.cpp b/src/wolff_ising.cpp index 410e046..0c9485d 100644 --- a/src/wolff_ising.cpp +++ b/src/wolff_ising.cpp @@ -99,7 +99,7 @@ int main(int argc, char *argv[]) { gsl_rng_set(r, rand_seed()); // define spin-spin coupling - std::function Z = [] (ising_t s1, ising_t s2) -> double { + std::function Z = [] (const ising_t& s1, const ising_t& s2) -> double { if (s1.x == s2.x) { return 1.0; } else { @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) { }; // define spin-field coupling - std::function B = [=] (ising_t s) -> double { + std::function B = [=] (const ising_t& s) -> double { if (s.x) { return -H; } else { diff --git a/src/wolff_potts.cpp b/src/wolff_potts.cpp index 07663d5..2bc306b 100644 --- a/src/wolff_potts.cpp +++ b/src/wolff_potts.cpp @@ -99,7 +99,7 @@ int main(int argc, char *argv[]) { gsl_rng_set(r, rand_seed()); // define spin-spin coupling - std::function , potts_t)> Z = [] (potts_t s1, potts_t s2) -> double { + std::function &, const potts_t&)> Z = [] (const potts_t& s1, const potts_t& s2) -> double { if (s1.x == s2.x) { return 1.0; } else { @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) { }; // define spin-field coupling - std::function )> B = [=] (potts_t s) -> double { + std::function &)> B = [=] (const potts_t& s) -> double { return H_vec[s.x]; }; -- cgit v1.2.3-70-g09d2