From 19d657b60b22159359f7a229f5a5b73e729cff79 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Wed, 6 Nov 2019 13:55:38 -0500 Subject: added support for saving networks to the library and revamped the sample method --- lib/include/graph.hpp | 3 ++ lib/include/network.hpp | 3 ++ lib/src/graph.cpp | 59 +++++++++++++++++++++++++++++++++++++++ lib/src/network.cpp | 37 +++++++++++++++++++++++++ src/sample.cpp | 58 +++++++++------------------------------ src/sample.hpp | 4 ++- src/sample_fracture.cpp | 73 ++++++++++++++++++++++++++++++++++++------------- 7 files changed, 172 insertions(+), 65 deletions(-) diff --git a/lib/include/graph.hpp b/lib/include/graph.hpp index 9d2b5a1..c78fe9f 100644 --- a/lib/include/graph.hpp +++ b/lib/include/graph.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -46,4 +47,6 @@ public: void helper(unsigned n, std::mt19937& rng); graph const rotate(); + + std::string write() const; }; diff --git a/lib/include/network.hpp b/lib/include/network.hpp index 5b474bb..d562530 100644 --- a/lib/include/network.hpp +++ b/lib/include/network.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,8 @@ public: current_info empty; return empty; }; + + std::string write(); }; class fuse_network : public network { diff --git a/lib/src/graph.cpp b/lib/src/graph.cpp index 1475c40..c75d466 100644 --- a/lib/src/graph.cpp +++ b/lib/src/graph.cpp @@ -1,6 +1,7 @@ #include "graph.hpp" #include +#include #define JC_VORONOI_IMPLEMENTATION #define JCV_REAL_TYPE double @@ -477,3 +478,61 @@ graph const graph::rotate() { return g2; } + +std::string graph::write() const { + std::string output; + + output += "\"vr\"->{"; + for (const graph::vertex &v : vertices) { + output += "{" + std::to_string(v.r.x) + "," + std::to_string(v.r.y) + "},"; + } + output.pop_back(); + output += "},\"vp\"->{"; + for (const graph::vertex &v : vertices) { + output += "{"; + for (const graph::coordinate &r : v.polygon) { + output += "{" + std::to_string(r.x) + "," + std::to_string(r.y) + "},"; + } + output.pop_back(); + output += "},"; + } + output.pop_back(); + output += "},\"ur\"->{"; + for (const graph::vertex &v : dual_vertices) { + output += "{" + std::to_string(v.r.x) + "," + std::to_string(v.r.y) + "},"; + } + output.pop_back(); + output += "},\"up\"->{"; + for (const graph::vertex &v : dual_vertices) { + output += "{"; + for (const graph::coordinate &r : v.polygon) { + output += "{" + std::to_string(r.x) + "," + std::to_string(r.y) + "},"; + } + output.pop_back(); + output += "},"; + } + output.pop_back(); + output += "},\"e\"->{"; + for (const graph::edge &e : edges) { + output += "{" + std::to_string(e.v[0]) + "," + std::to_string(e.v[1]) + "},"; + } + output.pop_back(); + output += "},\"ec\"->{"; + for (const graph::edge &e : edges) { + output += "{" + std::to_string(e.crossings[0]) + "," + std::to_string(e.crossings[1]) + "},"; + } + output.pop_back(); + output += "},\"d\"->{"; + for (const graph::edge &e : dual_edges) { + output += "{" + std::to_string(e.v[0]) + "," + std::to_string(e.v[1]) + "},"; + } + output.pop_back(); + output += "},\"dc\"->{"; + for (const graph::edge &e : dual_edges) { + output += "{" + std::to_string(e.crossings[0]) + "," + std::to_string(e.crossings[1]) + "},"; + } + output.pop_back(); + output += "}"; + + return output; +} diff --git a/lib/src/network.cpp b/lib/src/network.cpp index 05fc0dd..561796b 100644 --- a/lib/src/network.cpp +++ b/lib/src/network.cpp @@ -1,5 +1,6 @@ #include "network.hpp" +#include class nofuseException : public std::exception { virtual const char* what() const throw() { return "No valid fuse was available to break."; } @@ -511,6 +512,42 @@ void network::break_edge(unsigned e, bool unbreak) { py.break_edge(e, unbreak); } +std::string network::write() { + std::string output; + + current_info c = this->get_current_info(); + + output += "\"fuses\"->{"; + for (unsigned i = 0; i < G.edges.size(); i++) { + if (!fuses[i]) { + output += std::to_string(i) +","; + } + } + output.pop_back(); + output += "},\"backbone\"->{"; + for (unsigned i = 0; i < G.edges.size(); i++) { + if (!backbone[i]) { + output += std::to_string(i) + ","; + } + } + output.pop_back(); + output += "},\"thresholds\"->{"; + for (const long double& t : thresholds) { + output += std::to_string(t) + ","; + } + output.pop_back(); + output += "},\"conductivity\"->{" + std::to_string(c.conductivity[0]) + "," + std::to_string(c.conductivity[1]); + output += "},\"currents\"->{"; + for (const double& t : c.currents) { + output += std::to_string(t) + ","; + } + output.pop_back(); + output += "}," + G.write(); + + return output; +}; + + fuse_network::fuse_network(const graph& G, cholmod_common* c, double weight) : network(G, c), weight(weight) {} diff --git a/src/sample.cpp b/src/sample.cpp index febff19..e7ca09b 100644 --- a/src/sample.cpp +++ b/src/sample.cpp @@ -3,8 +3,13 @@ #include #include -sample::sample(double Lx, double Ly, double beta) { - std::string filename = "fracture_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_" + std::to_string(beta) + "_sample.dat"; +sample::sample(unsigned n, double a, double beta) { + std::string filename = "sample_" + std::to_string(n) + "_" + std::to_string(a) + "_" + std::to_string(beta) + ".dat"; + sample_file.open(filename, std::ifstream::app); +} + +sample::sample(unsigned Lx, unsigned Ly, double beta) { + std::string filename = "sample_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_" + std::to_string(beta) + ".dat"; sample_file.open(filename, std::ifstream::app); } @@ -14,56 +19,19 @@ sample::~sample() { void sample::pre_fracture(const network& n) { sample_file << "<|"; - sample_file << "\"vr\"->{"; - for (const graph::vertex &v : n.G.vertices) { - sample_file <{"; - for (const graph::vertex &v : n.G.vertices) { - sample_file << "{"; - for (const graph::coordinate &r : v.polygon) { - sample_file <{"; - for (const graph::vertex &v : n.G.dual_vertices) { - sample_file <{"; - for (const graph::vertex &v : n.G.dual_vertices) { - sample_file << "{"; - for (const graph::coordinate &r : v.polygon) { - sample_file <{"; - for (const graph::edge &e : n.G.edges) { - sample_file << "{" << e.v[0] << "," << e.v[1] << "},"; - } - sample_file << "},\"ec\"->{"; - for (const graph::edge &e : n.G.edges) { - sample_file << "{" << e.crossings[0] << "," << e.crossings[1] << "},"; - } - sample_file << "},\"d\"->{"; - for (const graph::edge &e : n.G.dual_edges) { - sample_file << "{" << e.v[0] << "," << e.v[1] << "},"; - } - sample_file << "},\"dc\"->{"; - for (const graph::edge &e : n.G.dual_edges) { - sample_file << "{" << e.crossings[0] << "," << e.crossings[1] << "},"; - } - sample_file << "},\"data\"->{"; + sample_file << n.G.write(); + sample_file << ",\"data\"->{"; } void sample::bond_broken(const network& net, const current_info& cur, unsigned i) { long double c = logl(cur.conductivity[0] / fabs(cur.currents[i])) + net.thresholds[i]; - sample_file << "{" << i << "," << c << "," << cur.conductivity[0] << "},"; + sample_string += "{" + std::to_string(i) + "," + std::to_string(c) + "," + std::to_string(cur.conductivity[0]) + "},"; } void sample::post_fracture(network &n) { - sample_file << "}"; + sample_string.pop_back(); + sample_file << sample_string + "}"; sample_file << "|>\n"; + sample_string.clear(); } diff --git a/src/sample.hpp b/src/sample.hpp index d5d6fe5..c21daca 100644 --- a/src/sample.hpp +++ b/src/sample.hpp @@ -18,9 +18,11 @@ class sample : public hooks { // private: std::ofstream sample_file; + std::string sample_string; public: - sample(double, double, double); + sample(unsigned, double, double); + sample(unsigned, unsigned, double); ~sample(); void pre_fracture(const network &) override; diff --git a/src/sample_fracture.cpp b/src/sample_fracture.cpp index eaba69e..b01b4d6 100644 --- a/src/sample_fracture.cpp +++ b/src/sample_fracture.cpp @@ -16,28 +16,43 @@ #include int main(int argc, char* argv[]) { - int opt; unsigned N = 1; unsigned Lx = 16; - double Ly = 16; + unsigned Ly = 16; double beta = 0.5; - while ((opt = getopt(argc, argv, "N:X:Y:b:")) != -1) { + unsigned n = 128; + double a = 1.0; + bool use_aN = false; + double w = 0.5; + + while ((opt = getopt(argc, argv, "N:X:Y:b:n:a:w:")) != -1) { switch (opt) { case 'N': N = (unsigned)atof(optarg); break; case 'X': - Lx = atof(optarg); + Lx = atoi(optarg); break; case 'Y': - Ly = atof(optarg); + Ly = atoi(optarg); break; case 'b': beta = atof(optarg); break; + case 'n': + n = (unsigned)atof(optarg); + use_aN = true; + break; + case 'a': + a = atof(optarg); + use_aN = true; + break; + case 'w': + w = atof(optarg); + break; default: exit(1); } @@ -46,23 +61,43 @@ int main(int argc, char* argv[]) { cholmod_common c; CHOL_F(start)(&c); - sample meas(Lx, Ly, beta); - //sample meas2(Ly, Lx, beta); - randutils::auto_seed_128 seeds; std::mt19937 rng{seeds}; - for (unsigned trial = 0; trial < N; trial++) { - graph G(Lx, 1.0, rng); - elastic_network network(G, &c); - network.set_thresholds(beta, rng); - network.fracture(meas); - - /*graph G2 = G.rotate(); - class network network2(G2, &c); - network2.thresholds = network.thresholds; - network2.fracture(meas2); - */ + if (use_aN) { + sample meas(n, a, beta); + + for (unsigned trial = 0; trial < N; trial++) { + while (true) { + try { + graph G(n, a, rng); + elastic_network fuse_network(G, &c, w); + fuse_network.set_thresholds(beta, rng); + fuse_network.fracture(meas); + break; + } catch (std::exception &e) { + std::cout << e.what() << '\n'; + } + } + } + } else { + sample meas(Lx, Ly, beta); + + const graph G(Lx, Ly); + const elastic_network fuse_network(G, &c, w); + + for (unsigned trial = 0; trial < N; trial++) { + while (true) { + try { + elastic_network net = fuse_network; + net.set_thresholds(beta, rng); + net.fracture(meas); + break; + } catch (std::exception &e) { + std::cout << e.what() << '\n'; + } + } + } } CHOL_F(finish)(&c); -- cgit v1.2.3-54-g00ecf