diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2018-10-10 21:45:32 -0400 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2018-10-10 21:45:32 -0400 |
commit | a43ff1f98e9b9814f858bccb11c174b418458491 (patch) | |
tree | ae7e094d914eddb8a1ae9548420ba8c2f329ffae /examples/src/models/roughening | |
parent | 6e264d243f0b29d90e90b605b6cdeab8227129c9 (diff) | |
download | c++-a43ff1f98e9b9814f858bccb11c174b418458491.tar.gz c++-a43ff1f98e9b9814f858bccb11c174b418458491.tar.bz2 c++-a43ff1f98e9b9814f858bccb11c174b418458491.zip |
big rearrangement of files to make libraries and example (research) files clearer, and changed to c++ std lib random numbers
Diffstat (limited to 'examples/src/models/roughening')
-rw-r--r-- | examples/src/models/roughening/CMakeLists.txt | 21 | ||||
-rw-r--r-- | examples/src/models/roughening/dihedral_inf.hpp | 47 | ||||
-rw-r--r-- | examples/src/models/roughening/height.hpp | 75 | ||||
-rw-r--r-- | examples/src/models/roughening/wolff_cgm.cpp | 167 | ||||
-rw-r--r-- | examples/src/models/roughening/wolff_dgm.cpp | 164 |
5 files changed, 474 insertions, 0 deletions
diff --git a/examples/src/models/roughening/CMakeLists.txt b/examples/src/models/roughening/CMakeLists.txt new file mode 100644 index 0000000..51a8644 --- /dev/null +++ b/examples/src/models/roughening/CMakeLists.txt @@ -0,0 +1,21 @@ + +add_executable(wolff_dgm wolff_dgm.cpp) +add_executable(wolff_cgm wolff_cgm.cpp) + +find_library(GL NAMES GL) +find_library(GLU NAMES GLU) +find_library(GLUT NAMES glut) + +if (${GLUT} MATCHES "GLUT-NOTFOUND") + target_link_libraries(wolff_dgm wolff wolff_examples) + target_link_libraries(wolff_cgm wolff wolff_examples) +else() + target_compile_definitions(wolff_dgm PUBLIC HAVE_GLUT) + target_compile_definitions(wolff_cgm PUBLIC HAVE_GLUT) + + target_link_libraries(wolff_dgm wolff wolff_examples glut GL GLU) + target_link_libraries(wolff_cgm wolff wolff_examples glut GL GLU) +endif() + +install(TARGETS wolff_dgm wolff_cgm DESTINATION ${CMAKE_INSTALL_BINDIR}) + diff --git a/examples/src/models/roughening/dihedral_inf.hpp b/examples/src/models/roughening/dihedral_inf.hpp new file mode 100644 index 0000000..19fa195 --- /dev/null +++ b/examples/src/models/roughening/dihedral_inf.hpp @@ -0,0 +1,47 @@ + +#include <wolff/types.h> +#include <cmath> +#include "height.hpp" + +template <class T> +class dihedral_inf_t { + public: + bool is_reflection; + T x; + + dihedral_inf_t() : is_reflection(false), x(0) {} + dihedral_inf_t(bool x, T y) : is_reflection(x), x(y) {} + + height_t<T> act(const height_t<T>& h) const { + if (this->is_reflection) { + return height_t(this->x - h.x); + } else { + return height_t(this->x + h.x); + } + } + + dihedral_inf_t<T> act(const dihedral_inf_t<T>& r) const { + if (this->is_reflection) { + return dihedral_inf_t<T>(!r.is_reflection, this->x - r.x); + } else { + return dihedral_inf_t<T>(r.is_reflection, this->x + r.x); + } + } + + height_t<T> act_inverse(const height_t<T>& h) const { + if (this->is_reflection) { + return this->act(h); + } else { + return height_t(h.x - this->x); + } + } + + dihedral_inf_t<T> act_inverse(const dihedral_inf_t<T>& r) const { + if (this->is_reflection) { + return this->act(r); + } else { + return dihedral_inf_t<T>(r.is_reflection, r.x - this->x); + } + } +}; + diff --git a/examples/src/models/roughening/height.hpp b/examples/src/models/roughening/height.hpp new file mode 100644 index 0000000..4023063 --- /dev/null +++ b/examples/src/models/roughening/height.hpp @@ -0,0 +1,75 @@ + +#pragma once + +#include <cmath> +#include <stdio.h> + +#include <wolff/types.h> + +/* 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; + + typedef T M_t; + typedef double F_t; + + height_t() : x(0) {} + + height_t(T x) : x(x) {} + + inline T operator*(v_t a) const { + return x * a; + } + + inline double operator*(double a) const { + return x * a; + } + + inline T operator-(const height_t& h) const { + return x - h.x; + } +}; + +template <class T> +inline T& operator+=(T& M, const height_t<T> &h) { + M += h.x; + + return M; +} + +template <class T> +inline T& operator-=(T& M, const height_t<T> &h) { + M -= h.x; + + return M; +} + +double norm_squared(double h) { + return pow(h, 2); +} + +template <class T> +void write_magnetization(T M, FILE *outfile) { + fwrite(&M, sizeof(T), 1, outfile); +} + diff --git a/examples/src/models/roughening/wolff_cgm.cpp b/examples/src/models/roughening/wolff_cgm.cpp new file mode 100644 index 0000000..65f8d66 --- /dev/null +++ b/examples/src/models/roughening/wolff_cgm.cpp @@ -0,0 +1,167 @@ + +#include <getopt.h> + +#ifdef HAVE_GLUT +#include <GL/glut.h> +#endif + +// include your group and spin space +#include "dihedral_inf.hpp" +#include "height.hpp" + +#include <randutils/randutils.hpp> + +// include wolff.h +#include <wolff.hpp> + +typedef state_t <dihedral_inf_t<double>, height_t<double>> 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; + double 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 + randutils::auto_seed_128 seeds; + std::mt19937 rng{seeds}; + + // define spin-spin coupling + std::function <double(const height_t<double>&, const height_t<double>&)> Z = [] (const height_t<double>& h1, const height_t<double>& h2) -> double { + return -pow(h1.x - h2.x, 2); + }; + + // define spin-field coupling + std::function <double(height_t<double>)> B = [=] (height_t<double> 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<double>(std::mt19937&, height_t<double>)> gen_R = [=] (std::mt19937& r, height_t<double> h) -> dihedral_inf_t<double> { + dihedral_inf_t<double> rot; + rot.is_reflection = true; + std::normal_distribution<double> dist(0.0,1.0); + + double amount = epsilon * dist(r); + + rot.x = 2 * h.x + 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 + +#ifdef HAVE_GLUT + // 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); + double max_h = INT64_MIN; + double min_h = INT64_MAX; + for (v_t i = 0; i < pow(L, 2); i++) { + double cur_h = (s.R.act_inverse(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++) { + double cur_h = (s.R.act_inverse(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(); + }; +#endif + } + + // run wolff for N cluster flips + wolff(N, s, gen_R, measurement, rng, 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 + + if (draw) { + } + + return 0; + +} + diff --git a/examples/src/models/roughening/wolff_dgm.cpp b/examples/src/models/roughening/wolff_dgm.cpp new file mode 100644 index 0000000..8395382 --- /dev/null +++ b/examples/src/models/roughening/wolff_dgm.cpp @@ -0,0 +1,164 @@ + +#include <getopt.h> + +#ifdef HAVE_GLUT +#include <GL/glut.h> +#endif + +// include your group and spin space +#include "dihedral_inf.hpp" +#include "height.hpp" + +#include <randutils/randutils.hpp> + +// include wolff.h +#include <wolff.hpp> + +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 + randutils::auto_seed_128 seeds; + std::mt19937 rng{seeds}; + + // define spin-spin coupling + std::function <double(const height_t<int64_t>&, const height_t<int64_t>&)> Z = [] (const height_t<int64_t>& h1, const height_t<int64_t>& h2) -> double { + return -pow(h1.x - h2.x, 2); + }; + + // define spin-field coupling + std::function <double(const height_t<int64_t> &)> B = [=] (const 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>(std::mt19937&, height_t<int64_t>)> gen_R = [=] (std::mt19937& r, height_t<int64_t> h) -> dihedral_inf_t<int64_t> { + dihedral_inf_t<int64_t> rot; + rot.is_reflection = true; + + std::uniform_int_distribution<int64_t> dist(-epsilon,epsilon); + + rot.x = 2 * h.x + dist(r); + + 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 + +#ifdef HAVE_GLUT + // 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 = (s.R.act_inverse(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 = (s.R.act_inverse(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(); + }; +#endif + } + + // run wolff for N cluster flips + wolff(N, s, gen_R, measurement, rng, 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); + + if (draw) { + } + + return 0; + +} + |