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

#include "fracture.h"

double *net_voltages(const net_t *net, cholmod_common *c) {
  cholmod_dense *b = net->boundary_cond;
  cholmod_factor *factor = net->factor;

  cholmod_dense *x = CHOL_F(solve)(CHOLMOD_A, factor, b, c);

  if (((double *)x->x)[0] != ((double *)x->x)[0]) {
    printf("GET_VOLTAGE: value is NaN\n");
    exit(EXIT_FAILURE);
  }

  double *t_voltages = (double *)x->x;
  x->x = NULL;
  CHOL_F(free_dense)(&x, c);

  const graph_t *g = net->graph;

  if (g->boundary == TORUS_BOUND) {
    return t_voltages;
  } else if (net->voltage_bound) {
    double *voltages = (double *)malloc(g->nv * sizeof(double));
    for (uint_t i = 0; i < g->nv - g->bi[g->nb]; i++) {
      voltages[g->nbi[i]] = t_voltages[i];
    }
    for (uint_t i = 0; i < 2; i++) {
      for (uint_t j = 0; j < g->bi[i + 1] - g->bi[i]; j++) {
        voltages[g->b[g->bi[i] + j]] = 1 - i;
      }
    }

    free(t_voltages);
    return voltages;
  } else {
    return t_voltages;
  }
}