summaryrefslogtreecommitdiff
path: root/src/perc_meas.cpp
blob: e400982f5eecea8021fb725bae9ea5022de95a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

#include "perc_meas.hpp"
#include <iostream>
#include <cstdio>

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<uint64_t>& data, std::string model_string) {
  std::string filename = model_string + id + ".dat";
  std::ifstream file(filename);

  std::vector<uint64_t> 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 <<std::fixed<< data_old[i] + data[i] << " ";
  }

  file_out.close();
}

void update_distribution_file(std::string id, const std::vector<std::vector<uint64_t>>& data, std::string model_string) {
  std::string filename = model_string + id + ".dat";
  std::ifstream file(filename);

  std::vector<std::vector<uint64_t>> data_old(data.size());

  for (unsigned i = 0; i < data.size(); i++) {
    data_old[i].resize(data[0].size());
  }

  if (file.is_open()) {
    for (unsigned i = 0; i < data.size(); i++) {
      for (unsigned j = 0; j < data[0].size(); j++) {
        uint64_t num;
        file >> num;
        data_old[i][j] = num;
      }
    }

    file.close();
  }

  std::ofstream file_out(filename);

  for (unsigned i = 0; i < data.size(); i++) {
    for (unsigned j = 0; j < data[0].size(); j++) {
      file_out << std::fixed << data_old[i][j] + data[i][j] << " ";
    }
    file_out << "\n";
  }

  file_out.close();
}

pm::pm(unsigned n, double a) :
  G(2 * n),
  rank(2 * n),
  parent(2 * n),
  ds(&rank[0], &parent[0]),
  sn(3 * n),
  ss(2 * n),
  sm(2 * n),
  sl(2 * n),
  sb(3 * n),
  sd(3 * n)
{
  model_string = "percolation_" + std::to_string(n) + "_" + std::to_string(a) + "_";
  for (std::vector<uint64_t> &x : sn) {
    x.resize(2 * n);
  }
  for (std::vector<uint64_t> &x : sb) {
    x.resize(n);
  }
}

pm::pm(unsigned Lx, unsigned Ly) :
  G(Lx * Ly / 2),
  rank(Lx * Ly / 2),
  parent(Lx * Ly / 2),
  ds(&rank[0], &parent[0]),
  sn(Lx * Ly),
  ss(Lx * Ly / 2),
  sm(Lx * Ly / 2),
  sl(Lx * Ly / 2),
  sb(Lx * Ly),
  sd(Lx * Ly)
{
  model_string = "percolation_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_";
  for (std::vector<uint64_t> &x : sn) {
    x.resize(Lx * Ly / 2);
  }
  for (std::vector<uint64_t> &x : sb) {
    x.resize(Lx * Ly / 2);
  }
}

pm::~pm() {
  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);
}

void pm::pre_fracture(const network&) {
  boost::remove_edge_if(trivial, G);
  initialize_incremental_components(G, ds);
  incremental_components(G, ds);
  r = 0;
}

void pm::bond_broken(const network& net, const current_info& cur, unsigned i) {
  boost::add_edge(net.G.dual_edges[i].v[0], net.G.dual_edges[i].v[1], {i}, G);
  ds.union_set(net.G.dual_edges[i].v[0], net.G.dual_edges[i].v[1]);

  boost::component_index<VertexIndex> components(parent.begin(), parent.end());
  std::vector<unsigned> counts(components.size());

  BOOST_FOREACH(VertexIndex current_index, components) {
    unsigned comp_size = 0;
    BOOST_FOREACH(VertexIndex child_index, components[current_index]) {
      comp_size++;
    }

    sn[r][comp_size - 1]++;
  }

  std::vector<bool> vertex_in(net.G.vertices.size());

  for (unsigned i = 0; i < net.G.edges.size(); i++) {
    if (cur.currents[i] > CURRENT_CUTOFF) {
      vertex_in[net.G.edges[i].v[0]] = true;
      vertex_in[net.G.edges[i].v[1]] = true;
    }
  }

  unsigned bb_size = 0;

  for (unsigned i = 0; i < net.G.vertices.size(); i++) {
    if (vertex_in[i]) bb_size++;
  }

  sb[r][bb_size - 1]++;

  r++;
  last_cur = cur;
}

void pm::post_fracture(network &n) {
  auto post_cracks = find_minimal_crack(G, n);
  std::vector<unsigned> 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) {
    sl[c.second.size() - 1]++;
  }
  unsigned crack_component = component[n.G.dual_edges[post_cracks.front().second.front()].v[0]];

  std::vector<std::list<graph::coordinate>> 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]++;
    } else {
      ss[components[i].size() - 1]++;
    }
    for (unsigned j = r; j < sn.size(); j++) {
      sn[j][components[i].size() - 1]++;
    }
  }


  std::vector<bool> vertex_in(n.G.vertices.size());

  for (unsigned i = 0; i < n.G.edges.size(); i++) {
    if (last_cur.currents[i] > CURRENT_CUTOFF) {
      vertex_in[n.G.edges[i].v[0]] = true;
      vertex_in[n.G.edges[i].v[1]] = true;
    }
  }

  unsigned bb_size = 0;

  for (unsigned i = 0; i < n.G.vertices.size(); i++) {
    if (vertex_in[i]) bb_size++;
  }

  for (unsigned i = r; i < sb.size(); i++) {
    sb[i][bb_size - 1]++;
  }

  sd[r - 1]++;
}