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

#include "fracture.h"

double *get_thres(uint_t ne, double beta) {
	assert(beta > 0);

	double *thres = (double *)malloc(ne * sizeof(double));
	assert(thres != NULL);

	gsl_set_error_handler_off();

	gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
	{
		FILE *rf = fopen("/dev/urandom", "r");
		unsigned long int seed;
		fread(&seed, sizeof(unsigned long int), 1, rf);
		fclose(rf);
		gsl_rng_set(r, seed);
	}

	for (uint_t i = 0; i < ne; i++) {
		while ((thres[i] = exp(log(gsl_ran_flat(r, 0, 1)) / beta)) == 0.0);
	}

	gsl_rng_free(r);

	return thres;
}


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->fuses = (bool *)calloc(g->ne, sizeof(bool));
	assert(net->fuses != NULL);
	net->thres = get_thres(g->ne, beta);
	net->inf = inf;

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

	if (g->boundary != TORUS_BOUND) net->adjacency = gen_adjacency(net, false, false, 0, c);
	else net->adjacency = gen_adjacency(net, true, false, 0, c);

	net->marks = (uint_t *)malloc((net->graph->break_dim) * sizeof(uint_t));
	net->dual_marks = (uint_t *)malloc((net->graph->dnv) * sizeof(uint_t));
	assert(net->marks != NULL);

	for (uint_t i = 0; i < (net->graph->break_dim); i++) {
		net->marks[i] = 1;
	}
	for (uint_t i = 0; i < (net->graph->dnv); i++) {
		net->dual_marks[i] = i+1;
	}
	net->num_components = 1;

	net_notch(net, notch_len, c);

	{
		cholmod_sparse *laplacian = gen_laplacian(net, c, true);
		net->factor = CHOL_F(analyze)(laplacian, c);
		CHOL_F(factorize)(laplacian, net->factor, c);
		CHOL_F(free_sparse)(&laplacian, 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(double);
	net_copy->thres = (double *)malloc(thres_size);
	assert(net_copy->thres != NULL);
	memcpy(net_copy->thres, net->thres, thres_size);

	size_t marks_size = (net->graph->break_dim) * sizeof(uint_t);
	net_copy->marks = (uint_t *)malloc(marks_size);
	assert(net_copy->marks != NULL);
	memcpy(net_copy->marks, net->marks, marks_size);

	size_t dual_marks_size = (net->graph->dnv) * sizeof(uint_t);
	net_copy->dual_marks = (uint_t *)malloc(dual_marks_size);
	assert(net_copy->dual_marks != NULL);
	memcpy(net_copy->dual_marks, net->dual_marks, dual_marks_size);

	net_copy->adjacency = CHOL_F(copy_sparse)(net->adjacency, c);
	net_copy->boundary_cond = CHOL_F(copy_dense)(net->boundary_cond, c);
	net_copy->factor = CHOL_F(copy_factor)(net->factor, 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_sparse)(&(net->adjacency), c);
	CHOL_F(free_factor)(&(net->factor), c);
	free(net->marks);
	free(net->dual_marks);
	free(net);
}

bool check_instance(const net_t *instance, cholmod_common *c) {
	assert(instance != NULL);
	assert(instance->fuses != NULL);
	assert(CHOL_F(check_dense)(instance->boundary_cond, c));
	assert(CHOL_F(check_factor)(instance->factor, c));

	return true;
}