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

#include "fracture.h"

cholmod_sparse *gen_voltcurmat(unsigned int num_edges, unsigned int num_verts,
                               unsigned int *edges_to_verts,
                               cholmod_common *c) {

  cholmod_triplet *t_m = CHOL_F(allocate_triplet)(
      num_edges, num_verts, num_edges * 2, 0, CHOLMOD_REAL, c);
  assert(t_m != NULL);

  int_t *rowind = (int_t *)t_m->i;
  int_t *colind = (int_t *)t_m->j;
  double *acoo = (double *)t_m->x;

  for (unsigned int i = 0; i < num_edges; i++) {
    unsigned int v1 = edges_to_verts[2 * i];
    unsigned int v2 = edges_to_verts[2 * i + 1];
    rowind[2 * i] = i;
    rowind[2 * i + 1] = i;
    colind[2 * i] = v1;
    colind[2 * i + 1] = v2;
    acoo[2 * i] = 1;
    acoo[2 * i + 1] = -1;
  }

  t_m->nnz = num_edges * 2;

  assert(CHOL_F(check_triplet)(t_m, c));

  cholmod_sparse *m = CHOL_F(triplet_to_sparse)(t_m, num_edges * 2, c);

  CHOL_F(free_triplet)(&t_m, c);

  return m;
}