summaryrefslogtreecommitdiff
path: root/lib/src/network.cpp
blob: 1edc9734bcac640f9b873087a3cb545fd814def4 (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

#include "network.hpp"

class nanException: public std::exception
{
  virtual const char* what() const throw()
  {
    return "The linear problem returned NaN.";
  }
} nanex;

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, cholmod_common *c) : c(c), G(G), fuses(G.edges.size(), false), thresholds(G.edges.size(), 1) {
  b = CHOL_F(zeros)(G.vertices.size(), 1, CHOLMOD_REAL, c);
  for (unsigned i = 0; i < G.edges.size(); i++) {
    double v0y = G.vertices[G.edges[i].v[0]].r.y;
    double v1y = G.vertices[G.edges[i].v[1]].r.y;

    if (G.edges[i].crossings[1]) {
      bool ind = v0y < v1y ? 0 : 1;

      ((double *)b->x)[G.edges[i].v[ind]] += 1.0;
      ((double *)b->x)[G.edges[i].v[!ind]] -= 1.0;
    }
  }

  unsigned nnz = G.vertices.size() + G.edges.size();

  cholmod_triplet *t = CHOL_F(allocate_triplet)(G.vertices.size(), G.vertices.size(), nnz,  1, CHOLMOD_REAL, c);

  for (unsigned i = 0; i < G.vertices.size(); i++) {
    ((CHOL_INT *)t->i)[i] = i;
    ((CHOL_INT *)t->j)[i] = i;
    ((double *)t->x)[i] = 0.0;
  }

  unsigned terms = G.vertices.size();

  std::unordered_map<std::array<unsigned, 2>, unsigned> known_edges;

  for (unsigned i = 0; i < G.edges.size(); i++) {
    unsigned v0 = G.edges[i].v[0];
    unsigned v1 = G.edges[i].v[1];

    ((double *)t->x)[v0]++;
    ((double *)t->x)[v1]++;

    unsigned s0 = v0 < v1 ? v0 : v1;
    unsigned s1 = v0 < v1 ? v1 : v0;

    auto it = known_edges.find({s0, s1});

    if (it == known_edges.end()) {
      ((CHOL_INT *)t->i)[terms] = s1;
      ((CHOL_INT *)t->j)[terms] = s0;
      ((double *)t->x)[terms] = -1.0;

      known_edges[{s0, s1}] = terms;
      terms++;
    } else {
      ((double *)t->x)[it->second] -= 1.0;
    }
  }

  ((double *)t->x)[0]++;

  t->nnz = terms;

  cholmod_sparse *laplacian = CHOL_F(triplet_to_sparse)(t, terms, c);
  CHOL_F(free_triplet)(&t, c);
  factor = CHOL_F(analyze)(laplacian, c);
  CHOL_F(factorize)(laplacian, factor, c);
  CHOL_F(free_sparse)(&laplacian, c);

  t = CHOL_F(allocate_triplet)(G.edges.size(), G.vertices.size(), 2 * G.edges.size(), 0, CHOLMOD_REAL, c);

  t->nnz = 2 * G.edges.size();

  for (unsigned i = 0; i < G.edges.size(); i++) {
    ((CHOL_INT *)t->i)[2 * i] = i;
    ((CHOL_INT *)t->j)[2 * i] = G.edges[i].v[0];
    ((double *)t->x)[2 * i] = 1.0;

    ((CHOL_INT *)t->i)[2 * i + 1] = i;
    ((CHOL_INT *)t->j)[2 * i + 1] = G.edges[i].v[1];
    ((double *)t->x)[2 * i + 1] = -1.0;
  }

  voltcurmat = CHOL_F(triplet_to_sparse)(t, 2 * G.edges.size(), c);

  CHOL_F(free_triplet)(&t, c);
}

network::network(const network &other) : c(other.c), G(other.G), fuses(other.fuses), thresholds(other.thresholds) {
  b = CHOL_F(copy_dense)(other.b, c);
  factor = CHOL_F(copy_factor)(other.factor, c);
  voltcurmat = CHOL_F(copy_sparse)(other.voltcurmat, c);
} 

network::~network() {
  CHOL_F(free_sparse)(&voltcurmat, c);
  CHOL_F(free_dense)(&b, c);
  CHOL_F(free_factor)(&factor, c);
}

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::break_edge(unsigned e, bool unbreak) {
  fuses[e] = !unbreak;
  unsigned v0 = G.edges[e].v[0];
  unsigned v1 = G.edges[e].v[1];

  unsigned n = factor->n;

  cholmod_sparse *update_mat = CHOL_F(allocate_sparse)(n, n, 2, true, true, 0, CHOLMOD_REAL, c);

  unsigned s1, s2;
  s1 = v0 < v1 ? v0 : v1;
  s2 = v0 < v1 ? v1 : v0;

  CHOL_INT *pp = (CHOL_INT *)update_mat->p;
  CHOL_INT *ii = (CHOL_INT *)update_mat->i;
  double *xx = (double *)update_mat->x;

  for (unsigned i = 0; i <= s1; i++) {
    pp[i] = 0;
  }

  for (unsigned i = s1 + 1; i <= n; i++) {
    pp[i] = 2;
  }

  ii[0] = s1;
  ii[1] = s2;
  xx[0] = 1;
  xx[1] = -1;

  cholmod_sparse *perm_update_mat = CHOL_F(submatrix)(
      update_mat, (CHOL_INT *)factor->Perm, factor->n, NULL, -1, true, true, c);

  CHOL_F(updown)(unbreak, perm_update_mat, factor, c);

  CHOL_F(free_sparse)(&perm_update_mat, c);
  CHOL_F(free_sparse)(&update_mat, c);

  double v0y = G.vertices[v0].r.y;
  double v1y = G.vertices[v1].r.y;

  if (G.edges[e].crossings[1]) {
    bool ind = v0y < v1y ? unbreak : !unbreak;

    ((double *)b->x)[G.edges[e].v[ind]] -= 1.0;
    ((double *)b->x)[G.edges[e].v[!ind]] += 1.0;
  }
}

current_info network::get_current_info() {
  cholmod_dense *x = CHOL_F(solve)(CHOLMOD_A, factor, b, c);

  if (((double *)x->x)[0] != ((double *)x->x)[0]) {
    throw nanex;
  }

  cholmod_dense *y = CHOL_F(allocate_dense)(G.edges.size(), 1, G.edges.size(), CHOLMOD_REAL, c);

  double alpha[2] = {1, 0};
  double beta[2] = {0, 0};
  CHOL_F(sdmult)(voltcurmat, 0, alpha, beta, x, y, c);

  std::vector<double> currents(G.edges.size());

  double total_current = 0;

  for (int i = 0; i < G.edges.size(); i++) {
    if (fuses[i]) {
      currents[i] = 0;
    } else {
      currents[i] = ((double *)y->x)[i];

      double v0y = G.vertices[G.edges[i].v[0]].r.y;
      double v1y = G.vertices[G.edges[i].v[1]].r.y;

      if (G.edges[i].crossings[1]) {
        if (v0y > v1y) {
          currents[i] += 1.0;
          total_current += currents[i];
        } else {
          currents[i] -= 1.0;
          total_current -= currents[i];
        }
      }
    }

  }

  CHOL_F(free_dense)(&x, c);
  CHOL_F(free_dense)(&y, c);

  return {total_current, currents};
}

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

  while (true) {
    current_info ci = this->get_current_info();

    if (ci.conductivity < cutoff * G.vertices.size()) {
      break;
    }

    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 (!fuses[i] && fabs(ci.currents[i]) > cutoff) {
        long double val = logl(fabs(ci.currents[i])) - thresholds[i];
        if (val > max_val) {
          max_val = val;
          max_pos = i;
        }
      }
    }

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

    this->break_edge(max_pos);

    m.bond_broken(*this, ci, max_pos);
  }

  m.post_fracture(*this);
}