From 1343a3fe6bd17a2487f12a0d61be8dc83cd722a0 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Mon, 15 Oct 2018 22:57:17 -0400 Subject: many changes, including reworking the measurements system --- examples/src/models/On/wolff_On.cpp | 25 +++++++++++------------ examples/src/models/ising/ising.hpp | 33 +++++++++++++++++++++++++++---- examples/src/models/ising/wolff_ising.cpp | 25 ++++++++++------------- examples/src/models/potts/symmetric.hpp | 1 + examples/src/models/potts/wolff_clock.cpp | 22 ++++++++------------- examples/src/models/potts/wolff_potts.cpp | 25 +++++++++++------------ 6 files changed, 70 insertions(+), 61 deletions(-) (limited to 'examples/src') diff --git a/examples/src/models/On/wolff_On.cpp b/examples/src/models/On/wolff_On.cpp index ad2ac77..67f28a5 100644 --- a/examples/src/models/On/wolff_On.cpp +++ b/examples/src/models/On/wolff_On.cpp @@ -188,14 +188,12 @@ int main(int argc, char *argv[]) { fclose(outfile_info); - FILE **outfiles = measure_setup_files(measurement_flags, timestamp); - - std::function other_f; + 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; + other_f = [&] (const On_t& s, const wolff_research_measurements& m) { + sum_of_clusterSize += m.last_cluster_size; }; } else if (draw) { #ifdef HAVE_GLUT @@ -209,7 +207,7 @@ int main(int argc, char *argv[]) { glLoadIdentity(); gluOrtho2D(0.0, L, 0.0, L); - other_f = [&] (const On_t& s) { + other_f = [&] (const On_t& s, const wolff_research_measurements& m) { glClear(GL_COLOR_BUFFER_BIT); for (v_t i = 0; i < pow(L, 2); i++) { #ifdef NOFIELD @@ -228,11 +226,9 @@ int main(int argc, char *argv[]) { }; #endif } else { - other_f = [] (const On_t& s) {}; + other_f = [] (const On_t& s, const wolff_research_measurements& m) {}; } - std::function measurements = measure_function_write_files(measurement_flags, outfiles, other_f); - std::function H; if (modulated_field) { @@ -251,20 +247,21 @@ int main(int argc, char *argv[]) { state_t s(D, L, T, dot ); #endif + wolff_research_measurements m(measurement_flags, timestamp, other_f, s, silent); + 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, rng, silent); + 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, m.E, m.last_cluster_size); + wolff (N, s, gen_R, m, rng); 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); + 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, m.E, m.last_cluster_size); } else { - wolff (N, s, gen_R, measurements, rng, silent); + wolff (N, s, gen_R, m, rng); } - measure_free_files(measurement_flags, outfiles); free(H_vec); return 0; diff --git a/examples/src/models/ising/ising.hpp b/examples/src/models/ising/ising.hpp index ae20840..73b06ed 100644 --- a/examples/src/models/ising/ising.hpp +++ b/examples/src/models/ising/ising.hpp @@ -5,17 +5,31 @@ #include +// all that is required to use wolff.hpp is a default constructor class ising_t { public: bool x; - typedef int M_t; - typedef double F_t; - ising_t() : x(false) {} - ising_t(bool x) : x(x) {} + + // optional constructors for syntactic sugar + ising_t(bool x) : x(x) {} ising_t(int x) : x((bool)x) {} + /* below this comment is code required only for using measure.hpp in the + * examples folder, which provides an interface for measuring several + * generic features of models. these require + * + * - an M_t, representing the magnetization or sum of all spins + * - an F_t, representing a double-weighted version of the magnetization + * - the overloaded operator *, which takes a v_t (unsigned int) and returns an M_t + * - the overloaded operator *, which takes a double and returns an F_t + * - the overloaded operator -, which takes another X_t and returns an M_t + */ + + typedef int M_t; + typedef double F_t; + inline int operator*(v_t a) const { if (x) { return -(int)a; @@ -45,6 +59,12 @@ class ising_t { } }; +/* using measure.hpp additionally requires a norm_squared function which takes + * an F_t to a double, and a write_magnetization function, which takes an M_t + * and a FILE pointer and appropriately records the contents of the former to + * the latter. + */ + double norm_squared(double s) { return pow(s, 2); } @@ -53,6 +73,11 @@ void write_magnetization(int M, FILE *outfile) { fwrite(&M, sizeof(int), 1, outfile); } +/* these definitions allow wolff/finite_states.hpp to be invoked and provide + * much faster performance for models whose number of possible spin + * configurations is finite. + */ + #define N_STATES 2 const ising_t states[2] = {ising_t(0), ising_t(1)}; q_t state_to_ind(ising_t state) { return (q_t)state.x; } diff --git a/examples/src/models/ising/wolff_ising.cpp b/examples/src/models/ising/wolff_ising.cpp index f7a3ada..de04f32 100644 --- a/examples/src/models/ising/wolff_ising.cpp +++ b/examples/src/models/ising/wolff_ising.cpp @@ -127,14 +127,12 @@ int main(int argc, char *argv[]) { return z2_t(true); }; - FILE **outfiles = measure_setup_files(measurement_flags, timestamp); - - std::function &)> other_f; + std::function &, const wolff_research_measurements&)> other_f; uint64_t sum_of_clusterSize = 0; if (N_is_sweeps) { - other_f = [&] (const state_t& s) { - sum_of_clusterSize += s.last_cluster_size; + other_f = [&] (const state_t& s, const wolff_research_measurements& meas) { + sum_of_clusterSize += meas.last_cluster_size; }; } else if (draw) { #ifdef HAVE_GLUT @@ -148,7 +146,7 @@ int main(int argc, char *argv[]) { glLoadIdentity(); gluOrtho2D(0.0, L, 0.0, L); - other_f = [] (const state_t & s) { + other_f = [] (const state_t & s, const wolff_research_measurements& meas) { glClear(GL_COLOR_BUFFER_BIT); for (v_t i = 0; i < pow(s.L, 2); i++) { #ifdef NOFIELD @@ -166,10 +164,10 @@ int main(int argc, char *argv[]) { }; #endif } else { - other_f = [] (const state_t& s) {}; + other_f = [] (const state_t& s, const wolff_research_measurements& meas) {}; } - std::function &)> measurements = measure_function_write_files(measurement_flags, outfiles, other_f); + wolff_research_measurements m(measurement_flags, timestamp, other_f, s, silent); // add line to metadata file with run info { @@ -185,18 +183,15 @@ int main(int argc, char *argv[]) { 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, rng, silent); + 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, m.E, m.last_cluster_size); + wolff(N, s, gen_R, m, rng); 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); + 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, m.E, m.last_cluster_size); } else { - wolff(N, s, gen_R, measurements, rng, silent); + wolff(N, s, gen_R, m, rng); } - measure_free_files(measurement_flags, outfiles); - return 0; - } diff --git a/examples/src/models/potts/symmetric.hpp b/examples/src/models/potts/symmetric.hpp index 8636f15..bc8673f 100644 --- a/examples/src/models/potts/symmetric.hpp +++ b/examples/src/models/potts/symmetric.hpp @@ -36,6 +36,7 @@ class symmetric_t : public std::array { } } + printf("Your spin wasn't a valid state!", s.x); exit(EXIT_FAILURE); } diff --git a/examples/src/models/potts/wolff_clock.cpp b/examples/src/models/potts/wolff_clock.cpp index 020415d..0706cc5 100644 --- a/examples/src/models/potts/wolff_clock.cpp +++ b/examples/src/models/potts/wolff_clock.cpp @@ -9,6 +9,7 @@ #include "dihedral.hpp" #include "potts.hpp" #include +#include // hack to speed things up considerably #define N_STATES POTTSQ @@ -95,7 +96,7 @@ int main(int argc, char *argv[]) { std::function (std::mt19937&, potts_t)> gen_R = [] (std::mt19937& r, potts_t v) -> dihedral_t { dihedral_t rot; rot.is_reflection = true; - std::uniform_int_distribution dist(0, POTTSQ - 1); + std::uniform_int_distribution dist(0, POTTSQ - 2); q_t x = dist(r); rot.x = (2 * v.x + x + 1) % POTTSQ; @@ -103,13 +104,11 @@ int main(int argc, char *argv[]) { }; // define function that updates any number of measurements - std::function measurement; + std::function , potts_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[0] / (double)N / (double)s.nv; + measurement = [&] (const sim_t& s, const wolff_research_measurements, potts_t>&) { }; } else { // a more complex example: measure the average magnetization, and draw the spin configuration to the screen @@ -125,8 +124,7 @@ int main(int argc, char *argv[]) { glLoadIdentity(); gluOrtho2D(0.0, L, 0.0, L); - measurement = [&] (const sim_t& s) { - average_M += (double)s.M[0] / (double)N / (double)s.nv; + measurement = [&] (const sim_t& s, const wolff_research_measurements, potts_t>&) { glClear(GL_COLOR_BUFFER_BIT); for (v_t i = 0; i < pow(L, 2); i++) { potts_t tmp_s = s.R.act_inverse(s.spins[i]); @@ -138,17 +136,13 @@ int main(int argc, char *argv[]) { #endif } - // run wolff for N cluster flips - wolff(N, s, gen_R, measurement, rng, silent); + wolff_research_measurements, potts_t> m(0, 0, measurement, s, silent); - // tell us what we found! - printf("%" PRIcount " %d-Potts runs completed. D = %" PRID ", L = %" PRIL ", T = %g, H = %g, = %g\n", N, POTTSQ, D, L, T, H_vec[0], average_M); + // run wolff for N cluster flips + wolff(N, s, gen_R, m, rng); // free the random number generator - if (draw) { - } - return 0; } diff --git a/examples/src/models/potts/wolff_potts.cpp b/examples/src/models/potts/wolff_potts.cpp index a1e9284..7b92ac1 100644 --- a/examples/src/models/potts/wolff_potts.cpp +++ b/examples/src/models/potts/wolff_potts.cpp @@ -119,7 +119,7 @@ int main(int argc, char *argv[]) { std::function (std::mt19937&, potts_t)> gen_R = [] (std::mt19937& r, potts_t v) -> symmetric_t { symmetric_t rot; - std::uniform_int_distribution dist(0, POTTSQ - 1); + std::uniform_int_distribution dist(0, POTTSQ - 2); q_t j = dist(r); q_t swap_v; if (j < v.x) { @@ -134,14 +134,12 @@ int main(int argc, char *argv[]) { return rot; }; - FILE **outfiles = measure_setup_files(measurement_flags, timestamp); - - std::function other_f; + std::function , potts_t>&)> other_f; uint64_t sum_of_clusterSize = 0; if (N_is_sweeps) { - other_f = [&] (const sim_t& s) { - sum_of_clusterSize += s.last_cluster_size; + other_f = [&] (const sim_t& s, const wolff_research_measurements, potts_t>& m) { + sum_of_clusterSize += m.last_cluster_size; }; } else if (draw) { #ifdef HAVE_GLUT @@ -155,7 +153,7 @@ int main(int argc, char *argv[]) { glLoadIdentity(); gluOrtho2D(0.0, L, 0.0, L); - other_f = [] (const sim_t& s) { + other_f = [] (const sim_t& s, const wolff_research_measurements, potts_t>& m) { glClear(GL_COLOR_BUFFER_BIT); for (v_t i = 0; i < pow(s.L, 2); i++) { potts_t tmp_s = s.R.act_inverse(s.spins[i]); @@ -166,10 +164,10 @@ int main(int argc, char *argv[]) { }; #endif } else { - other_f = [] (const sim_t& s) {}; + other_f = [] (const sim_t& s, const wolff_research_measurements, potts_t>& m) {}; } - std::function measurements = measure_function_write_files(measurement_flags, outfiles, other_f); + wolff_research_measurements, potts_t> m(measurement_flags, timestamp, other_f, s, silent); // add line to metadata file with run info { @@ -194,18 +192,17 @@ int main(int argc, char *argv[]) { 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, rng, silent); + 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, m.E, m.last_cluster_size); + wolff(N, s, gen_R, m, rng); 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); + 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, m.E, m.last_cluster_size); } else { - wolff(N, s, gen_R, measurements, rng, silent); + wolff(N, s, gen_R, m, rng); } // free the random number generator free(H_vec); - measure_free_files(measurement_flags, outfiles); return 0; -- cgit v1.2.3-70-g09d2