From 639552a2649139ba14363f30daa20786532b21b0 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Mon, 23 Jul 2018 13:51:13 -0400 Subject: implemented the discrete gaussian model for roughening --- CMakeLists.txt | 10 +++- lib/dihedral_inf.h | 78 ++++++++++++++++++++++++ lib/height.h | 75 +++++++++++++++-------- lib/ising.h | 1 + lib/orthogonal.h | 14 ++--- lib/state.h | 1 - lib/wolff.h | 4 +- src/wolff_On.cpp | 2 +- src/wolff_dgm.cpp | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wolff_ising.cpp | 2 +- src/wolff_potts.cpp | 20 +++---- 11 files changed, 327 insertions(+), 48 deletions(-) create mode 100644 lib/dihedral_inf.h create mode 100644 src/wolff_dgm.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5269c48..84c8fb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,15 @@ file(GLOB CPPSOURCES lib/*.cpp) add_executable(wolff_finite src/wolff_finite.c ${CSOURCES}) add_executable(wolff_ising src/wolff_ising.cpp ${CPPSOURCES} ${CSOURCES}) +add_executable(wolff_dgm src/wolff_dgm.cpp ${CPPSOURCES} ${CSOURCES}) add_executable(wolff_3potts src/wolff_potts.cpp ${CPPSOURCES} ${CSOURCES}) +add_executable(wolff_4potts src/wolff_potts.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}) SET_TARGET_PROPERTIES(wolff_3potts PROPERTIES COMPILE_FLAGS "-DPOTTSQ=3") +SET_TARGET_PROPERTIES(wolff_4potts PROPERTIES COMPILE_FLAGS "-DPOTTSQ=4") SET_TARGET_PROPERTIES(wolff_planar PROPERTIES COMPILE_FLAGS "-DN_COMP=2") SET_TARGET_PROPERTIES(wolff_heisenberg PROPERTIES COMPILE_FLAGS "-DN_COMP=3") @@ -37,19 +40,24 @@ target_link_libraries(wolff_finite cblas gsl m) target_link_libraries(analyze_correlations cblas gsl fftw3 m) if (${GLUT} MATCHES "GLUT-NOTFOUND") target_link_libraries(wolff_ising cblas gsl m) + target_link_libraries(wolff_dgm cblas gsl m) target_link_libraries(wolff_3potts cblas gsl m) target_link_libraries(wolff_heisenberg cblas gsl m) target_link_libraries(wolff_planar 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) target_link_libraries(wolff_3potts cblas gsl m glut GL GLU) + target_link_libraries(wolff_4potts 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_compile_definitions(wolff_ising PUBLIC HAVE_GLUT) + target_compile_definitions(wolff_dgm PUBLIC HAVE_GLUT) target_compile_definitions(wolff_3potts PUBLIC HAVE_GLUT) + target_compile_definitions(wolff_4potts PUBLIC HAVE_GLUT) target_compile_definitions(wolff_planar PUBLIC HAVE_GLUT) target_compile_definitions(wolff_heisenberg PUBLIC HAVE_GLUT) endif() -install(TARGETS wolff_finite wolff_ising wolff_3potts wolff_heisenberg wolff_planar analyze_correlations DESTINATION bin) +install(TARGETS wolff_finite wolff_ising wolff_dgm wolff_3potts wolff_heisenberg wolff_planar analyze_correlations DESTINATION bin) 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 +#include "height.h" + +template +struct dihedral_inf_t { bool is_reflection; T x; }; + +template +void init(dihedral_inf_t *ptr) { + ptr->is_reflection = false; + ptr->x = (T)0; +} + +template +dihedral_inf_t copy(dihedral_inf_t r) { + return r; +} + +template +void free_spin(dihedral_inf_t r) { + // do nothing! +} + +template +height_t act(dihedral_inf_t r, height_t h) { + height_t h2; + if (r.is_reflection) { + h2.x = r.x - h.x; + } else { + h2.x = r.x + h.x; + } + + return h2; +} + +template +dihedral_inf_t act(dihedral_inf_t r1, dihedral_inf_t r2) { + dihedral_inf_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 +height_t act_inverse(dihedral_inf_t r, height_t h) { + height_t h2; + if (r.is_reflection) { + h2.x = r.x - h.x; + } else { + h2.x = h.x - r.x; + } + + return h2; +} + +template +dihedral_inf_t act_inverse(dihedral_inf_t r1, dihedral_inf_t r2) { + dihedral_inf_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 +#include #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 -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 -void init(height_t *ptr) { +void init(height_t *ptr) { ptr->x = (T)0; } template -height_t copy (height_t h) { - return h; +void free_spin(height_t h) { + // do nothing! } template -void add (height_t *h1, height_t h2) { - h1->x += h2.x; +void free_spin(T h) { + // do nothing! } -template -void subtract (height_t *h1, height_T h2) { - h1->x -= h2.x; +void free_spin(double h) { + // do nothing! } template -height_t scalar_multiple(v_t a, height_t h) { - height_t hm; - hm.x = a * h.x; +height_t copy(height_t h) { + return h; +} - return hm; +template +void add(T *h1, int a, height_t h2) { + (*h1) += a * h2.x; } template -void free_spin (height_t h) { +void add(double *h1, double a, height_t h2) { + (*h1) += a * h2.x; } template -void write_magnetization(height_t M, FILE *outfile) { - fwrite(&(M.x), sizeof(T), 1, outfile); +T scalar_multiple(int factor, height_t h) { + return factor * h.x; } template -double correlation_component(height_t h) { - return (double)h.x; +double scalar_multiple(double factor, height_t h) { + return factor * h.x; } -// below here are not necessary for operation +double norm_squared(double h) { + return pow(h, 2); +} template -T dot(height_t h1, height_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 act_inverse (orthogonal_t m1, orthogonal_t m2) } template -orthogonal_t generate_rotation_uniform (gsl_rng *r, const state_t , vector_t > *s) { +orthogonal_t generate_rotation_uniform (gsl_rng *r, vector_t v) { orthogonal_t ptr; ptr.is_reflection = true; ptr.x = (double *)calloc(q, sizeof(double)); @@ -166,7 +166,7 @@ orthogonal_t generate_rotation_uniform (gsl_rng *r, const state_t -orthogonal_t generate_rotation_perturbation (gsl_rng *r, const state_t , vector_t > *s, double epsilon) { +orthogonal_t generate_rotation_perturbation (gsl_rng *r, vector_t v, double epsilon) { orthogonal_t m; vector_t tmp_v; m.is_reflection = true; @@ -179,15 +179,15 @@ orthogonal_t 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 generate_rotation_perturbation (gsl_rng *r, const state tmp_v.x[i] /= mag_v; } - vector_t 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 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 -void wolff(count_t N, state_t *s, std::function *)> gen_R, std::function *)> measurements, gsl_rng *r, bool silent) { +void wolff(count_t N, state_t *s, std::function gen_R, std::function *)> 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 (s, v0, step, r); free_spin(step); diff --git a/src/wolff_On.cpp b/src/wolff_On.cpp index a59876f..9f95f4a 100644 --- a/src/wolff_On.cpp +++ b/src/wolff_On.cpp @@ -125,7 +125,7 @@ int main(int argc, char *argv[]) { const char *pert_type; - std::function gen_R; + std::function gen_R; if (use_pert) { gen_R = std::bind(generate_rotation_perturbation , std::placeholders::_1, std::placeholders::_2, epsilon); diff --git a/src/wolff_dgm.cpp b/src/wolff_dgm.cpp new file mode 100644 index 0000000..6337a5e --- /dev/null +++ b/src/wolff_dgm.cpp @@ -0,0 +1,168 @@ + +#include + +#ifdef HAVE_GLUT +#include +#endif + +// include your group and spin space +#include +#include + +// include wolff.h +#include + +typedef state_t , height_t> sim_t; + +int main(int argc, char *argv[]) { + + count_t N = (count_t)1e4; + + D_t D = 2; + L_t L = 128; + double T = 2.26918531421; + double H = 0; + + bool silent = false; + bool draw = false; + unsigned int window_size = 512; + uint64_t epsilon = 1; + + int opt; + + while ((opt = getopt(argc, argv, "N:D:L:T:H:sdw:e:")) != -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 = atof(optarg); + break; + case 'e': // external field. nth call couples to state n + epsilon = atof(optarg); + break; + case 's': // don't print anything during simulation. speeds up slightly + silent = 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); + } + } + + // initialize random number generator + gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); + gsl_rng_set(r, rand_seed()); + + // define spin-spin coupling + std::function , height_t)> Z = [] (height_t h1, height_t h2) -> double { + return -pow(h1.x - h2.x, 2); + }; + + // define spin-field coupling + std::function )> B = [=] (height_t h) -> double { + return -H * pow(h.x, 2);; + }; + + // initialize state object + sim_t s(D, L, T, Z, B); + + // define function that generates self-inverse rotations + std::function (gsl_rng *, height_t)> gen_R = [=] (gsl_rng *r, height_t h) -> dihedral_inf_t { + dihedral_inf_t rot; + rot.is_reflection = true; + + int direction = gsl_rng_uniform_int(r, 2); + int64_t amount = gsl_rng_uniform_int(r, epsilon); + + if (direction == 0) { + rot.x = 2 * h.x + (1 + amount); + } else { + rot.x = 2 * h.x - (1 + amount); + } + + return rot; + }; + + // define function that updates any number of measurements + std::function measurement; + + double average_M = 0; + if (!draw) { + // a very simple example: measure the average magnetization + measurement = [&] (const sim_t *s) { + average_M += (double)s->M / (double)N / (double)s->nv; + }; + } else { + // a more complex example: measure the average magnetization, and draw the spin configuration to the screen + + // 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); + + measurement = [&] (const sim_t *s) { + average_M += (double)s->M / (double)N / (double)s->nv; + glClear(GL_COLOR_BUFFER_BIT); + int64_t max_h = INT64_MIN; + int64_t min_h = INT64_MAX; + for (v_t i = 0; i < pow(L, 2); i++) { + int64_t cur_h = act_inverse(s->R, s->spins[i]).x; + if (cur_h < min_h) { + min_h = cur_h; + } + if (cur_h > max_h) { + max_h = cur_h; + } + } + + for (v_t i = 0; i < pow(L, 2); i++) { + int64_t cur_h = act_inverse(s->R, s->spins[i]).x; + double mag = ((double)(cur_h - min_h)) / ((double)(max_h - min_h)); + glColor3f(mag, mag, mag); + glRecti(i / L, i % L, (i / L) + 1, (i % L) + 1); + } + glFlush(); + }; + } + + // run wolff for N cluster flips + wolff(N, &s, gen_R, measurement, r, silent); + + // tell us what we found! + printf("%" PRIcount " DGM runs completed. D = %" PRID ", L = %" PRIL ", T = %g, H = %g, = %g\n", N, D, L, T, H, average_M); + + // free the random number generator + gsl_rng_free(r); + + if (draw) { + } + + return 0; + +} + diff --git a/src/wolff_ising.cpp b/src/wolff_ising.cpp index 83b6448..e072d6a 100644 --- a/src/wolff_ising.cpp +++ b/src/wolff_ising.cpp @@ -88,7 +88,7 @@ int main(int argc, char *argv[]) { state_t s(D, L, T, Z, B); // define function that generates self-inverse rotations - std::function *)> gen_R = [] (gsl_rng *, const state_t *) -> z2_t { + std::function gen_R = [] (gsl_rng *, ising_t s) -> z2_t { z2_t rot; rot.x = true; return rot; diff --git a/src/wolff_potts.cpp b/src/wolff_potts.cpp index 3b55472..74e0ea9 100644 --- a/src/wolff_potts.cpp +++ b/src/wolff_potts.cpp @@ -90,21 +90,21 @@ int main(int argc, char *argv[]) { state_t , potts_t> s(D, L, T, Z, B); // define function that generates self-inverse rotations - std::function (gsl_rng *, const sim_t *)> gen_R = [] (gsl_rng *r, const sim_t *s) -> symmetric_t { + std::function (gsl_rng *, potts_t)> gen_R = [] (gsl_rng *r, potts_t v) -> symmetric_t { symmetric_t rot; init(&rot); - for (int i = POTTSQ - 1; i >= 0; i--) { - if (rot.perm[i] == i) { - q_t j = gsl_rng_uniform_int(r, i + 1); - if (rot.perm[j] == j) { - q_t tmp = rot.perm[i]; - rot.perm[i] = rot.perm[j]; - rot.perm[j] = tmp; - } - } + q_t j = gsl_rng_uniform_int(r, POTTSQ - 1); + q_t swap_v; + if (j < v.x) { + swap_v = j; + } else { + swap_v = j + 1; } + rot.perm[v.x] = swap_v; + rot.perm[swap_v] = v.x; + return rot; }; -- cgit v1.2.3-70-g09d2