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

#include "fracture.h"

double *get_voltage(const net_t *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]) {
		for (uint_t i = 0; i < instance->graph->ne; i++) { 
			printf("%d ", instance->fuses[i]);
		}
		printf("\n");
		printf("GET_VOLTAGE: value is NaN\n");
		exit(EXIT_FAILURE);
	}

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

	CHOL_F(free_dense)(&x, c);


	return field;
}

double *get_current(const net_t *instance, cholmod_common *c) {
	unsigned int num_edges = instance->graph->ne;
	unsigned int num_verts = instance->graph->nv_break;
	unsigned int num_gverts = instance->graph->break_dim;
	unsigned int num_bounds = instance->graph->num_bounds;
	cholmod_sparse *voltcurmat = instance->graph->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 net_t *instance, double *voltages, cholmod_common *c) {
	unsigned int num_edges = instance->graph->ne;
	unsigned int num_verts = instance->graph->nv_break;
	unsigned int num_gverts = instance->graph->break_dim;
	unsigned int num_bounds = instance->graph->num_bounds;
	cholmod_sparse *voltcurmat = instance->graph->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;
}