diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2018-07-23 13:51:13 -0400 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2018-07-23 13:51:13 -0400 |
commit | 639552a2649139ba14363f30daa20786532b21b0 (patch) | |
tree | 624ef3537222183d5474d3a2d05a8bf09611d330 | |
parent | dd2c47db3512658858685c83dd772603203aaab1 (diff) | |
download | c++-639552a2649139ba14363f30daa20786532b21b0.tar.gz c++-639552a2649139ba14363f30daa20786532b21b0.tar.bz2 c++-639552a2649139ba14363f30daa20786532b21b0.zip |
implemented the discrete gaussian model for roughening
-rw-r--r-- | CMakeLists.txt | 10 | ||||
-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 | ||||
-rw-r--r-- | src/wolff_On.cpp | 2 | ||||
-rw-r--r-- | src/wolff_dgm.cpp | 168 | ||||
-rw-r--r-- | src/wolff_ising.cpp | 2 | ||||
-rw-r--r-- | src/wolff_potts.cpp | 20 |
11 files changed, 327 insertions, 48 deletions
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 <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); 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 <orthogonal_R_t(gsl_rng *, const On_t *)> gen_R; + std::function <orthogonal_R_t(gsl_rng *, vector_R_t)> gen_R; if (use_pert) { gen_R = std::bind(generate_rotation_perturbation <N_COMP>, 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 <getopt.h> + +#ifdef HAVE_GLUT +#include <GL/glut.h> +#endif + +// include your group and spin space +#include <dihedral_inf.h> +#include <height.h> + +// include wolff.h +#include <wolff.h> + +typedef state_t <dihedral_inf_t<int64_t>, height_t<int64_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 <double(height_t<int64_t>, height_t<int64_t>)> Z = [] (height_t<int64_t> h1, height_t<int64_t> h2) -> double { + return -pow(h1.x - h2.x, 2); + }; + + // define spin-field coupling + std::function <double(height_t<int64_t>)> B = [=] (height_t<int64_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 <dihedral_inf_t<int64_t>(gsl_rng *, height_t<int64_t>)> gen_R = [=] (gsl_rng *r, height_t<int64_t> h) -> dihedral_inf_t<int64_t> { + dihedral_inf_t<int64_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 <void(const sim_t *)> 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, <M> = %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 <z2_t, ising_t> s(D, L, T, Z, B); // define function that generates self-inverse rotations - std::function <z2_t(gsl_rng *, const state_t <z2_t, ising_t> *)> gen_R = [] (gsl_rng *, const state_t <z2_t, ising_t> *) -> z2_t { + std::function <z2_t(gsl_rng *, ising_t)> 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 <symmetric_t<POTTSQ>, potts_t<POTTSQ>> s(D, L, T, Z, B); // define function that generates self-inverse rotations - std::function <symmetric_t<POTTSQ>(gsl_rng *, const sim_t *)> gen_R = [] (gsl_rng *r, const sim_t *s) -> symmetric_t<POTTSQ> { + std::function <symmetric_t<POTTSQ>(gsl_rng *, potts_t<POTTSQ>)> gen_R = [] (gsl_rng *r, potts_t<POTTSQ> v) -> symmetric_t<POTTSQ> { symmetric_t<POTTSQ> 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; }; |