From 3eb67e3bca774eb0441db60158e1968ad901273b Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Thu, 28 Jun 2018 14:20:25 -0400 Subject: cleaned up the operation of the finite-group wolff code --- CMakeLists.txt | 6 +- lib/cluster_finite.h | 4 + lib/graph.h | 2 + lib/initial_finite.c | 211 ++++++++++++++++++++++ lib/initial_finite.h | 24 +++ lib/measurement.h | 5 - lib/wolff_finite.c | 70 -------- src/wolff_finite.c | 433 +++++++++++++++++++++++++++++++++++++++++++++ src/wolff_potts.c | 485 --------------------------------------------------- 9 files changed, 677 insertions(+), 563 deletions(-) create mode 100644 lib/initial_finite.c create mode 100644 lib/initial_finite.h delete mode 100644 lib/wolff_finite.c create mode 100644 src/wolff_finite.c delete mode 100644 src/wolff_potts.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ef18d5..4b46bcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ include_directories(lib ~/.local/include) link_directories(~/.local/lib) file(GLOB SOURCES lib/*.c) -add_executable(wolff_potts src/wolff_potts.c ${SOURCES}) +add_executable(wolff_finite src/wolff_finite.c ${SOURCES}) add_executable(wolff_vector src/wolff_vector.c ${SOURCES}) add_executable(wolff_dgm src/wolff_dgm.c ${SOURCES}) @@ -20,9 +20,9 @@ if (OPENMP_FOUND) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") endif() -target_link_libraries(wolff_potts gsl m cblas fftw3) +target_link_libraries(wolff_finite gsl m cblas fftw3) target_link_libraries(wolff_vector gsl m cblas fftw3) target_link_libraries(wolff_dgm gsl m cblas fftw3) -install(TARGETS wolff_potts wolff_vector wolff_dgm DESTINATION bin) +install(TARGETS wolff_finite wolff_vector wolff_dgm DESTINATION bin) diff --git a/lib/cluster_finite.h b/lib/cluster_finite.h index abdc8fc..ad45ed3 100644 --- a/lib/cluster_finite.h +++ b/lib/cluster_finite.h @@ -23,6 +23,10 @@ #include "yule_walker.h" typedef struct { + D_t D; + L_t L; + v_t nv; + v_t ne; graph_t *g; q_t q; R_t n_transformations; diff --git a/lib/graph.h b/lib/graph.h index 9c80dd6..cb47faa 100644 --- a/lib/graph.h +++ b/lib/graph.h @@ -1,4 +1,6 @@ +#pragma once + #include #include #include diff --git a/lib/initial_finite.c b/lib/initial_finite.c new file mode 100644 index 0000000..f286dcc --- /dev/null +++ b/lib/initial_finite.c @@ -0,0 +1,211 @@ + +#include "initial_finite.h" + +double *Jprobs_from_J(q_t q, double T, double *J) { + double *J_probs = (double *)calloc(pow(q, 2), sizeof(double)); + + for (q_t i = 0; i < q; i++) { + for (q_t j = 0; j < q; j++) { + J_probs[q * i + j] = 1.0 - exp((J[i] - J[j]) / T); + } + } + + return J_probs; +} + +q_t *initialize_R(q_t q) { + q_t *R = (q_t *)malloc(q * sizeof(q_t)); + + for (q_t i = 0; i < q; i++) { + R[i] = i; + } + + return R; +} + +state_finite_t *initial_finite_prepare_ising(D_t D, L_t L, double T, double *H) { + state_finite_t *s = (state_finite_t *)calloc(1, sizeof(state_finite_t)); + + s->D = D; + s->L = L; + + { + graph_t *g = graph_create_square(D, L); + s->nv = g->nv; + s->ne = g->ne; + s->g = graph_add_ext(g); + graph_free(g); + } + + s->q = 2; + s->n_transformations = 1; + + s->transformations = (q_t *)malloc(2 * sizeof(q_t)); + s->transformations[0] = 1; + s->transformations[1] = 0; + + s->T = T; + s->J = (double *)malloc(2 * sizeof(double)); + s->J[0] = 1.0; + s->J[1] = -1.0; + s->H = (double *)malloc(2 * sizeof(double)); + s->H[0] = H[0]; + s->H[1] = -H[0]; + + s->J_probs = Jprobs_from_J(2, T, s->J); + s->H_probs = Jprobs_from_J(2, T, s->H); + + s->spins = (q_t *)calloc(s->nv, sizeof(q_t)); + s->R = initialize_R(2); + + s->E = - ((double)s->ne) * s->J[0] - ((double)s->nv) * s->H[0]; + s->M = (v_t *)calloc(2, sizeof(v_t)); + s->M[0] = s->nv; // everyone starts in state 0, remember? + + return s; +} + +state_finite_t *initial_finite_prepare_potts(D_t D, L_t L, q_t q, double T, double *H) { + state_finite_t *s = (state_finite_t *)calloc(1, sizeof(state_finite_t)); + + s->D = D; + s->L = L; + + { + graph_t *g = graph_create_square(D, L); + s->nv = g->nv; + s->ne = g->ne; + s->g = graph_add_ext(g); + graph_free(g); + } + + s->q = q; + s->n_transformations = q; + s->transformations = dihedral_gen_transformations(q); + + s->T = T; + s->J = (double *)calloc(q, sizeof(double)); + s->J[0] = 1.0; + + s->H = (double *)malloc(q * sizeof(double)); + for (q_t i = 0; i < q; i++) { + s->H[i] = H[i]; + } + + s->J_probs = Jprobs_from_J(q, T, s->J); + s->H_probs = Jprobs_from_J(q, T, s->H); + + s->spins = (q_t *)calloc(s->nv, sizeof(q_t)); + s->R = initialize_R(q); + + s->E = - ((double)s->ne) * s->J[0] - ((double)s->nv) * s->H[0]; + s->M = (v_t *)calloc(q, sizeof(v_t)); + s->M[0] = s->nv; // everyone starts in state 0, remember? + + return s; +} + +state_finite_t *initial_finite_prepare_clock(D_t D, L_t L, q_t q, double T, double *H) { + state_finite_t *s = (state_finite_t *)calloc(1, sizeof(state_finite_t)); + + s->D = D; + s->L = L; + + { + graph_t *g = graph_create_square(D, L); + s->nv = g->nv; + s->ne = g->ne; + s->g = graph_add_ext(g); + graph_free(g); + } + + s->q = q; + s->n_transformations = q; + s->transformations = dihedral_gen_transformations(q); + + s->T = T; + s->J = (double *)malloc(q * sizeof(double)); + + for (q_t i = 0; i < q; i++) { + s->J[i] = cos(2 * M_PI * i / ((double)q)); + } + + + s->H = (double *)malloc(q * sizeof(double)); + for (q_t i = 0; i < q; i++) { + s->H[i] = H[i]; + } + + s->J_probs = Jprobs_from_J(q, T, s->J); + s->H_probs = Jprobs_from_J(q, T, s->H); + + s->spins = (q_t *)calloc(s->nv, sizeof(q_t)); + s->R = initialize_R(q); + + s->E = - ((double)s->ne) * s->J[0] - ((double)s->nv) * s->H[0]; + s->M = (v_t *)calloc(q, sizeof(v_t)); + s->M[0] = s->nv; // everyone starts in state 0, remember? + + return s; +} + + +state_finite_t *initial_finite_prepare_dgm(D_t D, L_t L, q_t q, double T, double *H) { + state_finite_t *s = (state_finite_t *)calloc(1, sizeof(state_finite_t)); + + s->D = D; + s->L = L; + + { + graph_t *g = graph_create_square(D, L); + s->nv = g->nv; + s->ne = g->ne; + s->g = graph_add_ext(g); + graph_free(g); + } + + s->q = q; + s->n_transformations = q; + s->transformations = dihedral_gen_transformations(q); + + s->T = T; + s->J = (double *)malloc(q * sizeof(double)); + + for (q_t i = 0; i < q / 2 + 1; i++) { + s->J[i] = -pow(i, 2); + } + for (q_t i = 1; i < (q + 1) / 2; i++) { + s->J[q - i] = -pow(i, 2); + } + + s->H = (double *)malloc(q * sizeof(double)); + for (q_t i = 0; i < q; i++) { + s->H[i] = H[i]; + } + + s->J_probs = Jprobs_from_J(q, T, s->J); + s->H_probs = Jprobs_from_J(q, T, s->H); + + s->spins = (q_t *)calloc(s->nv, sizeof(q_t)); + s->R = initialize_R(q); + + s->E = - ((double)s->ne) * s->J[0] - ((double)s->nv) * s->H[0]; + s->M = (v_t *)calloc(q, sizeof(v_t)); + s->M[0] = s->nv; // everyone starts in state 0, remember? + + return s; +} + +void state_finite_free(state_finite_t *s) { + graph_free(s->g); + free(s->J); + free(s->H); + free(s->J_probs); + free(s->H_probs); + free(s->spins); + free(s->R); + free(s->M); + free(s->transformations); + free(s); +} + diff --git a/lib/initial_finite.h b/lib/initial_finite.h new file mode 100644 index 0000000..65414cd --- /dev/null +++ b/lib/initial_finite.h @@ -0,0 +1,24 @@ + +#pragma once + +#include + +#include "types.h" +#include "dihedral.h" +#include "cluster_finite.h" + +typedef enum { + ISING, + POTTS, + CLOCK, + DGM +} finite_model_t; + +state_finite_t *initial_finite_prepare_ising(D_t D, L_t L, double T, double *H); +state_finite_t *initial_finite_prepare_potts(D_t D, L_t L, q_t q, double T, double *H); +state_finite_t *initial_finite_prepare_clock(D_t D, L_t L, q_t q, double T, double *H); +state_finite_t *initial_finite_prepare_dgm(D_t D, L_t L, q_t q, double T, double *H); + +void state_finite_free(state_finite_t *s); + + diff --git a/lib/measurement.h b/lib/measurement.h index 46c034f..eaa260b 100644 --- a/lib/measurement.h +++ b/lib/measurement.h @@ -24,11 +24,6 @@ typedef struct { double O2; } autocorr_t; -typedef struct { - void (*f)(state_finite_t *, void *); - void *data; -} measurement_t; - void meas_update(meas_t *m, double x); double meas_dx(const meas_t *m); diff --git a/lib/wolff_finite.c b/lib/wolff_finite.c deleted file mode 100644 index 64de9ba..0000000 --- a/lib/wolff_finite.c +++ /dev/null @@ -1,70 +0,0 @@ - -#include "cluster_finite.h" - -void wolff_finite(state_finite_t *s, count_t sweeps, count_t sweeps_per_measurement, count_t n_measurements, measurement_t *measurements) { - for (count_t i = 0; i < sweeps; i++) { - - count_t n_flips = 0; - - while (n_flips / h->nv < sweeps_per_measurement) { - v_t v0 = gsl_rng_uniform_int(r, h->nv); - R_t step; - - bool changed = false; - while (!changed) { - step = gsl_rng_uniform_int(r, s->n_transformations); - if v(symmetric_act(s->transformations + q * step, s->spins[v0]) != s->spins[v0]) { - changed = true; - } - } - - v_t tmp_flips = flip_cluster_finite(s, v0, step, r); - n_flips += tmp_flips; - - if (n_runs > 0) { - n_steps++; - meas_update(clust, tmp_flips); - - if (record_autocorrelation && n_steps % ac_skip == 0) { - update_autocorr(autocorr, s->E); - } - - } - - } - - for (q_t i = 0; i < q; i++) { - meas_update(M[i], s->M[i]); - } - meas_update(E, s->E); - - q_t n_at_max = 0; - q_t max_M_i = 0; - v_t max_M = 0; - - for (q_t i = 0; i < q; i++) { - if (s->M[i] > max_M) { - n_at_max = 1; - max_M_i = i; - max_M = s->M[i]; - } else if (s->M[i] == max_M) { - n_at_max++; - } - } - - if (record_distribution) { - mag_dist[s->M[0]]++; - } - - if (n_at_max == 1) { - for (q_t i = 0; i < q; i++) { - meas_update(sM[max_M_i][i], s->M[i]); - } - meas_update(sE[max_M_i], s->E); - freqs[max_M_i]++; - } - - diff = fabs(meas_dx(clust) / clust->x); - } -} - diff --git a/src/wolff_finite.c b/src/wolff_finite.c new file mode 100644 index 0000000..47fcc88 --- /dev/null +++ b/src/wolff_finite.c @@ -0,0 +1,433 @@ + +#include + +#include + +int main(int argc, char *argv[]) { + + L_t L = 128; + count_t N = (count_t)1e7; + count_t min_runs = 10; + count_t n = 3; + q_t q = 2; + D_t D = 2; + double T = 2.26918531421; + double *J = (double *)calloc(MAX_Q, sizeof(double)); + J[0] = 1.0; + double *H = (double *)calloc(MAX_Q, sizeof(double)); + double eps = 0; + bool silent = false; + bool snapshots = false; + bool snapshot = false; + bool record_autocorrelation = false; + bool record_distribution = false; + count_t W = 10; + count_t ac_skip = 1; + + finite_model_t model = ISING; + + int opt; + q_t J_ind = 0; + q_t H_ind = 0; + + while ((opt = getopt(argc, argv, "N:n:D:L:q:T:J:H:m:e:IpsSPak:W:drt:")) != -1) { + switch (opt) { + case 'N': + N = (count_t)atof(optarg); + break; + case 'n': + n = (count_t)atof(optarg); + break; + case 'D': + D = atoi(optarg); + break; + case 'L': + L = atoi(optarg); + break; + case 'q': + q = atoi(optarg); + break; + case 'T': + T = atof(optarg); + break; + case 'J': + J[J_ind] = atof(optarg); + J_ind++; + break; + case 'H': + H[H_ind] = atof(optarg); + H_ind++; + break; + case 'm': + min_runs = atoi(optarg); + break; + case 'e': + eps = atof(optarg); + break; + case 's': + silent = true; + break; + case 'S': + snapshots = true; + break; + case 'P': + snapshot = true; + break; + case 'a': + record_autocorrelation = true; + break; + case 'k': + ac_skip = (count_t)atof(optarg); + break; + case 'W': + W = (count_t)atof(optarg); + break; + case 'd': + record_distribution = true; + break; + case 't': + model = (finite_model_t)atoi(optarg); + break; + default: + exit(EXIT_FAILURE); + } + } + + state_finite_t *s; + + gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); + gsl_rng_set(r, rand_seed()); + + switch (model) { + case ISING: + s = initial_finite_prepare_ising(D, L, T, H); + break; + case POTTS: + s = initial_finite_prepare_potts(D, L, q, T, H); + break; + case CLOCK: + s = initial_finite_prepare_clock(D, L, q, T, H); + break; + case DGM: + s = initial_finite_prepare_dgm(D, L, q, T, H); + break; + default: + printf("Not a valid model!\n"); + return 1; + } + + free(J); + free(H); + + + double diff = 1e31; + count_t n_runs = 0; + count_t n_steps = 0; + + meas_t *E, *clust, **M, **sE, ***sM; + + M = (meas_t **)malloc(q * sizeof(meas_t *)); + for (q_t i = 0; i < q; i++) { + M[i] = (meas_t *)calloc(1, sizeof(meas_t)); + } + + E = calloc(1, sizeof(meas_t)); + clust = calloc(1, sizeof(meas_t)); + + sE = (meas_t **)malloc(q * sizeof(meas_t *)); + sM = (meas_t ***)malloc(q * sizeof(meas_t **)); + + for (q_t i = 0; i < q; i++) { + sE[i] = (meas_t *)calloc(1, sizeof(meas_t)); + sM[i] = (meas_t **)malloc(q * sizeof(meas_t *)); + for (q_t j = 0; j < q; j++) { + sM[i][j] = (meas_t *)calloc(1, sizeof(meas_t)); + } + } + + count_t *freqs = (count_t *)calloc(q, sizeof(count_t)); + q_t cur_M = 0; + + autocorr_t *autocorr; + if (record_autocorrelation) { + autocorr = (autocorr_t *)calloc(1, sizeof(autocorr_t)); + autocorr->W = 2 * W + 1; + autocorr->OO = (double *)calloc(2 * W + 1, sizeof(double)); + } + + count_t *mag_dist; + if (record_distribution) { + mag_dist = (count_t *)calloc(s->nv + 1, sizeof(count_t)); + } + + if (!silent) printf("\n"); + while (((diff > eps || diff != diff) && n_runs < N) || n_runs < min_runs) { + if (!silent) printf("\033[F\033[JWOLFF: sweep %" PRIu64 + ", dH/H = %.4f, dM/M = %.4f, dC/C = %.4f, dX/X = %.4f, cps: %.1f\n", + n_runs, fabs(meas_dx(E) / E->x), meas_dx(M[0]) / M[0]->x, meas_dc(E) / meas_c(E), meas_dc(M[0]) / meas_c(M[0]), s->nv / clust->x); + + count_t n_flips = 0; + + while (n_flips / s->nv < n) { + v_t v0 = gsl_rng_uniform_int(r, s->nv); + R_t step; + + bool changed = false; + while (!changed) { + step = gsl_rng_uniform_int(r, s->n_transformations); + if (symmetric_act(s->transformations + q * step, s->spins[v0]) != s->spins[v0]) { + changed = true; + } + } + + v_t tmp_flips = flip_cluster_finite(s, v0, step, r); + n_flips += tmp_flips; + + if (n_runs > 0) { + n_steps++; + meas_update(clust, tmp_flips); + + if (record_autocorrelation && n_steps % ac_skip == 0) { + update_autocorr(autocorr, s->E); + } + + } + + } + + for (q_t i = 0; i < q; i++) { + meas_update(M[i], s->M[i]); + } + meas_update(E, s->E); + + q_t n_at_max = 0; + q_t max_M_i = 0; + v_t max_M = 0; + + for (q_t i = 0; i < q; i++) { + if (s->M[i] > max_M) { + n_at_max = 1; + max_M_i = i; + max_M = s->M[i]; + } else if (s->M[i] == max_M) { + n_at_max++; + } + } + + if (record_distribution) { + mag_dist[s->M[0]]++; + } + + if (n_at_max == 1) { + for (q_t i = 0; i < q; i++) { + meas_update(sM[max_M_i][i], s->M[i]); + } + meas_update(sE[max_M_i], s->E); + freqs[max_M_i]++; + } + + diff = fabs(meas_dx(clust) / clust->x); + + n_runs++; + } + if (!silent) { + printf("\033[F\033[J"); + } + printf("WOLFF: sweep %" PRIu64 + ", dH/H = %.4f, dM/M = %.4f, dC/C = %.4f, dX/X = %.4f, cps: %.1f\n", + n_runs, fabs(meas_dx(E) / E->x), meas_dx(M[0]) / M[0]->x, meas_dc(E) / meas_c(E), meas_dc(M[0]) / meas_c(M[0]), s->nv / clust->x); + + if (snapshots) { + FILE *snapfile = fopen("snapshots.m", "a"); + fprintf(snapfile, "\n"); + } + + if (snapshot) { + q_t *R_inv = symmetric_invert(q, s->R); + FILE *snapfile = fopen("snapshot.m", "a"); + fprintf(snapfile, "{{"); + for (L_t i = 0; i < L; i++) { + fprintf(snapfile, "{"); + for (L_t j = 0; j < L; j++) { + fprintf(snapfile, "%" PRIq, symmetric_act(R_inv, s->spins[L * i + j])); + if (j != L - 1) { + fprintf(snapfile, ","); + } + } + fprintf(snapfile, "}"); + if (i != L - 1) { + fprintf(snapfile, ","); + } + } + fprintf(snapfile, "}}\n"); + fclose(snapfile); + } + + double tau = 0; + int tau_failed = 0; + + if (record_autocorrelation) { + double *Gammas = (double *)malloc((W + 1) * sizeof(double)); + + Gammas[0] = 1 + rho(autocorr, 0); + for (uint64_t i = 0; i < W; i++) { + Gammas[1 + i] = rho(autocorr, 2 * i + 1) + rho(autocorr, 2 * i + 2); + } + + uint64_t n; + for (n = 0; n < W + 1; n++) { + if (Gammas[n] <= 0) { + break; + } + } + + if (n == W + 1) { + printf("WARNING: correlation function never hit the noise floor.\n"); + tau_failed = 1; + } + + if (n < 2) { + printf("WARNING: correlation function only has one nonnegative term.\n"); + tau_failed = 2; + } + + double *conv_Gamma = get_convex_minorant(n, Gammas); + + double ttau = - 0.5; + + for (uint64_t i = 0; i < n + 1; i++) { + ttau += conv_Gamma[i]; + } + + tau = ttau * ac_skip * clust->x / s->nv; + + free(Gammas); + free(autocorr->OO); + while (autocorr->Op != NULL) { + stack_pop_d(&(autocorr->Op)); + } + free(autocorr); + } + + if (tau_failed) { + //tau = 0; + } + + { + FILE *outfile = fopen("out.m", "a"); + + fprintf(outfile, "<|N->%" PRIcount ",n->%" PRIcount ",D->%" PRID ",L->%" PRIL ",q->%" PRIq ",T->%.15f,J->{", N, n, D, L, q, T); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", s->J[i]); + if (i != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},H->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", s->H[i]); + if (i != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},E->%.15f,\\[Delta]E->%.15f,C->%.15f,\\[Delta]C->%.15f,M->{", E->x / s->nv, meas_dx(E) / s->nv, meas_c(E) / s->nv, meas_dc(E) / s->nv); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", M[i]->x / s->nv); + if (i != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},\\[Delta]M->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", meas_dx(M[i]) / s->nv); + if (i != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},\\[Chi]->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", meas_c(M[i]) / s->nv); + if (i != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},\\[Delta]\\[Chi]->{"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "%.15f", meas_dc(M[i]) / s->nv); + if (i != q-1) { + fprintf(outfile, ","); + } + } + for (q_t i = 0; i < q; i++) { + fprintf(outfile, "},Subscript[E,%" PRIq "]->%.15f,Subscript[\\[Delta]E,%" PRIq "]->%.15f,Subscript[C,%" PRIq "]->%.15f,Subscript[\\[Delta]C,%" PRIq "]->%.15f,Subscript[M,%" PRIq "]->{", i, sE[i]->x / s->nv, i, meas_dx(sE[i]) / s->nv, i, meas_c(sE[i]) / s->nv, i, meas_dc(sE[i]) / s->nv, i); + for (q_t j = 0; j < q; j++) { + fprintf(outfile, "%.15f", sM[i][j]->x / s->nv); + if (j != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},Subscript[\\[Delta]M,%" PRIq "]->{", i); + for (q_t j = 0; j < q; j++) { + fprintf(outfile, "%.15f", meas_dx(sM[i][j]) / s->nv); + if (j != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},Subscript[\\[Chi],%" PRIq "]->{", i); + for (q_t j = 0; j < q; j++) { + fprintf(outfile, "%.15f", meas_c(sM[i][j]) / s->nv); + if (j != q-1) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "},Subscript[\\[Delta]\\[Chi],%" PRIq "]->{", i); + for (q_t j = 0; j < q; j++) { + fprintf(outfile, "%.15f", meas_dc(sM[i][j]) / s->nv); + if (j != q-1) { + fprintf(outfile, ","); + } + } + } + fprintf(outfile,"}"); + for (q_t i = 0; i < q; i++) { + fprintf(outfile, ",Subscript[f,%" PRIq "]->%.15f,Subscript[\\[Delta]f,%" PRIq "]->%.15f", i, (double)freqs[i] / (double)n_runs, i, sqrt(freqs[i]) / (double)n_runs); + } + fprintf(outfile, ",Subscript[n,\"clust\"]->%.15f,Subscript[\\[Delta]n,\"clust\"]->%.15f,Subscript[m,\"clust\"]->%.15f,Subscript[\\[Delta]m,\"clust\"]->%.15f,\\[Tau]->%.15f,\\[Tau]s->%d", clust->x / s->nv, meas_dx(clust) / s->nv, meas_c(clust) / s->nv, meas_dc(clust) / s->nv,tau,tau_failed); + if (record_distribution) { + fprintf(outfile, ",S->{"); + for (v_t i = 0; i < s->nv + 1; i++) { + fprintf(outfile, "%" PRIcount, mag_dist[i]); + if (i != s->nv) { + fprintf(outfile, ","); + } + } + fprintf(outfile, "}"); + free(mag_dist); + } + fprintf(outfile, "|>\n"); + + fclose(outfile); + } + + free(E); + free(clust); + for (q_t i = 0; i < q; i++) { + free(M[i]); + for (q_t j = 0; j < q; j++) { + free(sM[i][j]); + } + free(sM[i]); + } + free(M); + free(sM); + for (q_t i = 0; i < q; i++) { + free(sE[i]); + } + free(freqs); + free(sE); + state_finite_free(s); + gsl_rng_free(r); + + return 0; +} + diff --git a/src/wolff_potts.c b/src/wolff_potts.c deleted file mode 100644 index b081bec..0000000 --- a/src/wolff_potts.c +++ /dev/null @@ -1,485 +0,0 @@ - -#include - -#include -#include - -int main(int argc, char *argv[]) { - - L_t L = 128; - count_t N = (count_t)1e7; - count_t min_runs = 10; - count_t n = 3; - q_t q = 2; - D_t D = 2; - double T = 2.26918531421; - double *J = (double *)calloc(MAX_Q, sizeof(double)); - J[0] = 1.0; - double *H = (double *)calloc(MAX_Q, sizeof(double)); - double eps = 0; - bool pretend_ising = false; - bool planar_potts = false; - bool sim_dgm = false; - bool silent = false; - bool snapshots = false; - bool snapshot = false; - bool record_autocorrelation = false; - bool record_distribution = false; - count_t W = 10; - count_t ac_skip = 1; - - int opt; - q_t J_ind = 0; - q_t H_ind = 0; - - while ((opt = getopt(argc, argv, "N:n:D:L:q:T:J:H:m:e:IpsSPak:W:dr")) != -1) { - switch (opt) { - case 'N': - N = (count_t)atof(optarg); - break; - case 'n': - n = (count_t)atof(optarg); - break; - case 'D': - D = atoi(optarg); - break; - case 'L': - L = atoi(optarg); - break; - case 'q': - q = atoi(optarg); - break; - case 'T': - T = atof(optarg); - break; - case 'J': - J[J_ind] = atof(optarg); - J_ind++; - break; - case 'H': - H[H_ind] = atof(optarg); - H_ind++; - break; - case 'm': - min_runs = atoi(optarg); - break; - case 'e': - eps = atof(optarg); - break; - case 'I': - pretend_ising = true; - break; - case 'p': - planar_potts = true; - break; - case 's': - silent = true; - break; - case 'S': - snapshots = true; - break; - case 'P': - snapshot = true; - break; - case 'a': - record_autocorrelation = true; - break; - case 'k': - ac_skip = (count_t)atof(optarg); - break; - case 'W': - W = (count_t)atof(optarg); - break; - case 'd': - record_distribution = true; - break; - case 'r': - sim_dgm = true; - break; - default: - exit(EXIT_FAILURE); - } - } - - gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); - gsl_rng_set(r, rand_seed()); - - if (pretend_ising) { - q = 2; - H[1] = -H[0]; - J[1] = -J[0]; - } - - if (planar_potts) { - for (q_t i = 0; i < q; i++) { - J[i] = cos(2 * M_PI * i / ((double)q)); - } - } - - if (sim_dgm) { - for (q_t i = 0; i < q / 2 + 1; i++) { - J[i] = -pow(i, 2); - } - for (q_t i = 1; i < (q + 1) / 2; i++) { - J[q - i] = -pow(i, 2); - } - } - - state_finite_t *s = (state_finite_t *)calloc(1, sizeof(state_finite_t)); - - graph_t *h = graph_create_square(D, L); - s->g = graph_add_ext(h); - - s->q = q; - s->n_transformations = q; - s->transformations = dihedral_gen_transformations(q); - - s->T = T; - s->J = J; - s->H = H; - - s->J_probs = (double *)calloc(pow(q, 2), sizeof(double)); - for (q_t i = 0; i < q; i++) { - for (q_t j = 0; j < q; j++) { - s->J_probs[q * i + j] = 1.0 - exp((s->J[i] - s->J[j]) / T); - } - } - s->H_probs = (double *)calloc(pow(q, 2), sizeof(double)); - for (q_t i = 0; i < q; i++) { - for (q_t j = 0; j < q; j++) { - s->H_probs[q * i + j] = 1.0 - exp((s->H[i] - s->H[j]) / T); - } - } - - s->spins = (q_t *)calloc(h->nv, sizeof(q_t)); // everyone starts in state 0 - s->R = (q_t *)malloc(q * sizeof(q_t)); // transformation is the identity, (1 ... q) - - for (q_t i = 0; i < q; i++) { - s->R[i] = i; - } - - // energy is the number of edges times the energy of an aligned edge minus - // the number of vertices times the energy of a 0-aligned vertex - s->E = - ((double)h->ne) * s->J[0] - ((double)h->nv) * s->H[0]; - s->M = (v_t *)calloc(q, sizeof(v_t)); - s->M[0] = h->nv; // everyone starts in state 0, remember? - - double diff = 1e31; - count_t n_runs = 0; - count_t n_steps = 0; - - meas_t *E, *clust, **M, **sE, ***sM; - - M = (meas_t **)malloc(q * sizeof(meas_t *)); - for (q_t i = 0; i < q; i++) { - M[i] = (meas_t *)calloc(1, sizeof(meas_t)); - } - - E = calloc(1, sizeof(meas_t)); - clust = calloc(1, sizeof(meas_t)); - - sE = (meas_t **)malloc(q * sizeof(meas_t *)); - sM = (meas_t ***)malloc(q * sizeof(meas_t **)); - - for (q_t i = 0; i < q; i++) { - sE[i] = (meas_t *)calloc(1, sizeof(meas_t)); - sM[i] = (meas_t **)malloc(q * sizeof(meas_t *)); - for (q_t j = 0; j < q; j++) { - sM[i][j] = (meas_t *)calloc(1, sizeof(meas_t)); - } - } - - count_t *freqs = (count_t *)calloc(q, sizeof(count_t)); - q_t cur_M = 0; - - autocorr_t *autocorr; - if (record_autocorrelation) { - autocorr = (autocorr_t *)calloc(1, sizeof(autocorr_t)); - autocorr->W = 2 * W + 1; - autocorr->OO = (double *)calloc(2 * W + 1, sizeof(double)); - } - - count_t *mag_dist; - if (record_distribution) { - mag_dist = (count_t *)calloc(h->nv + 1, sizeof(count_t)); - } - - if (!silent) printf("\n"); - while (((diff > eps || diff != diff) && n_runs < N) || n_runs < min_runs) { - if (!silent) printf("\033[F\033[JWOLFF: sweep %" PRIu64 - ", dH/H = %.4f, dM/M = %.4f, dC/C = %.4f, dX/X = %.4f, cps: %.1f\n", - n_runs, fabs(meas_dx(E) / E->x), meas_dx(M[0]) / M[0]->x, meas_dc(E) / meas_c(E), meas_dc(M[0]) / meas_c(M[0]), h->nv / clust->x); - - count_t n_flips = 0; - - while (n_flips / h->nv < n) { - v_t v0 = gsl_rng_uniform_int(r, h->nv); - R_t step; - - bool changed = false; - while (!changed) { - step = gsl_rng_uniform_int(r, s->n_transformations); - if (symmetric_act(s->transformations + q * step, s->spins[v0]) != s->spins[v0]) { - changed = true; - } - } - - v_t tmp_flips = flip_cluster_finite(s, v0, step, r); - n_flips += tmp_flips; - - if (n_runs > 0) { - n_steps++; - meas_update(clust, tmp_flips); - - if (record_autocorrelation && n_steps % ac_skip == 0) { - update_autocorr(autocorr, s->E); - } - - } - - } - - for (q_t i = 0; i < q; i++) { - meas_update(M[i], s->M[i]); - } - meas_update(E, s->E); - - q_t n_at_max = 0; - q_t max_M_i = 0; - v_t max_M = 0; - - for (q_t i = 0; i < q; i++) { - if (s->M[i] > max_M) { - n_at_max = 1; - max_M_i = i; - max_M = s->M[i]; - } else if (s->M[i] == max_M) { - n_at_max++; - } - } - - if (record_distribution) { - mag_dist[s->M[0]]++; - } - - if (n_at_max == 1) { - for (q_t i = 0; i < q; i++) { - meas_update(sM[max_M_i][i], s->M[i]); - } - meas_update(sE[max_M_i], s->E); - freqs[max_M_i]++; - } - - diff = fabs(meas_dx(clust) / clust->x); - - n_runs++; - } - if (!silent) { - printf("\033[F\033[J"); - } - printf("WOLFF: sweep %" PRIu64 - ", dH/H = %.4f, dM/M = %.4f, dC/C = %.4f, dX/X = %.4f, cps: %.1f\n", - n_runs, fabs(meas_dx(E) / E->x), meas_dx(M[0]) / M[0]->x, meas_dc(E) / meas_c(E), meas_dc(M[0]) / meas_c(M[0]), h->nv / clust->x); - - if (snapshots) { - FILE *snapfile = fopen("snapshots.m", "a"); - fprintf(snapfile, "\n"); - } - - if (snapshot) { - q_t *R_inv = symmetric_invert(q, s->R); - FILE *snapfile = fopen("snapshot.m", "a"); - fprintf(snapfile, "{{"); - for (L_t i = 0; i < L; i++) { - fprintf(snapfile, "{"); - for (L_t j = 0; j < L; j++) { - fprintf(snapfile, "%" PRIq, symmetric_act(R_inv, s->spins[L * i + j])); - if (j != L - 1) { - fprintf(snapfile, ","); - } - } - fprintf(snapfile, "}"); - if (i != L - 1) { - fprintf(snapfile, ","); - } - } - fprintf(snapfile, "}}\n"); - fclose(snapfile); - } - - double tau = 0; - int tau_failed = 0; - - if (record_autocorrelation) { - double *Gammas = (double *)malloc((W + 1) * sizeof(double)); - - Gammas[0] = 1 + rho(autocorr, 0); - for (uint64_t i = 0; i < W; i++) { - Gammas[1 + i] = rho(autocorr, 2 * i + 1) + rho(autocorr, 2 * i + 2); - } - - uint64_t n; - for (n = 0; n < W + 1; n++) { - if (Gammas[n] <= 0) { - break; - } - } - - if (n == W + 1) { - printf("WARNING: correlation function never hit the noise floor.\n"); - tau_failed = 1; - } - - if (n < 2) { - printf("WARNING: correlation function only has one nonnegative term.\n"); - tau_failed = 2; - } - - double *conv_Gamma = get_convex_minorant(n, Gammas); - - double ttau = - 0.5; - - for (uint64_t i = 0; i < n + 1; i++) { - ttau += conv_Gamma[i]; - } - - tau = ttau * ac_skip * clust->x / h->nv; - - free(Gammas); - free(autocorr->OO); - while (autocorr->Op != NULL) { - stack_pop_d(&(autocorr->Op)); - } - free(autocorr); - } - - if (tau_failed) { - //tau = 0; - } - - FILE *outfile = fopen("out.m", "a"); - - fprintf(outfile, "<|N->%" PRIcount ",n->%" PRIcount ",D->%" PRID ",L->%" PRIL ",q->%" PRIq ",T->%.15f,J->{", N, n, D, L, q, T); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "%.15f", J[i]); - if (i != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},H->{"); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "%.15f", H[i]); - if (i != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},E->%.15f,\\[Delta]E->%.15f,C->%.15f,\\[Delta]C->%.15f,M->{", E->x / h->nv, meas_dx(E) / h->nv, meas_c(E) / h->nv, meas_dc(E) / h->nv); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "%.15f", M[i]->x / h->nv); - if (i != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},\\[Delta]M->{"); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "%.15f", meas_dx(M[i]) / h->nv); - if (i != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},\\[Chi]->{"); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "%.15f", meas_c(M[i]) / h->nv); - if (i != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},\\[Delta]\\[Chi]->{"); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "%.15f", meas_dc(M[i]) / h->nv); - if (i != q-1) { - fprintf(outfile, ","); - } - } - for (q_t i = 0; i < q; i++) { - fprintf(outfile, "},Subscript[E,%" PRIq "]->%.15f,Subscript[\\[Delta]E,%" PRIq "]->%.15f,Subscript[C,%" PRIq "]->%.15f,Subscript[\\[Delta]C,%" PRIq "]->%.15f,Subscript[M,%" PRIq "]->{", i, sE[i]->x / h->nv, i, meas_dx(sE[i]) / h->nv, i, meas_c(sE[i]) / h->nv, i, meas_dc(sE[i]) / h->nv, i); - for (q_t j = 0; j < q; j++) { - fprintf(outfile, "%.15f", sM[i][j]->x / h->nv); - if (j != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},Subscript[\\[Delta]M,%" PRIq "]->{", i); - for (q_t j = 0; j < q; j++) { - fprintf(outfile, "%.15f", meas_dx(sM[i][j]) / h->nv); - if (j != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},Subscript[\\[Chi],%" PRIq "]->{", i); - for (q_t j = 0; j < q; j++) { - fprintf(outfile, "%.15f", meas_c(sM[i][j]) / h->nv); - if (j != q-1) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "},Subscript[\\[Delta]\\[Chi],%" PRIq "]->{", i); - for (q_t j = 0; j < q; j++) { - fprintf(outfile, "%.15f", meas_dc(sM[i][j]) / h->nv); - if (j != q-1) { - fprintf(outfile, ","); - } - } - } - fprintf(outfile,"}"); - for (q_t i = 0; i < q; i++) { - fprintf(outfile, ",Subscript[f,%" PRIq "]->%.15f,Subscript[\\[Delta]f,%" PRIq "]->%.15f", i, (double)freqs[i] / (double)n_runs, i, sqrt(freqs[i]) / (double)n_runs); - } - fprintf(outfile, ",Subscript[n,\"clust\"]->%.15f,Subscript[\\[Delta]n,\"clust\"]->%.15f,Subscript[m,\"clust\"]->%.15f,Subscript[\\[Delta]m,\"clust\"]->%.15f,\\[Tau]->%.15f,\\[Tau]s->%d", clust->x / h->nv, meas_dx(clust) / h->nv, meas_c(clust) / h->nv, meas_dc(clust) / h->nv,tau,tau_failed); - if (record_distribution) { - fprintf(outfile, ",S->{"); - for (v_t i = 0; i < h->nv + 1; i++) { - fprintf(outfile, "%" PRIcount, mag_dist[i]); - if (i != h->nv) { - fprintf(outfile, ","); - } - } - fprintf(outfile, "}"); - free(mag_dist); - } - fprintf(outfile, "|>\n"); - - fclose(outfile); - - free(E); - free(clust); - for (q_t i = 0; i < q; i++) { - free(M[i]); - for (q_t j = 0; j < q; j++) { - free(sM[i][j]); - } - free(sM[i]); - } - free(M); - free(sM); - for (q_t i = 0; i < q; i++) { - free(sE[i]); - } - free(freqs); - free(sE); - free(s->H_probs); - free(s->J_probs); - free(s->M); - free(s->spins); - free(s->R); - free(s->transformations); - graph_free(s->g); - free(s); - free(H); - free(J); - graph_free(h); - gsl_rng_free(r); - - return 0; -} - -- cgit v1.2.3-70-g09d2