From 643baba78eb15a685d959aae718ee3eeade2f806 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Fri, 19 Oct 2018 01:26:14 -0400 Subject: big library overhual, fixed all examples, added documentation with sphinx --- examples/CMakeLists.txt | 4 +- examples/On.cpp | 10 ++- examples/animate_ising.cpp | 133 -------------------------------------- examples/ising.cpp | 14 ++-- examples/ising_animation.cpp | 139 ++++++++++++++++++++++++++++++++++++++++ examples/simple_measurement.hpp | 18 +++--- 6 files changed, 167 insertions(+), 151 deletions(-) delete mode 100644 examples/animate_ising.cpp create mode 100644 examples/ising_animation.cpp (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 73922c6..a4a4a5c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,11 +1,11 @@ add_executable(ising ising.cpp) -add_executable(animate_ising animate_ising.cpp) +add_executable(ising_animation ising_animation.cpp) add_executable(xy On.cpp) add_compile_definitions(xy WOLFF_N=2) target_link_libraries(ising wolff) -target_link_libraries(animate_ising wolff GL GLU glut) +target_link_libraries(ising_animation wolff GL GLU glut) target_link_libraries(xy wolff) diff --git a/examples/On.cpp b/examples/On.cpp index e045f52..6c3f0fd 100644 --- a/examples/On.cpp +++ b/examples/On.cpp @@ -7,6 +7,7 @@ #include #include + #include int main(int argc, char *argv[]) { @@ -56,10 +57,13 @@ int main(int argc, char *argv[]) { return H * s; }; + // initialize the lattice + graph G(D, L); + // initialize the system - wolff_system, vector_t> S(D, L, T, Z, B); + system, vector_t> S(G, T, Z, B); - std::function (std::mt19937&, const vector_t&)> gen_R = generate_rotation_uniform; + std::function (std::mt19937&, const system, vector_t>&, v_t)> gen_R = generate_rotation_uniform; // initailze the measurement object simple_measurement A(S); @@ -69,7 +73,7 @@ int main(int argc, char *argv[]) { std::mt19937 rng{seed}; // run wolff N times - wolff, vector_t>(N, S, gen_R, A, rng); + S.run_wolff(N, gen_R, A, rng); // print the result of our measurements std::cout << "Wolff complete!\nThe average energy per site was " << A.avgE() / S.nv diff --git a/examples/animate_ising.cpp b/examples/animate_ising.cpp deleted file mode 100644 index e33736e..0000000 --- a/examples/animate_ising.cpp +++ /dev/null @@ -1,133 +0,0 @@ - -#include -#include -#include - -#include - -#include -#include -#include - -class draw_ising : public wolff_measurement { - private: - unsigned int frame_skip; - v_t C; - public: - draw_ising(const wolff_system& S, unsigned int window_size, unsigned int frame_skip, int argc, char *argv[]) : frame_skip(frame_skip){ - 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, S.L, 0.0, S.L); - } - - void pre_cluster(N_t, N_t, const wolff_system& S, v_t, const ising_t&) { - glClear(GL_COLOR_BUFFER_BIT); - for (v_t i = 0; i < pow(S.L, 2); i++) { - if (S.s[i].x == S.s0.x) { - glColor3f(0.0, 0.0, 0.0); - } else { - glColor3f(1.0, 1.0, 1.0); - } - glRecti(i / S.L, i % S.L, (i / S.L) + 1, (i % S.L) + 1); - } - glFlush(); - C = 0; - } - - void plain_bond_visited(const wolff_system&, v_t, const ising_t&, v_t, double dE) {} - - void ghost_bond_visited(const wolff_system&, v_t, const ising_t& s_old, const ising_t& s_new, double dE) {} - - void plain_site_transformed(const wolff_system& S, v_t i, const ising_t&) { - glColor3f(1.0, 0.0, 0.0); - glRecti(i / S.L, i % S.L, (i / S.L) + 1, (i % S.L) + 1); - C++; - if (C % frame_skip == 0) { - glFlush(); - } - } - - void ghost_site_transformed(const wolff_system&, const ising_t&) {} - - void post_cluster(N_t, N_t, const wolff_system&) {} -}; - -int main(int argc, char *argv[]) { - - // set defaults - N_t N = (N_t)1e4; - D_t D = 2; - L_t L = 128; - double T = 2.26918531421; - double H = 0.0; - unsigned int window_size = 512; - unsigned int frame_skip = 1; - - int opt; - - // take command line arguments - while ((opt = getopt(argc, argv, "N:D:L:T:H:w:f:")) != -1) { - switch (opt) { - case 'N': // number of steps - N = (N_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 - H = atof(optarg); - break; - case 'w': - window_size = atoi(optarg); - break; - case 'f': - frame_skip = atoi(optarg); - break; - default: - exit(EXIT_FAILURE); - } - } - - // define the spin-spin coupling - std::function Z = [] (const ising_t& s1, const ising_t& s2) -> double { - return (double)(s1 * s2); - }; - - // define the spin-field coupling - std::function B = [=] (const ising_t& s) -> double { - return H * s; - }; - - // initialize the system - wolff_system S(D, L, T, Z, B); - - // define function that generates self-inverse rotations - std::function gen_R = [] (std::mt19937&, const ising_t& s) -> ising_t { - return ising_t(true); - }; - - // initailze the measurement object - draw_ising A(S, window_size, frame_skip, argc, argv); - - // initialize the random number generator - auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - std::mt19937 rng{seed}; - - // run wolff N times - wolff(N, S, gen_R, A, rng); - - // exit - return 0; -} - diff --git a/examples/ising.cpp b/examples/ising.cpp index ffcb7e4..fe21279 100644 --- a/examples/ising.cpp +++ b/examples/ising.cpp @@ -7,8 +7,11 @@ #include #include + #include +using namespace wolff; + int main(int argc, char *argv[]) { // set defaults @@ -53,23 +56,24 @@ int main(int argc, char *argv[]) { return H * s; }; + // initialize the lattice + graph G(D, L); + // initialize the system - wolff_system S(D, L, T, Z, B); + system S(G, T, Z, B); // initialize the random number generator auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); std::mt19937 rng{seed}; // define function that generates self-inverse rotations - std::function gen_R = [] (std::mt19937&, const ising_t& s) -> ising_t { - return ising_t(true); - }; + std::function &, v_t)> gen_r = gen_ising; // initailze the measurement object simple_measurement A(S); // run wolff N times - wolff(N, S, gen_R, A, rng); + S.run_wolff(N, gen_r, A, rng); // print the result of our measurements std::cout << "Wolff complete!\nThe average energy per site was " << A.avgE() / S.nv diff --git a/examples/ising_animation.cpp b/examples/ising_animation.cpp new file mode 100644 index 0000000..2104e91 --- /dev/null +++ b/examples/ising_animation.cpp @@ -0,0 +1,139 @@ + +#include +#include +#include + +#include + +#include +#include + +#include + +using namespace wolff; + +class draw_ising : public measurement { + private: + unsigned int frame_skip; + v_t C; + public: + draw_ising(const system& S, unsigned int window_size, unsigned int frame_skip, int argc, char *argv[]) : frame_skip(frame_skip){ + 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, S.G.L, 0.0, S.G.L); + } + + void pre_cluster(N_t, N_t, const system& S, v_t, const ising_t&) { + glClear(GL_COLOR_BUFFER_BIT); + for (v_t i = 0; i < pow(S.G.L, 2); i++) { + if (S.s[i].x == S.s0.x) { + glColor3f(0.0, 0.0, 0.0); + } else { + glColor3f(1.0, 1.0, 1.0); + } + glRecti(i / S.G.L, i % S.G.L, (i / S.G.L) + 1, (i % S.G.L) + 1); + } + glFlush(); + C = 0; + } + + void plain_bond_visited(const system&, v_t, const ising_t&, v_t, double dE) {} + + void ghost_bond_visited(const system&, v_t, const ising_t& s_old, const ising_t& s_new, double dE) {} + + void plain_site_transformed(const system& S, v_t i, const ising_t&) { + glColor3f(1.0, 0.0, 0.0); + glRecti(i / S.G.L, i % S.G.L, (i / S.G.L) + 1, (i % S.G.L) + 1); + C++; + if (C % frame_skip == 0) { + glFlush(); + } + } + + void ghost_site_transformed(const system&, const ising_t&) {} + + void post_cluster(N_t, N_t, const system&) {} +}; + +int main(int argc, char *argv[]) { + + // set defaults + N_t N = (N_t)1e4; + D_t D = 2; + L_t L = 128; + double T = 2.26918531421; + double H = 0.0; + unsigned int window_size = 512; + unsigned int frame_skip = 1; + + int opt; + + // take command line arguments + while ((opt = getopt(argc, argv, "N:D:L:T:H:w:f:")) != -1) { + switch (opt) { + case 'N': // number of steps + N = (N_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 + H = atof(optarg); + break; + case 'w': + window_size = atoi(optarg); + break; + case 'f': + frame_skip = atoi(optarg); + break; + default: + exit(EXIT_FAILURE); + } + } + + // define the spin-spin coupling + std::function Z = [] (const ising_t& s1, const ising_t& s2) -> double { + return (double)(s1 * s2); + }; + + // define the spin-field coupling + std::function B = [=] (const ising_t& s) -> double { + return H * s; + }; + + // initialize the lattice + graph G(D, L); + + // initialize the system + system S(G, T, Z, B); + + // define function that generates self-inverse rotations + std::function &, v_t)> gen_R = [] (std::mt19937&, const system&, v_t) -> ising_t { + return ising_t(true); + }; + + // initailze the measurement object + draw_ising A(S, window_size, frame_skip, argc, argv); + + // initialize the random number generator + auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + std::mt19937 rng{seed}; + + // run wolff N times + S.run_wolff(N, gen_R, A, rng); + + // exit + return 0; +} + diff --git a/examples/simple_measurement.hpp b/examples/simple_measurement.hpp index 2287c58..518631c 100644 --- a/examples/simple_measurement.hpp +++ b/examples/simple_measurement.hpp @@ -1,8 +1,10 @@ #include +using namespace wolff; + template -class simple_measurement : public wolff_measurement { +class simple_measurement : public measurement { private: N_t n; @@ -15,7 +17,7 @@ class simple_measurement : public wolff_measurement { double totalC; public: - simple_measurement(const wolff_system& S) { + simple_measurement(const system& S) { n = 0; M = S.nv * S.s[0]; E = - (S.ne * S.Z(S.s[0], S.s[0]) + S.nv * S.B(S.s[0])); @@ -25,26 +27,26 @@ class simple_measurement : public wolff_measurement { totalC = 0; } - void pre_cluster(N_t, N_t, const wolff_system&, v_t, const R_t&) { + void pre_cluster(N_t, N_t, const system&, v_t, const R_t&) { C = 0; } - void plain_bond_visited(const wolff_system&, v_t, const X_t&, v_t, double dE) { + void plain_bond_visited(const system&, v_t, const X_t&, v_t, double dE) { E += dE; } - void ghost_bond_visited(const wolff_system&, v_t, const X_t& s_old, const X_t& s_new, double dE) { + void ghost_bond_visited(const system&, v_t, const X_t& s_old, const X_t& s_new, double dE) { E += dE; M += s_new - s_old; } - void plain_site_transformed(const wolff_system&, v_t, const X_t&) { + void plain_site_transformed(const system&, v_t, const X_t&) { C++; } - void ghost_site_transformed(const wolff_system&, const R_t&) {} + void ghost_site_transformed(const system&, const R_t&) {} - void post_cluster(N_t, N_t, const wolff_system&) { + void post_cluster(N_t, N_t, const system&) { totalE += E; totalM += M; totalC += C; -- cgit v1.2.3-70-g09d2