#include "measurements.hpp" #include #include class badcycleException: public std::exception { virtual const char* what() const throw() { return "Could not find a valid cycle on the broken system."; } } badcycleex; void update_distribution_file(std::string id, const std::vector& data, std::string model_string) { std::string filename = model_string + id + ".dat"; std::ifstream file(filename); std::vector data_old(data.size(), 0); if (file.is_open()) { for (unsigned i = 0; i < data.size(); i++) { uint64_t num; file >> num; data_old[i] = num; } file.close(); } std::ofstream file_out(filename); for (unsigned i = 0; i < data.size(); i++) { file_out < void update_field_file(std::string id, const std::vector& data, std::string model_string) { std::string filename = model_string + id + ".dat"; std::ifstream file(filename); std::vector data_old(data.size(), 0); if (file.is_open()) { for (unsigned j = 0; j < data.size(); j++) { file >> data_old[j]; } file.close(); } std::ofstream file_out(filename); for (unsigned j = 0; j < data.size(); j++) { file_out << data_old[j] + data[j] << " "; } file_out.close(); } fftw_complex* data_transform(unsigned Mx, unsigned My, fftw_plan forward_plan, double *fftw_forward_in, fftw_complex *fftw_forward_out) { fftw_execute(forward_plan); fftw_complex* output = (fftw_complex*)malloc(Mx * (My / 2 + 1) * sizeof(fftw_complex)); for (unsigned i = 0; i < Mx * (My / 2 + 1); i++) { output[i][0] = fftw_forward_out[i][0]; output[i][1] = fftw_forward_out[i][1]; } return output; } template void correlation(unsigned Mx, unsigned My, std::vector>& data, const fftw_complex* tx1, const fftw_complex* tx2, fftw_plan reverse_plan, fftw_complex *fftw_reverse_in, double *fftw_reverse_out) { for (unsigned i = 0; i < Mx * (My / 2 + 1); i++) { fftw_reverse_in[i][0] = tx1[i][0] * tx2[i][0] + tx1[i][1] * tx2[i][1]; fftw_reverse_in[i][1] = tx1[i][0] * tx2[i][1] - tx1[i][1] * tx2[i][0]; } fftw_execute(reverse_plan); for (unsigned j = 0; j < data.size(); j++) { for (unsigned i = 0; i < (Mx / 2 + 1) * (My / 2 + 1); i++) { data[j][i] += (T)pow(fftw_reverse_out[Mx * (i / (Mx / 2 + 1)) + i % (Mx / 2 + 1)] / (Mx * My), j + 1); } } } void autocorrelation2(double Lx, double Ly, unsigned Mx, unsigned My, std::vector& data, const std::list& pos, std::array count) { for (std::list::const_iterator it1 = pos.begin(); it1 != pos.end(); it1++) { for (std::list::const_iterator it2 = it1; it2 != pos.end(); it2++) { double Δx_tmp = fabs(it1->x - it2->x); double Δx = Δx_tmp < Lx / 2 ? Δx_tmp : Lx - Δx_tmp; double Δy_tmp = fabs(it1->y - it2->y); double Δy = Δy_tmp < Ly / 2 ? Δy_tmp : Ly - Δy_tmp; if (count[0] % 2 == 0) { data[(unsigned)(Mx * (Δx / Lx)) + (Mx / 2) * (unsigned)(My * (Δy / Ly))]++; } else { data[(unsigned)(Mx * (Δy / Ly)) + (Mx / 2) * (unsigned)(My * (Δx / Lx))]++; } } } } void correlation2(double Lx, double Ly, unsigned Mx, unsigned My, std::vector& data, const std::list& pos1, const std::list& pos2) { for (std::list::const_iterator it1 = pos1.begin(); it1 != pos1.end(); it1++) { for (std::list::const_iterator it2 = pos2.begin(); it2 != pos2.end(); it2++) { double Δx_tmp = fabs(it1->x - it2->x); double Δx = Δx_tmp < Lx / 2 ? Δx_tmp : Lx - Δx_tmp; double Δy_tmp = fabs(it1->y - it2->y); double Δy = Δy_tmp < Ly / 2 ? Δy_tmp : Ly - Δy_tmp; data[floor((Mx * Δx) / Lx) + (Mx / 2) * floor((My * Δy) / Ly)]++; } } } template void autocorrelation(unsigned Mx, unsigned My, std::vector>& out_data, fftw_plan forward_plan, double *fftw_forward_in, fftw_complex *fftw_forward_out, fftw_plan reverse_plan, fftw_complex *fftw_reverse_in, double *fftw_reverse_out) { fftw_execute(forward_plan); for (unsigned i = 0; i < My * (Mx / 2 + 1); i++) { fftw_reverse_in[i][0] = pow(fftw_forward_out[i][0], 2) + pow(fftw_forward_out[i][1], 2); fftw_reverse_in[i][1] = 0.0; } fftw_execute(reverse_plan); for (unsigned j = 0; j < out_data.size(); j++) { for (unsigned i = 0; i < (Mx / 2 + 1) * (My / 2 + 1); i++) { out_data[j][i] += (T)pow(fftw_reverse_out[Mx * (i / (Mx / 2 + 1)) + i % (Mx / 2 + 1)] / (Mx * My), j + 1); } } } unsigned edge_r_to_ind(graph::coordinate r, double Lx, double Ly, unsigned Mx, unsigned My) { return floor((Mx * r.x) / Lx) + Mx * floor((My * r.y) / Ly); } ma::ma(unsigned n, double a, double beta, double weight, bool one) : G(2 * n), sn(2 * n), ss(2 * n), sm(2 * n), sl(2 * n), sb(n + 1), sd(3 * n), sa(3 * n), sA(3 * n), si(10000), sI(10000), cn(pow((unsigned)sqrt(n), 2)), cs(pow((unsigned)sqrt(n), 2)), cm(pow((unsigned)sqrt(n), 2)), cl(pow((unsigned)sqrt(n), 2)), cb(pow((unsigned)sqrt(n), 2)), ca(pow((unsigned)sqrt(n), 2)), cA(pow((unsigned)sqrt(n), 2)) { if (beta != 0.0) { model_string = "fracture_" + std::to_string(n) + "_" + std::to_string(a) + "_" + std::to_string(beta) + "_" + std::to_string(weight) + "_"; } else { model_string = "fracture_" + std::to_string(n) + "_" + std::to_string(a) + "_INF_" + std::to_string(weight) + "_"; } if (one) { model_string = model_string + "o_"; } else { model_string = model_string + "t_"; } } ma::ma(unsigned Lx, unsigned Ly, double beta, double weight, bool one) : G(Lx * Ly / 2), sn(Lx * Ly / 2), ss(Lx * Ly / 2), sm(Lx * Ly / 2), sl(Lx * Ly / 2), sb(Lx * Ly / 2 + 1), sd(Lx * Ly), sa(Lx * Ly), sA(Lx * Ly), si(10000), sI(10000) { if (beta != 0.0) { model_string = "fracture_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_" + std::to_string(beta) + "_" + std::to_string(weight) + "_"; } else { model_string = "fracture_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_INF_" + std::to_string(weight) + "_"; } if (one) { model_string = model_string + "o_"; } else { model_string = model_string + "t_"; } } ma::~ma() { update_distribution_file("sn", sn, model_string); update_distribution_file("ss", ss, model_string); update_distribution_file("sm", sm, model_string); update_distribution_file("sl", sl, model_string); update_distribution_file("sb", sb, model_string); update_distribution_file("sd", sd, model_string); update_distribution_file("sa", sa, model_string); update_distribution_file("sA", sA, model_string); update_distribution_file("si", si, model_string); update_distribution_file("sI", sI, model_string); update_field_file("cl", cl, model_string); update_field_file("cm", cm, model_string); update_field_file("cs", cs, model_string); update_field_file("cn", cn, model_string); update_field_file("cb", cb, model_string); update_field_file("ca", ca, model_string); update_field_file("cA", cA, model_string); } void ma::pre_fracture(const network&) { lv = std::numeric_limits::lowest(); boost::remove_edge_if(trivial, G); avalanches = {}; num = 0; } void ma::bond_broken(const network& net, const current_info& cur, unsigned i) { long double c = net.thresholds[i] - logl(cur.currents[i]); if (c > lv) { lv = c; avalanches.push_back({i}); } else { avalanches.back().push_back(i); } for (unsigned j = 0; j < cur.currents.size(); j++) { if (logl(cur.currents[j]) >= -100 && logl(cur.currents[j]) < 0 && (!net.backbone[j] || j == i)) { si[(unsigned)(10000 * (logl(cur.currents[j]) + 100) / 100)]++; } else if (logl(cur.currents[j]) >= -100 && logl(cur.currents[j]) < 0 && net.backbone[j] && j != i) { sI[(unsigned)(10000 * (logl(cur.currents[j]) + 100) / 100)]++; } } boost::add_edge(net.G.dual_edges[i].v[0], net.G.dual_edges[i].v[1], {i}, G); num++; } void ma::post_fracture(network &n) { auto post_cracks = find_minimal_crack(G, n); std::vector component(boost::num_vertices(G)); unsigned num = boost::connected_components(G, &component[0]); if (post_cracks.size() > 2 || post_cracks.size() == 0) { throw badcycleex; } for (auto c : post_cracks) { std::list cl_cs; sl[c.second.size() - 1]++; for (unsigned e : c.second) { cl_cs.push_back(n.G.dual_edges[e].r); } autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(cl.size()), 2 * sqrt(cl.size()), cl, cl_cs, c.first); } unsigned crack_component = component[n.G.dual_edges[post_cracks.front().second.front()].v[0]]; std::vector> components(num); for (unsigned i = 0; i < n.G.dual_vertices.size(); i++) { components[component[i]].push_back(n.G.dual_vertices[i].r); } for (unsigned i = 0; i < num; i++) { if (i != crack_component) { sm[components[i].size() - 1]++; autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(cm.size()), 2 * sqrt(cm.size()), cm, components[i], post_cracks.front().first); } else { ss[components[i].size() - 1]++; autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(cs.size()), 2 * sqrt(cs.size()), cs, components[i], post_cracks.front().first); } sn[components[i].size() - 1]++; autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(cn.size()), 2 * sqrt(cn.size()), cn, components[i], post_cracks.front().first); } std::vector vertex_in(n.G.vertices.size()); for (unsigned i = 0; i < n.G.edges.size(); i++) { if (!n.backbone[i]) { vertex_in[n.G.edges[i].v[0]] = true; vertex_in[n.G.edges[i].v[1]] = true; } } unsigned bb_size = 0; std::list cb_co; for (unsigned i = 0; i < n.G.vertices.size(); i++) { if (vertex_in[i]) { bb_size++; cb_co.push_back(n.G.vertices[i].r); } } sb[bb_size]++; autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(cb.size()), 2 * sqrt(cb.size()), cb, cb_co, post_cracks.front().first); auto av_it = avalanches.rbegin(); av_it++; while (av_it != avalanches.rend()) { sa[(*av_it).size() - 1]++; std::list ca_co; for (unsigned e : (*av_it)) { ca_co.push_back(n.G.edges[e].r); } autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(ca.size()), 2 * sqrt(ca.size()), ca, ca_co, post_cracks.front().first); av_it++; } sA[avalanches.back().size() - 1]++; std::list cA_co; for (unsigned e : avalanches.back()) { cA_co.push_back(n.G.edges[e].r); } autocorrelation2(n.G.L.x, n.G.L.y, 2 * sqrt(cA.size()), 2 * sqrt(cA.size()), cA, cA_co, post_cracks.front().first); sd[num - 1]++; }