summaryrefslogtreecommitdiff
path: root/lib/net.c
blob: ac89415afba9b346fe1793ed28ce8bae560e1ab1 (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

#include "fracture.h"

long double *get_thres(uint_t ne, double beta) {
  long double *thres = (long double *)malloc(ne * sizeof(long double));
  assert(thres != NULL);

  gsl_rng *r = gsl_rng_alloc(GSL_RAND_GEN);
  gsl_rng_set(r, jst_rand_seed());

  for (uint_t i = 0; i < ne; i++) {
    thres[i] = rand_dist_pow(r, beta);
  }

  gsl_rng_free(r);

  return thres;
}

void net_notch(net_t *net, double notch_len, cholmod_common *c) {
  for (uint_t i = 0; i < net->graph->ne; i++) {
    uint_t v1, v2;
    double v1x, v1y, v2x, v2y, dy;
    bool crosses_center, not_wrapping, correct_length;

    v1 = net->graph->ev[2 * i];
    v2 = net->graph->ev[2 * i + 1];

    v1x = net->graph->vx[2 * v1];
    v1y = net->graph->vx[2 * v1 + 1];
    v2x = net->graph->vx[2 * v2];
    v2y = net->graph->vx[2 * v2 + 1];

    dy = v1y - v2y;

    crosses_center = (v1y >= 0.5 && v2y <= 0.5) || (v1y <= 0.5 && v2y >= 0.5);
    not_wrapping = fabs(dy) < 0.5;
    // correct_length = v1x + dx / dy * (v1y - 0.5) <= notch_len;
    correct_length = v1x < notch_len && v2x < notch_len;

    if (crosses_center && not_wrapping && correct_length) {
      break_edge(net, i, c);
    }
  }
}

net_t *net_create(const graph_t *g, double inf, double beta, double notch_len,
                  bool vb, cholmod_common *c) {
  net_t *net = (net_t *)calloc(1, sizeof(net_t));
  assert(net != NULL);

  net->graph = g;
  net->num_broken = 0;

  net->fuses = (bool *)calloc(g->ne, sizeof(bool));
  assert(net->fuses != NULL);

  net->thres = get_thres(g->ne, beta);
  net->inf = inf;

  net->dim = g->nv;

  if (g->boundary == TORUS_BOUND) {
    net->nep = g->ne;
    net->evp = (uint_t *)malloc(2 * g->ne * sizeof(uint_t));
    memcpy(net->evp, g->ev, 2 * g->ne * sizeof(uint_t));
  } else {
    if (vb) {
      net->dim -= g->bi[g->nb];
      net->evp = (uint_t *)malloc(2 * g->ne * sizeof(uint_t));
      net->nep = 0;
      for (uint_t i = 0; i < g->ne; i++) {
        if (!(g->bq[g->ev[2 * i]] || g->bq[g->ev[2 * i + 1]])) {
          net->evp[2 * net->nep] = g->bni[g->ev[2 * i]];
          net->evp[2 * net->nep + 1] = g->bni[g->ev[2 * i + 1]];
          net->nep++;
        }
      }
    } else {
      net->dim += 2;
      net->evp = (uint_t *)malloc(2 * (g->ne + g->bi[2]) * sizeof(uint_t));
      memcpy(net->evp, g->ev, 2 * g->ne * sizeof(uint_t));
      net->nep = g->ne + g->bi[2];

      for (uint_t i = 0; i < 2; i++) {
        for (uint_t j = 0; j < g->bi[i + 1] - g->bi[i]; j++) {
          net->evp[2 * (g->ne + g->bi[i] + j)] = g->b[g->bi[i] + j];
          net->evp[2 * (g->ne + g->bi[i] + j) + 1] = g->nv + i;
        }
      }
    }
  }

  net->voltage_bound = vb;
  net->boundary_cond = bound_set(g, vb, notch_len, c);

  net_notch(net, notch_len, c);

  {
    cholmod_sparse *laplacian = gen_laplacian(net, c);
    net->factor = CHOL_F(analyze)(laplacian, c);
    CHOL_F(factorize)(laplacian, net->factor, c);
    CHOL_F(free_sparse)(&laplacian, c);
  }

  net->voltcurmat = gen_voltcurmat(g->ne, g->nv, g->ev, c);

  return net;
}

net_t *net_copy(const net_t *net, cholmod_common *c) {
  net_t *net_copy = (net_t *)calloc(1, sizeof(net_t));
  assert(net_copy != NULL);
  memcpy(net_copy, net, sizeof(net_t));

  size_t fuses_size = (net->graph)->ne * sizeof(bool);
  net_copy->fuses = (bool *)malloc(fuses_size);
  assert(net_copy->fuses != NULL);
  memcpy(net_copy->fuses, net->fuses, fuses_size);

  size_t thres_size = (net->graph)->ne * sizeof(long double);
  net_copy->thres = (long double *)malloc(thres_size);
  assert(net_copy->thres != NULL);
  memcpy(net_copy->thres, net->thres, thres_size);

  size_t evp_size = 2 * net->nep * sizeof(uint_t);
  net_copy->evp = (uint_t *)malloc(thres_size);
  assert(net_copy->evp != NULL);
  memcpy(net_copy->evp, net->evp, evp_size);

  net_copy->boundary_cond = CHOL_F(copy_dense)(net->boundary_cond, c);
  net_copy->factor = CHOL_F(copy_factor)(net->factor, c);
  net_copy->voltcurmat = CHOL_F(copy_sparse)(net->voltcurmat, c);

  return net_copy;
}

void net_free(net_t *net, cholmod_common *c) {
  free(net->fuses);
  free(net->thres);
  CHOL_F(free_dense)(&(net->boundary_cond), c);
  CHOL_F(free_factor)(&(net->factor), c);
  CHOL_F(free_sparse)(&(net->voltcurmat), c);
  free(net->evp);
  free(net);
}