summaryrefslogtreecommitdiff
path: root/src/get_current.c
blob: 5360054daa1f0391bb72fc70dfa7bb316c1d7e57 (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

#include "fracture.h"

double *get_voltage(const finst *instance, cholmod_common *c) {
	cholmod_dense *b = instance->boundary_cond;
	cholmod_factor *factor = instance->factor;

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

	if (((double *)x->x)[0] != ((double *)x->x)[0]) {
		printf("ERROR: GET_VOLTAGE FAILED\n\n");
		CHOL_F(free_dense)(&x, c);
		return NULL;
	}

	double *field = (double *)x->x;
	x->x = NULL;

	CHOL_F(free_dense)(&x, c);


	return field;
}

double *get_current(const finst *instance, cholmod_common *c) {
	unsigned int num_edges = instance->network->num_edges;
	unsigned int num_verts = instance->network->num_verts_break;
	unsigned int num_gverts = instance->network->break_dim;
	unsigned int num_bounds = instance->network->num_bounds;
	cholmod_sparse *voltcurmat = instance->network->voltcurmat;

	double *voltages = get_voltage(instance, c);
	if (voltages == NULL) {
		return NULL;
	}
	cholmod_dense *x = CHOL_F(allocate_dense)(
			num_gverts, 1, num_gverts, CHOLMOD_REAL, c);
	double *tmp_x = x->x;
	x->x = voltages;

	cholmod_dense *y =
			CHOL_F(allocate_dense)(num_edges, 1, num_edges, CHOLMOD_REAL, c);

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

	double *field = (double *)malloc(num_edges * sizeof(double));

	for (int i = 0; i < num_edges; i++) {
		if (instance->fuses[i])
			field[i] = 0;
		else
			field[i] = ((double *)y->x)[i];
	}

	x->x = tmp_x;
	free(voltages);
	CHOL_F(free_dense)(&x, c);
	CHOL_F(free_dense)(&y, c);

	return field;
}


double *get_current_v(const finst *instance, double *voltages, cholmod_common *c) {
	unsigned int num_edges = instance->network->num_edges;
	unsigned int num_verts = instance->network->num_verts_break;
	unsigned int num_gverts = instance->network->break_dim;
	unsigned int num_bounds = instance->network->num_bounds;
	cholmod_sparse *voltcurmat = instance->network->voltcurmat;

	cholmod_dense *x = CHOL_F(allocate_dense)(
			num_gverts, 1, num_gverts, CHOLMOD_REAL, c);
	double *tmp_x = x->x;
	x->x = voltages;

	cholmod_dense *y =
			CHOL_F(allocate_dense)(num_edges, 1, num_edges, CHOLMOD_REAL, c);

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

	double *field = (double *)malloc(num_edges * sizeof(double));

	for (int i = 0; i < num_edges; i++) {
		if (instance->fuses[i])
			field[i] = 0;
		else
			field[i] = ((double *)y->x)[i];
	}

	x->x = tmp_x;
	CHOL_F(free_dense)(&x, c);
	CHOL_F(free_dense)(&y, c);

	return field;
}