summaryrefslogtreecommitdiff
path: root/src/measurements.cpp
blob: 2af1ba3e0c46bf2d30c8ae86ffbb8c70e1c2f981 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

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

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

  uint64_t N_old = 0;
  std::vector<uint64_t> data_old(data.size(), 0);

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

    file.close();
  }

  std::ofstream file_out(filename);

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

  file_out.close();
}

template <class T>
void update_field_file(std::string id, const std::vector<T>& data, unsigned N, std::string model_string, unsigned Mx, unsigned My) {
  std::string filename = model_string + id + "_" + std::to_string(Mx) + "_" + std::to_string(My) + ".dat";
  std::ifstream file(filename);

  uint64_t N_old = 0;
  std::vector<T> data_old(data.size(), 0);

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

  std::ofstream file_out(filename);

  file_out <<std::fixed<< N_old + N <<  "\n";
  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 <class T>
void correlation(unsigned Mx, unsigned My, std::vector<std::vector<T>>& 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<uint64_t>& data, const std::list<graph::coordinate>& pos) {
  for (std::list<graph::coordinate>::const_iterator it1 = pos.begin(); it1 != pos.end(); it1++) {
    for (std::list<graph::coordinate>::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;

      data[(unsigned)(Mx * (Δx / Lx)) + (Mx / 2) * (unsigned)(My * (Δy / Ly))]++;
    }
  }
}

void correlation2(double Lx, double Ly, unsigned Mx, unsigned My, std::vector<uint64_t>& data, const std::list<graph::coordinate>& pos1, const std::list<graph::coordinate>& pos2) {
  for (std::list<graph::coordinate>::const_iterator it1 = pos1.begin(); it1 != pos1.end(); it1++) {
    for (std::list<graph::coordinate>::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 <class T>
void autocorrelation(unsigned Mx, unsigned My, std::vector<std::vector<T>>& 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, unsigned Mx, unsigned My, double beta) :
  Mx(Mx), My(My), G(2 * n),
  sc(3 * n, 0),
  ss(3 * n, 0),
  sm(3 * n, 0),
  sa(3 * n, 0),
  sl(3 * n, 0),
  sb(3 * n, 0),
  sD(3 * n, 0),
  ccc((Mx / 2) * (My / 2), 0),
  css((Mx / 2) * (My / 2), 0),
  cmm((Mx / 2) * (My / 2), 0),
  caa((Mx / 2) * (My / 2), 0),
  cll((Mx / 2) * (My / 2), 0),
  cbb((Mx / 2) * (My / 2), 0),
  cDD((Mx / 2) * (My / 2), 0),
  csD((Mx / 2) * (My / 2), 0)
{
  N = 0;
  Nc = 0;
  Na = 0;
  Nb = 0;

  if (beta != 0.0) {
    model_string = "fracture_" + std::to_string(n) + "_" + std::to_string(a) + "_" + std::to_string(beta) + "_";
  } else {
    model_string = "fracture_" + std::to_string(n) + "_" + std::to_string(a) + "_INF_";
  }

  // FFTW setup for correlation functions
  /*
  fftw_set_timelimit(1);

  fftw_forward_in = (double *)fftw_malloc(Mx * My * sizeof(double));
  fftw_forward_out = (fftw_complex *)fftw_malloc(Mx * My * sizeof(fftw_complex));
  fftw_reverse_in = (fftw_complex *)fftw_malloc(Mx * My * sizeof(fftw_complex));
  fftw_reverse_out = (double *)fftw_malloc(Mx * My * sizeof(double));

  forward_plan = fftw_plan_dft_r2c_2d(My, Mx, fftw_forward_in, fftw_forward_out, 0);
  reverse_plan = fftw_plan_dft_c2r_2d(My, Mx, fftw_reverse_in, fftw_reverse_out, 0);
  */
}

ma::ma(double Lx, double Ly, unsigned Mx, unsigned My, double beta) :
  Mx(Mx), My(My), G(2 * (unsigned)ceil(Lx * Ly / 2)),
  sc(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  ss(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  sm(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  sa(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  sl(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  sb(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  sD(3 * (unsigned)ceil(Lx * Ly / 2), 0),
  ccc((Mx / 2) * (My / 2), 0),
  css((Mx / 2) * (My / 2), 0),
  cmm((Mx / 2) * (My / 2), 0),
  caa((Mx / 2) * (My / 2), 0),
  cll((Mx / 2) * (My / 2), 0),
  cbb((Mx / 2) * (My / 2), 0),
  cDD((Mx / 2) * (My / 2), 0),
  csD((Mx / 2) * (My / 2), 0)
{
  N = 0;
  Nc = 0;
  Na = 0;
  Nb = 0;

  if (beta != 0.0) {
    model_string = "fracture_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_" + std::to_string(beta) + "_";
  } else {
    model_string = "fracture_" + std::to_string(Lx) + "_" + std::to_string(Ly) + "_INF_";
  }

  // FFTW setup for correlation functions
  /*
  fftw_set_timelimit(1);

  fftw_forward_in = (double *)fftw_malloc(Mx * My * sizeof(double));
  fftw_forward_out = (fftw_complex *)fftw_malloc(Mx * My * sizeof(fftw_complex));
  fftw_reverse_in = (fftw_complex *)fftw_malloc(Mx * My * sizeof(fftw_complex));
  fftw_reverse_out = (double *)fftw_malloc(Mx * My * sizeof(double));

  forward_plan = fftw_plan_dft_r2c_2d(My, Mx, fftw_forward_in, fftw_forward_out, 0);
  reverse_plan = fftw_plan_dft_c2r_2d(My, Mx, fftw_reverse_in, fftw_reverse_out, 0);
  */
}

ma::~ma() {
  // clean up FFTW objects
  /*
  fftw_free(fftw_forward_in);
  fftw_free(fftw_forward_out);
  fftw_free(fftw_reverse_in);
  fftw_free(fftw_reverse_out);
  fftw_destroy_plan(forward_plan);
  fftw_destroy_plan(reverse_plan);
  fftw_cleanup();
  */

  update_distribution_file("sc", sc, Nc, model_string);
  update_distribution_file("ss", ss, N, model_string);
  update_distribution_file("sm", sm, N, model_string);
  update_distribution_file("sa", sa, Na, model_string);
  update_distribution_file("sl", sl, N, model_string);
  update_distribution_file("sb", sb, Nb, model_string);
  update_distribution_file("sD", sD, N, model_string);

  update_field_file("ccc", ccc, Nc, model_string, Mx, My);
  update_field_file("css", css, N, model_string, Mx, My);
  update_field_file("cmm", cmm, N, model_string, Mx, My);
  update_field_file("caa", caa, Na, model_string, Mx, My);
  update_field_file("cll", cll, N, model_string, Mx, My);
  update_field_file("cbb", cbb, Nb, model_string, Mx, My);
  update_field_file("cDD", cDD, N, model_string, Mx, My);
  update_field_file("csD", csD, N, model_string, Mx, My);

  //stress_file.close();
}

void ma::pre_fracture(const network&) {
  lv = std::numeric_limits<long double>::lowest();
  avalanches = {{}};
  boost::remove_edge_if(trivial, G);
}

void ma::bond_broken(const network& net, const current_info& cur, unsigned i) {
  long double c = logl(cur.conductivity / fabs(cur.currents[i])) + net.thresholds[i];
  if (c > lv && avalanches.back().size() > 0) {
    sa[avalanches.back().size() - 1]++;
    Na++;

    autocorrelation2(net.G.L.x, net.G.L.y, Mx, My, caa, avalanches.back());

    lv = c;
    avalanches.push_back({net.G.edges[i].r});
    last_avalanche = {i};
  } else {
    avalanches.back().push_back(net.G.edges[i].r);
    last_avalanche.push_back(i);
  }

  boost::add_edge(net.G.dual_edges[i].v[0], net.G.dual_edges[i].v[1], {i}, G);
}

void ma::post_fracture(network &n) {
  std::vector<unsigned> component(boost::num_vertices(G));
  unsigned num = boost::connected_components(G, &component[0]);

  std::list<unsigned> crack = find_minimal_crack(G, n);

  ss[crack.size() - 1]++;
  std::list<graph::coordinate> sr;

  for (auto edge : crack) {
    sr.push_back(n.G.dual_edges[edge].r);
  }

  autocorrelation2(n.G.L.x, n.G.L.y, Mx, My, css, sr);

  unsigned crack_component = component[n.G.dual_edges[crack.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 && components[i].size() > 0) {
      sc[components[i].size() - 1]++;
      Nc++;

      autocorrelation2(n.G.L.x, n.G.L.y, Mx, My, ccc, components[i]);
    }
  }

  // spanning cluster

  sm[components[crack_component].size() - 1]++;

  autocorrelation2(n.G.L.x, n.G.L.y, Mx, My, cmm, components[crack_component]);

  /// damage at end
  std::list<graph::coordinate> Dr;

  for (unsigned i = 0; i < n.G.edges.size(); i++) {
    if (n.fuses[i]) { 
      Dr.push_back(n.G.edges[i].r);
    }
  }

  sD[Dr.size() - 1]++;

  autocorrelation2(n.G.L.x, n.G.L.y, Mx, My, cDD, Dr);
  correlation2(n.G.L.x, n.G.L.y, Mx, My, csD, sr, Dr);

  // ********************** LAST AVALANCHE *********************

  // rewind the last avalanche
  sl[avalanches.back().size() - 1]++;

  autocorrelation2(n.G.L.x, n.G.L.y, Mx, My, cll, avalanches.back());

  for (unsigned e : last_avalanche) {
    boost::remove_edge(n.G.dual_edges[e].v[0], n.G.dual_edges[e].v[1], G);
  }

  num = boost::connected_components(G, &component[0]);

  std::vector<std::list<graph::coordinate>> new_components(num);

  for (unsigned i = 0; i < n.G.dual_vertices.size(); i++) {
    new_components[component[i]].push_back(n.G.dual_vertices[i].r);
  }

  for (unsigned i = 0; i < num; i++) {
    if (new_components[i].size() > 0) {
      sb[new_components[i].size() - 1]++;
      Nb++;

      autocorrelation2(n.G.L.x, n.G.L.y, Mx, My, cbb, new_components[i]);
    }
  }

  N++;
}