summaryrefslogtreecommitdiff
path: root/examples/src/models/On
diff options
context:
space:
mode:
authorJaron Kent-Dobias <jaron@kent-dobias.com>2018-10-10 21:45:32 -0400
committerJaron Kent-Dobias <jaron@kent-dobias.com>2018-10-10 21:45:32 -0400
commita43ff1f98e9b9814f858bccb11c174b418458491 (patch)
treeae7e094d914eddb8a1ae9548420ba8c2f329ffae /examples/src/models/On
parent6e264d243f0b29d90e90b605b6cdeab8227129c9 (diff)
downloadc++-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/On')
-rw-r--r--examples/src/models/On/CMakeLists.txt29
-rw-r--r--examples/src/models/On/orthogonal.hpp202
-rw-r--r--examples/src/models/On/vector.hpp118
-rw-r--r--examples/src/models/On/wolff_On.cpp268
4 files changed, 617 insertions, 0 deletions
diff --git a/examples/src/models/On/CMakeLists.txt b/examples/src/models/On/CMakeLists.txt
new file mode 100644
index 0000000..26985b9
--- /dev/null
+++ b/examples/src/models/On/CMakeLists.txt
@@ -0,0 +1,29 @@
+
+add_executable(wolff_planar wolff_On.cpp)
+add_executable(wolff_planar_2d_no-field wolff_On.cpp)
+add_executable(wolff_heisenberg wolff_On.cpp)
+
+set_target_properties(wolff_planar PROPERTIES COMPILE_FLAGS "-DN_COMP=2")
+set_target_properties(wolff_planar_2d_no-field PROPERTIES COMPILE_FLAGS "-DN_COMP=2 -DDIMENSION=2 -DNOFIELD")
+set_target_properties(wolff_heisenberg PROPERTIES COMPILE_FLAGS "-DN_COMP=3")
+
+find_library(GL NAMES GL)
+find_library(GLU NAMES GLU)
+find_library(GLUT NAMES glut)
+
+if (${GLUT} MATCHES "GLUT-NOTFOUND")
+ target_link_libraries(wolff_planar wolff wolff_examples)
+ target_link_libraries(wolff_planar_2d_no-field wolff wolff_examples)
+ target_link_libraries(wolff_heisenberg wolff wolff_examples)
+else()
+ target_compile_definitions(wolff_planar PUBLIC HAVE_GLUT)
+ target_compile_definitions(wolff_planar_2d_no-field PUBLIC HAVE_GLUT)
+ target_compile_definitions(wolff_heisenberg PUBLIC HAVE_GLUT)
+
+ target_link_libraries(wolff_planar wolff wolff_examples glut GL GLU)
+ target_link_libraries(wolff_planar_2d_no-field wolff wolff_examples glut GL GLU)
+ target_link_libraries(wolff_heisenberg wolff wolff_examples glut GL GLU)
+endif()
+
+install(TARGETS wolff_planar wolff_planar_2d_no-field wolff_heisenberg DESTINATION ${CMAKE_INSTALL_BINDIR})
+
diff --git a/examples/src/models/On/orthogonal.hpp b/examples/src/models/On/orthogonal.hpp
new file mode 100644
index 0000000..f13357f
--- /dev/null
+++ b/examples/src/models/On/orthogonal.hpp
@@ -0,0 +1,202 @@
+
+#pragma once
+
+#include <stdlib.h>
+#include <random>
+#include <cmath>
+
+#include <wolff/state.hpp>
+#include <wolff/types.h>
+#include "vector.hpp"
+
+template <q_t q, class T>
+class orthogonal_t : public std::array<std::array<T, q>, q> {
+ public :
+ bool is_reflection;
+
+ orthogonal_t() : is_reflection(false) {
+ for (q_t i = 0; i < q; i++) {
+ (*this)[i].fill(0);
+ (*this)[i][i] = (T)1;
+ }
+ }
+
+ vector_t<q, T> act(const vector_t <q, T>& v) const {
+ vector_t <q, T> v_rot;
+ v_rot.fill(0);
+
+ if (is_reflection) {
+ double prod = 0;
+ for (q_t i = 0; i < q; i++) {
+ prod += v[i] * (*this)[0][i];
+ }
+ for (q_t i = 0; i < q; i++) {
+ v_rot[i] = v[i] - 2 * prod * (*this)[0][i];
+ }
+ } else {
+ for (q_t i = 0; i < q; i++) {
+ for (q_t j = 0; j < q; j++) {
+ v_rot[i] += (*this)[i][j] * v[j];
+ }
+ }
+ }
+
+ return v_rot;
+ }
+
+ orthogonal_t<q, T> act(const orthogonal_t <q, T>& m) const {
+ orthogonal_t <q, T> m_rot;
+
+ m_rot.is_reflection = false;
+
+ if (is_reflection) {
+ for (q_t i = 0; i < q; i++) {
+ double akOki = 0;
+
+ for (q_t k = 0; k < q; k++) {
+ akOki += (*this)[0][k] * m[k][i];
+ }
+
+ for (q_t j = 0; j < q; j++) {
+ m_rot[j][i] = m[j][i] - 2 * akOki * (*this)[0][j];
+ }
+ }
+ } else {
+ for (q_t i = 0; i < q; i++) {
+ m_rot[i].fill(0);
+ for (q_t j = 0; j < q; j++) {
+ for (q_t k = 0; k < q; k++) {
+ m_rot[i][j] += (*this)[i][j] * m[j][k];
+ }
+ }
+ }
+ }
+
+ return m_rot;
+ }
+
+ vector_t <q, T> act_inverse(const vector_t <q, T>& v) const {
+ if (is_reflection) {
+ return this->act(v); // reflections are their own inverse
+ } else {
+ vector_t <q, T> v_rot;
+ v_rot.fill(0);
+
+ for (q_t i = 0; i < q; i++) {
+ for (q_t j = 0; j < q; j++) {
+ v_rot[i] += (*this)[j][i] * v[j];
+ }
+ }
+
+ return v_rot;
+ }
+ }
+
+ vector_t <q, T> act_inverse(const orthogonal_t <q, T>& m) const {
+ if (is_reflection) {
+ return this->act(m); // reflections are their own inverse
+ } else {
+ orthogonal_t <q, T> m_rot;
+ m_rot.is_reflection = false;
+
+ for (q_t i = 0; i < q; i++) {
+ m_rot[i].fill(0);
+ for (q_t j = 0; j < q; j++) {
+ for (q_t k = 0; k < q; k++) {
+ m_rot[i][j] += (*this)[j][i] * m[j][k];
+ }
+ }
+ }
+
+ return m_rot;
+ }
+ }
+
+};
+
+
+template <q_t q>
+orthogonal_t <q, double> generate_rotation_uniform (std::mt19937& r, const vector_t <q, double>& v) {
+ std::normal_distribution<double> dist(0.0,1.0);
+ orthogonal_t <q, double> ptr;
+ ptr.is_reflection = true;
+
+ double v2 = 0;
+
+ for (q_t i = 0; i < q; i++) {
+ ptr[0][i] = dist(r);
+ v2 += ptr[0][i] * ptr[0][i];
+ }
+
+ double mag_v = sqrt(v2);
+
+ for (q_t i = 0; i < q; i++) {
+ ptr[0][i] /= mag_v;
+ }
+
+ return ptr;
+}
+
+template <q_t q>
+orthogonal_t <q, double> generate_rotation_perturbation (std::mt19937& r, const vector_t <q, double>& v0, double epsilon, unsigned int n) {
+ std::normal_distribution<double> dist(0.0,1.0);
+ orthogonal_t <q, double> m;
+ m.is_reflection = true;
+
+ vector_t <q, double> v;
+
+ if (n > 1) {
+ std::uniform_int_distribution<unsigned int> udist(0, n);
+ unsigned int rotation = udist(r);
+
+ double cosr = cos(2 * M_PI * rotation / (double)n / 2.0);
+ double sinr = sin(2 * M_PI * rotation / (double)n / 2.0);
+
+ v[0] = v0[0] * cosr - v0[1] * sinr;
+ v[1] = v0[1] * cosr + v0[0] * sinr;
+
+ for (q_t i = 2; i < q; i++) {
+ v[i] = v0[i];
+ }
+ } else {
+ v = v0;
+ }
+
+ double m_dot_v = 0;
+
+ for (q_t i = 0; i < q; i++) {
+ m[0][i] = dist(r); // create a random vector
+ m_dot_v += m[0][i] * v[i];
+ }
+
+ double v2 = 0;
+
+ for (q_t i = 0; i < q; i++) {
+ m[0][i] = m[0][i] - m_dot_v * v[i]; // find the component orthogonal to v
+ v2 += pow(m[0][i], 2);
+ }
+
+ double mag_v = sqrt(v2);
+
+ for (q_t i = 0; i < q; i++) {
+ m[0][i] /= mag_v; // normalize
+ }
+
+ v2 = 0;
+
+ double factor = epsilon * dist(r);
+
+ for (q_t i = 0; i < q; i++) {
+ m[0][i] += factor * v[i]; // perturb orthogonal vector in original direction
+ v2 += pow(m[0][i], 2);
+ }
+
+ mag_v = sqrt(v2);
+
+ for (q_t i = 0; i < q; i++) {
+ m[0][i] /= mag_v; // normalize
+ }
+
+ return m;
+}
+
diff --git a/examples/src/models/On/vector.hpp b/examples/src/models/On/vector.hpp
new file mode 100644
index 0000000..1cdb60a
--- /dev/null
+++ b/examples/src/models/On/vector.hpp
@@ -0,0 +1,118 @@
+
+#pragma once
+
+#include <stdlib.h>
+#include <cmath>
+#include <array>
+
+#include <wolff/types.h>
+
+template <q_t q, class T>
+class vector_t : public std::array<T, q> {
+ public:
+
+ // M_t needs to hold the sum of nv spins
+ typedef vector_t <q, T> M_t;
+
+ // F_t needs to hold the double-weighted sum of spins
+ typedef vector_t <q, double> F_t;
+
+ vector_t() {
+ this->fill((T)0);
+ (*this)[1] = (T)1;
+ }
+
+ vector_t(const T *x) {
+ for (q_t i = 0; i < q; i++) {
+ (*this)[i] = x[i];
+ }
+ }
+
+ template <class U>
+ inline vector_t<q, T>& operator+=(const vector_t<q, U> &v) {
+ for (q_t i = 0; i < q; i++) {
+ (*this)[i] += (U)v[i];
+ }
+ return *this;
+ }
+
+ template <class U>
+ inline vector_t<q, T>& operator-=(const vector_t<q, U> &v) {
+ for (q_t i = 0; i < q; i++) {
+ (*this)[i] -= (U)v[i];
+ }
+ return *this;
+ }
+
+ inline vector_t<q, T> operator*(v_t x) const {
+ vector_t<q, T> result;
+ for (q_t i = 0; i < q; i++) {
+ result[i] = x * (*this)[i];
+ }
+
+ return result;
+ }
+
+ inline vector_t<q, double> operator*(double x) const {
+ vector_t<q, double> result;
+ for (q_t i = 0; i < q; i++) {
+ result[i] = x * (*this)[i];
+ }
+
+ return result;
+ }
+
+ inline vector_t<q, T> operator-(const vector_t<q, T>& v) const {
+ vector_t<q, T> diff = *this;
+ diff -= v;
+ return diff;
+ }
+};
+
+
+template<q_t q, class T>
+double norm_squared(vector_t<q, T> v) {
+ double tmp = 0;
+ for (T &x : v) {
+ tmp += pow(x, 2);
+ }
+
+ return tmp;
+}
+
+template <q_t q, class T>
+void write_magnetization(vector_t <q, T> M, FILE *outfile) {
+ for (q_t i = 0; i < q; i++) {
+ fwrite(&(M[i]), sizeof(T), q, outfile);
+ }
+}
+
+// below functions and definitions are unnecessary for wolff.h but useful.
+
+template <q_t q> // save some space and don't write whole doubles
+void write_magnetization(vector_t <q, double> M, FILE *outfile) {
+ for (q_t i = 0; i < q; i++) {
+ float M_tmp = (float)M[i];
+ fwrite(&M_tmp, sizeof(float), 1, outfile);
+ }
+}
+
+template <q_t q, class T>
+T dot(const vector_t <q, T>& v1, const vector_t <q, T>& v2) {
+ T prod = 0;
+
+ for (q_t i = 0; i < q; i++) {
+ prod += v1[i] * v2[i];
+ }
+
+ return prod;
+}
+
+template <q_t q, class T>
+double H_vector(const vector_t <q, T>& v1, T *H) {
+ vector_t <q, T> H_vec(H);
+ return (double)(dot <q, T> (v1, H_vec));
+}
+
+char const *ON_strings[] = {"TRIVIAL", "ISING", "PLANAR", "HEISENBERG"};
+
diff --git a/examples/src/models/On/wolff_On.cpp b/examples/src/models/On/wolff_On.cpp
new file mode 100644
index 0000000..e3568c7
--- /dev/null
+++ b/examples/src/models/On/wolff_On.cpp
@@ -0,0 +1,268 @@
+
+#include <getopt.h>
+#include <stdio.h>
+
+#ifdef HAVE_GLUT
+#include <GL/glut.h>
+#endif
+
+#include "orthogonal.hpp"
+#include "vector.hpp"
+
+#include <wolff.hpp>
+#include <measure.hpp>
+#include <colors.h>
+#include <randutils/randutils.hpp>
+
+typedef orthogonal_t <N_COMP, double> orthogonal_R_t;
+typedef vector_t <N_COMP, double> vector_R_t;
+typedef state_t <orthogonal_R_t, vector_R_t> On_t;
+
+// angle from the x-axis of a two-vector
+double theta(vector_R_t v) {
+ double x = v[0];
+ double y = v[1];
+
+ double val = atan(y / x);
+
+ if (x < 0.0 && y > 0.0) {
+ return M_PI + val;
+ } else if ( x < 0.0 && y < 0.0 ) {
+ return - M_PI + val;
+ } else {
+ return val;
+ }
+}
+
+double H_modulated(vector_R_t v, int order, double mag) {
+ return mag * cos(order * theta(v));
+}
+
+int main(int argc, char *argv[]) {
+
+ count_t N = (count_t)1e7;
+
+#ifdef DIMENSION
+ D_t D = DIMENSION;
+#else
+ D_t D = 2;
+#endif
+ L_t L = 128;
+ double T = 2.26918531421;
+ double *H_vec = (double *)calloc(MAX_Q, sizeof(double));
+
+ bool silent = false;
+ bool use_pert = false;
+ bool N_is_sweeps = false;
+ bool draw = false;
+ unsigned int window_size = 512;
+
+ bool modulated_field = false;
+ unsigned int order = 1;
+
+ int opt;
+ q_t H_ind = 0;
+ double epsilon = 1;
+
+// unsigned char measurement_flags = measurement_energy | measurement_clusterSize;
+
+ unsigned char measurement_flags = 0;
+
+ while ((opt = getopt(argc, argv, "N:D:L:T:H:spe:mo:M:Sdw:")) != -1) {
+ switch (opt) {
+ case 'N': // number of steps
+ N = (count_t)atof(optarg);
+ break;
+#ifdef DIMENSION
+ case 'D': // dimension
+ printf("Dimension was specified at compile time, you can't change it now!\n");
+ exit(EXIT_FAILURE);
+#else
+ case 'D': // dimension
+ D = atoi(optarg);
+ break;
+#endif
+ 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_vec[H_ind] = atof(optarg);
+ H_ind++;
+ break;
+ case 's': // don't print anything during simulation. speeds up slightly
+ silent = true;
+ break;
+ case 'p':
+ use_pert = true;
+ break;
+ case 'e':
+ epsilon = atof(optarg);
+ break;
+ case 'm':
+ modulated_field = true;
+ break;
+ case 'M':
+ measurement_flags ^= 1 << atoi(optarg);
+ break;
+ case 'o':
+ order = atoi(optarg);
+ break;
+ case 'S':
+ N_is_sweeps = 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);
+ }
+ }
+
+ unsigned long timestamp;
+
+ {
+ struct timespec spec;
+ clock_gettime(CLOCK_REALTIME, &spec);
+ timestamp = spec.tv_sec*1000000000LL + spec.tv_nsec;
+ }
+
+ const char *pert_type;
+
+ std::function <orthogonal_R_t(std::mt19937&, vector_R_t)> gen_R;
+
+ if (use_pert) {
+ double Hish;
+ if (modulated_field) {
+ Hish = fabs(H_vec[0]);
+ } else {
+ double H2 = 0;
+ for (q_t i = 0; i < N_COMP; i++) {
+ H2 += pow(H_vec[i], 2);
+ }
+ Hish = sqrt(H2);
+ }
+
+ epsilon = sqrt((N_COMP - 1) * T / (D + Hish / 2)) / 2;
+
+ gen_R = std::bind(generate_rotation_perturbation <N_COMP>, std::placeholders::_1, std::placeholders::_2, epsilon, order);
+ pert_type = "PERTURB5";
+ } else {
+ gen_R = generate_rotation_uniform <N_COMP>;
+ pert_type = "UNIFORM";
+ }
+
+ FILE *outfile_info = fopen("wolff_metadata.txt", "a");
+
+ fprintf(outfile_info, "<| \"ID\" -> %lu, \"MODEL\" -> \"%s\", \"q\" -> %d, \"D\" -> %" PRID ", \"L\" -> %" PRIL ", \"NV\" -> %" PRIv ", \"NE\" -> %" PRIv ", \"T\" -> %.15f, \"FIELD_TYPE\" -> ", timestamp, ON_strings[N_COMP], N_COMP, D, L, (v_t)pow(L, D), D * (v_t)pow(L, D), T);
+ if (modulated_field) {
+ fprintf(outfile_info, "\"MODULATED\", \"ORDER\" -> %d, \"H\" -> %.15f, ", order, H_vec[0]);
+ } else {
+ fprintf(outfile_info, "\"VECTOR\", \"H\" -> {");
+ for (q_t i = 0; i < N_COMP; i++) {
+ fprintf(outfile_info, "%.15f", H_vec[i]);
+ if (i < N_COMP - 1) {
+ fprintf(outfile_info, ", ");
+ }
+ }
+ fprintf(outfile_info, "}, ");
+ }
+
+ fprintf(outfile_info, "\"GENERATOR\" -> \"%s\"", pert_type);
+
+ if (use_pert) {
+ fprintf(outfile_info, ", \"EPS\" -> %g", epsilon);
+ }
+
+ fprintf(outfile_info, " |>\n");
+
+ fclose(outfile_info);
+
+ FILE **outfiles = measure_setup_files(measurement_flags, timestamp);
+
+ std::function <void(const On_t&)> 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;
+ };
+ } else if (draw) {
+#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);
+
+ other_f = [&] (const On_t& s) {
+ glClear(GL_COLOR_BUFFER_BIT);
+ for (v_t i = 0; i < pow(L, 2); i++) {
+ vector_R_t v_tmp = s.R.act_inverse(s.spins[i]);
+ double thetai = fmod(2 * M_PI + theta(v_tmp), 2 * M_PI);
+ double saturation = 0.7;
+ double value = 0.9;
+ double chroma = saturation * value;
+ glColor3f(chroma * hue_to_R(thetai) + (value - chroma), chroma * hue_to_G(thetai) + (value - chroma), chroma * hue_to_B(thetai) + (value - chroma));
+ glRecti(i / L, i % L, (i / L) + 1, (i % L) + 1);
+ }
+ glFlush();
+ };
+#endif
+ } else {
+ other_f = [] (const On_t& s) {};
+ }
+
+ std::function <void(const On_t&)> measurements = measure_function_write_files(measurement_flags, outfiles, other_f);
+
+ std::function <double(const vector_R_t&)> H;
+
+ if (modulated_field) {
+ H = std::bind(H_modulated, std::placeholders::_1, order, H_vec[0]);
+ } else {
+ H = std::bind(H_vector <N_COMP, double>, std::placeholders::_1, H_vec);
+ }
+
+ // initialize random number generator
+ randutils::auto_seed_128 seeds;
+ std::mt19937 rng{seeds};
+
+#ifndef NOFIELD
+ state_t <orthogonal_R_t, vector_R_t> s(D, L, T, dot <N_COMP, double>, H);
+#else
+ state_t <orthogonal_R_t, vector_R_t> s(D, L, T, dot <N_COMP, double>);
+#endif
+
+ 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 <orthogonal_R_t, vector_R_t> (N, s, gen_R, measurements, rng, silent);
+ 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);
+ } else {
+ wolff <orthogonal_R_t, vector_R_t> (N, s, gen_R, measurements, rng, silent);
+ }
+
+ measure_free_files(measurement_flags, outfiles);
+ free(H_vec);
+
+ return 0;
+}
+