diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2019-09-12 14:06:07 -0400 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2019-09-12 14:06:07 -0400 |
commit | cd5ec90e72f2a8607ef59ce1b4d75c3671b97fda (patch) | |
tree | 53c761a0a73cde7c366f250184ac4d1321ef6f64 | |
parent | 72e7dbdebedbea61e4d9d984fb03843ce9fe2b7e (diff) | |
download | ising_sampling-master.tar.gz ising_sampling-master.tar.bz2 ising_sampling-master.zip |
-rw-r--r-- | sample_ising.cpp | 91 |
1 files changed, 69 insertions, 22 deletions
diff --git a/sample_ising.cpp b/sample_ising.cpp index 8e52017..0a8e952 100644 --- a/sample_ising.cpp +++ b/sample_ising.cpp @@ -9,41 +9,68 @@ using namespace wolff; +class track : public measurement<ising_t, ising_t, graph<>> { +public: + int e; + int m; + track(const wolff::system<ising_t, ising_t, graph<>>& s) { + e = -s.ne; + m = s.nv; + } + + void ghost_bond_visited(const system<ising_t, ising_t, graph<>>&, const typename graph<>::vertex&, + const ising_t& s_old, const ising_t& s_new, double dE) override { + m += s_new - s_old; + } + + void plain_bond_visited(const wolff::system<ising_t, ising_t, graph<>>& s, + const typename graph<>::halfedge& ed, const ising_t& si_new, + double dE) override { + e -= 2 * (si_new * s.s[ed.neighbor.ind]); + } +}; + class sample : public measurement<ising_t, ising_t, graph<>> { private: - typedef struct _dat { - int e; - int m; - } dat; - std::ofstream e_file; - dat e; + int e; + int m; + int64_t e_tot; + int64_t m_tot; + unsigned N; public: - sample(const wolff::system<ising_t, ising_t, graph<>>& s, unsigned D, unsigned L, double T, - double H) { - e_file.open("sample_" + std::to_string(D) + "_" + std::to_string(L) + "_" + std::to_string(T) + - "_" + std::to_string(H) + ".bin", - std::ios::out | std::ios::binary | std::ios::app); - e.e = -s.ne; - e.m = s.nv; + sample(int eold, int mold) { + e = eold; + m = mold; + e_tot = 0; + m_tot = 0; + N = 0; } - ~sample() { e_file.close(); } - void ghost_bond_visited(const system<ising_t, ising_t, graph<>>&, const typename graph<>::vertex&, const ising_t& s_old, const ising_t& s_new, double dE) override { - e.m += s_new - s_old; + m += s_new - s_old; } void plain_bond_visited(const wolff::system<ising_t, ising_t, graph<>>& s, const typename graph<>::halfedge& ed, const ising_t& si_new, double dE) override { - e.e -= 2 * (si_new * s.s[ed.neighbor.ind]); + e -= 2 * (si_new * s.s[ed.neighbor.ind]); } void post_cluster(unsigned n, unsigned, const wolff::system<ising_t, ising_t, graph<>>&) override { - e_file.write((char*)&e, 2 * sizeof(int)); + e_tot += e; + m_tot += m; + N++; + } + + double e_avg() const { + return ((double)e_tot) / N; + } + + double m_avg() const { + return ((double)m_tot) / N; } }; @@ -55,11 +82,12 @@ int main(int argc, char* argv[]) { unsigned L = 128; double T = 2 / log(1 + sqrt(2)); double H = 0; + double w = 1.0; int opt; // take command line arguments - while ((opt = getopt(argc, argv, "N:D:L:T:H:")) != -1) { + while ((opt = getopt(argc, argv, "N:D:L:T:H:w:")) != -1) { switch (opt) { case 'N': // number of steps N = (unsigned)atof(optarg); @@ -76,11 +104,16 @@ int main(int argc, char* argv[]) { case 'H': // field H = atof(optarg); break; + case 'w': + w = atof(optarg); + break; default: exit(EXIT_FAILURE); } } + unsigned N_wait = w * pow(L, D); + // define the spin-spin coupling std::function<double(const ising_t&, const ising_t&)> Z = [](const ising_t& s1, const ising_t& s2) -> double { return (double)(s1 * s2); }; @@ -104,11 +137,25 @@ int main(int argc, char* argv[]) { gen_r = gen_ising<graph<>>; // initailze the measurement object - sample A(S, D, L, T, H); - - // run wolff N times + track AA(S); + S.run_wolff(N_wait, gen_r, AA, rng); + sample A(AA.e, AA.m); S.run_wolff(N, gen_r, A, rng); + std::ifstream checkfile("out.dat"); + bool already_exists = checkfile.good(); + if (already_exists) { + checkfile.close(); + } + + std::ofstream outfile; + outfile.open("out.dat", std::ios::app); + if (!already_exists) { + outfile << "N D L T H E M\n"; + } + outfile << N << " " << D << " " << L << " " << T << " " << H << " " << A.e_avg() / S.nv << " " << A.m_avg() / S.nv << "\n"; + outfile.close(); + // exit return 0; } |