summaryrefslogtreecommitdiff
path: root/lib/src/network.cpp
blob: 58dde781c59a48e7ed16f634c43b34757a91ba99 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636

#include "network.hpp"
#include <sstream>
#include <iostream>

class nofuseException : public std::exception {
  virtual const char* what() const throw() { return "No valid fuse was available to break."; }
} nofuseex;

network::network(const graph& G, bool two_sides, cholmod_common* c)
    : px(G, 0, c), py(G, 1, c), two_sides(two_sides), G(G),
    C(G.dual_vertices.size()), fuses(G.edges.size()), backbone(G.edges.size()),
    thresholds(G.edges.size()) {}

void network::set_thresholds(double beta, std::mt19937& rng) {
  if (beta == 0.0) {
    /* zero beta doesn't make any sense computationally, we interpret it as
     * "infinity" */
    for (long double& threshold : thresholds) {
      threshold = 1.0;
    }
  } else {
    std::uniform_real_distribution<long double> d(0.0, 1.0);

    for (long double& threshold : thresholds) {
      threshold = std::numeric_limits<long double>::lowest();

      while (threshold == std::numeric_limits<long double>::lowest()) {
        threshold = logl(d(rng)) / (long double)beta;
      }
    }
  }
}

void network::fracture(hooks& m) {
  m.pre_fracture(*this);

  while (true) {
    const double min_cond = 1.0 / G.edges.size();

    current_info c = this->get_current_info();

    if ((c.conductivity[0] < min_cond || c.conductivity[1] < min_cond) && two_sides) {
      break;
    } else if (c.conductivity[0] < min_cond && !two_sides) {
      break;
    }

    if (px.sol.conductivity[0] > min_cond) {
      this->update_backbone(px.sol.currents, c.conductivity);
    } else {
      this->update_backbone(py.sol.currents, c.conductivity);
    }

    unsigned max_pos = UINT_MAX;
    long double max_val = std::numeric_limits<long double>::lowest();

    for (unsigned i = 0; i < G.edges.size(); i++) {
      if (!backbone[i]) {
        long double val = logl(c.currents[i]) - thresholds[i];

        if (val > max_val) {
          max_val = val;
          max_pos = i;
        }
      }
    }

    if (max_pos == UINT_MAX) {
      throw nofuseex;
    }

    m.bond_broken(*this, c, max_pos);
    this->break_edge(max_pos);
  }

  m.post_fracture(*this);
}

void network::get_cycle_edges_helper(std::set<unsigned>& cycle_edges,
    std::set<unsigned>& seen_vertices, unsigned v_prev,
    unsigned v_cur) const {
  seen_vertices.insert(v_cur);
  for (unsigned ei : G.dual_vertices[v_cur].ne) {
    if (fuses[ei]) {
      const std::array<unsigned, 2>& e = G.dual_edges[ei].v;
      unsigned vn = e[0] == v_cur ? e[1] : e[0];

      if (vn != v_prev) {
        if (seen_vertices.contains(vn)) {
          cycle_edges.insert(ei);
        } else {
          this->get_cycle_edges_helper(cycle_edges, seen_vertices, v_cur, vn);
        }
      }
    }
  }
}

std::set<unsigned> network::get_cycle_edges(unsigned v0) const {
  std::set<unsigned> seen_vertices;
  std::set<unsigned> cycle_edges;

  this->get_cycle_edges_helper(cycle_edges, seen_vertices, v0, v0);

  return cycle_edges;
}

bool network::find_cycle_helper(std::array<unsigned, 2>& sig, const std::set<unsigned>& cycle_edges,
    unsigned vPrev, unsigned vCur, unsigned vEnd) const {
  for (unsigned ei : G.dual_vertices[vCur].ne) {
    if (fuses[ei]) {
      if (!cycle_edges.contains(ei)) {
        const std::array<unsigned, 2>& e = G.dual_edges[ei].v;
        unsigned vn = e[0] == vCur ? e[1] : e[0];
        if (vn != vPrev) {
          if (vn == vEnd) {
            if (G.dual_edges[ei].crossings[0])
              sig[0]++;
            if (G.dual_edges[ei].crossings[1])
              sig[1]++;
            return true;
          } else {
            if (this->find_cycle_helper(sig, cycle_edges, vCur, vn, vEnd)) {
              if (G.dual_edges[ei].crossings[0])
                sig[0]++;
              if (G.dual_edges[ei].crossings[1])
                sig[1]++;
              return true;
            }
          }
        }
      }
    }
  }

  return false;
}

std::array<unsigned, 2> network::find_cycle(const std::set<unsigned>& cycle_edges, unsigned v0,
    unsigned v1) const {
  std::array<unsigned, 2> sig = {0, 0};
  this->find_cycle_helper(sig, cycle_edges, v0, v0, v1);
  return sig;
}

bool network::get_cycle_helper(std::array<unsigned, 2>& sig, std::set<unsigned>& edges,
    const std::set<unsigned>& cycle_edges, unsigned vPrev, unsigned vCur,
    unsigned vEnd) const {
  for (unsigned ei : G.dual_vertices[vCur].ne) {
    if (fuses[ei]) {
      if (!cycle_edges.contains(ei)) {
        const std::array<unsigned, 2>& e = G.dual_edges[ei].v;
        unsigned vn = e[0] == vCur ? e[1] : e[0];
        if (vn != vPrev) {
          if (vn == vEnd) {
            edges.insert(ei);
            if (G.dual_edges[ei].crossings[0])
              sig[0]++;
            if (G.dual_edges[ei].crossings[1])
              sig[1]++;
            return true;
          } else {
            if (this->get_cycle_helper(sig, edges, cycle_edges, vCur, vn, vEnd)) {
              edges.insert(ei);
              if (G.dual_edges[ei].crossings[0])
                sig[0]++;
              if (G.dual_edges[ei].crossings[1])
                sig[1]++;
              return true;
            }
          }
        }
      }
    }
  }

  return false;
}

std::pair<std::array<unsigned, 2>, std::set<unsigned>>
network::get_cycle(const std::set<unsigned>& cycle_edges, unsigned v0, unsigned v1) const {
  std::set<unsigned> edges;
  std::array<unsigned, 2> sig = {0, 0};
  this->get_cycle_helper(sig, edges, cycle_edges, v0, v0, v1);
  return {sig, edges};
}

void network::get_cluster_edges_helper(std::set<unsigned>& seen_edges, unsigned v) const {
  for (unsigned ei : G.vertices[v].ne) {
    if (!backbone[ei]) {
      if (!seen_edges.contains(ei)) {
        const std::array<unsigned, 2>& e = G.edges[ei].v;
        unsigned vn = e[0] == v ? e[1] : e[0];

        seen_edges.insert(ei);
        this->get_cluster_edges_helper(seen_edges, vn);
      }
    }
  }
}

std::set<unsigned> network::get_cluster_edges(unsigned v0) const {
  std::set<unsigned> cluster_edges;
  this->get_cluster_edges_helper(cluster_edges, v0);
  return cluster_edges;
}

void network::get_tie_flaps_helper(std::set<unsigned>& added_edges, unsigned v0,
    unsigned vCur) const {
  for (unsigned ei : G.vertices[vCur].ne) {
    if (!backbone[ei]) {
      if (!added_edges.contains(ei)) {
        const std::array<unsigned, 2>& e = G.edges[ei].v;
        unsigned vn = e[0] == vCur ? e[1] : e[0];

        added_edges.insert(ei);
        if (vn != v0) {
          this->get_tie_flaps_helper(added_edges, v0, vn);
        }
      }
    }
  }
}

std::list<std::set<unsigned>> network::get_tie_flaps(unsigned v0) const {
  std::list<std::set<unsigned>> tie_flaps;

  for (unsigned ei : G.vertices[v0].ne) {
    if (!backbone[ei]) {
      bool seen_edge = false;
      for (const std::set<unsigned>& flap : tie_flaps) {
        if (flap.contains(ei)) {
          seen_edge = true;
          break;
        }
      }

      if (!seen_edge) {
        tie_flaps.push_back({ei});

        const std::array<unsigned, 2>& e = G.edges[ei].v;
        unsigned vn = e[0] == v0 ? e[1] : e[0];

        this->get_tie_flaps_helper(tie_flaps.back(), v0, vn);
      }
    }
  }

  return tie_flaps;
}

void network::update_backbone(const std::vector<double>& c, const std::array<double, 2>& cc) {
  bool done_x = cc[0] < 1.0 / G.edges.size();
  bool done_y = cc[1] < 1.0 / G.edges.size();

  auto cycle_check_current = [done_x, done_y](std::array<unsigned, 2> c) {
    return (c[0] % 2 == 0 && c[1] % 2 == 0) ||
      ((c[0] % 2 == 0 && done_x) || (c[1] % 2 == 0 && done_y));
  };
  auto cycle_check_wrap_x = [](std::array<unsigned, 2> c) {
    return (c[0] % 2 == 0 && c[1] % 2 == 1);
  };
  auto cycle_check_wrap_y = [](std::array<unsigned, 2> c) {
    return (c[0] % 2 == 1 && c[1] % 2 == 0);
  };

  // First, we will check for lollipops!
  for (unsigned i = 0; i < G.edges.size(); i++) {
    if ((!backbone[i]) && C.same_component(G.dual_edges[i].v[0], G.dual_edges[i].v[1])) {
      if ((!seen_pairs_x.contains({G.dual_edges[i].v[0], G.dual_edges[i].v[1]}) && !done_x) || (!seen_pairs_y.contains({G.dual_edges[i].v[0], G.dual_edges[i].v[1]}) && !done_y)) {
        // This is a candidate lollipop stem. First, we will identify any
        // cycles in the dual cluster that impinges on the stem and mark them
        // by an edge that uniquely severs each.
        std::set<unsigned> cedges = this->get_cycle_edges(G.dual_edges[i].v[0]);

        // Now, we create a crossing signature for each cycle. First, we do it
        // for the new cycle that would form by removing the stem.
        std::array<unsigned, 2> base_sig =
          this->find_cycle(cedges, G.dual_edges[i].v[0], G.dual_edges[i].v[1]);
        if (G.dual_edges[i].crossings[0])
          base_sig[0]++;
        if (G.dual_edges[i].crossings[1])
          base_sig[1]++;

        // Then, we do it for each of the existing cycles we found in the
        // previous step.
        std::list<std::array<unsigned, 2>> all_sigs = {base_sig};
        for (unsigned e : cedges) {
          std::array<unsigned, 2> new_sig_1 =
            this->find_cycle(cedges, G.dual_edges[i].v[0], G.dual_edges[e].v[0]);
          std::array<unsigned, 2> new_sig_2 =
            this->find_cycle(cedges, G.dual_edges[i].v[1], G.dual_edges[e].v[1]);
          std::array<unsigned, 2> new_sig = {new_sig_1[0] + new_sig_2[0],
            new_sig_1[1] + new_sig_2[1]};

          if (G.dual_edges[i].crossings[0])
            new_sig[0]++;
          if (G.dual_edges[i].crossings[1])
            new_sig[1]++;
          if (G.dual_edges[e].crossings[0])
            new_sig[0]++;
          if (G.dual_edges[e].crossings[1])
            new_sig[1]++;

          all_sigs.push_back(new_sig);
        }

        // Now, having found all cycles involving the candidate stem, we check
        // if any of them spans the torus and therefore can carry current.
        if (std::any_of(all_sigs.begin(), all_sigs.end(), cycle_check_current)) {
          // If none does, we remove it from the backbone!
          seen_pairs_x[{G.dual_edges[i].v[0], G.dual_edges[i].v[1]}] = true;
          seen_pairs_y[{G.dual_edges[i].v[0], G.dual_edges[i].v[1]}] = true;
          backbone[i] = true;

          // We're not done yet: we've severed the stem but the pop remains! We
          // check each side of the lollipop and sum up all of the currents
          // that span an edge. Any component without a sufficiently large net
          // current must be disconnected from the current-carrying cluster and
          // can also be removed from the backbone.
          for (unsigned j = 0; j < 2; j++) {
            std::set<unsigned> cluster_edges = this->get_cluster_edges(G.edges[i].v[j]);
            std::array<double, 2> total_current = {0.0, 0.0};

            for (unsigned e : cluster_edges) {
              if (G.edges[e].crossings[0]) {
                if (G.vertices[G.edges[e].v[0]].r.x < G.vertices[G.edges[e].v[1]].r.x) {
                  total_current[0] += c[e];
                } else {
                  total_current[0] -= c[e];
                }
              }
              if (G.edges[e].crossings[1]) {
                if (G.vertices[G.edges[e].v[0]].r.y < G.vertices[G.edges[e].v[1]].r.y) {
                  total_current[1] += c[e];
                } else {
                  total_current[1] -= c[e];
                }
              }
            }

            if (fabs(total_current[0]) < 1.0 / G.edges.size() &&
                fabs(total_current[1]) < 1.0 / G.edges.size()) {
              for (unsigned e : cluster_edges) {
                backbone[e] = true;
              }
            }
          }
        } else if (std::any_of(all_sigs.begin(), all_sigs.end(), cycle_check_wrap_x)) {
          // If the bond separates a wrapping path, breaking it would end the
          // fracture and therefore it will never be removed from the backbone
          // in this manner. We can skip it in the future.
          seen_pairs_x[{G.dual_edges[i].v[0], G.dual_edges[i].v[1]}] = true;
        } else  if (std::any_of(all_sigs.begin(), all_sigs.end(), cycle_check_wrap_y)) {
          seen_pairs_y[{G.dual_edges[i].v[0], G.dual_edges[i].v[1]}] = true;
        }
      }
    }
  }

  // Now, we will check for bow ties!
  std::set<unsigned> bb_verts;

  // First we get a list of all vertices that remain in the backbone.
  for (unsigned i = 0; i < G.edges.size(); i++) {
    if (!backbone[i]) {
      bb_verts.insert(G.edges[i].v[0]);
      bb_verts.insert(G.edges[i].v[1]);
    }
  }

  for (unsigned v : bb_verts) {
    bool found_pair = false;
    unsigned d1, d2, p1, p2;
    // For each vertex, we check to see if two of the faces adjacent to the
    // vertex are in the same component of the dual lattice and if so, we make
    // sure they do not belong to a contiguously connected series of such
    // faces.
    for (unsigned i = 0; i < G.vertices[v].nd.size(); i++) {
      for (unsigned j = 2; j < G.vertices[v].nd.size() - 1; j++) {
        unsigned l = (i + j) % G.vertices[v].nd.size();
        if (C.same_component(G.vertices[v].nd[i], G.vertices[v].nd[l])) {
          unsigned il = i < l ? i : l;
          unsigned ig = i < l ? l : i;
          if ((!seen_pairs_x.contains({G.vertices[v].nd[il], G.vertices[v].nd[ig]}) && !done_x) || (!seen_pairs_y.contains({G.vertices[v].nd[il], G.vertices[v].nd[ig]}) && !done_y)) {
            bool any_intervening1 = false;
            bool any_intervening2 = false;
            unsigned ie1 = 0;
            unsigned ie2 = 0;

            // yuck, think of something more elegant?
            for (unsigned k = il + 1; k < ig; k++) {
              if (!C.same_component(G.vertices[v].nd[i], G.vertices[v].nd[k])) {
                any_intervening1 = true;
                break;
              }
            }
            for (unsigned k = (ig + 1); k % G.vertices[v].nd.size() != il; k++) {
              if (!C.same_component(G.vertices[v].nd[i],
                                    G.vertices[v].nd[k % G.vertices[v].nd.size()])) {
                any_intervening2 = true;
                break;
              }
            }
            if (any_intervening2 && !any_intervening1) {
              for (unsigned k = il + 1; k <= ig; k++) {
                if (!backbone[G.vertices[v].ne[k]]) {
                  ie1++;
                }
              }
            }
            if (any_intervening1 && !any_intervening2) {
              for (unsigned k = ig + 1; k % G.vertices[v].nd.size() != il + 1; k++) {
                if (!backbone[G.vertices[v].ne[k % G.vertices[v].nd.size()]]) {
                  ie2++;
                }
              }
            }

            // If all these conditions are true, we process the pair. The same
            // cycle analysis as in the lollipop case is done.
            if ((any_intervening1 && any_intervening2) || (any_intervening1 && ie2 > 1) ||
                (any_intervening2 && ie1 > 1)) {
              found_pair = true;
              p1 = il;
              p2 = ig;
              d1 = G.vertices[v].nd[il];
              d2 = G.vertices[v].nd[ig];

              std::set<unsigned> cedges = this->get_cycle_edges(d1);

              std::array<unsigned, 2> base_sig = this->find_cycle(cedges, d1, d2);
              for (unsigned k = p1; k < p2; k++) {
                if (G.dual_edges[G.vertices[v].ne[k]].crossings[0])
                  base_sig[0]++;
                if (G.dual_edges[G.vertices[v].ne[k]].crossings[1])
                  base_sig[1]++;
              }

              std::list<std::array<unsigned, 2>> all_sigs = {base_sig};
              for (unsigned e : cedges) {
                std::array<unsigned, 2> new_sig_1 =
                    this->find_cycle(cedges, d1, G.dual_edges[e].v[0]);
                std::array<unsigned, 2> new_sig_2 =
                    this->find_cycle(cedges, d2, G.dual_edges[e].v[1]);
                std::array<unsigned, 2> new_sig = {new_sig_1[0] + new_sig_2[0],
                                                   new_sig_1[1] + new_sig_2[1]};

                for (unsigned k = p1; k < p2; k++) {
                  if (G.dual_edges[G.vertices[v].ne[k]].crossings[0])
                    new_sig[0]++;
                  if (G.dual_edges[G.vertices[v].ne[k]].crossings[1])
                    new_sig[1]++;
                }
                if (G.dual_edges[e].crossings[0])
                  new_sig[0]++;
                if (G.dual_edges[e].crossings[1])
                  new_sig[1]++;

                all_sigs.push_back(new_sig);
              }

              if (std::any_of(all_sigs.begin(), all_sigs.end(), cycle_check_current)) {
                // one of our pairs qualifies for trimming!
                seen_pairs_x[{d1, d2}] = true;
                seen_pairs_y[{d1, d2}] = true;

                // We separate the flaps of the bowtie (there may be more than
                // two!) into distinct sets.
                std::list<std::set<unsigned>> flaps = this->get_tie_flaps(v);

                // All the bonds in each flap without current are removed from
                // the backbone.
                for (const std::set<unsigned>& flap : flaps) {
                  std::array<double, 2> total_current = {0.0, 0.0};
                  for (unsigned e : flap) {
                    if (G.edges[e].crossings[0]) {
                      if (G.vertices[G.edges[e].v[0]].r.x < G.vertices[G.edges[e].v[1]].r.x) {
                        total_current[0] += c[e];
                      } else {
                        total_current[0] -= c[e];
                      }
                    }
                    if (G.edges[e].crossings[1]) {
                      if (G.vertices[G.edges[e].v[0]].r.y < G.vertices[G.edges[e].v[1]].r.y) {
                        total_current[1] += c[e];
                      } else {
                        total_current[1] -= c[e];
                      }
                    }
                  }

                  if (fabs(total_current[0]) < 1.0 / G.edges.size() &&
                      fabs(total_current[1]) < 1.0 / G.edges.size()) {
                    for (unsigned e : flap) {
                      backbone[e] = true;
                    }
                  }
                }
              } else if (std::any_of(all_sigs.begin(), all_sigs.end(), cycle_check_wrap_x)) {
                seen_pairs_x[{d1, d2}] = true;
              } else if (std::any_of(all_sigs.begin(), all_sigs.end(), cycle_check_wrap_y)) {
                seen_pairs_y[{d1, d2}] = true;
              }
            }
          }
        }
      }
    }
  }
}

void network::break_edge(unsigned e, bool unbreak) {
  fuses[e] = !unbreak;
  backbone[e] = !unbreak;
  C.add_bond(G.dual_edges[e]);
  px.break_edge(e, 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) : network(G, false, c), weight(1.0) {}

fuse_network::fuse_network(const fuse_network& n) : network(n), weight(1.0) {}

current_info fuse_network::get_current_info() {
  px.solve(fuses);
  py.solve(fuses);

  bool done_x = px.sol.conductivity[0] < 1.0 / G.edges.size();

  current_info ctot;
  ctot.currents.resize(G.edges.size());
  ctot.conductivity = {px.sol.conductivity[0], py.sol.conductivity[1]};

  if (!done_x) {
    for (unsigned i = 0; i < G.edges.size(); i++) {
      ctot.currents[i] = fabs(px.sol.currents[i] / px.sol.conductivity[0]);
    }
  }

  return ctot;
}

elastic_network::elastic_network(const graph& G, cholmod_common* c, double weight)
    : network(G, true, c), weight(weight) {}

elastic_network::elastic_network(const elastic_network& n) : network(n), weight(n.weight) {}

current_info elastic_network::get_current_info() {
  px.solve(fuses);
  py.solve(fuses);

  bool done_x = px.sol.conductivity[0] < 1.0 / G.edges.size();
  bool done_y = py.sol.conductivity[1] < 1.0 / G.edges.size();

  current_info ctot;
  ctot.currents.resize(G.edges.size());
  ctot.conductivity[0] = px.sol.conductivity[0];
  ctot.conductivity[1] = py.sol.conductivity[1];

  if (done_x && !done_y) {
    for (unsigned i = 0; i < G.edges.size(); i++) {
      ctot.currents[i] = weight * fabs(py.sol.currents[i]) / py.sol.conductivity[1];
    }
  } else if (done_y && !done_x) {
    for (unsigned i = 0; i < G.edges.size(); i++) {
      ctot.currents[i] = (1 - weight) * fabs(px.sol.currents[i]) / px.sol.conductivity[0];
    }
  } else if (!done_x && !done_y) {
    for (unsigned i = 0; i < G.edges.size(); i++) {
      ctot.currents[i] = sqrt(pow((1 - weight) * px.sol.currents[i] / px.sol.conductivity[0], 2) +
                              pow(weight * py.sol.currents[i] / py.sol.conductivity[1], 2));
    }
  }

  return ctot;
}

percolation_network::percolation_network(const graph& G, cholmod_common* c) : network(G, true, c) {}

percolation_network::percolation_network(const percolation_network& n) : network(n) {}

current_info percolation_network::get_current_info() {
  current_info ctot;
  ctot.currents.resize(G.edges.size(), 0.0);

  px.solve(fuses);
  py.solve(fuses);

  ctot.conductivity = {px.sol.conductivity[0], py.sol.conductivity[1]};

  for (unsigned i = 0; i < G.edges.size(); i++) {
    if (fabs(px.sol.currents[i]) > CURRENT_CUTOFF || fabs(py.sol.currents[i]) > CURRENT_CUTOFF) {
      ctot.currents[i] = 1.0;
    }
  }

  return ctot;
}